@ai-sdk/amazon-bedrock 5.0.0-beta.45 → 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 +15 -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 +21 -21
- package/dist/index.js +328 -292
- package/dist/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +66 -66
- package/package.json +3 -3
- package/src/{bedrock-api-types.ts → amazon-bedrock-api-types.ts} +60 -55
- 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} +211 -190
- 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} +31 -24
- 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} +3 -3
- package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +40 -36
- package/src/amazon-bedrock-image-settings.ts +9 -0
- package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +18 -17
- package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +40 -38
- package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
- package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +4 -2
- 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} +18 -16
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +3 -3
- package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +78 -56
- package/src/index.ts +15 -8
- package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +3 -3
- 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} +29 -21
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
- package/src/bedrock-image-settings.ts +0 -6
- package/src/bedrock-reasoning-metadata.ts +0 -10
|
@@ -17,12 +17,12 @@ import {
|
|
|
17
17
|
} from '@ai-sdk/provider-utils';
|
|
18
18
|
import {
|
|
19
19
|
amazonBedrockEmbeddingModelOptionsSchema,
|
|
20
|
-
type
|
|
21
|
-
} from './bedrock-embedding-options';
|
|
22
|
-
import {
|
|
20
|
+
type AmazonBedrockEmbeddingModelId,
|
|
21
|
+
} from './amazon-bedrock-embedding-model-options';
|
|
22
|
+
import { AmazonBedrockErrorSchema } from './amazon-bedrock-error';
|
|
23
23
|
import { z } from 'zod/v4';
|
|
24
24
|
|
|
25
|
-
type
|
|
25
|
+
type AmazonBedrockEmbeddingConfig = {
|
|
26
26
|
baseUrl: () => string;
|
|
27
27
|
headers?: Resolvable<Record<string, string | undefined>>;
|
|
28
28
|
fetch?: FetchFunction;
|
|
@@ -30,13 +30,13 @@ type BedrockEmbeddingConfig = {
|
|
|
30
30
|
|
|
31
31
|
type DoEmbedResponse = Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>;
|
|
32
32
|
|
|
33
|
-
export class
|
|
33
|
+
export class AmazonBedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
34
34
|
readonly specificationVersion = 'v4';
|
|
35
35
|
readonly provider = 'amazon-bedrock';
|
|
36
36
|
readonly maxEmbeddingsPerCall = 1;
|
|
37
37
|
readonly supportsParallelCalls = true;
|
|
38
38
|
|
|
39
|
-
static [WORKFLOW_SERIALIZE](model:
|
|
39
|
+
static [WORKFLOW_SERIALIZE](model: AmazonBedrockEmbeddingModel) {
|
|
40
40
|
return serializeModelOptions({
|
|
41
41
|
modelId: model.modelId,
|
|
42
42
|
config: model.config,
|
|
@@ -45,14 +45,14 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
45
45
|
|
|
46
46
|
static [WORKFLOW_DESERIALIZE](options: {
|
|
47
47
|
modelId: string;
|
|
48
|
-
config:
|
|
48
|
+
config: AmazonBedrockEmbeddingConfig;
|
|
49
49
|
}) {
|
|
50
|
-
return new
|
|
50
|
+
return new AmazonBedrockEmbeddingModel(options.modelId, options.config);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
constructor(
|
|
54
|
-
readonly modelId:
|
|
55
|
-
private readonly config:
|
|
54
|
+
readonly modelId: AmazonBedrockEmbeddingModelId,
|
|
55
|
+
private readonly config: AmazonBedrockEmbeddingConfig,
|
|
56
56
|
) {}
|
|
57
57
|
|
|
58
58
|
private getUrl(modelId: string): string {
|
|
@@ -75,13 +75,20 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
// Parse provider options
|
|
79
|
-
|
|
78
|
+
// Parse provider options. Prefer `amazonBedrock`; fall back to legacy
|
|
79
|
+
// `bedrock` key for backward compatibility.
|
|
80
|
+
const amazonBedrockOptions =
|
|
81
|
+
(await parseProviderOptions({
|
|
82
|
+
provider: 'amazonBedrock',
|
|
83
|
+
providerOptions,
|
|
84
|
+
schema: amazonBedrockEmbeddingModelOptionsSchema,
|
|
85
|
+
})) ??
|
|
80
86
|
(await parseProviderOptions({
|
|
81
87
|
provider: 'bedrock',
|
|
82
88
|
providerOptions,
|
|
83
89
|
schema: amazonBedrockEmbeddingModelOptionsSchema,
|
|
84
|
-
})) ??
|
|
90
|
+
})) ??
|
|
91
|
+
{};
|
|
85
92
|
|
|
86
93
|
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
|
|
87
94
|
//
|
|
@@ -97,10 +104,10 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
97
104
|
taskType: 'SINGLE_EMBEDDING',
|
|
98
105
|
singleEmbeddingParams: {
|
|
99
106
|
embeddingPurpose:
|
|
100
|
-
|
|
101
|
-
embeddingDimension:
|
|
107
|
+
amazonBedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
|
|
108
|
+
embeddingDimension: amazonBedrockOptions.embeddingDimension ?? 1024,
|
|
102
109
|
text: {
|
|
103
|
-
truncationMode:
|
|
110
|
+
truncationMode: amazonBedrockOptions.truncate ?? 'END',
|
|
104
111
|
value: values[0],
|
|
105
112
|
},
|
|
106
113
|
},
|
|
@@ -109,15 +116,15 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
109
116
|
? {
|
|
110
117
|
// Cohere embedding models on Bedrock require `input_type`.
|
|
111
118
|
// Without it, the service attempts other schema branches and rejects the request.
|
|
112
|
-
input_type:
|
|
119
|
+
input_type: amazonBedrockOptions.inputType ?? 'search_query',
|
|
113
120
|
texts: [values[0]],
|
|
114
|
-
truncate:
|
|
115
|
-
output_dimension:
|
|
121
|
+
truncate: amazonBedrockOptions.truncate,
|
|
122
|
+
output_dimension: amazonBedrockOptions.outputDimension,
|
|
116
123
|
}
|
|
117
124
|
: {
|
|
118
125
|
inputText: values[0],
|
|
119
|
-
dimensions:
|
|
120
|
-
normalize:
|
|
126
|
+
dimensions: amazonBedrockOptions.dimensions,
|
|
127
|
+
normalize: amazonBedrockOptions.normalize,
|
|
121
128
|
};
|
|
122
129
|
|
|
123
130
|
const url = this.getUrl(this.modelId);
|
|
@@ -131,11 +138,11 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
131
138
|
),
|
|
132
139
|
body: args,
|
|
133
140
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
134
|
-
errorSchema:
|
|
141
|
+
errorSchema: AmazonBedrockErrorSchema,
|
|
135
142
|
errorToMessage: error => `${error.type}: ${error.message}`,
|
|
136
143
|
}),
|
|
137
144
|
successfulResponseHandler: createJsonResponseHandler(
|
|
138
|
-
|
|
145
|
+
AmazonBedrockEmbeddingResponseSchema,
|
|
139
146
|
),
|
|
140
147
|
fetch: this.config.fetch,
|
|
141
148
|
abortSignal,
|
|
@@ -180,7 +187,7 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
180
187
|
}
|
|
181
188
|
}
|
|
182
189
|
|
|
183
|
-
const
|
|
190
|
+
const AmazonBedrockEmbeddingResponseSchema = z.union([
|
|
184
191
|
// Titan-style response
|
|
185
192
|
z.object({
|
|
186
193
|
embedding: z.array(z.number()),
|
|
@@ -7,9 +7,9 @@ import {
|
|
|
7
7
|
type ResponseHandler,
|
|
8
8
|
} from '@ai-sdk/provider-utils';
|
|
9
9
|
import type { ZodType } from 'zod/v4';
|
|
10
|
-
import {
|
|
10
|
+
import { createAmazonBedrockEventStreamDecoder } from './amazon-bedrock-event-stream-decoder';
|
|
11
11
|
|
|
12
|
-
export const
|
|
12
|
+
export const createAmazonBedrockEventStreamResponseHandler =
|
|
13
13
|
<T>(
|
|
14
14
|
chunkSchema: ZodType<T, any>,
|
|
15
15
|
): ResponseHandler<ReadableStream<ParseResult<T>>> =>
|
|
@@ -22,7 +22,7 @@ export const createBedrockEventStreamResponseHandler =
|
|
|
22
22
|
|
|
23
23
|
return {
|
|
24
24
|
responseHeaders,
|
|
25
|
-
value:
|
|
25
|
+
value: createAmazonBedrockEventStreamDecoder<ParseResult<T>>(
|
|
26
26
|
response.body,
|
|
27
27
|
async (event, controller) => {
|
|
28
28
|
if (event.messageType === 'event') {
|
|
@@ -18,12 +18,12 @@ import {
|
|
|
18
18
|
} from '@ai-sdk/provider-utils';
|
|
19
19
|
import {
|
|
20
20
|
modelMaxImagesPerCall,
|
|
21
|
-
type
|
|
22
|
-
} from './bedrock-image-settings';
|
|
23
|
-
import {
|
|
21
|
+
type AmazonBedrockImageModelId,
|
|
22
|
+
} from './amazon-bedrock-image-settings';
|
|
23
|
+
import { AmazonBedrockErrorSchema } from './amazon-bedrock-error';
|
|
24
24
|
import { z } from 'zod/v4';
|
|
25
25
|
|
|
26
|
-
type
|
|
26
|
+
type AmazonBedrockImageModelConfig = {
|
|
27
27
|
baseUrl: () => string;
|
|
28
28
|
headers?: Resolvable<Record<string, string | undefined>>;
|
|
29
29
|
fetch?: FetchFunction;
|
|
@@ -32,11 +32,11 @@ type BedrockImageModelConfig = {
|
|
|
32
32
|
};
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
export class
|
|
35
|
+
export class AmazonBedrockImageModel implements ImageModelV4 {
|
|
36
36
|
readonly specificationVersion = 'v4';
|
|
37
37
|
readonly provider = 'amazon-bedrock';
|
|
38
38
|
|
|
39
|
-
static [WORKFLOW_SERIALIZE](model:
|
|
39
|
+
static [WORKFLOW_SERIALIZE](model: AmazonBedrockImageModel) {
|
|
40
40
|
return serializeModelOptions({
|
|
41
41
|
modelId: model.modelId,
|
|
42
42
|
config: model.config,
|
|
@@ -45,9 +45,9 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
45
45
|
|
|
46
46
|
static [WORKFLOW_DESERIALIZE](options: {
|
|
47
47
|
modelId: string;
|
|
48
|
-
config:
|
|
48
|
+
config: AmazonBedrockImageModelConfig;
|
|
49
49
|
}) {
|
|
50
|
-
return new
|
|
50
|
+
return new AmazonBedrockImageModel(options.modelId, options.config);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
get maxImagesPerCall(): number {
|
|
@@ -60,8 +60,8 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
constructor(
|
|
63
|
-
readonly modelId:
|
|
64
|
-
private readonly config:
|
|
63
|
+
readonly modelId: AmazonBedrockImageModelId,
|
|
64
|
+
private readonly config: AmazonBedrockImageModelConfig,
|
|
65
65
|
) {}
|
|
66
66
|
|
|
67
67
|
async doGenerate({
|
|
@@ -83,17 +83,22 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
83
83
|
|
|
84
84
|
const hasFiles = files != null && files.length > 0;
|
|
85
85
|
|
|
86
|
+
// Prefer the new `amazonBedrock` providerOptions key; fall back to the
|
|
87
|
+
// legacy `bedrock` key for backward compatibility.
|
|
88
|
+
const amazonBedrockOptions = (providerOptions?.amazonBedrock ??
|
|
89
|
+
providerOptions?.bedrock) as Record<string, any> | undefined;
|
|
90
|
+
|
|
86
91
|
// Build image generation config (common to most modes)
|
|
87
92
|
const imageGenerationConfig = {
|
|
88
93
|
...(width ? { width } : {}),
|
|
89
94
|
...(height ? { height } : {}),
|
|
90
95
|
...(seed ? { seed } : {}),
|
|
91
96
|
...(n ? { numberOfImages: n } : {}),
|
|
92
|
-
...(
|
|
93
|
-
? { quality:
|
|
97
|
+
...(amazonBedrockOptions?.quality
|
|
98
|
+
? { quality: amazonBedrockOptions.quality }
|
|
94
99
|
: {}),
|
|
95
|
-
...(
|
|
96
|
-
? { cfgScale:
|
|
100
|
+
...(amazonBedrockOptions?.cfgScale
|
|
101
|
+
? { cfgScale: amazonBedrockOptions.cfgScale }
|
|
97
102
|
: {}),
|
|
98
103
|
};
|
|
99
104
|
|
|
@@ -102,11 +107,11 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
102
107
|
if (hasFiles) {
|
|
103
108
|
// Check if mask is actually provided (has valid data, not just an empty object)
|
|
104
109
|
const hasMask = mask?.type != null;
|
|
105
|
-
const hasMaskPrompt =
|
|
110
|
+
const hasMaskPrompt = amazonBedrockOptions?.maskPrompt != null;
|
|
106
111
|
|
|
107
112
|
// Determine task type from provider options, or infer from mask presence
|
|
108
113
|
const taskType =
|
|
109
|
-
|
|
114
|
+
amazonBedrockOptions?.taskType ??
|
|
110
115
|
(hasMask || hasMaskPrompt ? 'INPAINTING' : 'IMAGE_VARIATION');
|
|
111
116
|
|
|
112
117
|
const sourceImageBase64 = getBase64Data(files[0]);
|
|
@@ -116,8 +121,8 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
116
121
|
const inPaintingParams: Record<string, unknown> = {
|
|
117
122
|
image: sourceImageBase64,
|
|
118
123
|
...(prompt ? { text: prompt } : {}),
|
|
119
|
-
...(
|
|
120
|
-
? { negativeText:
|
|
124
|
+
...(amazonBedrockOptions?.negativeText
|
|
125
|
+
? { negativeText: amazonBedrockOptions.negativeText }
|
|
121
126
|
: {}),
|
|
122
127
|
};
|
|
123
128
|
|
|
@@ -125,7 +130,7 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
125
130
|
if (hasMask) {
|
|
126
131
|
inPaintingParams.maskImage = getBase64Data(mask);
|
|
127
132
|
} else if (hasMaskPrompt) {
|
|
128
|
-
inPaintingParams.maskPrompt =
|
|
133
|
+
inPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
|
|
129
134
|
}
|
|
130
135
|
|
|
131
136
|
args = {
|
|
@@ -140,11 +145,11 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
140
145
|
const outPaintingParams: Record<string, unknown> = {
|
|
141
146
|
image: sourceImageBase64,
|
|
142
147
|
...(prompt ? { text: prompt } : {}),
|
|
143
|
-
...(
|
|
144
|
-
? { negativeText:
|
|
148
|
+
...(amazonBedrockOptions?.negativeText
|
|
149
|
+
? { negativeText: amazonBedrockOptions.negativeText }
|
|
145
150
|
: {}),
|
|
146
|
-
...(
|
|
147
|
-
? { outPaintingMode:
|
|
151
|
+
...(amazonBedrockOptions?.outPaintingMode
|
|
152
|
+
? { outPaintingMode: amazonBedrockOptions.outPaintingMode }
|
|
148
153
|
: {}),
|
|
149
154
|
};
|
|
150
155
|
|
|
@@ -152,7 +157,7 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
152
157
|
if (hasMask) {
|
|
153
158
|
outPaintingParams.maskImage = getBase64Data(mask);
|
|
154
159
|
} else if (hasMaskPrompt) {
|
|
155
|
-
outPaintingParams.maskPrompt =
|
|
160
|
+
outPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
args = {
|
|
@@ -181,13 +186,12 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
181
186
|
const imageVariationParams: Record<string, unknown> = {
|
|
182
187
|
images,
|
|
183
188
|
...(prompt ? { text: prompt } : {}),
|
|
184
|
-
...(
|
|
185
|
-
? { negativeText:
|
|
189
|
+
...(amazonBedrockOptions?.negativeText
|
|
190
|
+
? { negativeText: amazonBedrockOptions.negativeText }
|
|
186
191
|
: {}),
|
|
187
|
-
...(
|
|
192
|
+
...(amazonBedrockOptions?.similarityStrength != null
|
|
188
193
|
? {
|
|
189
|
-
similarityStrength:
|
|
190
|
-
providerOptions.bedrock.similarityStrength,
|
|
194
|
+
similarityStrength: amazonBedrockOptions.similarityStrength,
|
|
191
195
|
}
|
|
192
196
|
: {}),
|
|
193
197
|
};
|
|
@@ -209,14 +213,14 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
209
213
|
taskType: 'TEXT_IMAGE',
|
|
210
214
|
textToImageParams: {
|
|
211
215
|
text: prompt,
|
|
212
|
-
...(
|
|
216
|
+
...(amazonBedrockOptions?.negativeText
|
|
213
217
|
? {
|
|
214
|
-
negativeText:
|
|
218
|
+
negativeText: amazonBedrockOptions.negativeText,
|
|
215
219
|
}
|
|
216
220
|
: {}),
|
|
217
|
-
...(
|
|
221
|
+
...(amazonBedrockOptions?.style
|
|
218
222
|
? {
|
|
219
|
-
style:
|
|
223
|
+
style: amazonBedrockOptions.style,
|
|
220
224
|
}
|
|
221
225
|
: {}),
|
|
222
226
|
},
|
|
@@ -244,11 +248,11 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
244
248
|
),
|
|
245
249
|
body: args,
|
|
246
250
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
247
|
-
errorSchema:
|
|
251
|
+
errorSchema: AmazonBedrockErrorSchema,
|
|
248
252
|
errorToMessage: error => `${error.type}: ${error.message}`,
|
|
249
253
|
}),
|
|
250
254
|
successfulResponseHandler: createJsonResponseHandler(
|
|
251
|
-
|
|
255
|
+
amazonBedrockImageResponseSchema,
|
|
252
256
|
),
|
|
253
257
|
abortSignal,
|
|
254
258
|
fetch: this.config.fetch,
|
|
@@ -303,7 +307,7 @@ function getBase64Data(file: ImageModelV4File): string {
|
|
|
303
307
|
|
|
304
308
|
// minimal version of the schema, focussed on what is needed for the implementation
|
|
305
309
|
// this approach limits breakages when the API changes and increases efficiency
|
|
306
|
-
const
|
|
310
|
+
const amazonBedrockImageResponseSchema = z.object({
|
|
307
311
|
// Normal successful response
|
|
308
312
|
images: z.array(z.string()).optional(),
|
|
309
313
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type AmazonBedrockImageModelId =
|
|
2
|
+
| 'amazon.nova-canvas-v1:0'
|
|
3
|
+
| (string & {});
|
|
4
|
+
|
|
5
|
+
// https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html
|
|
6
|
+
export const modelMaxImagesPerCall: Record<AmazonBedrockImageModelId, number> =
|
|
7
|
+
{
|
|
8
|
+
'amazon.nova-canvas-v1:0': 5,
|
|
9
|
+
};
|
|
@@ -10,9 +10,9 @@ import {
|
|
|
10
10
|
prepareTools as prepareAnthropicTools,
|
|
11
11
|
} from '@ai-sdk/anthropic/internal';
|
|
12
12
|
import type {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from './bedrock-api-types';
|
|
13
|
+
AmazonBedrockTool,
|
|
14
|
+
AmazonBedrockToolConfiguration,
|
|
15
|
+
} from './amazon-bedrock-api-types';
|
|
16
16
|
|
|
17
17
|
export async function prepareTools({
|
|
18
18
|
tools,
|
|
@@ -23,7 +23,7 @@ export async function prepareTools({
|
|
|
23
23
|
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
24
24
|
modelId: string;
|
|
25
25
|
}): Promise<{
|
|
26
|
-
toolConfig:
|
|
26
|
+
toolConfig: AmazonBedrockToolConfiguration;
|
|
27
27
|
additionalTools: Record<string, unknown> | undefined;
|
|
28
28
|
betas: Set<string>;
|
|
29
29
|
toolWarnings: SharedV4Warning[];
|
|
@@ -71,7 +71,7 @@ export async function prepareTools({
|
|
|
71
71
|
const functionTools = supportedTools.filter(t => t.type === 'function');
|
|
72
72
|
|
|
73
73
|
let additionalTools: Record<string, unknown> | undefined = undefined;
|
|
74
|
-
const
|
|
74
|
+
const amazonBedrockTools: AmazonBedrockTool[] = [];
|
|
75
75
|
|
|
76
76
|
const usingAnthropicTools = isAnthropicModel && ProviderTools.length > 0;
|
|
77
77
|
|
|
@@ -108,7 +108,7 @@ export async function prepareTools({
|
|
|
108
108
|
|
|
109
109
|
if (toolFactory != null) {
|
|
110
110
|
const fullToolDefinition = (toolFactory as (args: any) => any)({});
|
|
111
|
-
|
|
111
|
+
amazonBedrockTools.push({
|
|
112
112
|
toolSpec: {
|
|
113
113
|
name: tool.name,
|
|
114
114
|
inputSchema: {
|
|
@@ -134,7 +134,7 @@ export async function prepareTools({
|
|
|
134
134
|
: functionTools;
|
|
135
135
|
|
|
136
136
|
for (const tool of filteredFunctionTools) {
|
|
137
|
-
|
|
137
|
+
amazonBedrockTools.push({
|
|
138
138
|
toolSpec: {
|
|
139
139
|
name: tool.name,
|
|
140
140
|
...(tool.description?.trim() !== ''
|
|
@@ -149,22 +149,23 @@ export async function prepareTools({
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
// Handle toolChoice for standard Bedrock tools, but NOT for Anthropic provider-defined tools
|
|
152
|
-
let
|
|
153
|
-
|
|
152
|
+
let amazonBedrockToolChoice: AmazonBedrockToolConfiguration['toolChoice'] =
|
|
153
|
+
undefined;
|
|
154
|
+
if (!usingAnthropicTools && amazonBedrockTools.length > 0 && toolChoice) {
|
|
154
155
|
const type = toolChoice.type;
|
|
155
156
|
switch (type) {
|
|
156
157
|
case 'auto':
|
|
157
|
-
|
|
158
|
+
amazonBedrockToolChoice = { auto: {} };
|
|
158
159
|
break;
|
|
159
160
|
case 'required':
|
|
160
|
-
|
|
161
|
+
amazonBedrockToolChoice = { any: {} };
|
|
161
162
|
break;
|
|
162
163
|
case 'none':
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
amazonBedrockTools.length = 0;
|
|
165
|
+
amazonBedrockToolChoice = undefined;
|
|
165
166
|
break;
|
|
166
167
|
case 'tool':
|
|
167
|
-
|
|
168
|
+
amazonBedrockToolChoice = { tool: { name: toolChoice.toolName } };
|
|
168
169
|
break;
|
|
169
170
|
default: {
|
|
170
171
|
const _exhaustiveCheck: never = type;
|
|
@@ -175,9 +176,9 @@ export async function prepareTools({
|
|
|
175
176
|
}
|
|
176
177
|
}
|
|
177
178
|
|
|
178
|
-
const toolConfig:
|
|
179
|
-
|
|
180
|
-
? { tools:
|
|
179
|
+
const toolConfig: AmazonBedrockToolConfiguration =
|
|
180
|
+
amazonBedrockTools.length > 0
|
|
181
|
+
? { tools: amazonBedrockTools, toolChoice: amazonBedrockToolChoice }
|
|
181
182
|
: {};
|
|
182
183
|
|
|
183
184
|
return {
|
|
@@ -14,19 +14,19 @@ import {
|
|
|
14
14
|
withUserAgentSuffix,
|
|
15
15
|
type FetchFunction,
|
|
16
16
|
} from '@ai-sdk/provider-utils';
|
|
17
|
-
import {
|
|
18
|
-
import type {
|
|
19
|
-
import {
|
|
20
|
-
import type {
|
|
21
|
-
import {
|
|
22
|
-
import type {
|
|
17
|
+
import { AmazonBedrockChatLanguageModel } from './amazon-bedrock-chat-language-model';
|
|
18
|
+
import type { AmazonBedrockChatModelId } from './amazon-bedrock-chat-language-model-options';
|
|
19
|
+
import { AmazonBedrockEmbeddingModel } from './amazon-bedrock-embedding-model';
|
|
20
|
+
import type { AmazonBedrockEmbeddingModelId } from './amazon-bedrock-embedding-model-options';
|
|
21
|
+
import { AmazonBedrockImageModel } from './amazon-bedrock-image-model';
|
|
22
|
+
import type { AmazonBedrockImageModelId } from './amazon-bedrock-image-settings';
|
|
23
23
|
import {
|
|
24
24
|
createApiKeyFetchFunction,
|
|
25
25
|
createSigV4FetchFunction,
|
|
26
|
-
type
|
|
27
|
-
} from './bedrock-sigv4-fetch';
|
|
28
|
-
import {
|
|
29
|
-
import type {
|
|
26
|
+
type AmazonBedrockCredentials,
|
|
27
|
+
} from './amazon-bedrock-sigv4-fetch';
|
|
28
|
+
import { AmazonBedrockRerankingModel } from './reranking/amazon-bedrock-reranking-model';
|
|
29
|
+
import type { AmazonBedrockRerankingModelId } from './reranking/amazon-bedrock-reranking-model-options';
|
|
30
30
|
import { VERSION } from './version';
|
|
31
31
|
|
|
32
32
|
export interface AmazonBedrockProviderSettings {
|
|
@@ -103,56 +103,58 @@ export interface AmazonBedrockProviderSettings {
|
|
|
103
103
|
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
104
104
|
* and `sessionToken` settings.
|
|
105
105
|
*/
|
|
106
|
-
credentialProvider?: () => PromiseLike<
|
|
106
|
+
credentialProvider?: () => PromiseLike<
|
|
107
|
+
Omit<AmazonBedrockCredentials, 'region'>
|
|
108
|
+
>;
|
|
107
109
|
|
|
108
110
|
// for testing
|
|
109
111
|
generateId?: () => string;
|
|
110
112
|
}
|
|
111
113
|
|
|
112
114
|
export interface AmazonBedrockProvider extends ProviderV4 {
|
|
113
|
-
(modelId:
|
|
115
|
+
(modelId: AmazonBedrockChatModelId): LanguageModelV4;
|
|
114
116
|
|
|
115
|
-
languageModel(modelId:
|
|
117
|
+
languageModel(modelId: AmazonBedrockChatModelId): LanguageModelV4;
|
|
116
118
|
|
|
117
119
|
/**
|
|
118
120
|
* Creates a model for text embeddings.
|
|
119
121
|
*/
|
|
120
|
-
embedding(modelId:
|
|
122
|
+
embedding(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
121
123
|
|
|
122
124
|
/**
|
|
123
125
|
* Creates a model for text embeddings.
|
|
124
126
|
*/
|
|
125
|
-
embeddingModel(modelId:
|
|
127
|
+
embeddingModel(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
126
128
|
|
|
127
129
|
/**
|
|
128
130
|
* @deprecated Use `embedding` instead.
|
|
129
131
|
*/
|
|
130
|
-
textEmbedding(modelId:
|
|
132
|
+
textEmbedding(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
131
133
|
|
|
132
134
|
/**
|
|
133
135
|
* @deprecated Use `embeddingModel` instead.
|
|
134
136
|
*/
|
|
135
|
-
textEmbeddingModel(modelId:
|
|
137
|
+
textEmbeddingModel(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
136
138
|
|
|
137
139
|
/**
|
|
138
140
|
* Creates a model for image generation.
|
|
139
141
|
*/
|
|
140
|
-
image(modelId:
|
|
142
|
+
image(modelId: AmazonBedrockImageModelId): ImageModelV4;
|
|
141
143
|
|
|
142
144
|
/**
|
|
143
145
|
* Creates a model for image generation.
|
|
144
146
|
*/
|
|
145
|
-
imageModel(modelId:
|
|
147
|
+
imageModel(modelId: AmazonBedrockImageModelId): ImageModelV4;
|
|
146
148
|
|
|
147
149
|
/**
|
|
148
150
|
* Creates a model for reranking documents.
|
|
149
151
|
*/
|
|
150
|
-
reranking(modelId:
|
|
152
|
+
reranking(modelId: AmazonBedrockRerankingModelId): RerankingModelV4;
|
|
151
153
|
|
|
152
154
|
/**
|
|
153
155
|
* Creates a model for reranking documents.
|
|
154
156
|
*/
|
|
155
|
-
rerankingModel(modelId:
|
|
157
|
+
rerankingModel(modelId: AmazonBedrockRerankingModelId): RerankingModelV4;
|
|
156
158
|
|
|
157
159
|
/**
|
|
158
160
|
* Anthropic-specific tools that can be used with Anthropic models on Bedrock.
|
|
@@ -268,7 +270,7 @@ export function createAmazonBedrock(
|
|
|
268
270
|
return withUserAgentSuffix(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
|
|
269
271
|
};
|
|
270
272
|
|
|
271
|
-
const
|
|
273
|
+
const getAmazonBedrockRuntimeBaseUrl = (): string =>
|
|
272
274
|
withoutTrailingSlash(
|
|
273
275
|
options.baseURL ??
|
|
274
276
|
`https://bedrock-runtime.${loadSetting({
|
|
@@ -279,7 +281,7 @@ export function createAmazonBedrock(
|
|
|
279
281
|
})}.amazonaws.com`,
|
|
280
282
|
) ?? `https://bedrock-runtime.us-east-1.amazonaws.com`;
|
|
281
283
|
|
|
282
|
-
const
|
|
284
|
+
const getAmazonBedrockAgentRuntimeBaseUrl = (): string =>
|
|
283
285
|
withoutTrailingSlash(
|
|
284
286
|
options.baseURL ??
|
|
285
287
|
`https://bedrock-agent-runtime.${loadSetting({
|
|
@@ -290,15 +292,15 @@ export function createAmazonBedrock(
|
|
|
290
292
|
})}.amazonaws.com`,
|
|
291
293
|
) ?? `https://bedrock-agent-runtime.us-west-2.amazonaws.com`;
|
|
292
294
|
|
|
293
|
-
const createChatModel = (modelId:
|
|
294
|
-
new
|
|
295
|
-
baseUrl:
|
|
295
|
+
const createChatModel = (modelId: AmazonBedrockChatModelId) =>
|
|
296
|
+
new AmazonBedrockChatLanguageModel(modelId, {
|
|
297
|
+
baseUrl: getAmazonBedrockRuntimeBaseUrl,
|
|
296
298
|
headers: getHeaders,
|
|
297
299
|
fetch: fetchFunction,
|
|
298
300
|
generateId,
|
|
299
301
|
});
|
|
300
302
|
|
|
301
|
-
const provider = function (modelId:
|
|
303
|
+
const provider = function (modelId: AmazonBedrockChatModelId) {
|
|
302
304
|
if (new.target) {
|
|
303
305
|
throw new Error(
|
|
304
306
|
'The Amazon Bedrock model function cannot be called with the new keyword.',
|
|
@@ -308,23 +310,23 @@ export function createAmazonBedrock(
|
|
|
308
310
|
return createChatModel(modelId);
|
|
309
311
|
};
|
|
310
312
|
|
|
311
|
-
const createEmbeddingModel = (modelId:
|
|
312
|
-
new
|
|
313
|
-
baseUrl:
|
|
313
|
+
const createEmbeddingModel = (modelId: AmazonBedrockEmbeddingModelId) =>
|
|
314
|
+
new AmazonBedrockEmbeddingModel(modelId, {
|
|
315
|
+
baseUrl: getAmazonBedrockRuntimeBaseUrl,
|
|
314
316
|
headers: getHeaders,
|
|
315
317
|
fetch: fetchFunction,
|
|
316
318
|
});
|
|
317
319
|
|
|
318
|
-
const createImageModel = (modelId:
|
|
319
|
-
new
|
|
320
|
-
baseUrl:
|
|
320
|
+
const createImageModel = (modelId: AmazonBedrockImageModelId) =>
|
|
321
|
+
new AmazonBedrockImageModel(modelId, {
|
|
322
|
+
baseUrl: getAmazonBedrockRuntimeBaseUrl,
|
|
321
323
|
headers: getHeaders,
|
|
322
324
|
fetch: fetchFunction,
|
|
323
325
|
});
|
|
324
326
|
|
|
325
|
-
const createRerankingModel = (modelId:
|
|
326
|
-
new
|
|
327
|
-
baseUrl:
|
|
327
|
+
const createRerankingModel = (modelId: AmazonBedrockRerankingModelId) =>
|
|
328
|
+
new AmazonBedrockRerankingModel(modelId, {
|
|
329
|
+
baseUrl: getAmazonBedrockAgentRuntimeBaseUrl,
|
|
328
330
|
region: loadSetting({
|
|
329
331
|
settingValue: options.region,
|
|
330
332
|
settingName: 'region',
|
|
@@ -351,6 +353,6 @@ export function createAmazonBedrock(
|
|
|
351
353
|
}
|
|
352
354
|
|
|
353
355
|
/**
|
|
354
|
-
* Default Bedrock provider instance.
|
|
356
|
+
* Default Amazon Bedrock provider instance.
|
|
355
357
|
*/
|
|
356
|
-
export const
|
|
358
|
+
export const amazonBedrock = createAmazonBedrock();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
export const amazonBedrockReasoningMetadataSchema = z.object({
|
|
4
|
+
signature: z.string().optional(),
|
|
5
|
+
redactedData: z.string().optional(),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export type AmazonBedrockReasoningMetadata = z.infer<
|
|
9
|
+
typeof amazonBedrockReasoningMetadataSchema
|
|
10
|
+
>;
|