@ai-sdk/amazon-bedrock 5.0.0-beta.2 → 5.0.0-beta.21
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 +154 -4
- package/README.md +2 -0
- package/dist/anthropic/index.d.mts +4 -4
- package/dist/anthropic/index.d.ts +4 -4
- package/dist/anthropic/index.js +28 -6
- package/dist/anthropic/index.js.map +1 -1
- package/dist/anthropic/index.mjs +25 -3
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/index.d.mts +18 -12
- package/dist/index.d.ts +18 -12
- package/dist/index.js +130 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +129 -35
- package/dist/index.mjs.map +1 -1
- package/docs/08-amazon-bedrock.mdx +52 -6
- package/package.json +4 -6
- package/src/anthropic/bedrock-anthropic-fetch.ts +26 -0
- package/src/anthropic/bedrock-anthropic-provider.ts +10 -6
- package/src/bedrock-api-types.ts +3 -0
- package/src/bedrock-chat-language-model.ts +119 -28
- package/src/bedrock-chat-options.ts +10 -0
- package/src/bedrock-embedding-model.ts +5 -5
- package/src/bedrock-image-model.ts +9 -9
- package/src/bedrock-prepare-tools.ts +7 -6
- package/src/bedrock-provider.ts +17 -17
- package/src/convert-bedrock-usage.ts +2 -2
- package/src/convert-to-bedrock-chat-messages.ts +50 -35
- package/src/map-bedrock-finish-reason.ts +2 -2
- package/src/reranking/bedrock-reranking-model.ts +5 -5
package/src/bedrock-provider.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { anthropicTools } from '@ai-sdk/anthropic/internal';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
EmbeddingModelV4,
|
|
4
|
+
ImageModelV4,
|
|
5
|
+
LanguageModelV4,
|
|
6
|
+
ProviderV4,
|
|
7
|
+
RerankingModelV4,
|
|
8
8
|
} from '@ai-sdk/provider';
|
|
9
9
|
import {
|
|
10
10
|
FetchFunction,
|
|
@@ -107,50 +107,50 @@ export interface AmazonBedrockProviderSettings {
|
|
|
107
107
|
generateId?: () => string;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export interface AmazonBedrockProvider extends
|
|
111
|
-
(modelId: BedrockChatModelId):
|
|
110
|
+
export interface AmazonBedrockProvider extends ProviderV4 {
|
|
111
|
+
(modelId: BedrockChatModelId): LanguageModelV4;
|
|
112
112
|
|
|
113
|
-
languageModel(modelId: BedrockChatModelId):
|
|
113
|
+
languageModel(modelId: BedrockChatModelId): LanguageModelV4;
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
116
|
* Creates a model for text embeddings.
|
|
117
117
|
*/
|
|
118
|
-
embedding(modelId: BedrockEmbeddingModelId):
|
|
118
|
+
embedding(modelId: BedrockEmbeddingModelId): EmbeddingModelV4;
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
121
|
* Creates a model for text embeddings.
|
|
122
122
|
*/
|
|
123
|
-
embeddingModel(modelId: BedrockEmbeddingModelId):
|
|
123
|
+
embeddingModel(modelId: BedrockEmbeddingModelId): EmbeddingModelV4;
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* @deprecated Use `embedding` instead.
|
|
127
127
|
*/
|
|
128
|
-
textEmbedding(modelId: BedrockEmbeddingModelId):
|
|
128
|
+
textEmbedding(modelId: BedrockEmbeddingModelId): EmbeddingModelV4;
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
131
|
* @deprecated Use `embeddingModel` instead.
|
|
132
132
|
*/
|
|
133
|
-
textEmbeddingModel(modelId: BedrockEmbeddingModelId):
|
|
133
|
+
textEmbeddingModel(modelId: BedrockEmbeddingModelId): EmbeddingModelV4;
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
136
|
* Creates a model for image generation.
|
|
137
137
|
*/
|
|
138
|
-
image(modelId: BedrockImageModelId):
|
|
138
|
+
image(modelId: BedrockImageModelId): ImageModelV4;
|
|
139
139
|
|
|
140
140
|
/**
|
|
141
141
|
* Creates a model for image generation.
|
|
142
142
|
*/
|
|
143
|
-
imageModel(modelId: BedrockImageModelId):
|
|
143
|
+
imageModel(modelId: BedrockImageModelId): ImageModelV4;
|
|
144
144
|
|
|
145
145
|
/**
|
|
146
146
|
* Creates a model for reranking documents.
|
|
147
147
|
*/
|
|
148
|
-
reranking(modelId: BedrockRerankingModelId):
|
|
148
|
+
reranking(modelId: BedrockRerankingModelId): RerankingModelV4;
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
151
|
* Creates a model for reranking documents.
|
|
152
152
|
*/
|
|
153
|
-
rerankingModel(modelId: BedrockRerankingModelId):
|
|
153
|
+
rerankingModel(modelId: BedrockRerankingModelId): RerankingModelV4;
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
156
|
* Anthropic-specific tools that can be used with Anthropic models on Bedrock.
|
|
@@ -330,7 +330,7 @@ export function createAmazonBedrock(
|
|
|
330
330
|
fetch: fetchFunction,
|
|
331
331
|
});
|
|
332
332
|
|
|
333
|
-
provider.specificationVersion = '
|
|
333
|
+
provider.specificationVersion = 'v4' as const;
|
|
334
334
|
provider.languageModel = createChatModel;
|
|
335
335
|
provider.embedding = createEmbeddingModel;
|
|
336
336
|
provider.embeddingModel = createEmbeddingModel;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export type BedrockUsage = {
|
|
4
4
|
inputTokens: number;
|
|
@@ -10,7 +10,7 @@ export type BedrockUsage = {
|
|
|
10
10
|
|
|
11
11
|
export function convertBedrockUsage(
|
|
12
12
|
usage: BedrockUsage | undefined | null,
|
|
13
|
-
):
|
|
13
|
+
): LanguageModelV4Usage {
|
|
14
14
|
if (usage == null) {
|
|
15
15
|
return {
|
|
16
16
|
inputTokens: {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
JSONObject,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
LanguageModelV4Message,
|
|
4
|
+
LanguageModelV4Prompt,
|
|
5
|
+
SharedV4ProviderMetadata,
|
|
6
6
|
UnsupportedFunctionalityError,
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
8
|
import {
|
|
9
9
|
convertToBase64,
|
|
10
|
+
isProviderReference,
|
|
10
11
|
parseProviderOptions,
|
|
11
12
|
stripFileExtension,
|
|
12
13
|
} from '@ai-sdk/provider-utils';
|
|
@@ -28,7 +29,7 @@ import { bedrockFilePartProviderOptions } from './bedrock-chat-options';
|
|
|
28
29
|
import { normalizeToolCallId } from './normalize-tool-call-id';
|
|
29
30
|
|
|
30
31
|
function getCachePoint(
|
|
31
|
-
providerMetadata:
|
|
32
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
32
33
|
): BedrockCachePoint | undefined {
|
|
33
34
|
const cachePointConfig = providerMetadata?.bedrock?.cachePoint as
|
|
34
35
|
| BedrockCachePoint['cachePoint']
|
|
@@ -42,7 +43,7 @@ function getCachePoint(
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
async function shouldEnableCitations(
|
|
45
|
-
providerMetadata:
|
|
46
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
46
47
|
): Promise<boolean> {
|
|
47
48
|
const bedrockOptions = await parseProviderOptions({
|
|
48
49
|
provider: 'bedrock',
|
|
@@ -54,7 +55,7 @@ async function shouldEnableCitations(
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
export async function convertToBedrockChatMessages(
|
|
57
|
-
prompt:
|
|
58
|
+
prompt: LanguageModelV4Prompt,
|
|
58
59
|
isMistral: boolean = false,
|
|
59
60
|
): Promise<{
|
|
60
61
|
system: BedrockSystemMessages;
|
|
@@ -112,6 +113,12 @@ export async function convertToBedrockChatMessages(
|
|
|
112
113
|
}
|
|
113
114
|
|
|
114
115
|
case 'file': {
|
|
116
|
+
if (isProviderReference(part.data)) {
|
|
117
|
+
throw new UnsupportedFunctionalityError({
|
|
118
|
+
functionality: 'file parts with provider references',
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
115
122
|
if (part.data instanceof URL) {
|
|
116
123
|
// The AI SDK automatically downloads files for user file parts with URLs
|
|
117
124
|
throw new UnsupportedFunctionalityError({
|
|
@@ -287,33 +294,41 @@ export async function convertToBedrockChatMessages(
|
|
|
287
294
|
schema: bedrockReasoningMetadataSchema,
|
|
288
295
|
});
|
|
289
296
|
|
|
290
|
-
if (reasoningMetadata != null) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
text: trimIfLast(
|
|
299
|
-
isLastBlock,
|
|
300
|
-
isLastMessage,
|
|
301
|
-
isLastContentPart,
|
|
302
|
-
part.text,
|
|
303
|
-
),
|
|
304
|
-
signature: reasoningMetadata.signature,
|
|
305
|
-
},
|
|
297
|
+
if (reasoningMetadata?.signature != null) {
|
|
298
|
+
// do not trim reasoning text when a signature is present:
|
|
299
|
+
// the signature validates the exact original bytes
|
|
300
|
+
bedrockContent.push({
|
|
301
|
+
reasoningContent: {
|
|
302
|
+
reasoningText: {
|
|
303
|
+
text: part.text,
|
|
304
|
+
signature: reasoningMetadata.signature,
|
|
306
305
|
},
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
} else if (reasoningMetadata?.redactedData != null) {
|
|
309
|
+
bedrockContent.push({
|
|
310
|
+
reasoningContent: {
|
|
311
|
+
redactedReasoning: {
|
|
312
|
+
data: reasoningMetadata.redactedData,
|
|
314
313
|
},
|
|
315
|
-
}
|
|
316
|
-
}
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
} else {
|
|
317
|
+
// trim the last text part if it's the last message in the block
|
|
318
|
+
// because Bedrock does not allow trailing whitespace
|
|
319
|
+
// in pre-filled assistant responses
|
|
320
|
+
bedrockContent.push({
|
|
321
|
+
reasoningContent: {
|
|
322
|
+
reasoningText: {
|
|
323
|
+
text: trimIfLast(
|
|
324
|
+
isLastBlock,
|
|
325
|
+
isLastMessage,
|
|
326
|
+
isLastContentPart,
|
|
327
|
+
part.text,
|
|
328
|
+
),
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
});
|
|
317
332
|
}
|
|
318
333
|
|
|
319
334
|
break;
|
|
@@ -400,19 +415,19 @@ function trimIfLast(
|
|
|
400
415
|
|
|
401
416
|
type SystemBlock = {
|
|
402
417
|
type: 'system';
|
|
403
|
-
messages: Array<
|
|
418
|
+
messages: Array<LanguageModelV4Message & { role: 'system' }>;
|
|
404
419
|
};
|
|
405
420
|
type AssistantBlock = {
|
|
406
421
|
type: 'assistant';
|
|
407
|
-
messages: Array<
|
|
422
|
+
messages: Array<LanguageModelV4Message & { role: 'assistant' }>;
|
|
408
423
|
};
|
|
409
424
|
type UserBlock = {
|
|
410
425
|
type: 'user';
|
|
411
|
-
messages: Array<
|
|
426
|
+
messages: Array<LanguageModelV4Message & { role: 'user' | 'tool' }>;
|
|
412
427
|
};
|
|
413
428
|
|
|
414
429
|
function groupIntoBlocks(
|
|
415
|
-
prompt:
|
|
430
|
+
prompt: LanguageModelV4Prompt,
|
|
416
431
|
): Array<SystemBlock | AssistantBlock | UserBlock> {
|
|
417
432
|
const blocks: Array<SystemBlock | AssistantBlock | UserBlock> = [];
|
|
418
433
|
let currentBlock: SystemBlock | AssistantBlock | UserBlock | undefined =
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
import { BedrockStopReason } from './bedrock-api-types';
|
|
3
3
|
|
|
4
4
|
export function mapBedrockFinishReason(
|
|
5
5
|
finishReason: BedrockStopReason,
|
|
6
6
|
isJsonResponseFromTool?: boolean,
|
|
7
|
-
):
|
|
7
|
+
): LanguageModelV4FinishReason['unified'] {
|
|
8
8
|
switch (finishReason) {
|
|
9
9
|
case 'stop_sequence':
|
|
10
10
|
case 'end_turn':
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RerankingModelV4 } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
3
|
FetchFunction,
|
|
4
4
|
Resolvable,
|
|
@@ -26,10 +26,10 @@ type BedrockRerankingConfig = {
|
|
|
26
26
|
fetch?: FetchFunction;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
type DoRerankResponse = Awaited<ReturnType<
|
|
29
|
+
type DoRerankResponse = Awaited<ReturnType<RerankingModelV4['doRerank']>>;
|
|
30
30
|
|
|
31
|
-
export class BedrockRerankingModel implements
|
|
32
|
-
readonly specificationVersion = '
|
|
31
|
+
export class BedrockRerankingModel implements RerankingModelV4 {
|
|
32
|
+
readonly specificationVersion = 'v4';
|
|
33
33
|
readonly provider = 'amazon-bedrock';
|
|
34
34
|
|
|
35
35
|
constructor(
|
|
@@ -44,7 +44,7 @@ export class BedrockRerankingModel implements RerankingModelV3 {
|
|
|
44
44
|
topN,
|
|
45
45
|
abortSignal,
|
|
46
46
|
providerOptions,
|
|
47
|
-
}: Parameters<
|
|
47
|
+
}: Parameters<RerankingModelV4['doRerank']>[0]): Promise<DoRerankResponse> {
|
|
48
48
|
const bedrockOptions = await parseProviderOptions({
|
|
49
49
|
provider: 'bedrock',
|
|
50
50
|
providerOptions,
|