From 90792a294ae63c8afedc8b6c65768f02f21c1731 Mon Sep 17 00:00:00 2001 From: valtzu Date: Mon, 7 Jul 2025 19:00:49 +0300 Subject: [PATCH] fix: Fix Gemini tool calling Resolves #379 --- .../Bridge/Google/Contract/ToolNormalizer.php | 18 +++----- src/Platform/Bridge/Google/ModelHandler.php | 2 +- .../Google/Contract/ToolNormalizerTest.php | 46 ++++++++----------- 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/Platform/Bridge/Google/Contract/ToolNormalizer.php b/src/Platform/Bridge/Google/Contract/ToolNormalizer.php index 7788404b..0ff48829 100644 --- a/src/Platform/Bridge/Google/Contract/ToolNormalizer.php +++ b/src/Platform/Bridge/Google/Contract/ToolNormalizer.php @@ -29,23 +29,17 @@ protected function supportsModel(Model $model): bool * @param Tool $data * * @return array{ - * functionDeclarations: array{ - * name: string, - * description: string, - * parameters: JsonSchema|array{type: 'object'} - * }[] + * name: string, + * description: string, + * parameters: JsonSchema|array{type: 'object'} * } */ public function normalize(mixed $data, ?string $format = null, array $context = []): array { return [ - 'functionDeclarations' => [ - [ - 'description' => $data->description, - 'name' => $data->name, - 'parameters' => $data->parameters ? $this->removeAdditionalProperties($data->parameters) : null, - ], - ], + 'description' => $data->description, + 'name' => $data->name, + 'parameters' => $data->parameters ? $this->removeAdditionalProperties($data->parameters) : null, ]; } diff --git a/src/Platform/Bridge/Google/ModelHandler.php b/src/Platform/Bridge/Google/ModelHandler.php index a166ebb5..55ce58c8 100644 --- a/src/Platform/Bridge/Google/ModelHandler.php +++ b/src/Platform/Bridge/Google/ModelHandler.php @@ -66,7 +66,7 @@ public function request(Model $model, array|string $payload, array $options = [] unset($generationConfig['generationConfig']['server_tools']); if (isset($options['tools'])) { - $generationConfig['tools'] = $options['tools']; + $generationConfig['tools'][] = ['functionDeclarations' => $options['tools']]; unset($options['tools']); } diff --git a/tests/Platform/Bridge/Google/Contract/ToolNormalizerTest.php b/tests/Platform/Bridge/Google/Contract/ToolNormalizerTest.php index 018716b2..79f08a97 100644 --- a/tests/Platform/Bridge/Google/Contract/ToolNormalizerTest.php +++ b/tests/Platform/Bridge/Google/Contract/ToolNormalizerTest.php @@ -92,29 +92,25 @@ public static function normalizeDataProvider(): iterable ], ), [ - 'functionDeclarations' => [ - [ - 'description' => 'A tool with required parameters', - 'name' => 'tool_required_params', - 'parameters' => [ + 'description' => 'A tool with required parameters', + 'name' => 'tool_required_params', + 'parameters' => [ + 'type' => 'object', + 'properties' => [ + 'text' => [ + 'type' => 'string', + 'description' => 'Text parameter', + ], + 'number' => [ + 'type' => 'integer', + 'description' => 'Number parameter', + ], + 'nestedObject' => [ 'type' => 'object', - 'properties' => [ - 'text' => [ - 'type' => 'string', - 'description' => 'Text parameter', - ], - 'number' => [ - 'type' => 'integer', - 'description' => 'Number parameter', - ], - 'nestedObject' => [ - 'type' => 'object', - 'description' => 'bar', - ], - ], - 'required' => ['text', 'number'], + 'description' => 'bar', ], ], + 'required' => ['text', 'number'], ], ], ]; @@ -127,13 +123,9 @@ public static function normalizeDataProvider(): iterable null, ), [ - 'functionDeclarations' => [ - [ - 'description' => 'A tool without parameters', - 'name' => 'tool_no_params', - 'parameters' => null, - ], - ], + 'description' => 'A tool without parameters', + 'name' => 'tool_no_params', + 'parameters' => null, ], ]; }