Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registry interface #3

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public static function forSystem(string $content): self
return new self($content, Role::System);
}

public function isSystem(): bool
{
return Role::System === $this->role;
}

/**
* @param array{name: string, arguments: string} $functionCall
*/
Expand All @@ -44,4 +39,14 @@ public static function ofFunctionCall(string $name, string $content): self
{
return new self($content, Role::FunctionCall, null, $name);
}

public function isSystem(): bool
{
return Role::System === $this->role;
}

public function hasFunctionCall(): bool
{
return null !== $this->functionCall && 0 !== count($this->functionCall);
}
}
16 changes: 2 additions & 14 deletions src/ToolBox/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,9 @@
use Psr\Log\LoggerInterface;

/**
* @phpstan-import-type ParameterDefinition from ParameterAnalyzer
*
* @phpstan-type ToolDefinition = array{
* type: 'function',
* function: array{
* name: string,
* description: string,
* parameters?: ParameterDefinition
* }
* }
* @phpstan-import-type ToolDefinition from RegistryInterface
*/
final class Registry
final class Registry implements RegistryInterface
{
/**
* @var list<object>
Expand All @@ -41,9 +32,6 @@ public function __construct(
$this->tools = $tools instanceof \Traversable ? iterator_to_array($tools) : $tools;
}

/**
* @return list<ToolDefinition>
*/
public function getMap(): array
{
if (isset($this->map)) {
Expand Down
27 changes: 27 additions & 0 deletions src/ToolBox/RegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\ToolBox;

/**
* @phpstan-import-type ParameterDefinition from ParameterAnalyzer
*
* @phpstan-type ToolDefinition = array{
* type: 'function',
* function: array{
* name: string,
* description: string,
* parameters?: ParameterDefinition
* }
* }
*/
interface RegistryInterface
{
/**
* @return list<ToolDefinition>
*/
public function getMap(): array;

public function execute(string $name, string $arguments): string;
}
4 changes: 2 additions & 2 deletions src/ToolChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\RegistryInterface;

final class ToolChain
{
public function __construct(
private LanguageModel $llm,
private Registry $toolRegistry,
private RegistryInterface $toolRegistry,
) {
}

Expand Down