@ai-sdk/amazon-bedrock 4.0.30 → 4.0.32
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 +14 -0
- package/dist/anthropic/index.js +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/index.js +65 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
- package/src/bedrock-embedding-model.ts +72 -25
- package/src/bedrock-embedding-options.ts +29 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/amazon-bedrock",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.32",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,17 +38,17 @@
|
|
|
38
38
|
"@smithy/eventstream-codec": "^4.0.1",
|
|
39
39
|
"@smithy/util-utf8": "^4.0.0",
|
|
40
40
|
"aws4fetch": "^1.0.20",
|
|
41
|
-
"@ai-sdk/anthropic": "3.0.
|
|
41
|
+
"@ai-sdk/anthropic": "3.0.24",
|
|
42
42
|
"@ai-sdk/provider": "3.0.5",
|
|
43
|
-
"@ai-sdk/provider-utils": "4.0.
|
|
43
|
+
"@ai-sdk/provider-utils": "4.0.10"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/node": "20.17.24",
|
|
47
47
|
"tsup": "^8.3.0",
|
|
48
48
|
"typescript": "5.8.3",
|
|
49
49
|
"zod": "3.25.76",
|
|
50
|
-
"@
|
|
51
|
-
"@ai-
|
|
50
|
+
"@ai-sdk/test-server": "1.0.3",
|
|
51
|
+
"@vercel/ai-tsconfig": "0.0.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -69,21 +69,39 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
|
|
|
69
69
|
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
|
|
70
70
|
//
|
|
71
71
|
// Note: Different embedding model families expect different request/response
|
|
72
|
-
// payloads (e.g. Titan vs Cohere). We keep the public interface stable and
|
|
72
|
+
// payloads (e.g. Titan vs Cohere vs Nova). We keep the public interface stable and
|
|
73
73
|
// adapt here based on the modelId.
|
|
74
|
-
const
|
|
74
|
+
const isNovaModel =
|
|
75
|
+
this.modelId.startsWith('amazon.nova-') && this.modelId.includes('embed');
|
|
76
|
+
const isCohereModel = this.modelId.startsWith('cohere.embed-');
|
|
77
|
+
|
|
78
|
+
const args = isNovaModel
|
|
75
79
|
? {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
taskType: 'SINGLE_EMBEDDING',
|
|
81
|
+
singleEmbeddingParams: {
|
|
82
|
+
embeddingPurpose:
|
|
83
|
+
bedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
|
|
84
|
+
embeddingDimension: bedrockOptions.embeddingDimension ?? 1024,
|
|
85
|
+
text: {
|
|
86
|
+
truncationMode: bedrockOptions.truncate ?? 'END',
|
|
87
|
+
value: values[0],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
81
90
|
}
|
|
82
|
-
:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
: isCohereModel
|
|
92
|
+
? {
|
|
93
|
+
// Cohere embedding models on Bedrock require `input_type`.
|
|
94
|
+
// Without it, the service attempts other schema branches and rejects the request.
|
|
95
|
+
input_type: bedrockOptions.inputType ?? 'search_query',
|
|
96
|
+
texts: [values[0]],
|
|
97
|
+
truncate: bedrockOptions.truncate,
|
|
98
|
+
}
|
|
99
|
+
: {
|
|
100
|
+
inputText: values[0],
|
|
101
|
+
dimensions: bedrockOptions.dimensions,
|
|
102
|
+
normalize: bedrockOptions.normalize,
|
|
103
|
+
};
|
|
104
|
+
|
|
87
105
|
const url = this.getUrl(this.modelId);
|
|
88
106
|
const { value: response } = await postJsonToApi({
|
|
89
107
|
url,
|
|
@@ -102,22 +120,41 @@ export class BedrockEmbeddingModel implements EmbeddingModelV3 {
|
|
|
102
120
|
abortSignal,
|
|
103
121
|
});
|
|
104
122
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
123
|
+
// Extract embedding based on response format
|
|
124
|
+
let embedding: number[];
|
|
125
|
+
if ('embedding' in response) {
|
|
126
|
+
// Titan response
|
|
127
|
+
embedding = response.embedding;
|
|
128
|
+
} else if (Array.isArray(response.embeddings)) {
|
|
129
|
+
const firstEmbedding = response.embeddings[0];
|
|
130
|
+
if (
|
|
131
|
+
typeof firstEmbedding === 'object' &&
|
|
132
|
+
firstEmbedding !== null &&
|
|
133
|
+
'embeddingType' in firstEmbedding
|
|
134
|
+
) {
|
|
135
|
+
// Nova response
|
|
136
|
+
embedding = firstEmbedding.embedding;
|
|
137
|
+
} else {
|
|
138
|
+
// Cohere v3 response
|
|
139
|
+
embedding = firstEmbedding as number[];
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
// Cohere v4 response
|
|
143
|
+
embedding = response.embeddings.float[0];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Extract token count based on response format
|
|
147
|
+
const tokens =
|
|
148
|
+
'inputTextTokenCount' in response
|
|
149
|
+
? response.inputTextTokenCount // Titan response
|
|
150
|
+
: 'inputTokenCount' in response
|
|
151
|
+
? (response.inputTokenCount ?? 0) // Nova response
|
|
152
|
+
: NaN; // Cohere doesn't return token count
|
|
111
153
|
|
|
112
154
|
return {
|
|
113
|
-
warnings: [],
|
|
114
155
|
embeddings: [embedding],
|
|
115
|
-
usage: {
|
|
116
|
-
|
|
117
|
-
'inputTextTokenCount' in response
|
|
118
|
-
? response.inputTextTokenCount
|
|
119
|
-
: NaN,
|
|
120
|
-
},
|
|
156
|
+
usage: { tokens },
|
|
157
|
+
warnings: [],
|
|
121
158
|
};
|
|
122
159
|
}
|
|
123
160
|
}
|
|
@@ -128,6 +165,16 @@ const BedrockEmbeddingResponseSchema = z.union([
|
|
|
128
165
|
embedding: z.array(z.number()),
|
|
129
166
|
inputTextTokenCount: z.number(),
|
|
130
167
|
}),
|
|
168
|
+
// Nova-style response
|
|
169
|
+
z.object({
|
|
170
|
+
embeddings: z.array(
|
|
171
|
+
z.object({
|
|
172
|
+
embeddingType: z.string(),
|
|
173
|
+
embedding: z.array(z.number()),
|
|
174
|
+
}),
|
|
175
|
+
),
|
|
176
|
+
inputTokenCount: z.number().optional(),
|
|
177
|
+
}),
|
|
131
178
|
// Cohere v3-style response
|
|
132
179
|
z.object({
|
|
133
180
|
embeddings: z.array(z.array(z.number())),
|
|
@@ -22,6 +22,33 @@ Only supported in amazon.titan-embed-text-v2:0.
|
|
|
22
22
|
*/
|
|
23
23
|
normalize: z.boolean().optional(),
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
The number of dimensions for Nova embedding models (defaults to 1024).
|
|
27
|
+
Supported values: 256, 384, 1024, 3072.
|
|
28
|
+
Only supported in amazon.nova-* embedding models.
|
|
29
|
+
*/
|
|
30
|
+
embeddingDimension: z
|
|
31
|
+
.union([z.literal(256), z.literal(384), z.literal(1024), z.literal(3072)])
|
|
32
|
+
.optional(),
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
The purpose of the embedding. Defaults to 'GENERIC_INDEX'.
|
|
36
|
+
Only supported in amazon.nova-* embedding models.
|
|
37
|
+
*/
|
|
38
|
+
embeddingPurpose: z
|
|
39
|
+
.enum([
|
|
40
|
+
'GENERIC_INDEX',
|
|
41
|
+
'TEXT_RETRIEVAL',
|
|
42
|
+
'IMAGE_RETRIEVAL',
|
|
43
|
+
'VIDEO_RETRIEVAL',
|
|
44
|
+
'DOCUMENT_RETRIEVAL',
|
|
45
|
+
'AUDIO_RETRIEVAL',
|
|
46
|
+
'GENERIC_RETRIEVAL',
|
|
47
|
+
'CLASSIFICATION',
|
|
48
|
+
'CLUSTERING',
|
|
49
|
+
])
|
|
50
|
+
.optional(),
|
|
51
|
+
|
|
25
52
|
/**
|
|
26
53
|
Input type for Cohere embedding models on Bedrock.
|
|
27
54
|
Common values: `search_document`, `search_query`, `classification`, `clustering`.
|
|
@@ -32,7 +59,8 @@ If not set, the provider defaults to `search_query`.
|
|
|
32
59
|
.optional(),
|
|
33
60
|
|
|
34
61
|
/**
|
|
35
|
-
Truncation behavior
|
|
62
|
+
Truncation behavior when input exceeds the model's context length.
|
|
63
|
+
Supported in Cohere and Nova embedding models. Defaults to 'END' for Nova models.
|
|
36
64
|
*/
|
|
37
65
|
truncate: z.enum(['NONE', 'START', 'END']).optional(),
|
|
38
66
|
});
|