@ai-sdk/amazon-bedrock 5.0.0-beta.2 → 5.0.0-beta.20
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 +144 -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 +125 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +123 -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 +43 -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,8 +1,8 @@
|
|
|
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 {
|
|
@@ -28,7 +28,7 @@ import { bedrockFilePartProviderOptions } from './bedrock-chat-options';
|
|
|
28
28
|
import { normalizeToolCallId } from './normalize-tool-call-id';
|
|
29
29
|
|
|
30
30
|
function getCachePoint(
|
|
31
|
-
providerMetadata:
|
|
31
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
32
32
|
): BedrockCachePoint | undefined {
|
|
33
33
|
const cachePointConfig = providerMetadata?.bedrock?.cachePoint as
|
|
34
34
|
| BedrockCachePoint['cachePoint']
|
|
@@ -42,7 +42,7 @@ function getCachePoint(
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
async function shouldEnableCitations(
|
|
45
|
-
providerMetadata:
|
|
45
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
46
46
|
): Promise<boolean> {
|
|
47
47
|
const bedrockOptions = await parseProviderOptions({
|
|
48
48
|
provider: 'bedrock',
|
|
@@ -54,7 +54,7 @@ async function shouldEnableCitations(
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
export async function convertToBedrockChatMessages(
|
|
57
|
-
prompt:
|
|
57
|
+
prompt: LanguageModelV4Prompt,
|
|
58
58
|
isMistral: boolean = false,
|
|
59
59
|
): Promise<{
|
|
60
60
|
system: BedrockSystemMessages;
|
|
@@ -287,33 +287,41 @@ export async function convertToBedrockChatMessages(
|
|
|
287
287
|
schema: bedrockReasoningMetadataSchema,
|
|
288
288
|
});
|
|
289
289
|
|
|
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
|
-
},
|
|
290
|
+
if (reasoningMetadata?.signature != null) {
|
|
291
|
+
// do not trim reasoning text when a signature is present:
|
|
292
|
+
// the signature validates the exact original bytes
|
|
293
|
+
bedrockContent.push({
|
|
294
|
+
reasoningContent: {
|
|
295
|
+
reasoningText: {
|
|
296
|
+
text: part.text,
|
|
297
|
+
signature: reasoningMetadata.signature,
|
|
306
298
|
},
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
} else if (reasoningMetadata?.redactedData != null) {
|
|
302
|
+
bedrockContent.push({
|
|
303
|
+
reasoningContent: {
|
|
304
|
+
redactedReasoning: {
|
|
305
|
+
data: reasoningMetadata.redactedData,
|
|
314
306
|
},
|
|
315
|
-
}
|
|
316
|
-
}
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
} else {
|
|
310
|
+
// trim the last text part if it's the last message in the block
|
|
311
|
+
// because Bedrock does not allow trailing whitespace
|
|
312
|
+
// in pre-filled assistant responses
|
|
313
|
+
bedrockContent.push({
|
|
314
|
+
reasoningContent: {
|
|
315
|
+
reasoningText: {
|
|
316
|
+
text: trimIfLast(
|
|
317
|
+
isLastBlock,
|
|
318
|
+
isLastMessage,
|
|
319
|
+
isLastContentPart,
|
|
320
|
+
part.text,
|
|
321
|
+
),
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
});
|
|
317
325
|
}
|
|
318
326
|
|
|
319
327
|
break;
|
|
@@ -400,19 +408,19 @@ function trimIfLast(
|
|
|
400
408
|
|
|
401
409
|
type SystemBlock = {
|
|
402
410
|
type: 'system';
|
|
403
|
-
messages: Array<
|
|
411
|
+
messages: Array<LanguageModelV4Message & { role: 'system' }>;
|
|
404
412
|
};
|
|
405
413
|
type AssistantBlock = {
|
|
406
414
|
type: 'assistant';
|
|
407
|
-
messages: Array<
|
|
415
|
+
messages: Array<LanguageModelV4Message & { role: 'assistant' }>;
|
|
408
416
|
};
|
|
409
417
|
type UserBlock = {
|
|
410
418
|
type: 'user';
|
|
411
|
-
messages: Array<
|
|
419
|
+
messages: Array<LanguageModelV4Message & { role: 'user' | 'tool' }>;
|
|
412
420
|
};
|
|
413
421
|
|
|
414
422
|
function groupIntoBlocks(
|
|
415
|
-
prompt:
|
|
423
|
+
prompt: LanguageModelV4Prompt,
|
|
416
424
|
): Array<SystemBlock | AssistantBlock | UserBlock> {
|
|
417
425
|
const blocks: Array<SystemBlock | AssistantBlock | UserBlock> = [];
|
|
418
426
|
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,
|