@effect/ai-amazon-bedrock 0.6.2 → 0.7.1
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/AmazonBedrockTool/package.json +6 -0
- package/dist/cjs/AmazonBedrockClient.js +71 -145
- package/dist/cjs/AmazonBedrockClient.js.map +1 -1
- package/dist/cjs/AmazonBedrockLanguageModel.js +844 -337
- package/dist/cjs/AmazonBedrockLanguageModel.js.map +1 -1
- package/dist/cjs/AmazonBedrockSchema.js +94 -39
- package/dist/cjs/AmazonBedrockSchema.js.map +1 -1
- package/dist/cjs/AmazonBedrockTool.js +53 -0
- package/dist/cjs/AmazonBedrockTool.js.map +1 -0
- package/dist/cjs/EventStreamEncoding.js +5 -4
- package/dist/cjs/EventStreamEncoding.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/internal/utilities.js +3 -1
- package/dist/cjs/internal/utilities.js.map +1 -1
- package/dist/dts/AmazonBedrockClient.d.ts +34 -34
- package/dist/dts/AmazonBedrockClient.d.ts.map +1 -1
- package/dist/dts/AmazonBedrockLanguageModel.d.ts +72 -20
- package/dist/dts/AmazonBedrockLanguageModel.d.ts.map +1 -1
- package/dist/dts/AmazonBedrockSchema.d.ts +543 -102
- package/dist/dts/AmazonBedrockSchema.d.ts.map +1 -1
- package/dist/dts/AmazonBedrockTool.d.ts +184 -0
- package/dist/dts/AmazonBedrockTool.d.ts.map +1 -0
- package/dist/dts/EventStreamEncoding.d.ts +2 -2
- package/dist/dts/EventStreamEncoding.d.ts.map +1 -1
- package/dist/dts/index.d.ts +4 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/esm/AmazonBedrockClient.js +71 -144
- package/dist/esm/AmazonBedrockClient.js.map +1 -1
- package/dist/esm/AmazonBedrockLanguageModel.js +841 -333
- package/dist/esm/AmazonBedrockLanguageModel.js.map +1 -1
- package/dist/esm/AmazonBedrockSchema.js +93 -37
- package/dist/esm/AmazonBedrockSchema.js.map +1 -1
- package/dist/esm/AmazonBedrockTool.js +45 -0
- package/dist/esm/AmazonBedrockTool.js.map +1 -0
- package/dist/esm/EventStreamEncoding.js +5 -3
- package/dist/esm/EventStreamEncoding.js.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/utilities.js +2 -0
- package/dist/esm/internal/utilities.js.map +1 -1
- package/package.json +13 -4
- package/src/AmazonBedrockClient.ts +144 -241
- package/src/AmazonBedrockLanguageModel.ts +978 -372
- package/src/AmazonBedrockSchema.ts +134 -46
- package/src/AmazonBedrockTool.ts +251 -0
- package/src/EventStreamEncoding.ts +80 -73
- package/src/index.ts +5 -0
- package/src/internal/utilities.ts +6 -3
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
import * as
|
|
6
|
-
import * as
|
|
7
|
-
import * as
|
|
8
|
-
import
|
|
9
|
-
import
|
|
4
|
+
import { prepareTools as prepareAnthropicTools } from "@effect/ai-anthropic/AnthropicLanguageModel";
|
|
5
|
+
import * as AnthropicTool from "@effect/ai-anthropic/AnthropicTool";
|
|
6
|
+
import * as AiError from "@effect/ai/AiError";
|
|
7
|
+
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
8
|
+
import * as AiModel from "@effect/ai/Model";
|
|
9
|
+
import { addGenAIAnnotations } from "@effect/ai/Telemetry";
|
|
10
|
+
import * as Tool from "@effect/ai/Tool";
|
|
10
11
|
import * as Context from "effect/Context";
|
|
12
|
+
import * as DateTime from "effect/DateTime";
|
|
11
13
|
import * as Effect from "effect/Effect";
|
|
12
14
|
import * as Encoding from "effect/Encoding";
|
|
13
|
-
import { dual
|
|
15
|
+
import { dual } from "effect/Function";
|
|
14
16
|
import * as Layer from "effect/Layer";
|
|
15
|
-
import * as Option from "effect/Option";
|
|
16
17
|
import * as Predicate from "effect/Predicate";
|
|
17
18
|
import * as Stream from "effect/Stream";
|
|
18
19
|
import { AmazonBedrockClient } from "./AmazonBedrockClient.js";
|
|
20
|
+
import { ImageFormat } from "./AmazonBedrockSchema.js";
|
|
19
21
|
import * as InternalUtilities from "./internal/utilities.js";
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
+
const BEDROCK_CACHE_POINT = {
|
|
23
|
+
cachePoint: {
|
|
24
|
+
type: "default"
|
|
25
|
+
}
|
|
22
26
|
};
|
|
23
27
|
// =============================================================================
|
|
24
28
|
// Configuration
|
|
@@ -34,21 +38,13 @@ export class Config extends /*#__PURE__*/Context.Tag("@effect/ai-amazon-bedrock/
|
|
|
34
38
|
static getOrUndefined = /*#__PURE__*/Effect.map(/*#__PURE__*/Effect.context(), context => context.unsafeMap.get(Config.key));
|
|
35
39
|
}
|
|
36
40
|
// =============================================================================
|
|
37
|
-
// Amazon Bedrock Provider Metadata
|
|
38
|
-
// =============================================================================
|
|
39
|
-
/**
|
|
40
|
-
* @since 1.0.0
|
|
41
|
-
* @category Context
|
|
42
|
-
*/
|
|
43
|
-
export class ProviderMetadata extends /*#__PURE__*/Context.Tag(InternalUtilities.ProviderMetadataKey)() {}
|
|
44
|
-
// =============================================================================
|
|
45
41
|
// Amazon Bedrock Language Model
|
|
46
42
|
// =============================================================================
|
|
47
43
|
/**
|
|
48
44
|
* @since 1.0.0
|
|
49
45
|
* @category AiModels
|
|
50
46
|
*/
|
|
51
|
-
export const model = (model, config) => AiModel.make(layer({
|
|
47
|
+
export const model = (model, config) => AiModel.make("amazon-bedrock", layer({
|
|
52
48
|
model,
|
|
53
49
|
config
|
|
54
50
|
}));
|
|
@@ -58,107 +54,109 @@ export const model = (model, config) => AiModel.make(layer({
|
|
|
58
54
|
*/
|
|
59
55
|
export const make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
|
|
60
56
|
const client = yield* AmazonBedrockClient;
|
|
61
|
-
const
|
|
62
|
-
if (options.tools.length === 0) return undefined;
|
|
63
|
-
const isStructured = options.tools.length === 1 && options.tools[0].structured;
|
|
64
|
-
const tools = options.tools.map(tool => ({
|
|
65
|
-
toolSpec: {
|
|
66
|
-
name: tool.name,
|
|
67
|
-
description: tool.description,
|
|
68
|
-
inputSchema: {
|
|
69
|
-
json: tool.parameters
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}));
|
|
73
|
-
const toolConfig = {
|
|
74
|
-
tools
|
|
75
|
-
};
|
|
76
|
-
if (isStructured) {
|
|
77
|
-
toolConfig.toolChoice = {
|
|
78
|
-
tool: {
|
|
79
|
-
name: options.tools[0].name
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
return toolConfig;
|
|
83
|
-
}
|
|
84
|
-
if (options.toolChoice === "auto") {
|
|
85
|
-
toolConfig.toolChoice = {
|
|
86
|
-
auto: {}
|
|
87
|
-
};
|
|
88
|
-
} else if (options.toolChoice === "none") {
|
|
89
|
-
toolConfig.toolChoice = undefined;
|
|
90
|
-
} else if (options.toolChoice === "required") {
|
|
91
|
-
toolConfig.toolChoice = {
|
|
92
|
-
any: {}
|
|
93
|
-
};
|
|
94
|
-
} else {
|
|
95
|
-
toolConfig.toolChoice = {
|
|
96
|
-
tool: {
|
|
97
|
-
name: options.toolChoice.tool
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
return toolConfig;
|
|
102
|
-
};
|
|
103
|
-
const makeRequest = Effect.fnUntraced(function* (method, {
|
|
104
|
-
prompt,
|
|
105
|
-
system,
|
|
106
|
-
...rest
|
|
107
|
-
}) {
|
|
57
|
+
const makeRequest = Effect.fnUntraced(function* (providerOptions) {
|
|
108
58
|
const context = yield* Effect.context();
|
|
109
|
-
const
|
|
110
|
-
const toolConfig = makeToolConfig(rest);
|
|
111
|
-
return identity({
|
|
59
|
+
const config = {
|
|
112
60
|
modelId: options.model,
|
|
113
61
|
...options.config,
|
|
114
|
-
...context.unsafeMap.get(Config.key)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
text
|
|
118
|
-
}]), Option.getOrUndefined),
|
|
62
|
+
...context.unsafeMap.get(Config.key)
|
|
63
|
+
};
|
|
64
|
+
const {
|
|
119
65
|
messages,
|
|
66
|
+
system
|
|
67
|
+
} = yield* prepareMessages(providerOptions);
|
|
68
|
+
const {
|
|
69
|
+
additionalTools,
|
|
70
|
+
betas,
|
|
120
71
|
toolConfig
|
|
121
|
-
});
|
|
72
|
+
} = yield* prepareTools(providerOptions, config);
|
|
73
|
+
const responseFormat = providerOptions.responseFormat;
|
|
74
|
+
const request = {
|
|
75
|
+
...config,
|
|
76
|
+
system,
|
|
77
|
+
messages,
|
|
78
|
+
// Handle tool configuration
|
|
79
|
+
...(responseFormat.type === "json" ? {
|
|
80
|
+
toolConfig: {
|
|
81
|
+
tools: [{
|
|
82
|
+
toolSpec: {
|
|
83
|
+
name: responseFormat.objectName,
|
|
84
|
+
description: Tool.getDescriptionFromSchemaAst(responseFormat.schema.ast) ?? "Respond with a JSON object",
|
|
85
|
+
inputSchema: {
|
|
86
|
+
json: Tool.getJsonSchemaFromSchemaAst(responseFormat.schema.ast)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}],
|
|
90
|
+
toolChoice: {
|
|
91
|
+
tool: {
|
|
92
|
+
name: responseFormat.objectName
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} : Predicate.isNotUndefined(toolConfig.tools) && toolConfig.tools.length > 0 ? {
|
|
97
|
+
toolConfig
|
|
98
|
+
} : {}),
|
|
99
|
+
// Handle additional model request fields
|
|
100
|
+
...(Predicate.isNotUndefined(additionalTools) ? {
|
|
101
|
+
additionalModelRequestFields: {
|
|
102
|
+
...config.additionalModelRequestFields,
|
|
103
|
+
...additionalTools
|
|
104
|
+
}
|
|
105
|
+
} : {})
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
betas,
|
|
109
|
+
request
|
|
110
|
+
};
|
|
122
111
|
});
|
|
123
|
-
return yield*
|
|
124
|
-
generateText: Effect.fnUntraced(function* (
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
112
|
+
return yield* LanguageModel.make({
|
|
113
|
+
generateText: Effect.fnUntraced(function* (options) {
|
|
114
|
+
const {
|
|
115
|
+
betas,
|
|
116
|
+
request
|
|
117
|
+
} = yield* makeRequest(options);
|
|
118
|
+
annotateRequest(options.span, request);
|
|
119
|
+
const anthropicBeta = betas.size > 0 ? Array.from(betas).join(",") : undefined;
|
|
120
|
+
const rawResponse = yield* client.converse({
|
|
121
|
+
params: {
|
|
122
|
+
"anthropic-beta": anthropicBeta
|
|
123
|
+
},
|
|
124
|
+
payload: request
|
|
134
125
|
});
|
|
135
|
-
|
|
136
|
-
|
|
126
|
+
annotateResponse(options.span, request, rawResponse);
|
|
127
|
+
return yield* makeResponse(request, rawResponse, options);
|
|
128
|
+
}),
|
|
129
|
+
streamText: Effect.fnUntraced(function* (options) {
|
|
130
|
+
const {
|
|
131
|
+
betas,
|
|
132
|
+
request
|
|
133
|
+
} = yield* makeRequest(options);
|
|
134
|
+
annotateRequest(options.span, request);
|
|
135
|
+
const anthropicBeta = betas.size > 0 ? Array.from(betas).join(",") : undefined;
|
|
136
|
+
const stream = client.converseStream({
|
|
137
|
+
params: {
|
|
138
|
+
"anthropic-beta": anthropicBeta
|
|
139
|
+
},
|
|
140
|
+
payload: request
|
|
141
|
+
});
|
|
142
|
+
return {
|
|
143
|
+
request,
|
|
144
|
+
stream
|
|
145
|
+
};
|
|
146
|
+
}, (effect, options) => effect.pipe(Effect.flatMap(({
|
|
147
|
+
request,
|
|
148
|
+
stream
|
|
149
|
+
}) => makeStreamResponse(request, stream, options)), Stream.unwrap, Stream.map(response => {
|
|
150
|
+
annotateStreamResponse(options.span, response);
|
|
137
151
|
return response;
|
|
138
|
-
}
|
|
139
|
-
module: "AmazonBedrockLanguageModel",
|
|
140
|
-
method: "generateText",
|
|
141
|
-
description: "An error occurred",
|
|
142
|
-
cause
|
|
143
|
-
}))),
|
|
144
|
-
streamText(_options) {
|
|
145
|
-
return makeRequest("streamText", _options).pipe(Effect.tap(request => annotateRequest(_options.span, request)), Effect.map(client.stream), Stream.unwrap, Stream.map(response => {
|
|
146
|
-
annotateStreamResponse(_options.span, response);
|
|
147
|
-
return response;
|
|
148
|
-
}), Stream.catchAll(cause => AiError.is(cause) ? Effect.fail(cause) : Effect.fail(new AiError({
|
|
149
|
-
module: "AmazonBedrockLanguageModel",
|
|
150
|
-
method: "streamText",
|
|
151
|
-
description: "An error occurred",
|
|
152
|
-
cause
|
|
153
|
-
}))));
|
|
154
|
-
}
|
|
152
|
+
})))
|
|
155
153
|
});
|
|
156
154
|
});
|
|
157
155
|
/**
|
|
158
156
|
* @since 1.0.0
|
|
159
157
|
* @category Layers
|
|
160
158
|
*/
|
|
161
|
-
export const layer = options => Layer.effect(
|
|
159
|
+
export const layer = options => Layer.effect(LanguageModel.LanguageModel, make({
|
|
162
160
|
model: options.model,
|
|
163
161
|
config: options.config
|
|
164
162
|
}));
|
|
@@ -170,88 +168,163 @@ export const withConfigOverride = /*#__PURE__*/dual(2, (self, overrides) => Effe
|
|
|
170
168
|
...config,
|
|
171
169
|
...overrides
|
|
172
170
|
})));
|
|
173
|
-
|
|
171
|
+
// =============================================================================
|
|
172
|
+
// Prompt Conversion
|
|
173
|
+
// =============================================================================
|
|
174
|
+
const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* (options) {
|
|
175
|
+
const groups = groupMessages(options.prompt);
|
|
176
|
+
const system = [];
|
|
174
177
|
const messages = [];
|
|
175
|
-
let
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
178
|
+
let documentCounter = 0;
|
|
179
|
+
const nextDocumentName = () => `document-${++documentCounter}`;
|
|
180
|
+
for (let i = 0; i < groups.length; i++) {
|
|
181
|
+
const group = groups[i];
|
|
182
|
+
const isLastGroup = i === groups.length - 1;
|
|
183
|
+
switch (group.type) {
|
|
184
|
+
case "system":
|
|
179
185
|
{
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
+
if (messages.length > 0) {
|
|
187
|
+
return yield* new AiError.MalformedInput({
|
|
188
|
+
module: "AmazonBedrockLanguageModel",
|
|
189
|
+
method: "prepareMessages",
|
|
190
|
+
description: "Multiple system messages separated by user / assistant messages"
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
for (const message of group.messages) {
|
|
194
|
+
system.push({
|
|
195
|
+
text: message.content
|
|
196
|
+
});
|
|
197
|
+
if (Predicate.isNotUndefined(getCachePoint(message))) {
|
|
198
|
+
system.push(BEDROCK_CACHE_POINT);
|
|
199
|
+
}
|
|
186
200
|
}
|
|
187
|
-
current.messages.push(message);
|
|
188
201
|
break;
|
|
189
202
|
}
|
|
190
|
-
case "
|
|
191
|
-
case "UserMessage":
|
|
203
|
+
case "user":
|
|
192
204
|
{
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
205
|
+
const content = [];
|
|
206
|
+
for (const message of group.messages) {
|
|
207
|
+
switch (message.role) {
|
|
208
|
+
case "user":
|
|
209
|
+
{
|
|
210
|
+
for (let j = 0; j < message.content.length; j++) {
|
|
211
|
+
const part = message.content[j];
|
|
212
|
+
switch (part.type) {
|
|
213
|
+
case "text":
|
|
214
|
+
{
|
|
215
|
+
content.push({
|
|
216
|
+
text: part.text
|
|
217
|
+
});
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case "file":
|
|
221
|
+
{
|
|
222
|
+
if (part.data instanceof URL) {
|
|
223
|
+
// TODO(Max): support this
|
|
224
|
+
return yield* new AiError.MalformedInput({
|
|
225
|
+
module: "AmazonBedrockLanguageModel",
|
|
226
|
+
method: "makeMessages",
|
|
227
|
+
description: "File URL inputs are not supported at this time"
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (part.mediaType.startsWith("image/")) {
|
|
231
|
+
content.push({
|
|
232
|
+
image: {
|
|
233
|
+
format: yield* getImageFormat(part.mediaType),
|
|
234
|
+
source: {
|
|
235
|
+
bytes: convertToBase64(part.data)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
} else {
|
|
240
|
+
content.push({
|
|
241
|
+
document: {
|
|
242
|
+
format: yield* getDocumentFormat(part.mediaType),
|
|
243
|
+
name: nextDocumentName(),
|
|
244
|
+
source: {
|
|
245
|
+
bytes: convertToBase64(part.data)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
case "tool":
|
|
257
|
+
{
|
|
258
|
+
for (const part of message.content) {
|
|
259
|
+
content.push({
|
|
260
|
+
toolResult: {
|
|
261
|
+
toolUseId: part.id,
|
|
262
|
+
content: [{
|
|
263
|
+
text: JSON.stringify(part.result)
|
|
264
|
+
}]
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
199
271
|
}
|
|
200
|
-
|
|
272
|
+
messages.push({
|
|
273
|
+
role: "user",
|
|
274
|
+
content
|
|
275
|
+
});
|
|
201
276
|
break;
|
|
202
277
|
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return messages;
|
|
206
|
-
};
|
|
207
|
-
let fileCounter = 0;
|
|
208
|
-
const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
|
|
209
|
-
const messages = [];
|
|
210
|
-
const groups = groupMessages(prompt);
|
|
211
|
-
for (let i = 0; i < groups.length; i++) {
|
|
212
|
-
const group = groups[i];
|
|
213
|
-
const isLastGroup = i === groups.length - 1;
|
|
214
|
-
switch (group.type) {
|
|
215
278
|
case "assistant":
|
|
216
279
|
{
|
|
217
280
|
const content = [];
|
|
218
281
|
for (let j = 0; j < group.messages.length; j++) {
|
|
219
282
|
const message = group.messages[j];
|
|
220
283
|
const isLastMessage = j === group.messages.length - 1;
|
|
221
|
-
for (let k = 0; k < message.
|
|
222
|
-
const part = message.
|
|
223
|
-
const isLastPart = k === message.
|
|
224
|
-
switch (part.
|
|
225
|
-
case "
|
|
284
|
+
for (let k = 0; k < message.content.length; k++) {
|
|
285
|
+
const part = message.content[k];
|
|
286
|
+
const isLastPart = k === message.content.length - 1;
|
|
287
|
+
switch (part.type) {
|
|
288
|
+
case "text":
|
|
226
289
|
{
|
|
290
|
+
// Skip empty text blocks
|
|
291
|
+
if (part.text.trim().length === 0) {
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
227
294
|
content.push({
|
|
295
|
+
// Amazon Bedrock does not allow trailing whitespace in
|
|
296
|
+
// assistant content blocks
|
|
228
297
|
text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.text)
|
|
229
298
|
});
|
|
230
299
|
break;
|
|
231
300
|
}
|
|
232
|
-
case "
|
|
301
|
+
case "reasoning":
|
|
233
302
|
{
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
303
|
+
const options = part.options.bedrock;
|
|
304
|
+
if (Predicate.isNotUndefined(options)) {
|
|
305
|
+
if (options.type === "thinking") {
|
|
306
|
+
content.push({
|
|
307
|
+
reasoningContent: {
|
|
308
|
+
reasoningText: {
|
|
309
|
+
// Amazon Bedrock does not allow trailing whitespace in
|
|
310
|
+
// assistant content blocks
|
|
311
|
+
text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.text),
|
|
312
|
+
signature: options.signature
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
});
|
|
240
316
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
content.push({
|
|
248
|
-
reasoningContent: {
|
|
249
|
-
redactedContent
|
|
317
|
+
if (options.type === "redacted_thinking") {
|
|
318
|
+
content.push({
|
|
319
|
+
reasoningContent: {
|
|
320
|
+
redactedContent: options.redactedData
|
|
321
|
+
}
|
|
322
|
+
});
|
|
250
323
|
}
|
|
251
|
-
}
|
|
324
|
+
}
|
|
252
325
|
break;
|
|
253
326
|
}
|
|
254
|
-
case "
|
|
327
|
+
case "tool-call":
|
|
255
328
|
{
|
|
256
329
|
content.push({
|
|
257
330
|
toolUse: {
|
|
@@ -264,6 +337,9 @@ const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
|
|
|
264
337
|
}
|
|
265
338
|
}
|
|
266
339
|
}
|
|
340
|
+
if (getCachePoint(message)) {
|
|
341
|
+
content.push(BEDROCK_CACHE_POINT);
|
|
342
|
+
}
|
|
267
343
|
}
|
|
268
344
|
messages.push({
|
|
269
345
|
role: "assistant",
|
|
@@ -271,187 +347,526 @@ const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
|
|
|
271
347
|
});
|
|
272
348
|
break;
|
|
273
349
|
}
|
|
274
|
-
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return {
|
|
353
|
+
system,
|
|
354
|
+
messages
|
|
355
|
+
};
|
|
356
|
+
});
|
|
357
|
+
// =============================================================================
|
|
358
|
+
// Response Conversion
|
|
359
|
+
// =============================================================================
|
|
360
|
+
const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* (request, response, options) {
|
|
361
|
+
const parts = [];
|
|
362
|
+
parts.push({
|
|
363
|
+
type: "response-metadata",
|
|
364
|
+
modelId: request.modelId,
|
|
365
|
+
timestamp: DateTime.formatIso(yield* DateTime.now)
|
|
366
|
+
});
|
|
367
|
+
for (const part of response.output.message.content) {
|
|
368
|
+
switch (part.type) {
|
|
369
|
+
case "text":
|
|
275
370
|
{
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
371
|
+
// The text parts should only be added to the response here if the
|
|
372
|
+
// response format is `"text"`. If the response format is `"json"`,
|
|
373
|
+
// then the text parts must instead be added to the response when a
|
|
374
|
+
// tool call is received.
|
|
375
|
+
if (options.responseFormat.type === "text") {
|
|
376
|
+
parts.push({
|
|
377
|
+
type: "text",
|
|
378
|
+
text: part.text
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case "reasoningContent":
|
|
384
|
+
{
|
|
385
|
+
if (part.reasoningContent.type === "reasoning") {
|
|
386
|
+
const signature = part.reasoningContent.reasoningText.signature;
|
|
387
|
+
parts.push({
|
|
388
|
+
type: "reasoning",
|
|
389
|
+
text: part.reasoningContent.reasoningText.text,
|
|
390
|
+
metadata: Predicate.isNotUndefined(signature) ? {
|
|
391
|
+
bedrock: {
|
|
392
|
+
type: "thinking",
|
|
393
|
+
signature
|
|
394
|
+
}
|
|
395
|
+
} : undefined
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
if (part.reasoningContent.type === "redacted-reasoning") {
|
|
399
|
+
parts.push({
|
|
400
|
+
type: "reasoning",
|
|
401
|
+
text: "",
|
|
402
|
+
metadata: {
|
|
403
|
+
bedrock: {
|
|
404
|
+
type: "redacted_thinking",
|
|
405
|
+
redactedData: part.reasoningContent.redactedContent
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
case "toolUse":
|
|
413
|
+
{
|
|
414
|
+
// When a `"json"` response format is requested, the JSON that we need
|
|
415
|
+
// will be returned by the tool call injected into the request
|
|
416
|
+
if (options.responseFormat.type === "json") {
|
|
417
|
+
parts.push({
|
|
418
|
+
type: "text",
|
|
419
|
+
text: JSON.stringify(part.toolUse.input)
|
|
420
|
+
});
|
|
421
|
+
} else {
|
|
422
|
+
const providerTool = AnthropicTool.getProviderDefinedToolName(part.toolUse.name);
|
|
423
|
+
const name = Predicate.isNotUndefined(providerTool) ? providerTool : part.toolUse.name;
|
|
424
|
+
const providerName = Predicate.isNotUndefined(providerTool) ? part.toolUse.name : undefined;
|
|
425
|
+
parts.push({
|
|
426
|
+
type: "tool-call",
|
|
427
|
+
id: part.toolUse.toolUseId,
|
|
428
|
+
name,
|
|
429
|
+
params: part.toolUse.input,
|
|
430
|
+
providerName,
|
|
431
|
+
providerExecuted: false
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const finishReason = InternalUtilities.resolveFinishReason(response.stopReason);
|
|
439
|
+
parts.push({
|
|
440
|
+
type: "finish",
|
|
441
|
+
reason: finishReason,
|
|
442
|
+
usage: {
|
|
443
|
+
inputTokens: response.usage.inputTokens,
|
|
444
|
+
outputTokens: response.usage.outputTokens,
|
|
445
|
+
totalTokens: response.usage.totalTokens,
|
|
446
|
+
cachedInputTokens: response.usage.cacheReadInputTokens
|
|
447
|
+
},
|
|
448
|
+
metadata: {
|
|
449
|
+
bedrock: {
|
|
450
|
+
trace: response.trace,
|
|
451
|
+
usage: {
|
|
452
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
return parts;
|
|
458
|
+
});
|
|
459
|
+
const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* (request, stream, options) {
|
|
460
|
+
const contentBlocks = {};
|
|
461
|
+
let trace = undefined;
|
|
462
|
+
let cacheWriteInputTokens = undefined;
|
|
463
|
+
const usage = {
|
|
464
|
+
inputTokens: undefined,
|
|
465
|
+
outputTokens: undefined,
|
|
466
|
+
totalTokens: undefined
|
|
467
|
+
};
|
|
468
|
+
return stream.pipe(Stream.mapEffect(Effect.fnUntraced(function* (event) {
|
|
469
|
+
const parts = [];
|
|
470
|
+
switch (event.type) {
|
|
471
|
+
case "messageStart":
|
|
472
|
+
{
|
|
473
|
+
parts.push({
|
|
474
|
+
type: "response-metadata",
|
|
475
|
+
modelId: request.modelId,
|
|
476
|
+
timestamp: DateTime.formatIso(yield* DateTime.now)
|
|
477
|
+
});
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
case "messageStop":
|
|
481
|
+
{
|
|
482
|
+
const reason = InternalUtilities.resolveFinishReason(event.messageStop.stopReason);
|
|
483
|
+
parts.push({
|
|
484
|
+
type: "finish",
|
|
485
|
+
reason,
|
|
486
|
+
usage,
|
|
487
|
+
metadata: {
|
|
488
|
+
bedrock: {
|
|
489
|
+
trace,
|
|
490
|
+
usage: {
|
|
491
|
+
cacheWriteInputTokens
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
case "contentBlockStart":
|
|
499
|
+
{
|
|
500
|
+
const index = event.contentBlockStart.contentBlockIndex;
|
|
501
|
+
const block = event.contentBlockStart;
|
|
502
|
+
if (Predicate.isNotUndefined(block.start.toolUse)) {
|
|
503
|
+
const toolUse = block.start.toolUse;
|
|
504
|
+
const toolName = toolUse.name;
|
|
505
|
+
const providerTool = AnthropicTool.getProviderDefinedToolName(toolName);
|
|
506
|
+
const name = Predicate.isNotUndefined(providerTool) ? providerTool : toolName;
|
|
507
|
+
const providerName = Predicate.isNotUndefined(providerTool) ? toolName : undefined;
|
|
508
|
+
contentBlocks[index] = {
|
|
509
|
+
type: "tool-call",
|
|
510
|
+
id: toolUse.toolUseId,
|
|
511
|
+
name,
|
|
512
|
+
params: "",
|
|
513
|
+
providerName,
|
|
514
|
+
providerExecuted: false
|
|
515
|
+
};
|
|
516
|
+
// Only emit tool param delta events for text responses
|
|
517
|
+
if (options.responseFormat.type === "text") {
|
|
518
|
+
parts.push({
|
|
519
|
+
type: "tool-params-start",
|
|
520
|
+
id: toolUse.toolUseId,
|
|
521
|
+
name: toolUse.name,
|
|
522
|
+
providerExecuted: false
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
contentBlocks[index] = {
|
|
527
|
+
type: "text"
|
|
528
|
+
};
|
|
529
|
+
parts.push({
|
|
530
|
+
type: "text-start",
|
|
531
|
+
id: index.toString()
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
case "contentBlockDelta":
|
|
537
|
+
{
|
|
538
|
+
const index = event.contentBlockDelta.contentBlockIndex;
|
|
539
|
+
const delta = event.contentBlockDelta.delta;
|
|
540
|
+
switch (delta.type) {
|
|
541
|
+
case "text":
|
|
542
|
+
{
|
|
543
|
+
const block = contentBlocks[index];
|
|
544
|
+
if (Predicate.isUndefined(block)) {
|
|
545
|
+
contentBlocks[index] = {
|
|
546
|
+
type: "text"
|
|
547
|
+
};
|
|
548
|
+
// Only emit text delta events for text responses
|
|
549
|
+
if (options.responseFormat.type === "text") {
|
|
550
|
+
parts.push({
|
|
551
|
+
type: "text-start",
|
|
552
|
+
id: index.toString()
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// Only emit text delta events for text responses
|
|
557
|
+
if (options.responseFormat.type === "text") {
|
|
558
|
+
parts.push({
|
|
559
|
+
type: "text-delta",
|
|
560
|
+
id: index.toString(),
|
|
561
|
+
delta: delta.text
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
case "reasoningContent":
|
|
567
|
+
{
|
|
568
|
+
if ("text" in delta.reasoningContent) {
|
|
569
|
+
const block = contentBlocks[index];
|
|
570
|
+
if (Predicate.isUndefined(block)) {
|
|
571
|
+
contentBlocks[index] = {
|
|
572
|
+
type: "reasoning"
|
|
573
|
+
};
|
|
574
|
+
parts.push({
|
|
575
|
+
type: "reasoning-start",
|
|
576
|
+
id: index.toString()
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
parts.push({
|
|
580
|
+
type: "reasoning-delta",
|
|
581
|
+
id: index.toString(),
|
|
582
|
+
delta: delta.reasoningContent.text
|
|
583
|
+
});
|
|
584
|
+
} else if ("signature" in delta.reasoningContent) {
|
|
585
|
+
parts.push({
|
|
586
|
+
type: "reasoning-delta",
|
|
587
|
+
id: index.toString(),
|
|
588
|
+
delta: "",
|
|
589
|
+
metadata: {
|
|
590
|
+
bedrock: {
|
|
591
|
+
type: "thinking",
|
|
592
|
+
signature: delta.reasoningContent.signature
|
|
291
593
|
}
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
} else {
|
|
597
|
+
parts.push({
|
|
598
|
+
type: "reasoning-delta",
|
|
599
|
+
id: index.toString(),
|
|
600
|
+
delta: "",
|
|
601
|
+
metadata: {
|
|
602
|
+
bedrock: {
|
|
603
|
+
type: "redacted_thinking",
|
|
604
|
+
redactedData: delta.reasoningContent.redactedContent
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
case "toolUse":
|
|
612
|
+
{
|
|
613
|
+
const block = contentBlocks[index];
|
|
614
|
+
if (Predicate.isNotUndefined(block) && block.type === "tool-call") {
|
|
615
|
+
const params = delta.toolUse.input;
|
|
616
|
+
// Only emit tool params delta events for text responses
|
|
617
|
+
if (options.responseFormat.type === "text") {
|
|
618
|
+
parts.push({
|
|
619
|
+
type: "tool-params-delta",
|
|
620
|
+
id: block.id,
|
|
621
|
+
delta: params
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
block.params += params;
|
|
625
|
+
}
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case "contentBlockStop":
|
|
632
|
+
{
|
|
633
|
+
const index = event.contentBlockStop.contentBlockIndex;
|
|
634
|
+
const block = contentBlocks[index];
|
|
635
|
+
if (Predicate.isNotUndefined(block)) {
|
|
636
|
+
switch (block.type) {
|
|
637
|
+
case "text":
|
|
638
|
+
{
|
|
639
|
+
// Only emit text end events for text responses
|
|
640
|
+
if (options.responseFormat.type === "text") {
|
|
641
|
+
parts.push({
|
|
642
|
+
type: "text-end",
|
|
643
|
+
id: index.toString()
|
|
292
644
|
});
|
|
293
645
|
}
|
|
294
646
|
break;
|
|
295
647
|
}
|
|
296
|
-
case "
|
|
648
|
+
case "reasoning":
|
|
297
649
|
{
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
});
|
|
347
|
-
break;
|
|
348
|
-
}
|
|
349
|
-
case "ImageUrlPart":
|
|
350
|
-
{
|
|
351
|
-
// TODO: maybe auto-download images from URL
|
|
352
|
-
return yield* new AiError({
|
|
353
|
-
module: "AmazonBedrockLanguageModel",
|
|
354
|
-
method,
|
|
355
|
-
description: "Image URLs are not supported at this time"
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
case "TextPart":
|
|
359
|
-
{
|
|
360
|
-
content.push({
|
|
361
|
-
text: part.text
|
|
362
|
-
});
|
|
363
|
-
break;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
650
|
+
parts.push({
|
|
651
|
+
type: "reasoning-end",
|
|
652
|
+
id: index.toString()
|
|
653
|
+
});
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
case "tool-call":
|
|
657
|
+
{
|
|
658
|
+
// Only emit tool param events for text responses - for JSON
|
|
659
|
+
// responses, the structured output will be emitted as text
|
|
660
|
+
if (options.responseFormat.type === "text") {
|
|
661
|
+
parts.push({
|
|
662
|
+
type: "tool-params-end",
|
|
663
|
+
id: block.id
|
|
664
|
+
});
|
|
665
|
+
const toolName = block.name;
|
|
666
|
+
const toolParams = block.params;
|
|
667
|
+
const params = yield* Effect.try({
|
|
668
|
+
try: () => Tool.unsafeSecureJsonParse(toolParams),
|
|
669
|
+
catch: cause => new AiError.MalformedOutput({
|
|
670
|
+
module: "AmazonBedrockLanguageModel",
|
|
671
|
+
method: "makeStreamResponse",
|
|
672
|
+
description: "Failed to securely parse tool call parameters " + `for tool '${toolName}':\nParameters: ${toolParams}`,
|
|
673
|
+
cause
|
|
674
|
+
})
|
|
675
|
+
});
|
|
676
|
+
parts.push({
|
|
677
|
+
type: "tool-call",
|
|
678
|
+
id: block.id,
|
|
679
|
+
name: toolName,
|
|
680
|
+
params,
|
|
681
|
+
providerName: block.providerName,
|
|
682
|
+
providerExecuted: block.providerExecuted
|
|
683
|
+
});
|
|
684
|
+
} else {
|
|
685
|
+
parts.push({
|
|
686
|
+
type: "text-start",
|
|
687
|
+
id: index.toString()
|
|
688
|
+
});
|
|
689
|
+
parts.push({
|
|
690
|
+
type: "text-delta",
|
|
691
|
+
id: index.toString(),
|
|
692
|
+
delta: block.params
|
|
693
|
+
});
|
|
694
|
+
parts.push({
|
|
695
|
+
type: "text-end",
|
|
696
|
+
id: index.toString()
|
|
697
|
+
});
|
|
366
698
|
}
|
|
367
699
|
break;
|
|
368
700
|
}
|
|
369
701
|
}
|
|
702
|
+
delete contentBlocks[index];
|
|
370
703
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
case "metadata":
|
|
707
|
+
{
|
|
708
|
+
usage.inputTokens = event.metadata.usage.inputTokens;
|
|
709
|
+
usage.outputTokens = event.metadata.usage.outputTokens;
|
|
710
|
+
usage.totalTokens = event.metadata.usage.totalTokens;
|
|
711
|
+
usage.cachedInputTokens = event.metadata.usage.cacheReadInputTokens;
|
|
712
|
+
if (Predicate.isNotUndefined(event.metadata.usage.cacheWriteInputTokens)) {
|
|
713
|
+
cacheWriteInputTokens = event.metadata.usage.cacheWriteInputTokens;
|
|
714
|
+
}
|
|
715
|
+
if (Predicate.isNotUndefined(event.metadata.trace)) {
|
|
716
|
+
trace = event.metadata.trace;
|
|
717
|
+
}
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
720
|
+
case "internalServerException":
|
|
721
|
+
{
|
|
722
|
+
parts.push({
|
|
723
|
+
type: "error",
|
|
724
|
+
error: event.internalServerException
|
|
725
|
+
});
|
|
726
|
+
break;
|
|
727
|
+
}
|
|
728
|
+
case "modelStreamErrorException":
|
|
729
|
+
{
|
|
730
|
+
parts.push({
|
|
731
|
+
type: "error",
|
|
732
|
+
error: event.modelStreamErrorException
|
|
733
|
+
});
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
case "serviceUnavailableException":
|
|
737
|
+
{
|
|
738
|
+
parts.push({
|
|
739
|
+
type: "error",
|
|
740
|
+
error: event.serviceUnavailableException
|
|
741
|
+
});
|
|
742
|
+
break;
|
|
743
|
+
}
|
|
744
|
+
case "throttlingException":
|
|
745
|
+
{
|
|
746
|
+
parts.push({
|
|
747
|
+
type: "error",
|
|
748
|
+
error: event.throttlingException
|
|
749
|
+
});
|
|
750
|
+
break;
|
|
751
|
+
}
|
|
752
|
+
case "validationException":
|
|
753
|
+
{
|
|
754
|
+
parts.push({
|
|
755
|
+
type: "error",
|
|
756
|
+
error: event.validationException
|
|
374
757
|
});
|
|
375
758
|
break;
|
|
376
759
|
}
|
|
377
760
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
return messages;
|
|
381
|
-
}
|
|
382
|
-
return yield* new AiError({
|
|
383
|
-
module: "AmazonBedrockLanguageModel",
|
|
384
|
-
method,
|
|
385
|
-
description: "Prompt contained no messages"
|
|
386
|
-
});
|
|
761
|
+
return parts;
|
|
762
|
+
})), Stream.flattenIterables);
|
|
387
763
|
});
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
metadata.trace = response.trace;
|
|
764
|
+
// =============================================================================
|
|
765
|
+
// Tool Calling
|
|
766
|
+
// =============================================================================
|
|
767
|
+
const prepareTools = /*#__PURE__*/Effect.fnUntraced(function* (options, config) {
|
|
768
|
+
const betas = new Set();
|
|
769
|
+
if (options.tools.length === 0) {
|
|
770
|
+
return {
|
|
771
|
+
toolConfig: {},
|
|
772
|
+
betas
|
|
773
|
+
};
|
|
399
774
|
}
|
|
400
|
-
|
|
401
|
-
|
|
775
|
+
const isAnthropicModel = config.modelId.includes("anthropic.");
|
|
776
|
+
const userDefinedTools = [];
|
|
777
|
+
const providerDefinedTools = [];
|
|
778
|
+
for (const tool of options.tools) {
|
|
779
|
+
if (Tool.isUserDefined(tool)) {
|
|
780
|
+
userDefinedTools.push(tool);
|
|
781
|
+
} else {
|
|
782
|
+
providerDefinedTools.push(tool);
|
|
783
|
+
}
|
|
402
784
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
785
|
+
const hasAnthropicTools = isAnthropicModel && providerDefinedTools.length > 0;
|
|
786
|
+
let tools = [];
|
|
787
|
+
let additionalTools = undefined;
|
|
788
|
+
// Handle Anthropic provider-defined tools for Anthropic models on Bedrock
|
|
789
|
+
if (hasAnthropicTools) {
|
|
790
|
+
const prepared = yield* prepareAnthropicTools(options, {});
|
|
791
|
+
prepared.betas.forEach(beta => betas.add(beta));
|
|
792
|
+
if (Predicate.isNotUndefined(prepared.toolChoice)) {
|
|
793
|
+
// For Anthropic tools on Bedrock, only the 'tool_choice' goes into
|
|
794
|
+
// the `additionalModelRequestFields` parameter of the request, while
|
|
795
|
+
// the tool definitions themselves are sent in the usual `toolConfig`
|
|
796
|
+
additionalTools = {
|
|
797
|
+
tool_choice: prepared.toolChoice
|
|
798
|
+
};
|
|
415
799
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
800
|
+
// Handle conversion of provider-defined tools to Amazon Bedrock tool definitions
|
|
801
|
+
for (const providerDefinedTool of providerDefinedTools) {
|
|
802
|
+
tools.push({
|
|
803
|
+
toolSpec: {
|
|
804
|
+
name: providerDefinedTool.providerName,
|
|
805
|
+
description: Tool.getDescription(providerDefinedTool),
|
|
806
|
+
inputSchema: {
|
|
807
|
+
json: Tool.getJsonSchema(providerDefinedTool)
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
});
|
|
422
811
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
redactedText: Encoding.encodeBase64(content.redactedContent)
|
|
434
|
-
}, constDisableValidation));
|
|
812
|
+
}
|
|
813
|
+
// Handle conversion of user-defined tools to Amazon Bedrock tool definitions
|
|
814
|
+
for (const tool of userDefinedTools) {
|
|
815
|
+
tools.push({
|
|
816
|
+
toolSpec: {
|
|
817
|
+
name: tool.name,
|
|
818
|
+
description: Tool.getDescription(tool),
|
|
819
|
+
inputSchema: {
|
|
820
|
+
json: Tool.getJsonSchema(tool)
|
|
821
|
+
}
|
|
435
822
|
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
// Handle resolution of tool choice for **Amazon Bedrock** user-defined tools.
|
|
826
|
+
// The tool choice for Anthropic provider-defined tools is resolved above and
|
|
827
|
+
// inserted into the additional tool configuration object.
|
|
828
|
+
let toolChoice = undefined;
|
|
829
|
+
if (!hasAnthropicTools && tools.length > 0 && Predicate.isNotUndefined(options.toolChoice)) {
|
|
830
|
+
if (options.toolChoice === "none") {
|
|
831
|
+
tools.length = 0;
|
|
832
|
+
toolChoice = undefined;
|
|
833
|
+
} else if (options.toolChoice === "auto") {
|
|
834
|
+
toolChoice = {
|
|
835
|
+
auto: {}
|
|
836
|
+
};
|
|
837
|
+
} else if (options.toolChoice === "required") {
|
|
838
|
+
toolChoice = {
|
|
839
|
+
any: {}
|
|
840
|
+
};
|
|
841
|
+
} else if ("tool" in options.toolChoice) {
|
|
842
|
+
toolChoice = {
|
|
843
|
+
tool: {
|
|
844
|
+
name: options.toolChoice.tool
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
} else {
|
|
848
|
+
const allowedTools = new Set(options.toolChoice.oneOf);
|
|
849
|
+
tools = tools.filter(tool => allowedTools.has(tool.toolSpec?.name));
|
|
850
|
+
toolChoice = options.toolChoice.mode === "auto" ? {
|
|
851
|
+
auto: {}
|
|
852
|
+
} : {
|
|
853
|
+
any: {}
|
|
854
|
+
};
|
|
443
855
|
}
|
|
444
856
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
857
|
+
const toolConfig = tools.length > 0 ? {
|
|
858
|
+
tools,
|
|
859
|
+
toolChoice
|
|
860
|
+
} : {};
|
|
861
|
+
return {
|
|
862
|
+
additionalTools,
|
|
863
|
+
betas,
|
|
864
|
+
toolConfig
|
|
865
|
+
};
|
|
448
866
|
});
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
* the group.
|
|
453
|
-
*/
|
|
454
|
-
const trimIfLast = (isLastGroup, isLastMessage, isLastPart, text) => isLastGroup && isLastMessage && isLastPart ? text.trim() : text;
|
|
867
|
+
// =============================================================================
|
|
868
|
+
// Telemetry
|
|
869
|
+
// =============================================================================
|
|
455
870
|
const annotateRequest = (span, request) => {
|
|
456
871
|
addGenAIAnnotations(span, {
|
|
457
872
|
system: "anthropic",
|
|
@@ -467,10 +882,10 @@ const annotateRequest = (span, request) => {
|
|
|
467
882
|
}
|
|
468
883
|
});
|
|
469
884
|
};
|
|
470
|
-
const
|
|
885
|
+
const annotateResponse = (span, request, response) => {
|
|
471
886
|
addGenAIAnnotations(span, {
|
|
472
887
|
response: {
|
|
473
|
-
model: modelId,
|
|
888
|
+
model: request.modelId,
|
|
474
889
|
finishReasons: response.stopReason ? [response.stopReason] : undefined
|
|
475
890
|
},
|
|
476
891
|
usage: {
|
|
@@ -479,19 +894,112 @@ const annotateChatResponse = (span, modelId, response) => {
|
|
|
479
894
|
}
|
|
480
895
|
});
|
|
481
896
|
};
|
|
482
|
-
const annotateStreamResponse = (span,
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
897
|
+
const annotateStreamResponse = (span, part) => {
|
|
898
|
+
if (part.type === "response-metadata") {
|
|
899
|
+
addGenAIAnnotations(span, {
|
|
900
|
+
response: {
|
|
901
|
+
id: part.id,
|
|
902
|
+
model: part.modelId
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
if (part.type === "finish") {
|
|
907
|
+
addGenAIAnnotations(span, {
|
|
908
|
+
response: {
|
|
909
|
+
finishReasons: [part.reason]
|
|
910
|
+
},
|
|
911
|
+
usage: {
|
|
912
|
+
inputTokens: part.usage.inputTokens,
|
|
913
|
+
outputTokens: part.usage.outputTokens
|
|
914
|
+
}
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
const groupMessages = prompt => {
|
|
919
|
+
const messages = [];
|
|
920
|
+
let current = undefined;
|
|
921
|
+
for (const message of prompt.content) {
|
|
922
|
+
switch (message.role) {
|
|
923
|
+
case "system":
|
|
924
|
+
{
|
|
925
|
+
if (current?.type !== "system") {
|
|
926
|
+
current = {
|
|
927
|
+
type: "system",
|
|
928
|
+
messages: []
|
|
929
|
+
};
|
|
930
|
+
messages.push(current);
|
|
931
|
+
}
|
|
932
|
+
current.messages.push(message);
|
|
933
|
+
break;
|
|
934
|
+
}
|
|
935
|
+
case "assistant":
|
|
936
|
+
{
|
|
937
|
+
if (current?.type !== "assistant") {
|
|
938
|
+
current = {
|
|
939
|
+
type: "assistant",
|
|
940
|
+
messages: []
|
|
941
|
+
};
|
|
942
|
+
messages.push(current);
|
|
943
|
+
}
|
|
944
|
+
current.messages.push(message);
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
case "tool":
|
|
948
|
+
case "user":
|
|
949
|
+
{
|
|
950
|
+
if (current?.type !== "user") {
|
|
951
|
+
current = {
|
|
952
|
+
type: "user",
|
|
953
|
+
messages: []
|
|
954
|
+
};
|
|
955
|
+
messages.push(current);
|
|
956
|
+
}
|
|
957
|
+
current.messages.push(message);
|
|
958
|
+
break;
|
|
959
|
+
}
|
|
494
960
|
}
|
|
495
|
-
}
|
|
961
|
+
}
|
|
962
|
+
return messages;
|
|
496
963
|
};
|
|
964
|
+
/**
|
|
965
|
+
* Amazon Bedrock does not allow trailing whitespace in pre-fillled assistant
|
|
966
|
+
* responses, so we trim the final text part here if it's the last message in
|
|
967
|
+
* the group.
|
|
968
|
+
*/
|
|
969
|
+
const trimIfLast = (isLastGroup, isLastMessage, isLastPart, text) => isLastGroup && isLastMessage && isLastPart ? text.trim() : text;
|
|
970
|
+
const getCachePoint = part => part.options.bedrock?.cachePoint;
|
|
971
|
+
const convertToBase64 = data => typeof data === "string" ? data : Encoding.encodeBase64(data);
|
|
972
|
+
const DOCUMENT_MIME_TYPES = {
|
|
973
|
+
"application/pdf": "pdf",
|
|
974
|
+
"text/csv": "csv",
|
|
975
|
+
"application/msword": "doc",
|
|
976
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
977
|
+
"application/vnd.ms-excel": "xls",
|
|
978
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
979
|
+
"text/html": "html",
|
|
980
|
+
"text/plain": "txt",
|
|
981
|
+
"text/markdown": "md"
|
|
982
|
+
};
|
|
983
|
+
const getDocumentFormat = /*#__PURE__*/Effect.fnUntraced(function* (mediaType) {
|
|
984
|
+
const format = DOCUMENT_MIME_TYPES[mediaType];
|
|
985
|
+
if (Predicate.isUndefined(format)) {
|
|
986
|
+
return yield* new AiError.MalformedInput({
|
|
987
|
+
module: "AmazonBedrockLanguageModel",
|
|
988
|
+
method: "getDocumentFormat",
|
|
989
|
+
description: `Unsupported document MIME type: ${mediaType} - expected ` + `one of: ${Object.keys(DOCUMENT_MIME_TYPES)}`
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
return format;
|
|
993
|
+
});
|
|
994
|
+
const getImageFormat = /*#__PURE__*/Effect.fnUntraced(function* (mediaType) {
|
|
995
|
+
const format = ImageFormat.literals.find(format => mediaType === `image/${format}`);
|
|
996
|
+
if (Predicate.isUndefined(format)) {
|
|
997
|
+
return yield* new AiError.MalformedInput({
|
|
998
|
+
module: "AmazonBedrockLanguageModel",
|
|
999
|
+
method: "getImageFormat",
|
|
1000
|
+
description: `Unsupported image MIME type: ${mediaType} - expected ` + `one of: ${ImageFormat.literals.map(format => `image/${format}`).join(",")}`
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
return format;
|
|
1004
|
+
});
|
|
497
1005
|
//# sourceMappingURL=AmazonBedrockLanguageModel.js.map
|