-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
web.ts
405 lines (351 loc) Β· 11.3 KB
/
web.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { SignatureV4 } from "@smithy/signature-v4";
import { HttpRequest } from "@smithy/protocol-http";
import { EventStreamCodec } from "@smithy/eventstream-codec";
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
import { Sha256 } from "@aws-crypto/sha256-js";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import { GenerationChunk } from "@langchain/core/outputs";
import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms";
import {
BaseBedrockInput,
BedrockLLMInputOutputAdapter,
type CredentialType,
} from "../../utils/bedrock/index.js";
import type { SerializedFields } from "../../load/map_keys.js";
const AWS_REGIONS = [
"us",
"sa",
"me",
"il",
"eu",
"cn",
"ca",
"ap",
"af",
"us-gov",
];
const ALLOWED_MODEL_PROVIDERS = [
"ai21",
"anthropic",
"amazon",
"cohere",
"meta",
"mistral",
];
const PRELUDE_TOTAL_LENGTH_BYTES = 4;
/**
* A type of Large Language Model (LLM) that interacts with the Bedrock
* service. It extends the base `LLM` class and implements the
* `BaseBedrockInput` interface. The class is designed to authenticate and
* interact with the Bedrock service, which is a part of Amazon Web
* Services (AWS). It uses AWS credentials for authentication and can be
* configured with various parameters such as the model to use, the AWS
* region, and the maximum number of tokens to generate.
*/
export class Bedrock extends LLM implements BaseBedrockInput {
model = "amazon.titan-tg1-large";
modelProvider: string;
region: string;
credentials: CredentialType;
temperature?: number | undefined = undefined;
maxTokens?: number | undefined = undefined;
fetchFn: typeof fetch;
endpointHost?: string;
/** @deprecated */
stopSequences?: string[];
modelKwargs?: Record<string, unknown>;
codec: EventStreamCodec = new EventStreamCodec(toUtf8, fromUtf8);
streaming = false;
lc_serializable = true;
get lc_aliases(): Record<string, string> {
return {
model: "model_id",
region: "region_name",
};
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
"credentials.accessKeyId": "BEDROCK_AWS_ACCESS_KEY_ID",
"credentials.secretAccessKey": "BEDROCK_AWS_SECRET_ACCESS_KEY",
};
}
get lc_attributes(): SerializedFields | undefined {
return { region: this.region };
}
_llmType() {
return "bedrock";
}
static lc_name() {
return "Bedrock";
}
constructor(fields?: Partial<BaseBedrockInput> & BaseLLMParams) {
super(fields ?? {});
this.model = fields?.model ?? this.model;
this.modelProvider = getModelProvider(this.model);
if (!ALLOWED_MODEL_PROVIDERS.includes(this.modelProvider)) {
throw new Error(
`Unknown model provider: '${this.modelProvider}', only these are supported: ${ALLOWED_MODEL_PROVIDERS}`
);
}
const region =
fields?.region ?? getEnvironmentVariable("AWS_DEFAULT_REGION");
if (!region) {
throw new Error(
"Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field."
);
}
this.region = region;
const credentials = fields?.credentials;
if (!credentials) {
throw new Error(
"Please set the AWS credentials in the 'credentials' field."
);
}
this.credentials = credentials;
this.temperature = fields?.temperature ?? this.temperature;
this.maxTokens = fields?.maxTokens ?? this.maxTokens;
this.fetchFn = fields?.fetchFn ?? fetch.bind(globalThis);
this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
this.stopSequences = fields?.stopSequences;
this.modelKwargs = fields?.modelKwargs;
this.streaming = fields?.streaming ?? this.streaming;
}
/** Call out to Bedrock service model.
Arguments:
prompt: The prompt to pass into the model.
Returns:
The string generated by the model.
Example:
response = model.invoke("Tell me a joke.")
*/
async _call(
prompt: string,
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<string> {
const service = "bedrock-runtime";
const endpointHost =
this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
const provider = this.modelProvider;
if (this.streaming) {
const stream = this._streamResponseChunks(prompt, options, runManager);
let finalResult: GenerationChunk | undefined;
for await (const chunk of stream) {
if (finalResult === undefined) {
finalResult = chunk;
} else {
finalResult = finalResult.concat(chunk);
}
}
return finalResult?.text ?? "";
}
const response = await this._signedFetch(prompt, options, {
bedrockMethod: "invoke",
endpointHost,
provider,
});
const json = await response.json();
if (!response.ok) {
throw new Error(
`Error ${response.status}: ${json.message ?? JSON.stringify(json)}`
);
}
const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
return text;
}
async _signedFetch(
prompt: string,
options: this["ParsedCallOptions"],
fields: {
bedrockMethod: "invoke" | "invoke-with-response-stream";
endpointHost: string;
provider: string;
}
) {
const { bedrockMethod, endpointHost, provider } = fields;
const inputBody = BedrockLLMInputOutputAdapter.prepareInput(
provider,
prompt,
this.maxTokens,
this.temperature,
options.stop ?? this.stopSequences,
this.modelKwargs,
fields.bedrockMethod
);
const url = new URL(
`https://${endpointHost}/model/${this.model}/${bedrockMethod}`
);
const request = new HttpRequest({
hostname: url.hostname,
path: url.pathname,
protocol: url.protocol,
method: "POST", // method must be uppercase
body: JSON.stringify(inputBody),
query: Object.fromEntries(url.searchParams.entries()),
headers: {
// host is required by AWS Signature V4: https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
host: url.host,
accept: "application/json",
"content-type": "application/json",
},
});
const signer = new SignatureV4({
credentials: this.credentials,
service: "bedrock",
region: this.region,
sha256: Sha256,
});
const signedRequest = await signer.sign(request);
// Send request to AWS using the low-level fetch API
const response = await this.caller.callWithOptions(
{ signal: options.signal },
async () =>
this.fetchFn(url, {
headers: signedRequest.headers,
body: signedRequest.body,
method: signedRequest.method,
})
);
return response;
}
invocationParams(options?: this["ParsedCallOptions"]) {
return {
model: this.model,
region: this.region,
temperature: this.temperature,
maxTokens: this.maxTokens,
stop: options?.stop ?? this.stopSequences,
modelKwargs: this.modelKwargs,
};
}
async *_streamResponseChunks(
prompt: string,
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<GenerationChunk> {
const provider = this.modelProvider;
const bedrockMethod =
provider === "anthropic" ||
provider === "cohere" ||
provider === "meta" ||
provider === "mistral"
? "invoke-with-response-stream"
: "invoke";
const service = "bedrock-runtime";
const endpointHost =
this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
// Send request to AWS using the low-level fetch API
const response = await this._signedFetch(prompt, options, {
bedrockMethod,
endpointHost,
provider,
});
if (response.status < 200 || response.status >= 300) {
throw Error(
`Failed to access underlying url '${endpointHost}': got ${
response.status
} ${response.statusText}: ${await response.text()}`
);
}
if (
provider === "anthropic" ||
provider === "cohere" ||
provider === "meta" ||
provider === "mistral"
) {
const reader = response.body?.getReader();
const decoder = new TextDecoder();
for await (const chunk of this._readChunks(reader)) {
const event = this.codec.decode(chunk);
if (
(event.headers[":event-type"] !== undefined &&
event.headers[":event-type"].value !== "chunk") ||
event.headers[":content-type"].value !== "application/json"
) {
throw Error(`Failed to get event chunk: got ${chunk}`);
}
const body = JSON.parse(decoder.decode(event.body));
if (body.message) {
throw new Error(body.message);
}
if (body.bytes !== undefined) {
const chunkResult = JSON.parse(
decoder.decode(
Uint8Array.from(atob(body.bytes), (m) => m.codePointAt(0) ?? 0)
)
);
const text = BedrockLLMInputOutputAdapter.prepareOutput(
provider,
chunkResult
);
yield new GenerationChunk({
text,
generationInfo: {},
});
// eslint-disable-next-line no-void
void runManager?.handleLLMNewToken(text);
}
}
} else {
const json = await response.json();
const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
yield new GenerationChunk({
text,
generationInfo: {},
});
// eslint-disable-next-line no-void
void runManager?.handleLLMNewToken(text);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_readChunks(reader: any) {
function _concatChunks(a: Uint8Array, b: Uint8Array) {
const newBuffer = new Uint8Array(a.length + b.length);
newBuffer.set(a);
newBuffer.set(b, a.length);
return newBuffer;
}
function getMessageLength(buffer: Uint8Array) {
if (buffer.byteLength < PRELUDE_TOTAL_LENGTH_BYTES) return 0;
const view = new DataView(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength
);
return view.getUint32(0, false);
}
return {
async *[Symbol.asyncIterator]() {
let readResult = await reader.read();
let buffer: Uint8Array = new Uint8Array(0);
while (!readResult.done) {
const chunk: Uint8Array = readResult.value;
buffer = _concatChunks(buffer, chunk);
let messageLength = getMessageLength(buffer);
while (
buffer.byteLength >= PRELUDE_TOTAL_LENGTH_BYTES &&
buffer.byteLength >= messageLength
) {
yield buffer.slice(0, messageLength);
buffer = buffer.slice(messageLength);
messageLength = getMessageLength(buffer);
}
readResult = await reader.read();
}
},
};
}
}
function isInferenceModel(modelId: string): boolean {
const parts = modelId.split(".");
return AWS_REGIONS.some((region) => parts[0] === region);
}
function getModelProvider(modelId: string): string {
const parts = modelId.split(".");
if (isInferenceModel(modelId)) {
return parts[1];
} else {
return parts[0];
}
}