Skip to content

feat: support date time tool results #263

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 19, 2025
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
8 changes: 5 additions & 3 deletions examples/toolbox-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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]);

Expand Down
6 changes: 5 additions & 1 deletion src/Chain/Toolbox/ToolResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @param \JsonSerializable|\Stringable|array<int|string, mixed>|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;
Expand All @@ -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;
}
}
2 changes: 2 additions & 0 deletions tests/Chain/Toolbox/ToolResultConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down