From 1acb86a7db402add76b0f076b7e8c230eaafe619 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Thu, 20 Mar 2025 00:10:21 +0100 Subject: [PATCH] feat: support date time results --- examples/toolbox-clock.php | 8 +++++--- src/Chain/Toolbox/ToolResultConverter.php | 6 +++++- tests/Chain/Toolbox/ToolResultConverterTest.php | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/toolbox-clock.php b/examples/toolbox-clock.php index 4f55a68..92128df 100644 --- a/examples/toolbox-clock.php +++ b/examples/toolbox-clock.php @@ -4,10 +4,11 @@ use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; use PhpLlm\LlmChain\Chain; use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor; -use PhpLlm\LlmChain\Chain\Toolbox\Tool\Clock; +use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory; use PhpLlm\LlmChain\Chain\Toolbox\Toolbox; use PhpLlm\LlmChain\Model\Message\Message; use PhpLlm\LlmChain\Model\Message\MessageBag; +use Symfony\Component\Clock\Clock; use Symfony\Component\Dotenv\Dotenv; require_once dirname(__DIR__).'/vendor/autoload.php'; @@ -21,8 +22,9 @@ $platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); $llm = new GPT(GPT::GPT_4O_MINI); -$clock = new Clock(); -$toolbox = Toolbox::create($clock); +$metadataFactory = (new MemoryFactory()) + ->addTool(Clock::class, 'clock', 'Get the current date and time', 'now'); +$toolbox = new Toolbox($metadataFactory, [new Clock()]); $processor = new ChainProcessor($toolbox); $chain = new Chain($platform, $llm, [$processor], [$processor]); diff --git a/src/Chain/Toolbox/ToolResultConverter.php b/src/Chain/Toolbox/ToolResultConverter.php index 4fd57d5..140ecb2 100644 --- a/src/Chain/Toolbox/ToolResultConverter.php +++ b/src/Chain/Toolbox/ToolResultConverter.php @@ -9,7 +9,7 @@ /** * @param \JsonSerializable|\Stringable|array|float|string|null $result */ - public function convert(\JsonSerializable|\Stringable|array|float|string|null $result): ?string + public function convert(\JsonSerializable|\Stringable|array|float|string|\DateTimeInterface|null $result): ?string { if (null === $result) { return null; @@ -23,6 +23,10 @@ public function convert(\JsonSerializable|\Stringable|array|float|string|null $r return (string) $result; } + if ($result instanceof \DateTimeInterface) { + return $result->format(DATE_ATOM); + } + return $result; } } diff --git a/tests/Chain/Toolbox/ToolResultConverterTest.php b/tests/Chain/Toolbox/ToolResultConverterTest.php index e0642fe..8576034 100644 --- a/tests/Chain/Toolbox/ToolResultConverterTest.php +++ b/tests/Chain/Toolbox/ToolResultConverterTest.php @@ -34,6 +34,8 @@ public static function provideResults(): \Generator yield 'string' => ['plain string', 'plain string']; + yield 'datetime' => [new \DateTimeImmutable('2021-07-31 12:34:56'), '2021-07-31T12:34:56+00:00']; + yield 'stringable' => [ new class implements \Stringable { public function __toString(): string