@ai-sdk/amazon-bedrock 5.0.0-beta.7 → 5.0.0-beta.85
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 +677 -4
- package/README.md +2 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +151 -117
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +36 -23
- package/dist/index.js +881 -604
- package/dist/index.js.map +1 -1
- package/dist/mantle/index.cjs +253 -0
- package/dist/mantle/index.cjs.map +1 -0
- package/dist/mantle/index.d.cts +99 -0
- package/dist/mantle/index.d.ts +99 -0
- package/dist/mantle/index.js +240 -0
- package/dist/mantle/index.js.map +1 -0
- package/docs/08-amazon-bedrock.mdx +310 -84
- package/mantle/index.d.ts +1 -0
- package/package.json +27 -20
- package/src/amazon-bedrock-api-types.ts +228 -0
- package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +27 -8
- package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +350 -180
- 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} +61 -29
- 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} +62 -38
- 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} +17 -9
- package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
- package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +7 -1
- package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +40 -24
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
- package/src/convert-to-amazon-bedrock-chat-messages.ts +556 -0
- package/src/index.ts +15 -8
- package/src/inject-fetch-headers.ts +1 -1
- package/src/mantle/bedrock-mantle-options.ts +15 -0
- package/src/mantle/bedrock-mantle-provider.ts +283 -0
- package/src/mantle/index.ts +6 -0
- 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/dist/anthropic/index.d.mts +0 -91
- package/dist/anthropic/index.mjs +0 -397
- package/dist/anthropic/index.mjs.map +0 -1
- package/dist/index.d.mts +0 -194
- package/dist/index.mjs +0 -2329
- package/dist/index.mjs.map +0 -1
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -68
- package/src/bedrock-api-types.ts +0 -216
- package/src/bedrock-image-settings.ts +0 -6
- package/src/convert-to-bedrock-chat-messages.ts +0 -468
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OpenAIChatLanguageModel,
|
|
3
|
+
OpenAIResponsesLanguageModel,
|
|
4
|
+
} from '@ai-sdk/openai/internal';
|
|
5
|
+
import {
|
|
6
|
+
NoSuchModelError,
|
|
7
|
+
type LanguageModelV4,
|
|
8
|
+
type ProviderV4,
|
|
9
|
+
} from '@ai-sdk/provider';
|
|
10
|
+
import {
|
|
11
|
+
loadOptionalSetting,
|
|
12
|
+
loadSetting,
|
|
13
|
+
withoutTrailingSlash,
|
|
14
|
+
withUserAgentSuffix,
|
|
15
|
+
type FetchFunction,
|
|
16
|
+
} from '@ai-sdk/provider-utils';
|
|
17
|
+
import {
|
|
18
|
+
createApiKeyFetchFunction,
|
|
19
|
+
createSigV4FetchFunction,
|
|
20
|
+
type AmazonBedrockCredentials,
|
|
21
|
+
} from '../amazon-bedrock-sigv4-fetch';
|
|
22
|
+
import type {
|
|
23
|
+
BedrockMantleChatModelId,
|
|
24
|
+
BedrockMantleResponsesModelId,
|
|
25
|
+
} from './bedrock-mantle-options';
|
|
26
|
+
import { VERSION } from '../version';
|
|
27
|
+
|
|
28
|
+
export interface BedrockMantleProvider extends ProviderV4 {
|
|
29
|
+
/**
|
|
30
|
+
* Creates a model for text generation using the Chat Completions API.
|
|
31
|
+
* Chat Completions has the broadest model support on Mantle.
|
|
32
|
+
*/
|
|
33
|
+
(modelId: BedrockMantleChatModelId): LanguageModelV4;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates a model for text generation using the Chat Completions API.
|
|
37
|
+
*/
|
|
38
|
+
languageModel(modelId: BedrockMantleChatModelId): LanguageModelV4;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a model for text generation using the Chat Completions API.
|
|
42
|
+
*/
|
|
43
|
+
chat(modelId: BedrockMantleChatModelId): LanguageModelV4;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a model for text generation using the Responses API.
|
|
47
|
+
* Not all Mantle models support this API. Notably, gpt-oss-safeguard models
|
|
48
|
+
* are Chat-only.
|
|
49
|
+
*/
|
|
50
|
+
responses(modelId: BedrockMantleResponsesModelId): LanguageModelV4;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @deprecated Mantle does not support embedding models.
|
|
54
|
+
*/
|
|
55
|
+
textEmbeddingModel(modelId: string): never;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface BedrockMantleProviderSettings {
|
|
59
|
+
/**
|
|
60
|
+
* The AWS region to use for the Bedrock Mantle endpoint. Defaults to the value of the
|
|
61
|
+
* `AWS_REGION` environment variable.
|
|
62
|
+
*/
|
|
63
|
+
region?: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* API key for authenticating requests using Bearer token authentication.
|
|
67
|
+
* When provided, this will be used instead of AWS SigV4 authentication.
|
|
68
|
+
* Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
|
|
69
|
+
*/
|
|
70
|
+
apiKey?: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The AWS access key ID to use for SigV4 authentication. Defaults to the value of the
|
|
74
|
+
* `AWS_ACCESS_KEY_ID` environment variable.
|
|
75
|
+
*/
|
|
76
|
+
accessKeyId?: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The AWS secret access key to use for SigV4 authentication. Defaults to the value of the
|
|
80
|
+
* `AWS_SECRET_ACCESS_KEY` environment variable.
|
|
81
|
+
*/
|
|
82
|
+
secretAccessKey?: string;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The AWS session token to use for SigV4 authentication. Defaults to the value of the
|
|
86
|
+
* `AWS_SESSION_TOKEN` environment variable.
|
|
87
|
+
*/
|
|
88
|
+
sessionToken?: string;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Base URL for the Bedrock Mantle API calls.
|
|
92
|
+
*/
|
|
93
|
+
baseURL?: string;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Custom headers to include in the requests.
|
|
97
|
+
*/
|
|
98
|
+
headers?: Record<string, string | undefined>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
102
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
103
|
+
*/
|
|
104
|
+
fetch?: FetchFunction;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The AWS credential provider to use for SigV4 authentication to get dynamic
|
|
108
|
+
* credentials similar to the AWS SDK. Setting a provider here will cause its
|
|
109
|
+
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
110
|
+
* and `sessionToken` settings.
|
|
111
|
+
*/
|
|
112
|
+
credentialProvider?: () => PromiseLike<
|
|
113
|
+
Omit<AmazonBedrockCredentials, 'region'>
|
|
114
|
+
>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Create an Amazon Bedrock Mantle provider instance.
|
|
119
|
+
* This provider uses the OpenAI-compatible Mantle API for models that are
|
|
120
|
+
* only available through the Mantle endpoint (e.g. openai.gpt-oss-20b).
|
|
121
|
+
*/
|
|
122
|
+
export function createBedrockMantle(
|
|
123
|
+
options: BedrockMantleProviderSettings = {},
|
|
124
|
+
): BedrockMantleProvider {
|
|
125
|
+
// Check for API key authentication first
|
|
126
|
+
const rawApiKey = loadOptionalSetting({
|
|
127
|
+
settingValue: options.apiKey,
|
|
128
|
+
environmentVariableName: 'AWS_BEARER_TOKEN_BEDROCK',
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Only use API key if it's a non-empty, non-whitespace string
|
|
132
|
+
const apiKey =
|
|
133
|
+
rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : undefined;
|
|
134
|
+
|
|
135
|
+
// Use API key authentication if available, otherwise fall back to SigV4
|
|
136
|
+
const fetchFunction = apiKey
|
|
137
|
+
? createApiKeyFetchFunction(apiKey, options.fetch)
|
|
138
|
+
: createSigV4FetchFunction(
|
|
139
|
+
async () => {
|
|
140
|
+
const region = loadSetting({
|
|
141
|
+
settingValue: options.region,
|
|
142
|
+
settingName: 'region',
|
|
143
|
+
environmentVariableName: 'AWS_REGION',
|
|
144
|
+
description: 'AWS region',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// If a credential provider is provided, use it to get the credentials.
|
|
148
|
+
if (options.credentialProvider) {
|
|
149
|
+
try {
|
|
150
|
+
return {
|
|
151
|
+
...(await options.credentialProvider()),
|
|
152
|
+
region,
|
|
153
|
+
};
|
|
154
|
+
} catch (error) {
|
|
155
|
+
const errorMessage =
|
|
156
|
+
error instanceof Error ? error.message : String(error);
|
|
157
|
+
throw new Error(
|
|
158
|
+
`AWS credential provider failed: ${errorMessage}. ` +
|
|
159
|
+
'Please ensure your credential provider returns valid AWS credentials ' +
|
|
160
|
+
'with accessKeyId and secretAccessKey properties.',
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
return {
|
|
167
|
+
region,
|
|
168
|
+
accessKeyId: loadSetting({
|
|
169
|
+
settingValue: options.accessKeyId,
|
|
170
|
+
settingName: 'accessKeyId',
|
|
171
|
+
environmentVariableName: 'AWS_ACCESS_KEY_ID',
|
|
172
|
+
description: 'AWS access key ID',
|
|
173
|
+
}),
|
|
174
|
+
secretAccessKey: loadSetting({
|
|
175
|
+
settingValue: options.secretAccessKey,
|
|
176
|
+
settingName: 'secretAccessKey',
|
|
177
|
+
environmentVariableName: 'AWS_SECRET_ACCESS_KEY',
|
|
178
|
+
description: 'AWS secret access key',
|
|
179
|
+
}),
|
|
180
|
+
sessionToken: loadOptionalSetting({
|
|
181
|
+
settingValue: options.sessionToken,
|
|
182
|
+
environmentVariableName: 'AWS_SESSION_TOKEN',
|
|
183
|
+
}),
|
|
184
|
+
};
|
|
185
|
+
} catch (error) {
|
|
186
|
+
const errorMessage =
|
|
187
|
+
error instanceof Error ? error.message : String(error);
|
|
188
|
+
if (
|
|
189
|
+
errorMessage.includes('AWS_ACCESS_KEY_ID') ||
|
|
190
|
+
errorMessage.includes('accessKeyId')
|
|
191
|
+
) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
'AWS SigV4 authentication requires AWS credentials. Please provide either:\n' +
|
|
194
|
+
'1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables\n' +
|
|
195
|
+
'2. Provide accessKeyId and secretAccessKey in options\n' +
|
|
196
|
+
'3. Use a credentialProvider function\n' +
|
|
197
|
+
'4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option\n' +
|
|
198
|
+
`Original error: ${errorMessage}`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (
|
|
202
|
+
errorMessage.includes('AWS_SECRET_ACCESS_KEY') ||
|
|
203
|
+
errorMessage.includes('secretAccessKey')
|
|
204
|
+
) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
'AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. ' +
|
|
207
|
+
'Please ensure both credentials are provided.\n' +
|
|
208
|
+
`Original error: ${errorMessage}`,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
options.fetch,
|
|
215
|
+
'bedrock-mantle',
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const getBaseURL = (): string =>
|
|
219
|
+
withoutTrailingSlash(
|
|
220
|
+
options.baseURL ??
|
|
221
|
+
`https://bedrock-mantle.${loadSetting({
|
|
222
|
+
settingValue: options.region,
|
|
223
|
+
settingName: 'region',
|
|
224
|
+
environmentVariableName: 'AWS_REGION',
|
|
225
|
+
description: 'AWS region',
|
|
226
|
+
})}.api.aws/v1`,
|
|
227
|
+
) ?? 'https://bedrock-mantle.us-east-1.api.aws/v1';
|
|
228
|
+
|
|
229
|
+
const getHeaders = (): Record<string, string | undefined> =>
|
|
230
|
+
withUserAgentSuffix(
|
|
231
|
+
options.headers ?? {},
|
|
232
|
+
`ai-sdk/amazon-bedrock/${VERSION}`,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
const url = ({ path }: { path: string; modelId: string }): string =>
|
|
236
|
+
`${getBaseURL()}${path}`;
|
|
237
|
+
|
|
238
|
+
const createChatModel = (modelId: BedrockMantleChatModelId) =>
|
|
239
|
+
new OpenAIChatLanguageModel(modelId, {
|
|
240
|
+
provider: 'bedrock-mantle.chat',
|
|
241
|
+
url,
|
|
242
|
+
headers: getHeaders,
|
|
243
|
+
fetch: fetchFunction,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const createResponsesModel = (modelId: BedrockMantleResponsesModelId) =>
|
|
247
|
+
new OpenAIResponsesLanguageModel(modelId, {
|
|
248
|
+
provider: 'bedrock-mantle.responses',
|
|
249
|
+
url,
|
|
250
|
+
headers: getHeaders,
|
|
251
|
+
fetch: fetchFunction,
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const provider = function (modelId: BedrockMantleChatModelId) {
|
|
255
|
+
if (new.target) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
'The Bedrock Mantle model function cannot be called with the new keyword.',
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return createChatModel(modelId);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
provider.specificationVersion = 'v4' as const;
|
|
265
|
+
provider.languageModel = createChatModel;
|
|
266
|
+
provider.chat = createChatModel;
|
|
267
|
+
provider.responses = createResponsesModel;
|
|
268
|
+
|
|
269
|
+
provider.embeddingModel = (modelId: string) => {
|
|
270
|
+
throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
|
|
271
|
+
};
|
|
272
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
273
|
+
provider.imageModel = (modelId: string) => {
|
|
274
|
+
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
return provider as BedrockMantleProvider;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Default Bedrock Mantle provider instance.
|
|
282
|
+
*/
|
|
283
|
+
export const bedrockMantle = createBedrockMantle();
|
|
@@ -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,
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { ProviderV4, LanguageModelV4 } from '@ai-sdk/provider';
|
|
2
|
-
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
import { anthropicTools } from '@ai-sdk/anthropic/internal';
|
|
4
|
-
|
|
5
|
-
interface BedrockCredentials {
|
|
6
|
-
region: string;
|
|
7
|
-
accessKeyId: string;
|
|
8
|
-
secretAccessKey: string;
|
|
9
|
-
sessionToken?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type BedrockAnthropicModelId = 'anthropic.claude-opus-4-6-v1' | 'anthropic.claude-sonnet-4-6-v1' | 'anthropic.claude-opus-4-5-20251101-v1:0' | 'anthropic.claude-sonnet-4-5-20250929-v1:0' | 'anthropic.claude-opus-4-20250514-v1:0' | 'anthropic.claude-sonnet-4-20250514-v1:0' | 'anthropic.claude-opus-4-1-20250805-v1:0' | 'anthropic.claude-haiku-4-5-20251001-v1:0' | 'anthropic.claude-3-7-sonnet-20250219-v1:0' | 'anthropic.claude-3-5-sonnet-20241022-v2:0' | 'anthropic.claude-3-5-sonnet-20240620-v1:0' | 'anthropic.claude-3-5-haiku-20241022-v1:0' | 'anthropic.claude-3-opus-20240229-v1:0' | 'anthropic.claude-3-sonnet-20240229-v1:0' | 'anthropic.claude-3-haiku-20240307-v1:0' | 'us.anthropic.claude-opus-4-6-v1' | 'us.anthropic.claude-sonnet-4-6-v1' | 'us.anthropic.claude-opus-4-5-20251101-v1:0' | 'us.anthropic.claude-sonnet-4-5-20250929-v1:0' | 'us.anthropic.claude-opus-4-20250514-v1:0' | 'us.anthropic.claude-sonnet-4-20250514-v1:0' | 'us.anthropic.claude-opus-4-1-20250805-v1:0' | 'us.anthropic.claude-haiku-4-5-20251001-v1:0' | 'us.anthropic.claude-3-7-sonnet-20250219-v1:0' | 'us.anthropic.claude-3-5-sonnet-20241022-v2:0' | 'us.anthropic.claude-3-5-sonnet-20240620-v1:0' | 'us.anthropic.claude-3-5-haiku-20241022-v1:0' | 'us.anthropic.claude-3-opus-20240229-v1:0' | 'us.anthropic.claude-3-sonnet-20240229-v1:0' | 'us.anthropic.claude-3-haiku-20240307-v1:0' | (string & {});
|
|
13
|
-
|
|
14
|
-
interface BedrockAnthropicProvider extends ProviderV4 {
|
|
15
|
-
/**
|
|
16
|
-
* Creates a model for text generation.
|
|
17
|
-
*/
|
|
18
|
-
(modelId: BedrockAnthropicModelId): LanguageModelV4;
|
|
19
|
-
/**
|
|
20
|
-
* Creates a model for text generation.
|
|
21
|
-
*/
|
|
22
|
-
languageModel(modelId: BedrockAnthropicModelId): LanguageModelV4;
|
|
23
|
-
/**
|
|
24
|
-
* Anthropic-specific computer use tool.
|
|
25
|
-
*/
|
|
26
|
-
tools: typeof anthropicTools;
|
|
27
|
-
/**
|
|
28
|
-
* @deprecated Use `embeddingModel` instead.
|
|
29
|
-
*/
|
|
30
|
-
textEmbeddingModel(modelId: string): never;
|
|
31
|
-
}
|
|
32
|
-
interface BedrockAnthropicProviderSettings {
|
|
33
|
-
/**
|
|
34
|
-
* The AWS region to use for the Bedrock provider. Defaults to the value of the
|
|
35
|
-
* `AWS_REGION` environment variable.
|
|
36
|
-
*/
|
|
37
|
-
region?: string;
|
|
38
|
-
/**
|
|
39
|
-
* API key for authenticating requests using Bearer token authentication.
|
|
40
|
-
* When provided, this will be used instead of AWS SigV4 authentication.
|
|
41
|
-
* Defaults to the value of the `AWS_BEARER_TOKEN_BEDROCK` environment variable.
|
|
42
|
-
*/
|
|
43
|
-
apiKey?: string;
|
|
44
|
-
/**
|
|
45
|
-
* The AWS access key ID to use for the Bedrock provider. Defaults to the value of the
|
|
46
|
-
* `AWS_ACCESS_KEY_ID` environment variable.
|
|
47
|
-
*/
|
|
48
|
-
accessKeyId?: string;
|
|
49
|
-
/**
|
|
50
|
-
* The AWS secret access key to use for the Bedrock provider. Defaults to the value of the
|
|
51
|
-
* `AWS_SECRET_ACCESS_KEY` environment variable.
|
|
52
|
-
*/
|
|
53
|
-
secretAccessKey?: string;
|
|
54
|
-
/**
|
|
55
|
-
* The AWS session token to use for the Bedrock provider. Defaults to the value of the
|
|
56
|
-
* `AWS_SESSION_TOKEN` environment variable.
|
|
57
|
-
*/
|
|
58
|
-
sessionToken?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Base URL for the Bedrock API calls.
|
|
61
|
-
*/
|
|
62
|
-
baseURL?: string;
|
|
63
|
-
/**
|
|
64
|
-
* Custom headers to include in the requests.
|
|
65
|
-
*/
|
|
66
|
-
headers?: Resolvable<Record<string, string | undefined>>;
|
|
67
|
-
/**
|
|
68
|
-
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
69
|
-
* or to provide a custom fetch implementation for e.g. testing.
|
|
70
|
-
*/
|
|
71
|
-
fetch?: FetchFunction;
|
|
72
|
-
/**
|
|
73
|
-
* The AWS credential provider to use for the Bedrock provider to get dynamic
|
|
74
|
-
* credentials similar to the AWS SDK. Setting a provider here will cause its
|
|
75
|
-
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
76
|
-
* and `sessionToken` settings.
|
|
77
|
-
*/
|
|
78
|
-
credentialProvider?: () => PromiseLike<Omit<BedrockCredentials, 'region'>>;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Create an Amazon Bedrock Anthropic provider instance.
|
|
82
|
-
* This provider uses the native Anthropic API through Bedrock's InvokeModel endpoint,
|
|
83
|
-
* bypassing the Converse API for better feature compatibility.
|
|
84
|
-
*/
|
|
85
|
-
declare function createBedrockAnthropic(options?: BedrockAnthropicProviderSettings): BedrockAnthropicProvider;
|
|
86
|
-
/**
|
|
87
|
-
* Default Bedrock Anthropic provider instance.
|
|
88
|
-
*/
|
|
89
|
-
declare const bedrockAnthropic: BedrockAnthropicProvider;
|
|
90
|
-
|
|
91
|
-
export { type BedrockAnthropicModelId, type BedrockAnthropicProvider, type BedrockAnthropicProviderSettings, bedrockAnthropic, createBedrockAnthropic };
|