-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBedrock.php
More file actions
140 lines (108 loc) · 4.11 KB
/
Bedrock.php
File metadata and controls
140 lines (108 loc) · 4.11 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Prism\Bedrock;
use Aws\Credentials\Credentials;
use Aws\Signature\SignatureV4;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Prism\Bedrock\Enums\BedrockSchema;
use Prism\Prism\Concerns\InitializesClient;
use Prism\Prism\Contracts\PrismRequest;
use Prism\Prism\Embeddings\Request as EmbeddingRequest;
use Prism\Prism\Embeddings\Response as EmbeddingsResponse;
use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\Providers\Provider;
use Prism\Prism\Structured\Request as StructuredRequest;
use Prism\Prism\Structured\Response as StructuredResponse;
use Prism\Prism\Text\Request as TextRequest;
use Prism\Prism\Text\Response as TextResponse;
class Bedrock extends Provider
{
use InitializesClient;
const KEY = 'bedrock';
public function __construct(
#[\SensitiveParameter] protected Credentials $credentials,
protected string $region
) {}
#[\Override]
public function text(TextRequest $request): TextResponse
{
$schema = $this->schema($request);
$handler = $schema->textHandler();
if ($handler === null) {
throw new PrismException("Prism Bedrock does not support text for the {$schema->value} apiSchema.");
}
$client = $this->client(
$request,
$request->clientOptions(),
$request->clientRetry()
);
$handler = new $handler($this, $client);
return $handler->handle($request);
}
#[\Override]
public function structured(StructuredRequest $request): StructuredResponse
{
$schema = $this->schema($request);
$handler = $schema->structuredHandler();
if ($handler === null) {
throw new PrismException("Prism Bedrock does not support structured for the {$schema->value} apiSchema.");
}
$client = $this->client(
$request,
$request->clientOptions(),
$request->clientRetry()
);
$handler = new $handler($this, $client);
return $handler->handle($request);
}
#[\Override]
public function embeddings(EmbeddingRequest $request): EmbeddingsResponse
{
$schema = $this->schema($request);
$handler = $schema->embeddingsHandler();
if ($handler === null) {
throw new PrismException("Prism Bedrock does not support embeddings for the {$schema->value} apiSchema.");
}
$client = $this->client(
$request,
$request->clientOptions(),
$request->clientRetry()
);
$handler = new $handler($this, $client);
return $handler->handle($request);
}
public function schema(PrismRequest $request): BedrockSchema
{
$override = $request->providerOptions();
$override = data_get($override, 'apiSchema');
return $override ?? BedrockSchema::fromModelString($request->model());
}
public function apiVersion(PrismRequest $request): ?string
{
return $this->schema($request)->defaultApiVersion();
}
/**
* @param array<string, mixed> $options
* @param array<mixed> $retry
*/
protected function client(TextRequest|StructuredRequest|EmbeddingRequest $request, array $options = [], array $retry = []): PendingRequest
{
$model = $request->model();
$enableCaching = $request instanceof EmbeddingRequest
? false
: $request->providerOptions('enableCaching') ?? false;
return $this->baseClient()
->acceptJson()
->withHeader('explicitPromptCaching', $enableCaching ? 'enabled' : 'disabled')
->contentType('application/json')
->withOptions($options)
->retry(...$retry)
->baseUrl("https://bedrock-runtime.{$this->region}.amazonaws.com/model/$model/")
->beforeSending(function (Request $request) {
$request = $request->toPsrRequest();
$signature = new SignatureV4('bedrock', $this->region);
return $signature->signRequest($request, $this->credentials);
})
->throw();
}
}