-
-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathNativeOutputFormatStructuredStrategy.php
More file actions
56 lines (46 loc) · 1.71 KB
/
Copy pathNativeOutputFormatStructuredStrategy.php
File metadata and controls
56 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace Prism\Prism\Providers\Anthropic\Handlers\StructuredStrategies;
use Illuminate\Http\Client\Response as HttpResponse;
use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\Structured\Response as PrismResponse;
class NativeOutputFormatStructuredStrategy extends AnthropicStructuredStrategy
{
public function appendMessages(): void {}
/**
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
public function mutatePayload(array $payload): array
{
$schemaArray = $this->request->schema()->toArray();
$payload['output_config'] = array_merge($payload['output_config'] ?? [], [
'format' => [
'type' => 'json_schema',
'schema' => $schemaArray,
],
]);
return $payload;
}
public function mutateResponse(HttpResponse $httpResponse, PrismResponse $prismResponse): PrismResponse
{
$structured = json_decode($prismResponse->text, associative: true) ?? [];
return new PrismResponse(
steps: $prismResponse->steps,
text: '',
structured: $structured,
finishReason: $prismResponse->finishReason,
usage: $prismResponse->usage,
meta: $prismResponse->meta,
additionalContent: $prismResponse->additionalContent
);
}
protected function checkStrategySupport(): void
{
if ($this->request->providerOptions('citations') === true) {
throw new PrismException(
'Citations are not supported with native output_format. Please disable citations or use a different structured output strategy.'
);
}
}
}