Skip to content

Commit c03ca7f

Browse files
authored
fix: Allow 0 as a value for temperature, topP, etc. (#15)
1 parent 18fdc3d commit c03ca7f

File tree

8 files changed

+101
-4
lines changed

8 files changed

+101
-4
lines changed

src/Schemas/Anthropic/AnthropicStructuredHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function buildPayload(Request $request, ?string $apiVersion): arra
7979
'system' => MessageMap::mapSystemMessages($request->systemPrompts()),
8080
'temperature' => $request->temperature(),
8181
'top_p' => $request->topP(),
82-
]);
82+
], fn ($value): bool => $value !== null);
8383
}
8484

8585
protected function sendRequest(Request $request): void

src/Schemas/Anthropic/AnthropicTextHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function buildPayload(Request $request, ?string $apiVersion): arra
8080
'top_p' => $request->topP(),
8181
'tools' => ToolMap::map($request->tools()),
8282
'tool_choice' => ToolChoiceMap::map($request->toolChoice()),
83-
]);
83+
], fn ($value): bool => $value !== null);
8484
}
8585

8686
protected function sendRequest(Request $request): void

src/Schemas/Converse/ConverseStructuredHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function buildPayload(Request $request): array
7777
'maxTokens' => $request->maxTokens(),
7878
'temperature' => $request->temperature(),
7979
'topP' => $request->topP(),
80-
]),
80+
], fn ($value): bool => $value !== null),
8181
'messages' => MessageMap::map($request->messages()),
8282
'performanceConfig' => $request->providerOptions('performanceConfig'),
8383
'promptVariables' => $request->providerOptions('promptVariables'),

src/Schemas/Converse/ConverseTextHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function buildPayload(Request $request, int $stepCount = 0): array
7575
'maxTokens' => $request->maxTokens(),
7676
'temperature' => $request->temperature(),
7777
'topP' => $request->topP(),
78-
]),
78+
], fn ($value): bool => $value !== null),
7979
'messages' => MessageMap::map($request->messages()),
8080
'system' => MessageMap::mapSystemMessages($request->systemPrompts()),
8181
'toolConfig' => $request->tools() === []

tests/Schemas/Anthropic/AnthropicStructuredHandlerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Tests\Schemas\Anthropic;
66

7+
use Illuminate\Http\Client\Request;
8+
use Illuminate\Support\Facades\Http;
79
use Prism\Prism\Prism;
810
use Prism\Prism\Schema\BooleanSchema;
911
use Prism\Prism\Schema\ObjectSchema;
@@ -41,3 +43,32 @@
4143
expect($response->structured['game_time'])->toBeString();
4244
expect($response->structured['coat_required'])->toBeBool();
4345
});
46+
47+
it('does not remove 0 values from payloads', function (): void {
48+
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/structured');
49+
50+
$schema = new ObjectSchema(
51+
'output',
52+
'the output object',
53+
[
54+
new StringSchema('weather', 'The weather forecast'),
55+
new StringSchema('game_time', 'The tigers game time'),
56+
new BooleanSchema('coat_required', 'whether a coat is required'),
57+
],
58+
['weather', 'game_time', 'coat_required']
59+
);
60+
61+
Prism::structured()
62+
->withSchema($schema)
63+
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
64+
->withProviderOptions([
65+
'guardRailConfig' => null,
66+
])
67+
->withMaxTokens(2048)
68+
->usingTemperature(0)
69+
->asStructured();
70+
71+
Http::assertSent(fn (Request $request): \Pest\Mixins\Expectation|\Pest\Expectation => expect($request->data())->toMatchArray([
72+
'temperature' => 0,
73+
]));
74+
});

tests/Schemas/Anthropic/AnthropicTextHandlerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,17 @@
193193

194194
Http::assertSent(fn (Request $request): bool => $request->header('explicitPromptCaching')[0] === 'enabled');
195195
});
196+
197+
it('does not remove 0 values from payloads', function (): void {
198+
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-a-prompt');
199+
200+
Prism::text()
201+
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
202+
->withPrompt('Who are you?')
203+
->usingTemperature(0)
204+
->asText();
205+
206+
Http::assertSent(fn (Request $request): \Pest\Mixins\Expectation|\Pest\Expectation => expect($request->data())->toMatchArray([
207+
'temperature' => 0,
208+
]));
209+
});

tests/Schemas/Converse/ConverseStructuredHandlerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Tests\Schemas\Converse;
66

7+
use Illuminate\Http\Client\Request;
8+
use Illuminate\Support\Facades\Http;
79
use Prism\Bedrock\Enums\BedrockSchema;
810
use Prism\Prism\Prism;
911
use Prism\Prism\Schema\BooleanSchema;
@@ -94,3 +96,36 @@
9496

9597
$fake->assertRequest(fn (array $requests): mixed => expect($requests[0]->providerOptions())->toBe($providerOptions));
9698
});
99+
100+
it('does not remove 0 values from payloads', function (): void {
101+
FixtureResponse::fakeResponseSequence('converse', 'converse/structured');
102+
103+
$schema = new ObjectSchema(
104+
'output',
105+
'the output object',
106+
[
107+
new StringSchema('weather', 'The weather forecast'),
108+
new StringSchema('game_time', 'The tigers game time'),
109+
new BooleanSchema('coat_required', 'whether a coat is required'),
110+
],
111+
['weather', 'game_time', 'coat_required']
112+
);
113+
114+
Prism::structured()
115+
->withSchema($schema)
116+
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
117+
->withProviderOptions([
118+
'apiSchema' => BedrockSchema::Converse,
119+
'guardRailConfig' => null,
120+
])
121+
->withMaxTokens(2048)
122+
->usingTemperature(0)
123+
->asStructured();
124+
125+
Http::assertSent(fn (Request $request): \Pest\Mixins\Expectation|\Pest\Expectation => expect($request->data())->toMatchArray([
126+
'inferenceConfig' => [
127+
'maxTokens' => 2048,
128+
'temperature' => 0,
129+
],
130+
])->not()->toHaveKey('guardRailConfig'));
131+
});

tests/Schemas/Converse/ConverseTextHandlerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,20 @@
273273

274274
$fake->assertRequest(fn (array $requests): mixed => expect($requests[0]->providerOptions())->toBe($providerOptions));
275275
});
276+
277+
it('does not remove zero values from payload', function (): void {
278+
FixtureResponse::fakeResponseSequence('converse', 'converse/generate-text-with-a-prompt');
279+
280+
Prism::text()
281+
->using('bedrock', 'amazon.nova-micro-v1:0')
282+
->withPrompt('Who are you?')
283+
->usingTemperature(0)
284+
->asText();
285+
286+
Http::assertSent(fn (Request $request): \Pest\Mixins\Expectation|\Pest\Expectation => expect($request->data())->toMatchArray([
287+
'inferenceConfig' => [
288+
'temperature' => 0,
289+
'maxTokens' => 2048,
290+
],
291+
]));
292+
});

0 commit comments

Comments
 (0)