mirror of
https://github.com/symfony/ai.git
synced 2026-03-23 23:42:18 +01:00
[Platform][Generic][Scaleway] Fix tool call without arguments
This commit is contained in:
committed by
Christopher Hertel
parent
07f054655e
commit
59601c0bbc
@@ -70,7 +70,13 @@ trait CompletionsConversionTrait
|
||||
}
|
||||
|
||||
// add arguments delta to tool call
|
||||
$toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments'];
|
||||
if (isset($toolCall['function']['arguments'])) {
|
||||
if (!isset($toolCalls[$i]['function']['arguments'])) {
|
||||
$toolCalls[$i]['function']['arguments'] = '';
|
||||
}
|
||||
|
||||
$toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments'];
|
||||
}
|
||||
}
|
||||
|
||||
return $toolCalls;
|
||||
@@ -131,13 +137,17 @@ trait CompletionsConversionTrait
|
||||
* type: 'function',
|
||||
* function: array{
|
||||
* name: string,
|
||||
* arguments: string
|
||||
* arguments?: string
|
||||
* }
|
||||
* } $toolCall
|
||||
*/
|
||||
protected function convertToolCall(array $toolCall): ToolCall
|
||||
{
|
||||
$arguments = json_decode($toolCall['function']['arguments'], true, flags: \JSON_THROW_ON_ERROR);
|
||||
if (isset($toolCall['function']['arguments']) && '' !== $toolCall['function']['arguments']) {
|
||||
$arguments = json_decode($toolCall['function']['arguments'], true, flags: \JSON_THROW_ON_ERROR);
|
||||
} else {
|
||||
$arguments = [];
|
||||
}
|
||||
|
||||
return new ToolCall($toolCall['id'], $toolCall['function']['name'], $arguments);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class ResultConverterTest extends TestCase
|
||||
$this->assertSame('Hello world', $result->getContent());
|
||||
}
|
||||
|
||||
public function testConvertToolCallResult()
|
||||
public function testConvertToolWithArgsCallResult()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
$httpResponse = self::createMock(ResponseInterface::class);
|
||||
@@ -84,6 +84,77 @@ class ResultConverterTest extends TestCase
|
||||
$this->assertSame(['arg1' => 'value1'], $toolCalls[0]->getArguments());
|
||||
}
|
||||
|
||||
public function testConvertToolWithEmptyArgsCallResult()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
$httpResponse = self::createMock(ResponseInterface::class);
|
||||
$httpResponse->method('toArray')->willReturn([
|
||||
'choices' => [
|
||||
[
|
||||
'message' => [
|
||||
'role' => 'assistant',
|
||||
'content' => null,
|
||||
'tool_calls' => [
|
||||
[
|
||||
'id' => 'call_123',
|
||||
'type' => 'function',
|
||||
'function' => [
|
||||
'name' => 'test_function',
|
||||
'arguments' => '',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'finish_reason' => 'tool_calls',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$result = $converter->convert(new RawHttpResult($httpResponse));
|
||||
|
||||
$this->assertInstanceOf(ToolCallResult::class, $result);
|
||||
$toolCalls = $result->getContent();
|
||||
$this->assertCount(1, $toolCalls);
|
||||
$this->assertSame('call_123', $toolCalls[0]->getId());
|
||||
$this->assertSame('test_function', $toolCalls[0]->getName());
|
||||
$this->assertSame([], $toolCalls[0]->getArguments());
|
||||
}
|
||||
|
||||
public function testConvertToolWithoutArgsCallResult()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
$httpResponse = self::createMock(ResponseInterface::class);
|
||||
$httpResponse->method('toArray')->willReturn([
|
||||
'choices' => [
|
||||
[
|
||||
'message' => [
|
||||
'role' => 'assistant',
|
||||
'content' => null,
|
||||
'tool_calls' => [
|
||||
[
|
||||
'id' => 'call_123',
|
||||
'type' => 'function',
|
||||
'function' => [
|
||||
'name' => 'test_function',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'finish_reason' => 'tool_calls',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$result = $converter->convert(new RawHttpResult($httpResponse));
|
||||
|
||||
$this->assertInstanceOf(ToolCallResult::class, $result);
|
||||
$toolCalls = $result->getContent();
|
||||
$this->assertCount(1, $toolCalls);
|
||||
$this->assertSame('call_123', $toolCalls[0]->getId());
|
||||
$this->assertSame('test_function', $toolCalls[0]->getName());
|
||||
$this->assertSame([], $toolCalls[0]->getArguments());
|
||||
}
|
||||
|
||||
public function testConvertMultipleChoices()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
|
||||
@@ -49,7 +49,7 @@ final class ResultConverterTest extends TestCase
|
||||
$this->assertSame('Hello world', $result->getContent());
|
||||
}
|
||||
|
||||
public function testConvertToolCallResult()
|
||||
public function testConvertToolWithArgsCallResult()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
$httpResponse = self::createMock(ResponseInterface::class);
|
||||
@@ -85,6 +85,77 @@ final class ResultConverterTest extends TestCase
|
||||
$this->assertSame(['arg1' => 'value1'], $toolCalls[0]->getArguments());
|
||||
}
|
||||
|
||||
public function testConvertToolWithEmptyArgsCallResult()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
$httpResponse = self::createMock(ResponseInterface::class);
|
||||
$httpResponse->method('toArray')->willReturn([
|
||||
'choices' => [
|
||||
[
|
||||
'message' => [
|
||||
'role' => 'assistant',
|
||||
'content' => null,
|
||||
'tool_calls' => [
|
||||
[
|
||||
'id' => 'call_123',
|
||||
'type' => 'function',
|
||||
'function' => [
|
||||
'name' => 'test_function',
|
||||
'arguments' => '',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'finish_reason' => 'tool_calls',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$result = $converter->convert(new RawHttpResult($httpResponse));
|
||||
|
||||
$this->assertInstanceOf(ToolCallResult::class, $result);
|
||||
$toolCalls = $result->getContent();
|
||||
$this->assertCount(1, $toolCalls);
|
||||
$this->assertSame('call_123', $toolCalls[0]->getId());
|
||||
$this->assertSame('test_function', $toolCalls[0]->getName());
|
||||
$this->assertSame([], $toolCalls[0]->getArguments());
|
||||
}
|
||||
|
||||
public function testConvertToolWithoutArgsCallResult()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
$httpResponse = self::createMock(ResponseInterface::class);
|
||||
$httpResponse->method('toArray')->willReturn([
|
||||
'choices' => [
|
||||
[
|
||||
'message' => [
|
||||
'role' => 'assistant',
|
||||
'content' => null,
|
||||
'tool_calls' => [
|
||||
[
|
||||
'id' => 'call_123',
|
||||
'type' => 'function',
|
||||
'function' => [
|
||||
'name' => 'test_function',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'finish_reason' => 'tool_calls',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$result = $converter->convert(new RawHttpResult($httpResponse));
|
||||
|
||||
$this->assertInstanceOf(ToolCallResult::class, $result);
|
||||
$toolCalls = $result->getContent();
|
||||
$this->assertCount(1, $toolCalls);
|
||||
$this->assertSame('call_123', $toolCalls[0]->getId());
|
||||
$this->assertSame('test_function', $toolCalls[0]->getName());
|
||||
$this->assertSame([], $toolCalls[0]->getArguments());
|
||||
}
|
||||
|
||||
public function testConvertMultipleChoices()
|
||||
{
|
||||
$converter = new ResultConverter();
|
||||
|
||||
Reference in New Issue
Block a user