Skip to content

Commit 52d89c5

Browse files
authored
fix(gemini): tool property mapping (#379)
1 parent 640a2d2 commit 52d89c5

File tree

19 files changed

+193
-75
lines changed

19 files changed

+193
-75
lines changed

src/Providers/Anthropic/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function map(array $tools): array
2323
'description' => $tool->description(),
2424
'input_schema' => [
2525
'type' => 'object',
26-
'properties' => $tool->parameters(),
26+
'properties' => $tool->parametersAsArray(),
2727
'required' => $tool->requiredParameters(),
2828
],
2929
'cache_control' => $cacheType ? ['type' => $cacheType instanceof UnitEnum ? $cacheType->name : $cacheType] : null,

src/Providers/DeepSeek/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function Map(array $tools): array
2121
'description' => $tool->description(),
2222
'parameters' => [
2323
'type' => 'object',
24-
'properties' => $tool->parameters(),
24+
'properties' => $tool->parametersAsArray(),
2525
'required' => $tool->requiredParameters(),
2626
],
2727
],

src/Providers/Gemini/Maps/ToolMap.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Prism\Prism\Providers\Gemini\Maps;
66

7+
use Illuminate\Support\Arr;
8+
use Prism\Prism\Contracts\Schema;
79
use Prism\Prism\Tool;
810

911
class ToolMap
@@ -24,10 +26,21 @@ public static function map(array $tools): array
2426
...$tool->hasParameters() ? [
2527
'parameters' => [
2628
'type' => 'object',
27-
'properties' => $tool->parameters(),
29+
'properties' => self::mapProperties($tool->parameters()),
2830
'required' => $tool->requiredParameters(),
2931
],
3032
] : [],
3133
], $tools);
3234
}
35+
36+
/**
37+
* @param array<string,Schema> $properties
38+
* @return array<string,mixed>
39+
*/
40+
public static function mapProperties(array $properties): array
41+
{
42+
return Arr::mapWithKeys($properties, fn (Schema $schema, string $name) => [
43+
$name => (new SchemaMap($schema))->toArray(),
44+
]);
45+
}
3346
}

src/Providers/Groq/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function Map(array $tools): array
2121
'description' => $tool->description(),
2222
'parameters' => [
2323
'type' => 'object',
24-
'properties' => $tool->parameters(),
24+
'properties' => $tool->parametersAsArray(),
2525
'required' => $tool->requiredParameters(),
2626
],
2727
],

src/Providers/Mistral/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function map(array $tools): array
2121
'description' => $tool->description(),
2222
'parameters' => [
2323
'type' => 'object',
24-
'properties' => $tool->parameters(),
24+
'properties' => $tool->parametersAsArray(),
2525
'required' => $tool->requiredParameters(),
2626
],
2727
],

src/Providers/Ollama/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function map(array $tools): array
2121
'description' => $tool->description(),
2222
'parameters' => [
2323
'type' => 'object',
24-
...$tool->parameters() ? ['properties' => $tool->parameters()] : [],
24+
...$tool->parameters() ? ['properties' => $tool->parametersAsArray()] : [],
2525
'required' => $tool->requiredParameters(),
2626
],
2727
],

src/Providers/OpenAI/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function Map(array $tools): array
2222
...count($tool->parameters()) ? [
2323
'parameters' => [
2424
'type' => 'object',
25-
'properties' => $tool->parameters(),
25+
'properties' => $tool->parametersAsArray(),
2626
'required' => $tool->requiredParameters(),
2727
],
2828
] : [],

src/Providers/XAI/Maps/ToolMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function Map(array $tools): array
2121
'description' => $tool->description(),
2222
'parameters' => [
2323
'type' => 'object',
24-
'properties' => $tool->parameters(),
24+
'properties' => $tool->parametersAsArray(),
2525
'required' => $tool->requiredParameters(),
2626
],
2727
],

src/Tool.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArgumentCountError;
88
use Closure;
99
use Error;
10+
use Illuminate\Support\Arr;
1011
use InvalidArgumentException;
1112
use Prism\Prism\Concerns\HasProviderOptions;
1213
use Prism\Prism\Contracts\Schema;
@@ -28,7 +29,7 @@ class Tool
2829

2930
protected string $description;
3031

31-
/** @var array<string, array<string, mixed>> */
32+
/** @var array<string,Schema> */
3233
protected array $parameters = [];
3334

3435
/** @var array <int, string> */
@@ -60,7 +61,7 @@ public function using(Closure|callable $fn): self
6061

6162
public function withParameter(Schema $parameter, bool $required = true): self
6263
{
63-
$this->parameters[$parameter->name()] = $parameter->toArray();
64+
$this->parameters[$parameter->name()] = $parameter;
6465

6566
if ($required) {
6667
$this->requiredParameters[] = $parameter->name();
@@ -146,13 +147,23 @@ public function requiredParameters(): array
146147
}
147148

148149
/**
149-
* @return array<string, array<string, mixed>>
150+
* @return array<string,Schema>
150151
*/
151152
public function parameters(): array
152153
{
153154
return $this->parameters;
154155
}
155156

157+
/**
158+
* @return array<string, array<string,mixed>>
159+
*/
160+
public function parametersAsArray(): array
161+
{
162+
return Arr::mapWithKeys($this->parameters, fn (Schema $schema, string $name): array => [
163+
$name => $schema->toArray(),
164+
]);
165+
}
166+
156167
public function name(): string
157168
{
158169
return $this->name;
Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
{
2-
"candidates": [
3-
{
4-
"content": {
5-
"parts": [
6-
{
7-
"functionCall": {
8-
"name": "weather",
9-
"args": {
10-
"city": "London"
11-
}
12-
}
13-
}
14-
],
15-
"role": "model"
16-
},
17-
"finishReason": "STOP",
18-
"avgLogprobs": -0.036342903971672
19-
}
20-
],
21-
"usageMetadata": {
22-
"promptTokenCount": 91,
23-
"candidatesTokenCount": 3,
24-
"totalTokenCount": 94,
25-
"promptTokensDetails": [
26-
{
27-
"modality": "TEXT",
28-
"tokenCount": 23
2+
"candidates": [
3+
{
4+
"content": {
5+
"parts": [
6+
{
7+
"functionCall": {
8+
"name": "weather",
9+
"args": {
10+
"options": {
11+
"decimals": false,
12+
"days": 1,
13+
"alerts": [],
14+
"unit": "Fahrenheit"
15+
},
16+
"city": "Detroit"
17+
}
2918
}
19+
}
3020
],
31-
"candidatesTokensDetails": [
32-
{
33-
"modality": "TEXT",
34-
"tokenCount": 3
35-
}
36-
]
37-
},
38-
"modelVersion": "gemini-1.5-flash"
39-
}
21+
"role": "model"
22+
},
23+
"finishReason": "STOP",
24+
"avgLogprobs": -0.64373282591501868
25+
}
26+
],
27+
"usageMetadata": {
28+
"promptTokenCount": 124,
29+
"candidatesTokenCount": 12,
30+
"totalTokenCount": 136,
31+
"promptTokensDetails": [
32+
{
33+
"modality": "TEXT",
34+
"tokenCount": 124
35+
}
36+
],
37+
"candidatesTokensDetails": [
38+
{
39+
"modality": "TEXT",
40+
"tokenCount": 12
41+
}
42+
]
43+
},
44+
"modelVersion": "gemini-1.5-flash",
45+
"responseId": "TBM3aMLqO42RsbQP1MbQ6AY"
46+
}

tests/Providers/DeepSeek/ToolTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
'description' => $tool->description(),
2020
'parameters' => [
2121
'type' => 'object',
22-
'properties' => $tool->parameters(),
22+
'properties' => [
23+
'query' => [
24+
'description' => 'the detailed search query',
25+
'type' => 'string',
26+
],
27+
],
2328
'required' => $tool->requiredParameters(),
2429
],
2530
],
@@ -43,7 +48,12 @@
4348
'description' => $tool->description(),
4449
'parameters' => [
4550
'type' => 'object',
46-
'properties' => $tool->parameters(),
51+
'properties' => [
52+
'query' => [
53+
'description' => 'the detailed search query',
54+
'type' => 'string',
55+
],
56+
],
4757
'required' => $tool->requiredParameters(),
4858
],
4959
],

0 commit comments

Comments
 (0)