Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 1d5ef0a

Browse files
authored
feat: support date time tool results (#263)
1 parent 06bda00 commit 1d5ef0a

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

examples/toolbox-clock.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
55
use PhpLlm\LlmChain\Chain;
66
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
7-
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Clock;
7+
use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory;
88
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
99
use PhpLlm\LlmChain\Model\Message\Message;
1010
use PhpLlm\LlmChain\Model\Message\MessageBag;
11+
use Symfony\Component\Clock\Clock;
1112
use Symfony\Component\Dotenv\Dotenv;
1213

1314
require_once dirname(__DIR__).'/vendor/autoload.php';
@@ -21,8 +22,9 @@
2122
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
2223
$llm = new GPT(GPT::GPT_4O_MINI);
2324

24-
$clock = new Clock();
25-
$toolbox = Toolbox::create($clock);
25+
$metadataFactory = (new MemoryFactory())
26+
->addTool(Clock::class, 'clock', 'Get the current date and time', 'now');
27+
$toolbox = new Toolbox($metadataFactory, [new Clock()]);
2628
$processor = new ChainProcessor($toolbox);
2729
$chain = new Chain($platform, $llm, [$processor], [$processor]);
2830

src/Chain/Toolbox/ToolResultConverter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @param \JsonSerializable|\Stringable|array<int|string, mixed>|float|string|null $result
1111
*/
12-
public function convert(\JsonSerializable|\Stringable|array|float|string|null $result): ?string
12+
public function convert(\JsonSerializable|\Stringable|array|float|string|\DateTimeInterface|null $result): ?string
1313
{
1414
if (null === $result) {
1515
return null;
@@ -23,6 +23,10 @@ public function convert(\JsonSerializable|\Stringable|array|float|string|null $r
2323
return (string) $result;
2424
}
2525

26+
if ($result instanceof \DateTimeInterface) {
27+
return $result->format(DATE_ATOM);
28+
}
29+
2630
return $result;
2731
}
2832
}

tests/Chain/Toolbox/ToolResultConverterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public static function provideResults(): \Generator
3434

3535
yield 'string' => ['plain string', 'plain string'];
3636

37+
yield 'datetime' => [new \DateTimeImmutable('2021-07-31 12:34:56'), '2021-07-31T12:34:56+00:00'];
38+
3739
yield 'stringable' => [
3840
new class implements \Stringable {
3941
public function __toString(): string

0 commit comments

Comments
 (0)