|
97 | 97 | $fake->assertRequest(fn (array $requests): mixed => expect($requests[0]->providerOptions())->toBe($providerOptions)); |
98 | 98 | }); |
99 | 99 |
|
| 100 | +it('uses custom jsonModeMessage when provided via providerOptions', 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 | + $customMessage = 'Please return a JSON response using this custom format instruction'; |
| 115 | + |
| 116 | + Prism::structured() |
| 117 | + ->withSchema($schema) |
| 118 | + ->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0') |
| 119 | + ->withProviderOptions([ |
| 120 | + 'apiSchema' => BedrockSchema::Converse, |
| 121 | + 'jsonModeMessage' => $customMessage, |
| 122 | + ]) |
| 123 | + ->withSystemPrompt('The tigers game is at 3pm and the temperature will be 70º') |
| 124 | + ->withPrompt('What time is the tigers game today and should I wear a coat?') |
| 125 | + ->asStructured(); |
| 126 | + |
| 127 | + Http::assertSent(function (Request $request) use ($customMessage): bool { |
| 128 | + $messages = $request->data()['messages'] ?? []; |
| 129 | + $lastMessage = end($messages); |
| 130 | + |
| 131 | + return isset($lastMessage['content'][0]['text']) && |
| 132 | + str_contains((string) $lastMessage['content'][0]['text'], $customMessage); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +it('uses default jsonModeMessage when no custom message is provided', function (): void { |
| 137 | + FixtureResponse::fakeResponseSequence('converse', 'converse/structured'); |
| 138 | + |
| 139 | + $schema = new ObjectSchema( |
| 140 | + 'output', |
| 141 | + 'the output object', |
| 142 | + [ |
| 143 | + new StringSchema('weather', 'The weather forecast'), |
| 144 | + new StringSchema('game_time', 'The tigers game time'), |
| 145 | + new BooleanSchema('coat_required', 'whether a coat is required'), |
| 146 | + ], |
| 147 | + ['weather', 'game_time', 'coat_required'] |
| 148 | + ); |
| 149 | + |
| 150 | + $defaultMessage = 'Respond with ONLY JSON (i.e. not in backticks or a code block, with NO CONTENT outside the JSON) that matches the following schema:'; |
| 151 | + |
| 152 | + Prism::structured() |
| 153 | + ->withSchema($schema) |
| 154 | + ->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0') |
| 155 | + ->withProviderOptions([ |
| 156 | + 'apiSchema' => BedrockSchema::Converse, |
| 157 | + ]) |
| 158 | + ->withSystemPrompt('The tigers game is at 3pm and the temperature will be 70º') |
| 159 | + ->withPrompt('What time is the tigers game today and should I wear a coat?') |
| 160 | + ->asStructured(); |
| 161 | + |
| 162 | + Http::assertSent(function (Request $request) use ($defaultMessage): bool { |
| 163 | + $messages = $request->data()['messages'] ?? []; |
| 164 | + $lastMessage = end($messages); |
| 165 | + |
| 166 | + return isset($lastMessage['content'][0]['text']) && |
| 167 | + str_contains((string) $lastMessage['content'][0]['text'], $defaultMessage); |
| 168 | + }); |
| 169 | +}); |
| 170 | + |
100 | 171 | it('does not remove 0 values from payloads', function (): void { |
101 | 172 | FixtureResponse::fakeResponseSequence('converse', 'converse/structured'); |
102 | 173 |
|
|
0 commit comments