Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Dec 19, 2024
1 parent f8dfdcf commit 4d9de89
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Chain/ToolBox/ToolBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Chain/ToolBox/ToolBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,7 @@ protected function setUp(): void
new ToolReturningJsonSerializable(),
new ToolReturningInteger(),
new ToolReturningFloat(),
new ToolReturningStringable(),
]);
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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'))
);
}
}
21 changes: 21 additions & 0 deletions tests/Fixture/Tool/ToolReturningStringable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Fixture\Tool;

use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;

#[AsTool('tool_returning_stringable', 'A tool returning an object which implements \Stringable')]
final class ToolReturningStringable
{
public function __invoke(): \Stringable
{
return new class implements \Stringable {
public function __toString(): string
{
return 'Hi!';
}
};
}
}

0 comments on commit 4d9de89

Please sign in to comment.