@ai-sdk/amazon-bedrock
Advanced tools
Comparing version 1.1.6 to 2.0.0
# @ai-sdk/amazon-bedrock | ||
## 2.0.0 | ||
### Major Changes | ||
- 3ff4ef8: feat (provider/amazon-bedrock): remove dependence on AWS SDK Bedrock client library | ||
### Patch Changes | ||
- Updated dependencies [3ff4ef8] | ||
- @ai-sdk/provider-utils@2.1.7 | ||
## 1.1.6 | ||
@@ -4,0 +15,0 @@ |
import { ProviderV1, LanguageModelV1, EmbeddingModelV1 } from '@ai-sdk/provider'; | ||
import { BedrockRuntimeClientConfig } from '@aws-sdk/client-bedrock-runtime'; | ||
import { FetchFunction } from '@ai-sdk/provider-utils'; | ||
@@ -29,12 +29,34 @@ type BedrockChatModelId = 'amazon.titan-tg1-large' | 'amazon.titan-text-express-v1' | 'anthropic.claude-v2' | 'anthropic.claude-v2:1' | 'anthropic.claude-instant-v1' | 'anthropic.claude-3-5-sonnet-20240620-v1:0' | 'anthropic.claude-3-5-sonnet-20241022-v2:0' | 'anthropic.claude-3-5-haiku-20241022-v1:0' | 'anthropic.claude-3-sonnet-20240229-v1:0' | 'anthropic.claude-3-haiku-20240307-v1:0' | 'anthropic.claude-3-opus-20240229-v1:0' | 'cohere.command-text-v14' | 'cohere.command-light-text-v14' | 'cohere.command-r-v1:0' | 'cohere.command-r-plus-v1:0' | 'meta.llama3-70b-instruct-v1:0' | 'meta.llama3-8b-instruct-v1:0' | 'meta.llama3-1-405b-instruct-v1:0' | 'meta.llama3-1-70b-instruct-v1:0' | 'meta.llama3-1-8b-instruct-v1:0' | 'meta.llama3-2-11b-instruct-v1:0' | 'meta.llama3-2-1b-instruct-v1:0' | 'meta.llama3-2-3b-instruct-v1:0' | 'meta.llama3-2-90b-instruct-v1:0' | 'mistral.mistral-7b-instruct-v0:2' | 'mistral.mixtral-8x7b-instruct-v0:1' | 'mistral.mistral-large-2402-v1:0' | 'mistral.mistral-small-2402-v1:0' | 'amazon.titan-text-express-v1' | 'amazon.titan-text-lite-v1' | (string & {}); | ||
interface AmazonBedrockProviderSettings { | ||
/** | ||
The AWS region to use for the Bedrock provider. Defaults to the value of the | ||
`AWS_REGION` environment variable. | ||
*/ | ||
region?: string; | ||
/** | ||
The AWS access key ID to use for the Bedrock provider. Defaults to the value of the | ||
*/ | ||
accessKeyId?: string; | ||
/** | ||
The AWS secret access key to use for the Bedrock provider. Defaults to the value of the | ||
`AWS_SECRET_ACCESS_KEY` environment variable. | ||
*/ | ||
secretAccessKey?: string; | ||
/** | ||
The AWS session token to use for the Bedrock provider. Defaults to the value of the | ||
`AWS_SESSION_TOKEN` environment variable. | ||
*/ | ||
sessionToken?: string; | ||
/** | ||
* Complete Bedrock configuration for setting advanced authentication and | ||
* other options. When this is provided, the region, accessKeyId, and | ||
* secretAccessKey settings are ignored. | ||
Base URL for the Bedrock API calls. | ||
*/ | ||
bedrockOptions?: BedrockRuntimeClientConfig; | ||
baseURL?: string; | ||
/** | ||
Custom headers to include in the requests. | ||
*/ | ||
headers?: Record<string, string>; | ||
/** | ||
Custom fetch implementation. You can use it as a middleware to intercept requests, | ||
or to provide a custom fetch implementation for e.g. testing. | ||
*/ | ||
fetch?: FetchFunction; | ||
generateId?: () => string; | ||
@@ -41,0 +63,0 @@ } |
@@ -29,11 +29,102 @@ "use strict"; | ||
// src/bedrock-provider.ts | ||
var import_provider_utils2 = require("@ai-sdk/provider-utils"); | ||
var import_client_bedrock_runtime3 = require("@aws-sdk/client-bedrock-runtime"); | ||
var import_provider_utils6 = require("@ai-sdk/provider-utils"); | ||
// src/bedrock-chat-language-model.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_client_bedrock_runtime = require("@aws-sdk/client-bedrock-runtime"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
// src/bedrock-api-types.ts | ||
var BEDROCK_STOP_REASONS = [ | ||
"stop", | ||
"stop_sequence", | ||
"end_turn", | ||
"length", | ||
"max_tokens", | ||
"content-filter", | ||
"content_filtered", | ||
"guardrail_intervened", | ||
"tool-calls", | ||
"tool_use" | ||
]; | ||
// src/bedrock-error.ts | ||
var import_zod = require("zod"); | ||
var BedrockErrorSchema = import_zod.z.object({ | ||
message: import_zod.z.string(), | ||
type: import_zod.z.string().nullish() | ||
}); | ||
// src/bedrock-event-stream-response-handler.ts | ||
var import_provider = require("@ai-sdk/provider"); | ||
var import_provider_utils = require("@ai-sdk/provider-utils"); | ||
var import_eventstream_codec = require("@smithy/eventstream-codec"); | ||
var import_util_utf8 = require("@smithy/util-utf8"); | ||
var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response }) => { | ||
const responseHeaders = (0, import_provider_utils.extractResponseHeaders)(response); | ||
if (response.body == null) { | ||
throw new import_provider.EmptyResponseBodyError({}); | ||
} | ||
const codec = new import_eventstream_codec.EventStreamCodec(import_util_utf8.toUtf8, import_util_utf8.fromUtf8); | ||
let buffer = new Uint8Array(0); | ||
const textDecoder = new TextDecoder(); | ||
return { | ||
responseHeaders, | ||
value: response.body.pipeThrough( | ||
new TransformStream({ | ||
transform(chunk, controller) { | ||
var _a, _b; | ||
const newBuffer = new Uint8Array(buffer.length + chunk.length); | ||
newBuffer.set(buffer); | ||
newBuffer.set(chunk, buffer.length); | ||
buffer = newBuffer; | ||
while (buffer.length >= 4) { | ||
const totalLength = new DataView( | ||
buffer.buffer, | ||
buffer.byteOffset, | ||
buffer.byteLength | ||
).getUint32(0, false); | ||
if (buffer.length < totalLength) { | ||
break; | ||
} | ||
try { | ||
const subView = buffer.subarray(0, totalLength); | ||
const decoded = codec.decode(subView); | ||
buffer = buffer.slice(totalLength); | ||
if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") { | ||
const data = textDecoder.decode(decoded.body); | ||
const parsedDataResult = (0, import_provider_utils.safeParseJSON)({ text: data }); | ||
if (!parsedDataResult.success) { | ||
controller.enqueue(parsedDataResult); | ||
break; | ||
} | ||
delete parsedDataResult.value.p; | ||
let wrappedData = { | ||
[(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value | ||
}; | ||
const validatedWrappedData = (0, import_provider_utils.safeValidateTypes)({ | ||
value: wrappedData, | ||
schema: chunkSchema | ||
}); | ||
if (!validatedWrappedData.success) { | ||
controller.enqueue(validatedWrappedData); | ||
} else { | ||
controller.enqueue({ | ||
success: true, | ||
value: validatedWrappedData.value, | ||
rawValue: wrappedData | ||
}); | ||
} | ||
} | ||
} catch (e) { | ||
break; | ||
} | ||
} | ||
} | ||
}) | ||
) | ||
}; | ||
}; | ||
// src/bedrock-prepare-tools.ts | ||
var import_provider = require("@ai-sdk/provider"); | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools(mode) { | ||
@@ -99,3 +190,3 @@ var _a; | ||
const _exhaustiveCheck = type; | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
@@ -108,5 +199,5 @@ }); | ||
// src/convert-to-bedrock-chat-messages.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider_utils = require("@ai-sdk/provider-utils"); | ||
var generateFileId = (0, import_provider_utils.createIdGenerator)({ prefix: "file", size: 16 }); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils2 = require("@ai-sdk/provider-utils"); | ||
var generateFileId = (0, import_provider_utils2.createIdGenerator)({ prefix: "file", size: 16 }); | ||
function convertToBedrockChatMessages(prompt) { | ||
@@ -124,3 +215,3 @@ var _a, _b, _c, _d, _e; | ||
if (messages.length > 0) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "Multiple system messages that are separated by user/assistant messages" | ||
@@ -149,3 +240,3 @@ }); | ||
if (part.image instanceof URL) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
@@ -156,5 +247,9 @@ }); | ||
image: { | ||
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1], | ||
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split( | ||
"/" | ||
)) == null ? void 0 : _b[1], | ||
source: { | ||
bytes: (_c = part.image) != null ? _c : part.image | ||
bytes: (0, import_provider_utils2.convertUint8ArrayToBase64)( | ||
(_c = part.image) != null ? _c : part.image | ||
) | ||
} | ||
@@ -167,3 +262,3 @@ } | ||
if (part.data instanceof URL) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "File URLs in user messages" | ||
@@ -179,3 +274,3 @@ }); | ||
source: { | ||
bytes: Buffer.from(part.data, "base64") | ||
bytes: part.data | ||
} | ||
@@ -325,4 +420,8 @@ } | ||
// src/bedrock-chat-language-model.ts | ||
var import_zod2 = require("zod"); | ||
var BedrockChatLanguageModel = class { | ||
constructor(modelId, settings, config) { | ||
this.modelId = modelId; | ||
this.settings = settings; | ||
this.config = config; | ||
this.specificationVersion = "v1"; | ||
@@ -332,5 +431,2 @@ this.provider = "amazon-bedrock"; | ||
this.supportsImageUrls = false; | ||
this.modelId = modelId; | ||
this.settings = settings; | ||
this.config = config; | ||
} | ||
@@ -352,3 +448,3 @@ getArgs({ | ||
}) { | ||
var _a, _b; | ||
var _a; | ||
const type = mode.type; | ||
@@ -374,8 +470,2 @@ const warnings = []; | ||
} | ||
if (headers != null) { | ||
warnings.push({ | ||
type: "unsupported-setting", | ||
setting: "headers" | ||
}); | ||
} | ||
if (topK != null) { | ||
@@ -395,14 +485,16 @@ warnings.push({ | ||
const { system, messages } = convertToBedrockChatMessages(prompt); | ||
const inferenceConfig = { | ||
...maxTokens != null && { maxTokens }, | ||
...temperature != null && { temperature }, | ||
...topP != null && { topP }, | ||
...stopSequences != null && { stopSequences } | ||
}; | ||
const baseArgs = { | ||
modelId: this.modelId, | ||
system: system ? [{ text: system }] : void 0, | ||
additionalModelRequestFields: this.settings.additionalModelRequestFields, | ||
inferenceConfig: { | ||
maxTokens, | ||
temperature, | ||
topP, | ||
stopSequences | ||
...Object.keys(inferenceConfig).length > 0 && { | ||
inferenceConfig | ||
}, | ||
messages, | ||
guardrailConfig: (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.guardrailConfig | ||
...providerMetadata == null ? void 0 : providerMetadata.bedrock | ||
}; | ||
@@ -415,3 +507,3 @@ switch (type) { | ||
...baseArgs, | ||
...((_b = toolConfig.tools) == null ? void 0 : _b.length) ? { toolConfig } : {} | ||
...((_a = toolConfig.tools) == null ? void 0 : _a.length) ? { toolConfig } : {} | ||
}, | ||
@@ -422,3 +514,3 @@ warnings: [...warnings, ...toolWarnings] | ||
case "object-json": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "json-mode object generation" | ||
@@ -457,7 +549,25 @@ }); | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l; | ||
const { command, warnings } = this.getArgs(options); | ||
const response = await this.config.client.send( | ||
new import_client_bedrock_runtime.ConverseCommand(command) | ||
); | ||
const { messages: rawPrompt, ...rawSettings } = command; | ||
const { command: args, warnings } = this.getArgs(options); | ||
const url = `${this.getUrl(this.modelId)}/converse`; | ||
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({ | ||
url, | ||
headers: (0, import_provider_utils3.combineHeaders)( | ||
await (0, import_provider_utils3.resolve)(this.config.headers), | ||
options.headers | ||
), | ||
body: args, | ||
failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)({ | ||
errorSchema: BedrockErrorSchema, | ||
errorToMessage: (error) => { | ||
var _a2; | ||
return `${(_a2 = error.message) != null ? _a2 : "Unknown error"}`; | ||
} | ||
}), | ||
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)( | ||
BedrockResponseSchema | ||
), | ||
abortSignal: options.abortSignal, | ||
fetch: this.config.fetch | ||
}); | ||
const { messages: rawPrompt, ...rawSettings } = args; | ||
const providerMetadata = response.trace ? { bedrock: { trace: response.trace } } : void 0; | ||
@@ -478,3 +588,5 @@ return { | ||
}), | ||
finishReason: mapBedrockFinishReason(response.stopReason), | ||
finishReason: mapBedrockFinishReason( | ||
response.stopReason | ||
), | ||
usage: { | ||
@@ -485,12 +597,26 @@ promptTokens: (_j = (_i = response.usage) == null ? void 0 : _i.inputTokens) != null ? _j : Number.NaN, | ||
rawCall: { rawPrompt, rawSettings }, | ||
rawResponse: { headers: responseHeaders }, | ||
warnings, | ||
providerMetadata | ||
...providerMetadata && { providerMetadata } | ||
}; | ||
} | ||
async doStream(options) { | ||
const { command, warnings } = this.getArgs(options); | ||
const response = await this.config.client.send( | ||
new import_client_bedrock_runtime.ConverseStreamCommand(command) | ||
); | ||
const { messages: rawPrompt, ...rawSettings } = command; | ||
const { command: args, warnings } = this.getArgs(options); | ||
const url = `${this.getUrl(this.modelId)}/converse-stream`; | ||
const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({ | ||
url, | ||
headers: (0, import_provider_utils3.combineHeaders)( | ||
await (0, import_provider_utils3.resolve)(this.config.headers), | ||
options.headers | ||
), | ||
body: args, | ||
failedResponseHandler: (0, import_provider_utils3.createJsonErrorResponseHandler)({ | ||
errorSchema: BedrockErrorSchema, | ||
errorToMessage: (error) => `${error.type}: ${error.message}` | ||
}), | ||
successfulResponseHandler: createBedrockEventStreamResponseHandler(BedrockStreamSchema), | ||
abortSignal: options.abortSignal, | ||
fetch: this.config.fetch | ||
}); | ||
const { messages: rawPrompt, ...rawSettings } = args; | ||
let finishReason = "unknown"; | ||
@@ -502,22 +628,11 @@ let usage = { | ||
let providerMetadata = void 0; | ||
if (!response.stream) { | ||
throw new Error("No stream found"); | ||
} | ||
const stream = new ReadableStream({ | ||
async start(controller) { | ||
for await (const chunk of response.stream) { | ||
controller.enqueue({ success: true, value: chunk }); | ||
} | ||
controller.close(); | ||
} | ||
}); | ||
const toolCallContentBlocks = {}; | ||
return { | ||
stream: stream.pipeThrough( | ||
stream: response.pipeThrough( | ||
new TransformStream({ | ||
transform(chunk, controller) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i; | ||
function enqueueError(error) { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
function enqueueError(bedrockError) { | ||
finishReason = "error"; | ||
controller.enqueue({ type: "error", error }); | ||
controller.enqueue({ type: "error", error: bedrockError }); | ||
} | ||
@@ -563,3 +678,3 @@ if (!chunk.success) { | ||
} | ||
if ((_f = (_e = value.contentBlockDelta) == null ? void 0 : _e.delta) == null ? void 0 : _f.text) { | ||
if (((_e = value.contentBlockDelta) == null ? void 0 : _e.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) { | ||
controller.enqueue({ | ||
@@ -571,3 +686,3 @@ type: "text-delta", | ||
const contentBlockStart = value.contentBlockStart; | ||
if (((_g = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _g.toolUse) != null) { | ||
if (((_f = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _f.toolUse) != null) { | ||
const toolUse = contentBlockStart.start.toolUse; | ||
@@ -581,5 +696,5 @@ toolCallContentBlocks[contentBlockStart.contentBlockIndex] = { | ||
const contentBlockDelta = value.contentBlockDelta; | ||
if ((_h = contentBlockDelta == null ? void 0 : contentBlockDelta.delta) == null ? void 0 : _h.toolUse) { | ||
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) { | ||
const contentBlock = toolCallContentBlocks[contentBlockDelta.contentBlockIndex]; | ||
const delta = (_i = contentBlockDelta.delta.toolUse.input) != null ? _i : ""; | ||
const delta = (_g = contentBlockDelta.delta.toolUse.input) != null ? _g : ""; | ||
controller.enqueue({ | ||
@@ -615,3 +730,3 @@ type: "tool-call-delta", | ||
usage, | ||
providerMetadata | ||
...providerMetadata && { providerMetadata } | ||
}); | ||
@@ -622,11 +737,85 @@ } | ||
rawCall: { rawPrompt, rawSettings }, | ||
rawResponse: { headers: responseHeaders }, | ||
warnings | ||
}; | ||
} | ||
getUrl(modelId) { | ||
const encodedModelId = encodeURIComponent(modelId); | ||
return `${this.config.baseUrl()}/model/${encodedModelId}`; | ||
} | ||
}; | ||
var BedrockStopReasonSchema = import_zod2.z.union([ | ||
import_zod2.z.enum(BEDROCK_STOP_REASONS), | ||
import_zod2.z.string() | ||
]); | ||
var BedrockToolUseSchema = import_zod2.z.object({ | ||
toolUseId: import_zod2.z.string(), | ||
name: import_zod2.z.string(), | ||
input: import_zod2.z.unknown() | ||
}); | ||
var BedrockResponseSchema = import_zod2.z.object({ | ||
metrics: import_zod2.z.object({ | ||
latencyMs: import_zod2.z.number() | ||
}).nullish(), | ||
output: import_zod2.z.object({ | ||
message: import_zod2.z.object({ | ||
content: import_zod2.z.array( | ||
import_zod2.z.object({ | ||
text: import_zod2.z.string().nullish(), | ||
toolUse: BedrockToolUseSchema.nullish() | ||
}) | ||
), | ||
role: import_zod2.z.string() | ||
}) | ||
}), | ||
stopReason: BedrockStopReasonSchema, | ||
trace: import_zod2.z.unknown().nullish(), | ||
usage: import_zod2.z.object({ | ||
inputTokens: import_zod2.z.number(), | ||
outputTokens: import_zod2.z.number(), | ||
totalTokens: import_zod2.z.number() | ||
}) | ||
}); | ||
var BedrockStreamSchema = import_zod2.z.object({ | ||
contentBlockDelta: import_zod2.z.object({ | ||
contentBlockIndex: import_zod2.z.number(), | ||
delta: import_zod2.z.union([ | ||
import_zod2.z.object({ text: import_zod2.z.string() }), | ||
import_zod2.z.object({ toolUse: import_zod2.z.object({ input: import_zod2.z.string() }) }) | ||
]).nullish() | ||
}).nullish(), | ||
contentBlockStart: import_zod2.z.object({ | ||
contentBlockIndex: import_zod2.z.number(), | ||
start: import_zod2.z.object({ | ||
toolUse: BedrockToolUseSchema.nullish() | ||
}).nullish() | ||
}).nullish(), | ||
contentBlockStop: import_zod2.z.object({ | ||
contentBlockIndex: import_zod2.z.number() | ||
}).nullish(), | ||
internalServerException: import_zod2.z.record(import_zod2.z.unknown()).nullish(), | ||
messageStop: import_zod2.z.object({ | ||
additionalModelResponseFields: import_zod2.z.record(import_zod2.z.unknown()).nullish(), | ||
stopReason: BedrockStopReasonSchema | ||
}).nullish(), | ||
metadata: import_zod2.z.object({ | ||
trace: import_zod2.z.unknown().nullish(), | ||
usage: import_zod2.z.object({ | ||
inputTokens: import_zod2.z.number(), | ||
outputTokens: import_zod2.z.number() | ||
}).nullish() | ||
}).nullish(), | ||
modelStreamErrorException: import_zod2.z.record(import_zod2.z.unknown()).nullish(), | ||
throttlingException: import_zod2.z.record(import_zod2.z.unknown()).nullish(), | ||
validationException: import_zod2.z.record(import_zod2.z.unknown()).nullish() | ||
}); | ||
// src/bedrock-embedding-model.ts | ||
var import_client_bedrock_runtime2 = require("@aws-sdk/client-bedrock-runtime"); | ||
var import_provider_utils4 = require("@ai-sdk/provider-utils"); | ||
var import_zod3 = require("zod"); | ||
var BedrockEmbeddingModel = class { | ||
constructor(modelId, settings, config) { | ||
this.modelId = modelId; | ||
this.settings = settings; | ||
this.config = config; | ||
this.specificationVersion = "v1"; | ||
@@ -636,11 +825,14 @@ this.provider = "amazon-bedrock"; | ||
this.supportsParallelCalls = true; | ||
this.modelId = modelId; | ||
this.config = config; | ||
this.settings = settings; | ||
} | ||
getUrl(modelId) { | ||
const encodedModelId = encodeURIComponent(modelId); | ||
return `${this.config.baseUrl()}/model/${encodedModelId}/invoke`; | ||
} | ||
async doEmbed({ | ||
values | ||
values, | ||
headers, | ||
abortSignal | ||
}) { | ||
const fn = async (inputText) => { | ||
const payload = { | ||
const embedSingleText = async (inputText) => { | ||
const args = { | ||
inputText, | ||
@@ -650,61 +842,162 @@ dimensions: this.settings.dimensions, | ||
}; | ||
const command = new import_client_bedrock_runtime2.InvokeModelCommand({ | ||
contentType: "application/json", | ||
body: JSON.stringify(payload), | ||
modelId: this.modelId | ||
const url = this.getUrl(this.modelId); | ||
const { value: response } = await (0, import_provider_utils4.postJsonToApi)({ | ||
url, | ||
headers: await (0, import_provider_utils4.resolve)( | ||
(0, import_provider_utils4.combineHeaders)(await (0, import_provider_utils4.resolve)(this.config.headers), headers) | ||
), | ||
body: args, | ||
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)({ | ||
errorSchema: BedrockErrorSchema, | ||
errorToMessage: (error) => `${error.type}: ${error.message}` | ||
}), | ||
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)( | ||
BedrockEmbeddingResponseSchema | ||
), | ||
fetch: this.config.fetch, | ||
abortSignal | ||
}); | ||
const rawResponse = await this.config.client.send(command); | ||
const parsed = JSON.parse(new TextDecoder().decode(rawResponse.body)); | ||
return parsed; | ||
return { | ||
embedding: response.embedding, | ||
inputTextTokenCount: response.inputTextTokenCount | ||
}; | ||
}; | ||
const responses = await Promise.all(values.map(fn)); | ||
const response = responses.reduce( | ||
(acc, r) => { | ||
acc.embeddings.push(r.embedding); | ||
acc.usage.tokens += r.inputTextTokenCount; | ||
return acc; | ||
const responses = await Promise.all(values.map(embedSingleText)); | ||
return responses.reduce( | ||
(accumulated, response) => { | ||
accumulated.embeddings.push(response.embedding); | ||
accumulated.usage.tokens += response.inputTextTokenCount; | ||
return accumulated; | ||
}, | ||
{ embeddings: [], usage: { tokens: 0 } } | ||
); | ||
return response; | ||
} | ||
}; | ||
var BedrockEmbeddingResponseSchema = import_zod3.z.object({ | ||
embedding: import_zod3.z.array(import_zod3.z.number()), | ||
inputTextTokenCount: import_zod3.z.number() | ||
}); | ||
// src/headers-utils.ts | ||
function extractHeaders(headers) { | ||
let originalHeaders = {}; | ||
if (headers) { | ||
if (headers instanceof Headers) { | ||
originalHeaders = convertHeadersToRecord(headers); | ||
} else if (Array.isArray(headers)) { | ||
for (const [k, v] of headers) { | ||
originalHeaders[k.toLowerCase()] = v; | ||
} | ||
} else { | ||
originalHeaders = Object.fromEntries( | ||
Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]) | ||
); | ||
} | ||
} | ||
return originalHeaders; | ||
} | ||
function convertHeadersToRecord(headers) { | ||
const record = {}; | ||
headers.forEach((value, key) => { | ||
record[key.toLowerCase()] = value; | ||
}); | ||
return record; | ||
} | ||
// src/bedrock-sigv4-fetch.ts | ||
var import_provider_utils5 = require("@ai-sdk/provider-utils"); | ||
var import_aws4fetch = require("aws4fetch"); | ||
function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) { | ||
return async (input, init) => { | ||
var _a; | ||
if (((_a = init == null ? void 0 : init.method) == null ? void 0 : _a.toUpperCase()) !== "POST" || !(init == null ? void 0 : init.body)) { | ||
return fetch(input, init); | ||
} | ||
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; | ||
const originalHeaders = extractHeaders(init.headers); | ||
const body = prepareBodyString(init.body); | ||
const credentials = getCredentials(); | ||
const signer = new import_aws4fetch.AwsV4Signer({ | ||
url, | ||
method: "POST", | ||
headers: Object.entries((0, import_provider_utils5.removeUndefinedEntries)(originalHeaders)), | ||
body, | ||
region: credentials.region, | ||
accessKeyId: credentials.accessKeyId, | ||
secretAccessKey: credentials.secretAccessKey, | ||
sessionToken: credentials.sessionToken, | ||
service: "bedrock" | ||
}); | ||
const signingResult = await signer.sign(); | ||
const signedHeaders = convertHeadersToRecord(signingResult.headers); | ||
return fetch(input, { | ||
...init, | ||
body, | ||
headers: (0, import_provider_utils5.removeUndefinedEntries)( | ||
(0, import_provider_utils5.combineHeaders)(originalHeaders, signedHeaders) | ||
) | ||
}); | ||
}; | ||
} | ||
function prepareBodyString(body) { | ||
if (typeof body === "string") { | ||
return body; | ||
} else if (body instanceof Uint8Array) { | ||
return new TextDecoder().decode(body); | ||
} else if (body instanceof ArrayBuffer) { | ||
return new TextDecoder().decode(new Uint8Array(body)); | ||
} else { | ||
return JSON.stringify(body); | ||
} | ||
} | ||
// src/bedrock-provider.ts | ||
function createAmazonBedrock(options = {}) { | ||
const createBedrockRuntimeClient = () => { | ||
const sigv4Fetch = createSigV4FetchFunction( | ||
() => ({ | ||
region: (0, import_provider_utils6.loadSetting)({ | ||
settingValue: options.region, | ||
settingName: "region", | ||
environmentVariableName: "AWS_REGION", | ||
description: "AWS region" | ||
}), | ||
accessKeyId: (0, import_provider_utils6.loadSetting)({ | ||
settingValue: options.accessKeyId, | ||
settingName: "accessKeyId", | ||
environmentVariableName: "AWS_ACCESS_KEY_ID", | ||
description: "AWS access key ID" | ||
}), | ||
secretAccessKey: (0, import_provider_utils6.loadSetting)({ | ||
settingValue: options.secretAccessKey, | ||
settingName: "secretAccessKey", | ||
environmentVariableName: "AWS_SECRET_ACCESS_KEY", | ||
description: "AWS secret access key" | ||
}), | ||
sessionToken: (0, import_provider_utils6.loadOptionalSetting)({ | ||
settingValue: options.sessionToken, | ||
environmentVariableName: "AWS_SESSION_TOKEN" | ||
}) | ||
}), | ||
options.fetch | ||
); | ||
const getBaseUrl = () => { | ||
var _a, _b; | ||
return (_b = (0, import_provider_utils6.withoutTrailingSlash)( | ||
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${(0, import_provider_utils6.loadSetting)({ | ||
settingValue: options.region, | ||
settingName: "region", | ||
environmentVariableName: "AWS_REGION", | ||
description: "AWS region" | ||
})}.amazonaws.com` | ||
)) != null ? _b : `https://bedrock-runtime.us-east-1.amazonaws.com`; | ||
}; | ||
const createChatModel = (modelId, settings = {}) => { | ||
var _a; | ||
return new import_client_bedrock_runtime3.BedrockRuntimeClient( | ||
(_a = options.bedrockOptions) != null ? _a : { | ||
region: (0, import_provider_utils2.loadSetting)({ | ||
settingValue: options.region, | ||
settingName: "region", | ||
environmentVariableName: "AWS_REGION", | ||
description: "AWS region" | ||
}), | ||
credentials: { | ||
accessKeyId: (0, import_provider_utils2.loadSetting)({ | ||
settingValue: options.accessKeyId, | ||
settingName: "accessKeyId", | ||
environmentVariableName: "AWS_ACCESS_KEY_ID", | ||
description: "AWS access key ID" | ||
}), | ||
secretAccessKey: (0, import_provider_utils2.loadSetting)({ | ||
settingValue: options.secretAccessKey, | ||
settingName: "secretAccessKey", | ||
environmentVariableName: "AWS_SECRET_ACCESS_KEY", | ||
description: "AWS secret access key" | ||
}), | ||
sessionToken: (0, import_provider_utils2.loadOptionalSetting)({ | ||
settingValue: options.sessionToken, | ||
environmentVariableName: "AWS_SESSION_TOKEN" | ||
}) | ||
} | ||
} | ||
); | ||
return new BedrockChatLanguageModel(modelId, settings, { | ||
baseUrl: getBaseUrl, | ||
headers: (_a = options.headers) != null ? _a : {}, | ||
fetch: sigv4Fetch, | ||
generateId: import_provider_utils6.generateId | ||
}); | ||
}; | ||
const createChatModel = (modelId, settings = {}) => new BedrockChatLanguageModel(modelId, settings, { | ||
client: createBedrockRuntimeClient(), | ||
generateId: import_provider_utils2.generateId | ||
}); | ||
const provider = function(modelId, settings) { | ||
@@ -718,5 +1011,10 @@ if (new.target) { | ||
}; | ||
const createEmbeddingModel = (modelId, settings = {}) => new BedrockEmbeddingModel(modelId, settings, { | ||
client: createBedrockRuntimeClient() | ||
}); | ||
const createEmbeddingModel = (modelId, settings = {}) => { | ||
var _a; | ||
return new BedrockEmbeddingModel(modelId, settings, { | ||
baseUrl: getBaseUrl, | ||
headers: (_a = options.headers) != null ? _a : {}, | ||
fetch: sigv4Fetch | ||
}); | ||
}; | ||
provider.languageModel = createChatModel; | ||
@@ -723,0 +1021,0 @@ provider.embedding = createEmbeddingModel; |
{ | ||
"name": "@ai-sdk/amazon-bedrock", | ||
"version": "1.1.6", | ||
"version": "2.0.0", | ||
"license": "Apache-2.0", | ||
@@ -23,9 +23,9 @@ "sideEffects": false, | ||
"@ai-sdk/provider": "1.0.7", | ||
"@ai-sdk/provider-utils": "2.1.6", | ||
"@aws-sdk/client-bedrock-runtime": "^3.663.0" | ||
"@ai-sdk/provider-utils": "2.1.7", | ||
"@smithy/eventstream-codec": "^4.0.1", | ||
"@smithy/util-utf8": "^4.0.0", | ||
"aws4fetch": "^1.0.20" | ||
}, | ||
"devDependencies": { | ||
"@smithy/types": "^3.5.0", | ||
"@types/node": "^18.19.54", | ||
"aws-sdk-client-mock": "^4.0.2", | ||
"tsup": "^8.3.0", | ||
@@ -32,0 +32,0 @@ "typescript": "5.6.3", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
218436
5
2064
6
4
+ Added@smithy/util-utf8@^4.0.0
+ Addedaws4fetch@^1.0.20
+ Added@ai-sdk/provider-utils@2.1.7(transitive)
+ Addedaws4fetch@1.0.20(transitive)
- Removed@ai-sdk/provider-utils@2.1.6(transitive)
- Removed@aws-crypto/sha256-browser@5.2.0(transitive)
- Removed@aws-crypto/sha256-js@5.2.0(transitive)
- Removed@aws-crypto/supports-web-crypto@5.2.0(transitive)
- Removed@aws-sdk/client-bedrock-runtime@3.751.0(transitive)
- Removed@aws-sdk/client-sso@3.750.0(transitive)
- Removed@aws-sdk/core@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-env@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-http@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-ini@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-node@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-process@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-sso@3.750.0(transitive)
- Removed@aws-sdk/credential-provider-web-identity@3.750.0(transitive)
- Removed@aws-sdk/middleware-host-header@3.734.0(transitive)
- Removed@aws-sdk/middleware-logger@3.734.0(transitive)
- Removed@aws-sdk/middleware-recursion-detection@3.734.0(transitive)
- Removed@aws-sdk/middleware-user-agent@3.750.0(transitive)
- Removed@aws-sdk/nested-clients@3.750.0(transitive)
- Removed@aws-sdk/region-config-resolver@3.734.0(transitive)
- Removed@aws-sdk/token-providers@3.750.0(transitive)
- Removed@aws-sdk/util-endpoints@3.743.0(transitive)
- Removed@aws-sdk/util-locate-window@3.723.0(transitive)
- Removed@aws-sdk/util-user-agent-browser@3.734.0(transitive)
- Removed@aws-sdk/util-user-agent-node@3.750.0(transitive)
- Removed@smithy/abort-controller@4.0.1(transitive)
- Removed@smithy/config-resolver@4.0.1(transitive)
- Removed@smithy/core@3.1.4(transitive)
- Removed@smithy/credential-provider-imds@4.0.1(transitive)
- Removed@smithy/eventstream-serde-browser@4.0.1(transitive)
- Removed@smithy/eventstream-serde-config-resolver@4.0.1(transitive)
- Removed@smithy/eventstream-serde-node@4.0.1(transitive)
- Removed@smithy/eventstream-serde-universal@4.0.1(transitive)
- Removed@smithy/fetch-http-handler@5.0.1(transitive)
- Removed@smithy/hash-node@4.0.1(transitive)
- Removed@smithy/invalid-dependency@4.0.1(transitive)
- Removed@smithy/middleware-content-length@4.0.1(transitive)
- Removed@smithy/middleware-endpoint@4.0.5(transitive)
- Removed@smithy/middleware-retry@4.0.6(transitive)
- Removed@smithy/middleware-serde@4.0.2(transitive)
- Removed@smithy/middleware-stack@4.0.1(transitive)
- Removed@smithy/node-config-provider@4.0.1(transitive)
- Removed@smithy/node-http-handler@4.0.2(transitive)
- Removed@smithy/property-provider@4.0.1(transitive)
- Removed@smithy/protocol-http@5.0.1(transitive)
- Removed@smithy/querystring-builder@4.0.1(transitive)
- Removed@smithy/querystring-parser@4.0.1(transitive)
- Removed@smithy/service-error-classification@4.0.1(transitive)
- Removed@smithy/shared-ini-file-loader@4.0.1(transitive)
- Removed@smithy/signature-v4@5.0.1(transitive)
- Removed@smithy/smithy-client@4.1.5(transitive)
- Removed@smithy/url-parser@4.0.1(transitive)
- Removed@smithy/util-base64@4.0.0(transitive)
- Removed@smithy/util-body-length-browser@4.0.0(transitive)
- Removed@smithy/util-body-length-node@4.0.0(transitive)
- Removed@smithy/util-config-provider@4.0.0(transitive)
- Removed@smithy/util-defaults-mode-browser@4.0.6(transitive)
- Removed@smithy/util-defaults-mode-node@4.0.6(transitive)
- Removed@smithy/util-endpoints@3.0.1(transitive)
- Removed@smithy/util-middleware@4.0.1(transitive)
- Removed@smithy/util-retry@4.0.1(transitive)
- Removed@smithy/util-stream@4.1.1(transitive)
- Removed@smithy/util-uri-escape@4.0.0(transitive)
- Removed@types/uuid@9.0.8(transitive)
- Removedbowser@2.11.0(transitive)
- Removedfast-xml-parser@4.4.1(transitive)
- Removedstrnum@1.0.5(transitive)
- Removeduuid@9.0.1(transitive)
Updated@ai-sdk/provider-utils@2.1.7