@ai-sdk/amazon-bedrock 5.0.0-beta.44 → 5.0.0-beta.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +58 -46
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +25 -23
- package/dist/index.js +496 -436
- package/dist/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +80 -82
- package/package.json +5 -5
- package/src/amazon-bedrock-api-types.ts +224 -0
- package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +7 -7
- package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +217 -196
- package/src/{bedrock-embedding-options.ts → amazon-bedrock-embedding-model-options.ts} +1 -1
- package/src/{bedrock-embedding-model.ts → amazon-bedrock-embedding-model.ts} +34 -27
- package/src/{bedrock-error.ts → amazon-bedrock-error.ts} +1 -1
- package/src/{bedrock-event-stream-decoder.ts → amazon-bedrock-event-stream-decoder.ts} +1 -1
- package/src/{bedrock-event-stream-response-handler.ts → amazon-bedrock-event-stream-response-handler.ts} +6 -6
- package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +43 -39
- package/src/amazon-bedrock-image-settings.ts +9 -0
- package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +22 -18
- package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +53 -46
- package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
- package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +5 -3
- package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
- package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +1 -1
- package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +22 -20
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
- package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +146 -103
- package/src/index.ts +15 -8
- package/src/inject-fetch-headers.ts +1 -1
- package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +4 -4
- package/src/reranking/{bedrock-reranking-api.ts → amazon-bedrock-reranking-api.ts} +3 -3
- package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts} +1 -1
- package/src/reranking/{bedrock-reranking-model.ts → amazon-bedrock-reranking-model.ts} +32 -25
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
- package/src/bedrock-api-types.ts +0 -219
- package/src/bedrock-image-settings.ts +0 -6
package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts}
RENAMED
|
@@ -1,38 +1,41 @@
|
|
|
1
1
|
import {
|
|
2
|
-
JSONObject,
|
|
3
|
-
LanguageModelV4Message,
|
|
4
|
-
LanguageModelV4Prompt,
|
|
5
|
-
SharedV4ProviderMetadata,
|
|
6
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type JSONObject,
|
|
4
|
+
type LanguageModelV4Message,
|
|
5
|
+
type LanguageModelV4Prompt,
|
|
6
|
+
type SharedV4ProviderMetadata,
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
8
|
import {
|
|
9
9
|
convertToBase64,
|
|
10
|
-
|
|
10
|
+
getTopLevelMediaType,
|
|
11
|
+
isFullMediaType,
|
|
11
12
|
parseProviderOptions,
|
|
13
|
+
resolveFullMediaType,
|
|
12
14
|
stripFileExtension,
|
|
13
15
|
} from '@ai-sdk/provider-utils';
|
|
14
16
|
import {
|
|
15
17
|
BEDROCK_DOCUMENT_MIME_TYPES,
|
|
16
18
|
BEDROCK_IMAGE_MIME_TYPES,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} from './bedrock-api-types';
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
19
|
+
type AmazonBedrockAssistantMessage,
|
|
20
|
+
type AmazonBedrockCachePoint,
|
|
21
|
+
type AmazonBedrockDocumentFormat,
|
|
22
|
+
type AmazonBedrockDocumentMimeType,
|
|
23
|
+
type AmazonBedrockImageFormat,
|
|
24
|
+
type AmazonBedrockImageMimeType,
|
|
25
|
+
type AmazonBedrockMessages,
|
|
26
|
+
type AmazonBedrockSystemMessages,
|
|
27
|
+
type AmazonBedrockUserMessage,
|
|
28
|
+
} from './amazon-bedrock-api-types';
|
|
29
|
+
import { amazonBedrockFilePartProviderOptions } from './amazon-bedrock-chat-language-model-options';
|
|
30
|
+
import { amazonBedrockReasoningMetadataSchema } from './amazon-bedrock-reasoning-metadata';
|
|
29
31
|
import { normalizeToolCallId } from './normalize-tool-call-id';
|
|
30
32
|
|
|
31
33
|
function getCachePoint(
|
|
32
34
|
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
33
|
-
):
|
|
34
|
-
const cachePointConfig = providerMetadata?.
|
|
35
|
-
|
|
35
|
+
): AmazonBedrockCachePoint | undefined {
|
|
36
|
+
const cachePointConfig = (providerMetadata?.amazonBedrock?.cachePoint ??
|
|
37
|
+
providerMetadata?.bedrock?.cachePoint) as
|
|
38
|
+
| AmazonBedrockCachePoint['cachePoint']
|
|
36
39
|
| undefined;
|
|
37
40
|
|
|
38
41
|
if (!cachePointConfig) {
|
|
@@ -45,26 +48,32 @@ function getCachePoint(
|
|
|
45
48
|
async function shouldEnableCitations(
|
|
46
49
|
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
47
50
|
): Promise<boolean> {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
const amazonBedrockOptions =
|
|
52
|
+
(await parseProviderOptions({
|
|
53
|
+
provider: 'amazonBedrock',
|
|
54
|
+
providerOptions: providerMetadata,
|
|
55
|
+
schema: amazonBedrockFilePartProviderOptions,
|
|
56
|
+
})) ??
|
|
57
|
+
(await parseProviderOptions({
|
|
58
|
+
provider: 'bedrock',
|
|
59
|
+
providerOptions: providerMetadata,
|
|
60
|
+
schema: amazonBedrockFilePartProviderOptions,
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
return amazonBedrockOptions?.citations?.enabled ?? false;
|
|
55
64
|
}
|
|
56
65
|
|
|
57
|
-
export async function
|
|
66
|
+
export async function convertToAmazonBedrockChatMessages(
|
|
58
67
|
prompt: LanguageModelV4Prompt,
|
|
59
68
|
isMistral: boolean = false,
|
|
60
69
|
): Promise<{
|
|
61
|
-
system:
|
|
62
|
-
messages:
|
|
70
|
+
system: AmazonBedrockSystemMessages;
|
|
71
|
+
messages: AmazonBedrockMessages;
|
|
63
72
|
}> {
|
|
64
73
|
const blocks = groupIntoBlocks(prompt);
|
|
65
74
|
|
|
66
|
-
let system:
|
|
67
|
-
const messages:
|
|
75
|
+
let system: AmazonBedrockSystemMessages = [];
|
|
76
|
+
const messages: AmazonBedrockMessages = [];
|
|
68
77
|
|
|
69
78
|
let documentCounter = 0;
|
|
70
79
|
const generateDocumentName = () => `document-${++documentCounter}`;
|
|
@@ -95,7 +104,7 @@ export async function convertToBedrockChatMessages(
|
|
|
95
104
|
|
|
96
105
|
case 'user': {
|
|
97
106
|
// combines all user and tool messages in this block into a single message:
|
|
98
|
-
const
|
|
107
|
+
const amazonBedrockContent: AmazonBedrockUserMessage['content'] = [];
|
|
99
108
|
|
|
100
109
|
for (const message of block.messages) {
|
|
101
110
|
const { role, content, providerOptions } = message;
|
|
@@ -106,58 +115,87 @@ export async function convertToBedrockChatMessages(
|
|
|
106
115
|
|
|
107
116
|
switch (part.type) {
|
|
108
117
|
case 'text': {
|
|
109
|
-
|
|
118
|
+
amazonBedrockContent.push({
|
|
110
119
|
text: part.text,
|
|
111
120
|
});
|
|
112
121
|
break;
|
|
113
122
|
}
|
|
114
123
|
|
|
115
124
|
case 'file': {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
functionality: 'file parts with provider references',
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (part.data instanceof URL) {
|
|
123
|
-
// The AI SDK automatically downloads files for user file parts with URLs
|
|
124
|
-
throw new UnsupportedFunctionalityError({
|
|
125
|
-
functionality: 'File URL data',
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (part.mediaType.startsWith('image/')) {
|
|
130
|
-
bedrockContent.push({
|
|
131
|
-
image: {
|
|
132
|
-
format: getBedrockImageFormat(part.mediaType),
|
|
133
|
-
source: { bytes: convertToBase64(part.data) },
|
|
134
|
-
},
|
|
135
|
-
});
|
|
136
|
-
} else {
|
|
137
|
-
if (!part.mediaType) {
|
|
125
|
+
switch (part.data.type) {
|
|
126
|
+
case 'reference': {
|
|
138
127
|
throw new UnsupportedFunctionalityError({
|
|
139
|
-
functionality: 'file
|
|
140
|
-
message:
|
|
141
|
-
'File mime type is required in user message part content',
|
|
128
|
+
functionality: 'file parts with provider references',
|
|
142
129
|
});
|
|
143
130
|
}
|
|
131
|
+
case 'url': {
|
|
132
|
+
throw new UnsupportedFunctionalityError({
|
|
133
|
+
functionality: 'File URL data',
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
case 'text': {
|
|
137
|
+
const textMediaType = isFullMediaType(part.mediaType)
|
|
138
|
+
? part.mediaType
|
|
139
|
+
: 'text/plain';
|
|
140
|
+
const enableCitations = await shouldEnableCitations(
|
|
141
|
+
part.providerOptions,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
amazonBedrockContent.push({
|
|
145
|
+
document: {
|
|
146
|
+
format:
|
|
147
|
+
getAmazonBedrockDocumentFormat(textMediaType),
|
|
148
|
+
name: part.filename
|
|
149
|
+
? stripFileExtension(part.filename)
|
|
150
|
+
: generateDocumentName(),
|
|
151
|
+
source: {
|
|
152
|
+
bytes: convertToBase64(
|
|
153
|
+
new TextEncoder().encode(part.data.text),
|
|
154
|
+
),
|
|
155
|
+
},
|
|
156
|
+
...(enableCitations && {
|
|
157
|
+
citations: { enabled: true },
|
|
158
|
+
}),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
case 'data': {
|
|
164
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
165
|
+
|
|
166
|
+
if (getTopLevelMediaType(fullMediaType) === 'image') {
|
|
167
|
+
amazonBedrockContent.push({
|
|
168
|
+
image: {
|
|
169
|
+
format:
|
|
170
|
+
getAmazonBedrockImageFormat(fullMediaType),
|
|
171
|
+
source: {
|
|
172
|
+
bytes: convertToBase64(part.data.data),
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
const enableCitations = await shouldEnableCitations(
|
|
178
|
+
part.providerOptions,
|
|
179
|
+
);
|
|
144
180
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
181
|
+
amazonBedrockContent.push({
|
|
182
|
+
document: {
|
|
183
|
+
format:
|
|
184
|
+
getAmazonBedrockDocumentFormat(fullMediaType),
|
|
185
|
+
name: part.filename
|
|
186
|
+
? stripFileExtension(part.filename)
|
|
187
|
+
: generateDocumentName(),
|
|
188
|
+
source: {
|
|
189
|
+
bytes: convertToBase64(part.data.data),
|
|
190
|
+
},
|
|
191
|
+
...(enableCitations && {
|
|
192
|
+
citations: { enabled: true },
|
|
193
|
+
}),
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
161
199
|
}
|
|
162
200
|
|
|
163
201
|
break;
|
|
@@ -188,7 +226,7 @@ export async function convertToBedrockChatMessages(
|
|
|
188
226
|
});
|
|
189
227
|
}
|
|
190
228
|
|
|
191
|
-
const format =
|
|
229
|
+
const format = getAmazonBedrockImageFormat(
|
|
192
230
|
contentPart.mediaType,
|
|
193
231
|
);
|
|
194
232
|
|
|
@@ -225,7 +263,7 @@ export async function convertToBedrockChatMessages(
|
|
|
225
263
|
break;
|
|
226
264
|
}
|
|
227
265
|
|
|
228
|
-
|
|
266
|
+
amazonBedrockContent.push({
|
|
229
267
|
toolResult: {
|
|
230
268
|
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
|
|
231
269
|
content: toolResultContent,
|
|
@@ -243,18 +281,19 @@ export async function convertToBedrockChatMessages(
|
|
|
243
281
|
|
|
244
282
|
const cachePoint = getCachePoint(providerOptions);
|
|
245
283
|
if (cachePoint) {
|
|
246
|
-
|
|
284
|
+
amazonBedrockContent.push(cachePoint);
|
|
247
285
|
}
|
|
248
286
|
}
|
|
249
287
|
|
|
250
|
-
messages.push({ role: 'user', content:
|
|
288
|
+
messages.push({ role: 'user', content: amazonBedrockContent });
|
|
251
289
|
|
|
252
290
|
break;
|
|
253
291
|
}
|
|
254
292
|
|
|
255
293
|
case 'assistant': {
|
|
256
294
|
// combines multiple assistant messages in this block into a single message:
|
|
257
|
-
const
|
|
295
|
+
const amazonBedrockContent: AmazonBedrockAssistantMessage['content'] =
|
|
296
|
+
[];
|
|
258
297
|
|
|
259
298
|
for (let j = 0; j < block.messages.length; j++) {
|
|
260
299
|
const message = block.messages[j];
|
|
@@ -275,7 +314,7 @@ export async function convertToBedrockChatMessages(
|
|
|
275
314
|
break;
|
|
276
315
|
}
|
|
277
316
|
|
|
278
|
-
|
|
317
|
+
amazonBedrockContent.push({
|
|
279
318
|
text:
|
|
280
319
|
// trim the last text part if it's the last message in the block
|
|
281
320
|
// because Bedrock does not allow trailing whitespace
|
|
@@ -291,16 +330,22 @@ export async function convertToBedrockChatMessages(
|
|
|
291
330
|
}
|
|
292
331
|
|
|
293
332
|
case 'reasoning': {
|
|
294
|
-
const reasoningMetadata =
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
333
|
+
const reasoningMetadata =
|
|
334
|
+
(await parseProviderOptions({
|
|
335
|
+
provider: 'amazonBedrock',
|
|
336
|
+
providerOptions: part.providerOptions,
|
|
337
|
+
schema: amazonBedrockReasoningMetadataSchema,
|
|
338
|
+
})) ??
|
|
339
|
+
(await parseProviderOptions({
|
|
340
|
+
provider: 'bedrock',
|
|
341
|
+
providerOptions: part.providerOptions,
|
|
342
|
+
schema: amazonBedrockReasoningMetadataSchema,
|
|
343
|
+
}));
|
|
299
344
|
|
|
300
345
|
if (reasoningMetadata?.signature != null) {
|
|
301
346
|
// do not trim reasoning text when a signature is present:
|
|
302
347
|
// the signature validates the exact original bytes
|
|
303
|
-
|
|
348
|
+
amazonBedrockContent.push({
|
|
304
349
|
reasoningContent: {
|
|
305
350
|
reasoningText: {
|
|
306
351
|
text: part.text,
|
|
@@ -309,7 +354,7 @@ export async function convertToBedrockChatMessages(
|
|
|
309
354
|
},
|
|
310
355
|
});
|
|
311
356
|
} else if (reasoningMetadata?.redactedData != null) {
|
|
312
|
-
|
|
357
|
+
amazonBedrockContent.push({
|
|
313
358
|
reasoningContent: {
|
|
314
359
|
redactedReasoning: {
|
|
315
360
|
data: reasoningMetadata.redactedData,
|
|
@@ -320,7 +365,7 @@ export async function convertToBedrockChatMessages(
|
|
|
320
365
|
// trim the last text part if it's the last message in the block
|
|
321
366
|
// because Bedrock does not allow trailing whitespace
|
|
322
367
|
// in pre-filled assistant responses
|
|
323
|
-
|
|
368
|
+
amazonBedrockContent.push({
|
|
324
369
|
reasoningContent: {
|
|
325
370
|
reasoningText: {
|
|
326
371
|
text: trimIfLast(
|
|
@@ -338,7 +383,7 @@ export async function convertToBedrockChatMessages(
|
|
|
338
383
|
}
|
|
339
384
|
|
|
340
385
|
case 'tool-call': {
|
|
341
|
-
|
|
386
|
+
amazonBedrockContent.push({
|
|
342
387
|
toolUse: {
|
|
343
388
|
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
|
|
344
389
|
name: part.toolName,
|
|
@@ -351,11 +396,11 @@ export async function convertToBedrockChatMessages(
|
|
|
351
396
|
}
|
|
352
397
|
const cachePoint = getCachePoint(message.providerOptions);
|
|
353
398
|
if (cachePoint) {
|
|
354
|
-
|
|
399
|
+
amazonBedrockContent.push(cachePoint);
|
|
355
400
|
}
|
|
356
401
|
}
|
|
357
402
|
|
|
358
|
-
messages.push({ role: 'assistant', content:
|
|
403
|
+
messages.push({ role: 'assistant', content: amazonBedrockContent });
|
|
359
404
|
|
|
360
405
|
break;
|
|
361
406
|
}
|
|
@@ -370,15 +415,11 @@ export async function convertToBedrockChatMessages(
|
|
|
370
415
|
return { system, messages };
|
|
371
416
|
}
|
|
372
417
|
|
|
373
|
-
function
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
const format = BEDROCK_IMAGE_MIME_TYPES[mimeType as BedrockImageMimeType];
|
|
418
|
+
function getAmazonBedrockImageFormat(
|
|
419
|
+
mimeType: string,
|
|
420
|
+
): AmazonBedrockImageFormat {
|
|
421
|
+
const format =
|
|
422
|
+
BEDROCK_IMAGE_MIME_TYPES[mimeType as AmazonBedrockImageMimeType];
|
|
382
423
|
if (!format) {
|
|
383
424
|
throw new UnsupportedFunctionalityError({
|
|
384
425
|
functionality: `image mime type: ${mimeType}`,
|
|
@@ -389,9 +430,11 @@ function getBedrockImageFormat(mimeType?: string): BedrockImageFormat {
|
|
|
389
430
|
return format;
|
|
390
431
|
}
|
|
391
432
|
|
|
392
|
-
function
|
|
433
|
+
function getAmazonBedrockDocumentFormat(
|
|
434
|
+
mimeType: string,
|
|
435
|
+
): AmazonBedrockDocumentFormat {
|
|
393
436
|
const format =
|
|
394
|
-
BEDROCK_DOCUMENT_MIME_TYPES[mimeType as
|
|
437
|
+
BEDROCK_DOCUMENT_MIME_TYPES[mimeType as AmazonBedrockDocumentMimeType];
|
|
395
438
|
if (!format) {
|
|
396
439
|
throw new UnsupportedFunctionalityError({
|
|
397
440
|
functionality: `file mime type: ${mimeType}`,
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
export type { AnthropicProviderOptions } from '@ai-sdk/anthropic';
|
|
2
2
|
|
|
3
|
-
export type { AmazonBedrockEmbeddingModelOptions } from './bedrock-embedding-options';
|
|
3
|
+
export type { AmazonBedrockEmbeddingModelOptions } from './amazon-bedrock-embedding-model-options';
|
|
4
4
|
export type {
|
|
5
|
-
|
|
6
|
-
/** @deprecated Use `
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
AmazonBedrockLanguageModelChatOptions,
|
|
6
|
+
/** @deprecated Use `AmazonBedrockLanguageModelChatOptions` instead. */
|
|
7
|
+
AmazonBedrockLanguageModelChatOptions as AmazonBedrockLanguageModelOptions,
|
|
8
|
+
/** @deprecated Use `AmazonBedrockLanguageModelChatOptions` instead. */
|
|
9
|
+
AmazonBedrockLanguageModelChatOptions as BedrockProviderOptions,
|
|
10
|
+
} from './amazon-bedrock-chat-language-model-options';
|
|
11
|
+
export {
|
|
12
|
+
amazonBedrock,
|
|
13
|
+
/** @deprecated Use `amazonBedrock` instead. */
|
|
14
|
+
amazonBedrock as bedrock,
|
|
15
|
+
createAmazonBedrock,
|
|
16
|
+
} from './amazon-bedrock-provider';
|
|
10
17
|
export type {
|
|
11
18
|
AmazonBedrockProvider,
|
|
12
19
|
AmazonBedrockProviderSettings,
|
|
13
|
-
} from './bedrock-provider';
|
|
20
|
+
} from './amazon-bedrock-provider';
|
|
14
21
|
export type {
|
|
15
22
|
AmazonBedrockRerankingModelOptions,
|
|
16
23
|
/** @deprecated Use `AmazonBedrockRerankingModelOptions` instead. */
|
|
17
24
|
AmazonBedrockRerankingModelOptions as BedrockRerankingOptions,
|
|
18
|
-
} from './reranking/bedrock-reranking-options';
|
|
25
|
+
} from './reranking/amazon-bedrock-reranking-model-options';
|
|
19
26
|
export { VERSION } from './version';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
|
-
import {
|
|
1
|
+
import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
|
+
import type { AmazonBedrockStopReason } from './amazon-bedrock-api-types';
|
|
3
3
|
|
|
4
|
-
export function
|
|
5
|
-
finishReason:
|
|
4
|
+
export function mapAmazonBedrockFinishReason(
|
|
5
|
+
finishReason: AmazonBedrockStopReason,
|
|
6
6
|
isJsonResponseFromTool?: boolean,
|
|
7
7
|
): LanguageModelV4FinishReason['unified'] {
|
|
8
8
|
switch (finishReason) {
|
|
@@ -2,12 +2,12 @@ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
|
2
2
|
import { z } from 'zod/v4';
|
|
3
3
|
|
|
4
4
|
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Rerank.html
|
|
5
|
-
export type
|
|
5
|
+
export type AmazonBedrockRerankingInput = {
|
|
6
6
|
nextToken?: string;
|
|
7
7
|
queries: [{ type: 'TEXT'; textQuery: { text: string } }];
|
|
8
8
|
rerankingConfiguration: {
|
|
9
9
|
type: 'BEDROCK_RERANKING_MODEL';
|
|
10
|
-
|
|
10
|
+
amazonBedrockRerankingConfiguration: {
|
|
11
11
|
modelConfiguration: {
|
|
12
12
|
modelArn: string;
|
|
13
13
|
additionalModelRequestFields?: Record<string, unknown>;
|
|
@@ -29,7 +29,7 @@ export type BedrockRerankingInput = {
|
|
|
29
29
|
}[];
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
export const
|
|
32
|
+
export const amazonBedrockRerankingResponseSchema = lazySchema(() =>
|
|
33
33
|
zodSchema(
|
|
34
34
|
z.object({
|
|
35
35
|
results: z.array(
|
package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts}
RENAMED
|
@@ -2,7 +2,7 @@ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
|
2
2
|
import { z } from 'zod/v4';
|
|
3
3
|
|
|
4
4
|
// https://docs.aws.amazon.com/bedrock/latest/userguide/rerank-supported.html
|
|
5
|
-
export type
|
|
5
|
+
export type AmazonBedrockRerankingModelId =
|
|
6
6
|
| 'amazon.rerank-v1:0'
|
|
7
7
|
| 'cohere.rerank-v3-5:0'
|
|
8
8
|
| (string & {});
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import { RerankingModelV4 } from '@ai-sdk/provider';
|
|
1
|
+
import type { RerankingModelV4 } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
|
-
FetchFunction,
|
|
4
|
-
Resolvable,
|
|
5
3
|
combineHeaders,
|
|
6
4
|
createJsonErrorResponseHandler,
|
|
7
5
|
createJsonResponseHandler,
|
|
8
6
|
parseProviderOptions,
|
|
9
7
|
postJsonToApi,
|
|
10
8
|
resolve,
|
|
9
|
+
type FetchFunction,
|
|
10
|
+
type Resolvable,
|
|
11
11
|
} from '@ai-sdk/provider-utils';
|
|
12
|
-
import {
|
|
12
|
+
import { AmazonBedrockErrorSchema } from '../amazon-bedrock-error';
|
|
13
13
|
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from './bedrock-reranking-api';
|
|
14
|
+
amazonBedrockRerankingResponseSchema,
|
|
15
|
+
type AmazonBedrockRerankingInput,
|
|
16
|
+
} from './amazon-bedrock-reranking-api';
|
|
17
17
|
import {
|
|
18
|
-
BedrockRerankingModelId,
|
|
19
18
|
amazonBedrockRerankingModelOptionsSchema,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
type
|
|
19
|
+
type AmazonBedrockRerankingModelId,
|
|
20
|
+
} from './amazon-bedrock-reranking-model-options';
|
|
21
|
+
type AmazonBedrockRerankingConfig = {
|
|
23
22
|
baseUrl: () => string;
|
|
24
23
|
region: string;
|
|
25
24
|
headers: Resolvable<Record<string, string | undefined>>;
|
|
@@ -28,13 +27,13 @@ type BedrockRerankingConfig = {
|
|
|
28
27
|
|
|
29
28
|
type DoRerankResponse = Awaited<ReturnType<RerankingModelV4['doRerank']>>;
|
|
30
29
|
|
|
31
|
-
export class
|
|
30
|
+
export class AmazonBedrockRerankingModel implements RerankingModelV4 {
|
|
32
31
|
readonly specificationVersion = 'v4';
|
|
33
32
|
readonly provider = 'amazon-bedrock';
|
|
34
33
|
|
|
35
34
|
constructor(
|
|
36
|
-
readonly modelId:
|
|
37
|
-
private readonly config:
|
|
35
|
+
readonly modelId: AmazonBedrockRerankingModelId,
|
|
36
|
+
private readonly config: AmazonBedrockRerankingConfig,
|
|
38
37
|
) {}
|
|
39
38
|
|
|
40
39
|
async doRerank({
|
|
@@ -45,11 +44,19 @@ export class BedrockRerankingModel implements RerankingModelV4 {
|
|
|
45
44
|
abortSignal,
|
|
46
45
|
providerOptions,
|
|
47
46
|
}: Parameters<RerankingModelV4['doRerank']>[0]): Promise<DoRerankResponse> {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
// Prefer `amazonBedrock`; fall back to legacy `bedrock` for backward
|
|
48
|
+
// compatibility.
|
|
49
|
+
const amazonBedrockOptions =
|
|
50
|
+
(await parseProviderOptions({
|
|
51
|
+
provider: 'amazonBedrock',
|
|
52
|
+
providerOptions,
|
|
53
|
+
schema: amazonBedrockRerankingModelOptionsSchema,
|
|
54
|
+
})) ??
|
|
55
|
+
(await parseProviderOptions({
|
|
56
|
+
provider: 'bedrock',
|
|
57
|
+
providerOptions,
|
|
58
|
+
schema: amazonBedrockRerankingModelOptionsSchema,
|
|
59
|
+
}));
|
|
53
60
|
|
|
54
61
|
const {
|
|
55
62
|
value: response,
|
|
@@ -61,7 +68,7 @@ export class BedrockRerankingModel implements RerankingModelV4 {
|
|
|
61
68
|
combineHeaders(await resolve(this.config.headers), headers),
|
|
62
69
|
),
|
|
63
70
|
body: {
|
|
64
|
-
nextToken:
|
|
71
|
+
nextToken: amazonBedrockOptions?.nextToken,
|
|
65
72
|
queries: [
|
|
66
73
|
{
|
|
67
74
|
textQuery: { text: query },
|
|
@@ -69,11 +76,11 @@ export class BedrockRerankingModel implements RerankingModelV4 {
|
|
|
69
76
|
},
|
|
70
77
|
],
|
|
71
78
|
rerankingConfiguration: {
|
|
72
|
-
|
|
79
|
+
amazonBedrockRerankingConfiguration: {
|
|
73
80
|
modelConfiguration: {
|
|
74
81
|
modelArn: `arn:aws:bedrock:${this.config.region}::foundation-model/${this.modelId}`,
|
|
75
82
|
additionalModelRequestFields:
|
|
76
|
-
|
|
83
|
+
amazonBedrockOptions?.additionalModelRequestFields,
|
|
77
84
|
},
|
|
78
85
|
numberOfResults: topN,
|
|
79
86
|
},
|
|
@@ -92,13 +99,13 @@ export class BedrockRerankingModel implements RerankingModelV4 {
|
|
|
92
99
|
jsonDocument: value,
|
|
93
100
|
},
|
|
94
101
|
})),
|
|
95
|
-
} satisfies
|
|
102
|
+
} satisfies AmazonBedrockRerankingInput,
|
|
96
103
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
97
|
-
errorSchema:
|
|
104
|
+
errorSchema: AmazonBedrockErrorSchema,
|
|
98
105
|
errorToMessage: error => `${error.type}: ${error.message}`,
|
|
99
106
|
}),
|
|
100
107
|
successfulResponseHandler: createJsonResponseHandler(
|
|
101
|
-
|
|
108
|
+
amazonBedrockRerankingResponseSchema,
|
|
102
109
|
),
|
|
103
110
|
fetch: this.config.fetch,
|
|
104
111
|
abortSignal,
|