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

Rename Registry to Toolbox #15

Merged
merged 2 commits into from
Sep 22, 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
6 changes: 3 additions & 3 deletions examples/toolchain-clock.php → examples/toolbox-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\Tool\Clock;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use Symfony\Component\Clock\Clock as SymfonyClock;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -19,8 +19,8 @@
$llm = new Gpt($runtime, Version::GPT_4o_MINI);

$clock = new Clock(new SymfonyClock());
$registry = new Registry(new ToolAnalyzer(new ParameterAnalyzer()), [$clock]);
$chain = new Chain($llm, $registry);
$toolBox = new ToolBox(new ToolAnalyzer(new ParameterAnalyzer()), [$clock]);
$chain = new Chain($llm, $toolBox);

$messages = new MessageBag(Message::ofUser('What date and time is it?'));
$response = $chain->call($messages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\Tool\SerpApi;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -19,8 +19,8 @@
$llm = new Gpt($runtime, Version::GPT_4o_MINI);

$serpApi = new SerpApi($httpClient, getenv('SERP_API_KEY'));
$registry = new Registry(new ToolAnalyzer(new ParameterAnalyzer()), [$serpApi]);
$chain = new Chain($llm, $registry);
$toolBox = new ToolBox(new ToolAnalyzer(new ParameterAnalyzer()), [$serpApi]);
$chain = new Chain($llm, $toolBox);

$messages = new MessageBag(Message::ofUser('Who is the current chancellor of Germany?'));
$response = $chain->call($messages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\Tool\OpenMeteo;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -19,8 +19,8 @@
$llm = new Gpt($runtime, Version::GPT_4o_MINI);

$wikipedia = new OpenMeteo($httpClient);
$registry = new Registry(new ToolAnalyzer(new ParameterAnalyzer()), [$wikipedia]);
$chain = new Chain($llm, $registry);
$toolBox = new ToolBox(new ToolAnalyzer(new ParameterAnalyzer()), [$wikipedia]);
$chain = new Chain($llm, $toolBox);

$messages = new MessageBag(Message::ofUser('How is the weather currently in Berlin?'));
$response = $chain->call($messages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\Tool\Wikipedia;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -19,8 +19,8 @@
$llm = new Gpt($runtime, Version::GPT_4o_MINI);

$wikipedia = new Wikipedia($httpClient);
$registry = new Registry(new ToolAnalyzer(new ParameterAnalyzer()), [$wikipedia]);
$chain = new Chain($llm, $registry);
$toolBox = new ToolBox(new ToolAnalyzer(new ParameterAnalyzer()), [$wikipedia]);
$chain = new Chain($llm, $toolBox);

$messages = new MessageBag(Message::ofUser('Who is the current chancellor of Germany?'));
$response = $chain->call($messages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\Tool\YouTubeTranscriber;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -19,8 +19,8 @@
$llm = new Gpt($runtime, Version::GPT_4o_MINI);

$transcriber = new YouTubeTranscriber($httpClient);
$registry = new Registry(new ToolAnalyzer(new ParameterAnalyzer()), [$transcriber]);
$chain = new Chain($llm, $registry);
$toolBox = new ToolBox(new ToolAnalyzer(new ParameterAnalyzer()), [$transcriber]);
$chain = new Chain($llm, $toolBox);

$messages = new MessageBag(Message::ofUser('Please summarize this video for me: https://www.youtube.com/watch?v=6uXW-ulpj0s'));
$response = $chain->call($messages);
Expand Down
10 changes: 5 additions & 5 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\StructuredOutput\ResponseFormatFactory;
use PhpLlm\LlmChain\ToolBox\RegistryInterface;
use PhpLlm\LlmChain\ToolBox\ToolBoxInterface;
use Symfony\Component\Serializer\SerializerInterface;

final readonly class Chain
{
public function __construct(
private LanguageModel $llm,
private ?RegistryInterface $toolRegistry = null,
private ?ToolBoxInterface $toolBox = null,
private ?ResponseFormatFactory $responseFormatFactory = null,
private ?SerializerInterface $serializer = null,
) {
Expand All @@ -27,8 +27,8 @@ public function call(MessageBag $messages, array $options = []): string|object
{
$llmOptions = $options;

if (!array_key_exists('tools', $llmOptions) && null !== $this->toolRegistry && $this->llm->supportsToolCalling()) {
$llmOptions['tools'] = $this->toolRegistry->getMap();
if (!array_key_exists('tools', $llmOptions) && null !== $this->toolBox && $this->llm->supportsToolCalling()) {
$llmOptions['tools'] = $this->toolBox->getMap();
}

if (array_key_exists('output_structure', $llmOptions) && null !== $this->responseFormatFactory && $this->llm->supportsStructuredOutput()) {
Expand All @@ -43,7 +43,7 @@ public function call(MessageBag $messages, array $options = []): string|object
$clonedMessages[] = Message::ofAssistant(toolCalls: $response->getToolCalls());

foreach ($response->getToolCalls() as $toolCall) {
$result = $this->toolRegistry->execute($toolCall);
$result = $this->toolBox->execute($toolCall);
$clonedMessages[] = Message::ofToolCall($toolCall, $result);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ToolBox/Registry.php → src/ToolBox/ToolBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpLlm\LlmChain\Response\ToolCall;

final class Registry implements RegistryInterface
final class ToolBox implements ToolBoxInterface
{
/**
* @var list<object>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpLlm\LlmChain\Response\ToolCall;

interface RegistryInterface
interface ToolBoxInterface
{
/**
* @return Metadata[]
Expand Down
16 changes: 8 additions & 8 deletions tests/ToolBox/RegistryTest.php → tests/ToolBox/ToolBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@
use PhpLlm\LlmChain\ToolBox\AsTool;
use PhpLlm\LlmChain\ToolBox\Metadata;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PhpLlm\LlmChain\ToolBox\Registry;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Registry::class)]
#[CoversClass(ToolBox::class)]
#[UsesClass(ToolCall::class)]
#[UsesClass(AsTool::class)]
#[UsesClass(Metadata::class)]
#[UsesClass(ParameterAnalyzer::class)]
#[UsesClass(ToolAnalyzer::class)]
final class RegistryTest extends TestCase
final class ToolBoxTest extends TestCase
{
private Registry $registry;
private ToolBox $toolBox;

protected function setUp(): void
{
$toolAnalyzer = new ToolAnalyzer(new ParameterAnalyzer());
$this->registry = new Registry($toolAnalyzer, [
$this->toolBox = new ToolBox($toolAnalyzer, [
new ToolRequiredParams(),
new ToolOptionalParam(),
new ToolNoParams(),
]);
}

public function testFunctionsMap(): void
public function testToolsMap(): void
{
$actual = $this->registry->getMap();
$actual = $this->toolBox->getMap();
$expected = [
[
'type' => 'function',
Expand Down Expand Up @@ -102,7 +102,7 @@ public function testFunctionsMap(): void

public function testExecute(): void
{
$actual = $this->registry->execute(new ToolCall('call_1234', 'tool_required_params', ['text' => 'Hello', 'number' => 3]));
$actual = $this->toolBox->execute(new ToolCall('call_1234', 'tool_required_params', ['text' => 'Hello', 'number' => 3]));
$expected = 'Hello says "3".';

self::assertSame($expected, $actual);
Expand Down