diff --git a/src/Chain/ToolBox/ToolBox.php b/src/Chain/ToolBox/ToolBox.php index c4cd0f87..a75283dd 100644 --- a/src/Chain/ToolBox/ToolBox.php +++ b/src/Chain/ToolBox/ToolBox.php @@ -56,7 +56,7 @@ public function execute(ToolCall $toolCall): string return json_encode($result, flags: JSON_THROW_ON_ERROR); } - if (is_integer($result) || is_float($result)) { + if (is_integer($result) || is_float($result) || $result instanceof \Stringable) { return (string) $result; } diff --git a/tests/Chain/ToolBox/ToolBoxTest.php b/tests/Chain/ToolBox/ToolBoxTest.php index bf27caac..0ddd1a0e 100644 --- a/tests/Chain/ToolBox/ToolBoxTest.php +++ b/tests/Chain/ToolBox/ToolBoxTest.php @@ -18,6 +18,7 @@ use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolReturningFloat; use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolReturningInteger; use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolReturningJsonSerializable; +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolReturningStringable; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; @@ -43,6 +44,7 @@ protected function setUp(): void new ToolReturningJsonSerializable(), new ToolReturningInteger(), new ToolReturningFloat(), + new ToolReturningStringable(), ]); } @@ -133,6 +135,13 @@ public function toolsMap(): void 'description' => 'A tool returning a float', ], ], + [ + 'type' => 'function', + 'function' => [ + 'name' => 'tool_returning_stringable', + 'description' => 'A tool returning an object which implements \Stringable', + ], + ], ]; self::assertSame(json_encode($expected), json_encode($actual)); @@ -193,4 +202,13 @@ public function executeWithToolReturningFloat(): void $this->toolBox->execute(new ToolCall('call_1234', 'tool_returning_float')) ); } + + #[Test] + public function executeWithToolReturningStringable(): void + { + self::assertSame( + 'Hi!', + $this->toolBox->execute(new ToolCall('call_1234', 'tool_returning_stringable')) + ); + } } diff --git a/tests/Fixture/Tool/ToolReturningStringable.php b/tests/Fixture/Tool/ToolReturningStringable.php new file mode 100644 index 00000000..83c3cd78 --- /dev/null +++ b/tests/Fixture/Tool/ToolReturningStringable.php @@ -0,0 +1,21 @@ +