@effect/ai-amazon-bedrock 0.6.1 → 0.7.0
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 +833 -337
- package/dist/cjs/AmazonBedrockLanguageModel.js.map +1 -1
- package/dist/cjs/AmazonBedrockSchema.js +96 -41
- 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 +549 -108
- 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 +830 -333
- package/dist/esm/AmazonBedrockLanguageModel.js.map +1 -1
- package/dist/esm/AmazonBedrockSchema.js +95 -39
- 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 +962 -372
- package/src/AmazonBedrockSchema.ts +136 -48
- 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
|
@@ -3,30 +3,34 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.withConfigOverride = exports.model = exports.make = exports.layer = exports.
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
var
|
|
12
|
-
var
|
|
6
|
+
exports.withConfigOverride = exports.model = exports.make = exports.layer = exports.Config = void 0;
|
|
7
|
+
var _AnthropicLanguageModel = require("@effect/ai-anthropic/AnthropicLanguageModel");
|
|
8
|
+
var AnthropicTool = _interopRequireWildcard(require("@effect/ai-anthropic/AnthropicTool"));
|
|
9
|
+
var AiError = _interopRequireWildcard(require("@effect/ai/AiError"));
|
|
10
|
+
var LanguageModel = _interopRequireWildcard(require("@effect/ai/LanguageModel"));
|
|
11
|
+
var AiModel = _interopRequireWildcard(require("@effect/ai/Model"));
|
|
12
|
+
var _Telemetry = require("@effect/ai/Telemetry");
|
|
13
|
+
var Tool = _interopRequireWildcard(require("@effect/ai/Tool"));
|
|
13
14
|
var Context = _interopRequireWildcard(require("effect/Context"));
|
|
15
|
+
var DateTime = _interopRequireWildcard(require("effect/DateTime"));
|
|
14
16
|
var Effect = _interopRequireWildcard(require("effect/Effect"));
|
|
15
17
|
var Encoding = _interopRequireWildcard(require("effect/Encoding"));
|
|
16
18
|
var _Function = require("effect/Function");
|
|
17
19
|
var Layer = _interopRequireWildcard(require("effect/Layer"));
|
|
18
|
-
var Option = _interopRequireWildcard(require("effect/Option"));
|
|
19
20
|
var Predicate = _interopRequireWildcard(require("effect/Predicate"));
|
|
20
21
|
var Stream = _interopRequireWildcard(require("effect/Stream"));
|
|
21
22
|
var _AmazonBedrockClient = require("./AmazonBedrockClient.js");
|
|
23
|
+
var _AmazonBedrockSchema = require("./AmazonBedrockSchema.js");
|
|
22
24
|
var InternalUtilities = _interopRequireWildcard(require("./internal/utilities.js"));
|
|
23
25
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
24
26
|
/**
|
|
25
27
|
* @since 1.0.0
|
|
26
28
|
*/
|
|
27
29
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
+
const BEDROCK_CACHE_POINT = {
|
|
31
|
+
cachePoint: {
|
|
32
|
+
type: "default"
|
|
33
|
+
}
|
|
30
34
|
};
|
|
31
35
|
// =============================================================================
|
|
32
36
|
// Configuration
|
|
@@ -42,23 +46,14 @@ class Config extends /*#__PURE__*/Context.Tag("@effect/ai-amazon-bedrock/AmazonB
|
|
|
42
46
|
static getOrUndefined = /*#__PURE__*/Effect.map(/*#__PURE__*/Effect.context(), context => context.unsafeMap.get(Config.key));
|
|
43
47
|
}
|
|
44
48
|
// =============================================================================
|
|
45
|
-
// Amazon Bedrock Provider Metadata
|
|
46
|
-
// =============================================================================
|
|
47
|
-
/**
|
|
48
|
-
* @since 1.0.0
|
|
49
|
-
* @category Context
|
|
50
|
-
*/
|
|
51
|
-
exports.Config = Config;
|
|
52
|
-
class ProviderMetadata extends /*#__PURE__*/Context.Tag(InternalUtilities.ProviderMetadataKey)() {}
|
|
53
|
-
// =============================================================================
|
|
54
49
|
// Amazon Bedrock Language Model
|
|
55
50
|
// =============================================================================
|
|
56
51
|
/**
|
|
57
52
|
* @since 1.0.0
|
|
58
53
|
* @category AiModels
|
|
59
54
|
*/
|
|
60
|
-
exports.
|
|
61
|
-
const model = (model, config) => AiModel.make(layer({
|
|
55
|
+
exports.Config = Config;
|
|
56
|
+
const model = (model, config) => AiModel.make("amazon-bedrock", layer({
|
|
62
57
|
model,
|
|
63
58
|
config
|
|
64
59
|
}));
|
|
@@ -69,107 +64,109 @@ const model = (model, config) => AiModel.make(layer({
|
|
|
69
64
|
exports.model = model;
|
|
70
65
|
const make = exports.make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
|
|
71
66
|
const client = yield* _AmazonBedrockClient.AmazonBedrockClient;
|
|
72
|
-
const
|
|
73
|
-
if (options.tools.length === 0) return undefined;
|
|
74
|
-
const isStructured = options.tools.length === 1 && options.tools[0].structured;
|
|
75
|
-
const tools = options.tools.map(tool => ({
|
|
76
|
-
toolSpec: {
|
|
77
|
-
name: tool.name,
|
|
78
|
-
description: tool.description,
|
|
79
|
-
inputSchema: {
|
|
80
|
-
json: tool.parameters
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}));
|
|
84
|
-
const toolConfig = {
|
|
85
|
-
tools
|
|
86
|
-
};
|
|
87
|
-
if (isStructured) {
|
|
88
|
-
toolConfig.toolChoice = {
|
|
89
|
-
tool: {
|
|
90
|
-
name: options.tools[0].name
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
return toolConfig;
|
|
94
|
-
}
|
|
95
|
-
if (options.toolChoice === "auto") {
|
|
96
|
-
toolConfig.toolChoice = {
|
|
97
|
-
auto: {}
|
|
98
|
-
};
|
|
99
|
-
} else if (options.toolChoice === "none") {
|
|
100
|
-
toolConfig.toolChoice = undefined;
|
|
101
|
-
} else if (options.toolChoice === "required") {
|
|
102
|
-
toolConfig.toolChoice = {
|
|
103
|
-
any: {}
|
|
104
|
-
};
|
|
105
|
-
} else {
|
|
106
|
-
toolConfig.toolChoice = {
|
|
107
|
-
tool: {
|
|
108
|
-
name: options.toolChoice.tool
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
return toolConfig;
|
|
113
|
-
};
|
|
114
|
-
const makeRequest = Effect.fnUntraced(function* (method, {
|
|
115
|
-
prompt,
|
|
116
|
-
system,
|
|
117
|
-
...rest
|
|
118
|
-
}) {
|
|
67
|
+
const makeRequest = Effect.fnUntraced(function* (providerOptions) {
|
|
119
68
|
const context = yield* Effect.context();
|
|
120
|
-
const
|
|
121
|
-
const toolConfig = makeToolConfig(rest);
|
|
122
|
-
return (0, _Function.identity)({
|
|
69
|
+
const config = {
|
|
123
70
|
modelId: options.model,
|
|
124
71
|
...options.config,
|
|
125
|
-
...context.unsafeMap.get(Config.key)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
text
|
|
129
|
-
}]), Option.getOrUndefined),
|
|
72
|
+
...context.unsafeMap.get(Config.key)
|
|
73
|
+
};
|
|
74
|
+
const {
|
|
130
75
|
messages,
|
|
76
|
+
system
|
|
77
|
+
} = yield* prepareMessages(providerOptions);
|
|
78
|
+
const {
|
|
79
|
+
additionalTools,
|
|
80
|
+
betas,
|
|
131
81
|
toolConfig
|
|
132
|
-
});
|
|
82
|
+
} = yield* prepareTools(providerOptions, config);
|
|
83
|
+
const responseFormat = providerOptions.responseFormat;
|
|
84
|
+
const request = {
|
|
85
|
+
...config,
|
|
86
|
+
system,
|
|
87
|
+
messages,
|
|
88
|
+
// Handle tool configuration
|
|
89
|
+
...(responseFormat.type === "json" ? {
|
|
90
|
+
toolConfig: {
|
|
91
|
+
tools: [{
|
|
92
|
+
toolSpec: {
|
|
93
|
+
name: responseFormat.objectName,
|
|
94
|
+
description: Tool.getDescriptionFromSchemaAst(responseFormat.schema.ast) ?? "Respond with a JSON object",
|
|
95
|
+
inputSchema: {
|
|
96
|
+
json: Tool.getJsonSchemaFromSchemaAst(responseFormat.schema.ast)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}],
|
|
100
|
+
toolChoice: {
|
|
101
|
+
tool: {
|
|
102
|
+
name: responseFormat.objectName
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} : Predicate.isNotUndefined(toolConfig.tools) && toolConfig.tools.length > 0 ? {
|
|
107
|
+
toolConfig
|
|
108
|
+
} : {}),
|
|
109
|
+
// Handle additional model request fields
|
|
110
|
+
...(Predicate.isNotUndefined(additionalTools) ? {
|
|
111
|
+
additionalModelRequestFields: {
|
|
112
|
+
...config.additionalModelRequestFields,
|
|
113
|
+
...additionalTools
|
|
114
|
+
}
|
|
115
|
+
} : {})
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
betas,
|
|
119
|
+
request
|
|
120
|
+
};
|
|
133
121
|
});
|
|
134
|
-
return yield*
|
|
135
|
-
generateText: Effect.fnUntraced(function* (
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
122
|
+
return yield* LanguageModel.make({
|
|
123
|
+
generateText: Effect.fnUntraced(function* (options) {
|
|
124
|
+
const {
|
|
125
|
+
betas,
|
|
126
|
+
request
|
|
127
|
+
} = yield* makeRequest(options);
|
|
128
|
+
annotateRequest(options.span, request);
|
|
129
|
+
const anthropicBeta = betas.size > 0 ? Array.from(betas).join(",") : undefined;
|
|
130
|
+
const rawResponse = yield* client.converse({
|
|
131
|
+
params: {
|
|
132
|
+
"anthropic-beta": anthropicBeta
|
|
133
|
+
},
|
|
134
|
+
payload: request
|
|
145
135
|
});
|
|
146
|
-
|
|
147
|
-
|
|
136
|
+
annotateResponse(options.span, request, rawResponse);
|
|
137
|
+
return yield* makeResponse(request, rawResponse, options);
|
|
138
|
+
}),
|
|
139
|
+
streamText: Effect.fnUntraced(function* (options) {
|
|
140
|
+
const {
|
|
141
|
+
betas,
|
|
142
|
+
request
|
|
143
|
+
} = yield* makeRequest(options);
|
|
144
|
+
annotateRequest(options.span, request);
|
|
145
|
+
const anthropicBeta = betas.size > 0 ? Array.from(betas).join(",") : undefined;
|
|
146
|
+
const stream = client.converseStream({
|
|
147
|
+
params: {
|
|
148
|
+
"anthropic-beta": anthropicBeta
|
|
149
|
+
},
|
|
150
|
+
payload: request
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
request,
|
|
154
|
+
stream
|
|
155
|
+
};
|
|
156
|
+
}, (effect, options) => effect.pipe(Effect.flatMap(({
|
|
157
|
+
request,
|
|
158
|
+
stream
|
|
159
|
+
}) => makeStreamResponse(request, stream, options)), Stream.unwrap, Stream.map(response => {
|
|
160
|
+
annotateStreamResponse(options.span, response);
|
|
148
161
|
return response;
|
|
149
|
-
}
|
|
150
|
-
module: "AmazonBedrockLanguageModel",
|
|
151
|
-
method: "generateText",
|
|
152
|
-
description: "An error occurred",
|
|
153
|
-
cause
|
|
154
|
-
}))),
|
|
155
|
-
streamText(_options) {
|
|
156
|
-
return makeRequest("streamText", _options).pipe(Effect.tap(request => annotateRequest(_options.span, request)), Effect.map(client.stream), Stream.unwrap, Stream.map(response => {
|
|
157
|
-
annotateStreamResponse(_options.span, response);
|
|
158
|
-
return response;
|
|
159
|
-
}), Stream.catchAll(cause => _AiError.AiError.is(cause) ? Effect.fail(cause) : Effect.fail(new _AiError.AiError({
|
|
160
|
-
module: "AmazonBedrockLanguageModel",
|
|
161
|
-
method: "streamText",
|
|
162
|
-
description: "An error occurred",
|
|
163
|
-
cause
|
|
164
|
-
}))));
|
|
165
|
-
}
|
|
162
|
+
})))
|
|
166
163
|
});
|
|
167
164
|
});
|
|
168
165
|
/**
|
|
169
166
|
* @since 1.0.0
|
|
170
167
|
* @category Layers
|
|
171
168
|
*/
|
|
172
|
-
const layer = options => Layer.effect(
|
|
169
|
+
const layer = options => Layer.effect(LanguageModel.LanguageModel, make({
|
|
173
170
|
model: options.model,
|
|
174
171
|
config: options.config
|
|
175
172
|
}));
|
|
@@ -182,88 +179,163 @@ const withConfigOverride = exports.withConfigOverride = /*#__PURE__*/(0, _Functi
|
|
|
182
179
|
...config,
|
|
183
180
|
...overrides
|
|
184
181
|
})));
|
|
185
|
-
|
|
182
|
+
// =============================================================================
|
|
183
|
+
// Prompt Conversion
|
|
184
|
+
// =============================================================================
|
|
185
|
+
const prepareMessages = /*#__PURE__*/Effect.fnUntraced(function* (options) {
|
|
186
|
+
const groups = groupMessages(options.prompt);
|
|
187
|
+
const system = [];
|
|
186
188
|
const messages = [];
|
|
187
|
-
let
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
189
|
+
let documentCounter = 0;
|
|
190
|
+
const nextDocumentName = () => `document-${++documentCounter}`;
|
|
191
|
+
for (let i = 0; i < groups.length; i++) {
|
|
192
|
+
const group = groups[i];
|
|
193
|
+
const isLastGroup = i === groups.length - 1;
|
|
194
|
+
switch (group.type) {
|
|
195
|
+
case "system":
|
|
191
196
|
{
|
|
192
|
-
if (
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
197
|
+
if (messages.length > 0) {
|
|
198
|
+
return yield* new AiError.MalformedInput({
|
|
199
|
+
module: "AmazonBedrockLanguageModel",
|
|
200
|
+
method: "prepareMessages",
|
|
201
|
+
description: "Multiple system messages separated by user / assistant messages"
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
for (const message of group.messages) {
|
|
205
|
+
system.push({
|
|
206
|
+
text: message.content
|
|
207
|
+
});
|
|
208
|
+
if (Predicate.isNotUndefined(getCachePoint(message))) {
|
|
209
|
+
system.push(BEDROCK_CACHE_POINT);
|
|
210
|
+
}
|
|
198
211
|
}
|
|
199
|
-
current.messages.push(message);
|
|
200
212
|
break;
|
|
201
213
|
}
|
|
202
|
-
case "
|
|
203
|
-
case "UserMessage":
|
|
214
|
+
case "user":
|
|
204
215
|
{
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
216
|
+
const content = [];
|
|
217
|
+
for (const message of group.messages) {
|
|
218
|
+
switch (message.role) {
|
|
219
|
+
case "user":
|
|
220
|
+
{
|
|
221
|
+
for (let j = 0; j < message.content.length; j++) {
|
|
222
|
+
const part = message.content[j];
|
|
223
|
+
switch (part.type) {
|
|
224
|
+
case "text":
|
|
225
|
+
{
|
|
226
|
+
content.push({
|
|
227
|
+
text: part.text
|
|
228
|
+
});
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
case "file":
|
|
232
|
+
{
|
|
233
|
+
if (part.data instanceof URL) {
|
|
234
|
+
// TODO(Max): support this
|
|
235
|
+
return yield* new AiError.MalformedInput({
|
|
236
|
+
module: "AmazonBedrockLanguageModel",
|
|
237
|
+
method: "makeMessages",
|
|
238
|
+
description: "File URL inputs are not supported at this time"
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
if (part.mediaType.startsWith("image/")) {
|
|
242
|
+
content.push({
|
|
243
|
+
image: {
|
|
244
|
+
format: yield* getImageFormat(part.mediaType),
|
|
245
|
+
source: {
|
|
246
|
+
bytes: convertToBase64(part.data)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
content.push({
|
|
252
|
+
document: {
|
|
253
|
+
format: yield* getDocumentFormat(part.mediaType),
|
|
254
|
+
name: nextDocumentName(),
|
|
255
|
+
source: {
|
|
256
|
+
bytes: convertToBase64(part.data)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case "tool":
|
|
268
|
+
{
|
|
269
|
+
for (const part of message.content) {
|
|
270
|
+
content.push({
|
|
271
|
+
toolResult: {
|
|
272
|
+
toolUseId: part.id,
|
|
273
|
+
content: [{
|
|
274
|
+
text: JSON.stringify(part.result)
|
|
275
|
+
}]
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
211
282
|
}
|
|
212
|
-
|
|
283
|
+
messages.push({
|
|
284
|
+
role: "user",
|
|
285
|
+
content
|
|
286
|
+
});
|
|
213
287
|
break;
|
|
214
288
|
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return messages;
|
|
218
|
-
};
|
|
219
|
-
let fileCounter = 0;
|
|
220
|
-
const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
|
|
221
|
-
const messages = [];
|
|
222
|
-
const groups = groupMessages(prompt);
|
|
223
|
-
for (let i = 0; i < groups.length; i++) {
|
|
224
|
-
const group = groups[i];
|
|
225
|
-
const isLastGroup = i === groups.length - 1;
|
|
226
|
-
switch (group.type) {
|
|
227
289
|
case "assistant":
|
|
228
290
|
{
|
|
229
291
|
const content = [];
|
|
230
292
|
for (let j = 0; j < group.messages.length; j++) {
|
|
231
293
|
const message = group.messages[j];
|
|
232
294
|
const isLastMessage = j === group.messages.length - 1;
|
|
233
|
-
for (let k = 0; k < message.
|
|
234
|
-
const part = message.
|
|
235
|
-
const isLastPart = k === message.
|
|
236
|
-
switch (part.
|
|
237
|
-
case "
|
|
295
|
+
for (let k = 0; k < message.content.length; k++) {
|
|
296
|
+
const part = message.content[k];
|
|
297
|
+
const isLastPart = k === message.content.length - 1;
|
|
298
|
+
switch (part.type) {
|
|
299
|
+
case "text":
|
|
238
300
|
{
|
|
301
|
+
// Skip empty text blocks
|
|
302
|
+
if (part.text.trim().length === 0) {
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
239
305
|
content.push({
|
|
306
|
+
// Amazon Bedrock does not allow trailing whitespace in
|
|
307
|
+
// assistant content blocks
|
|
240
308
|
text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.text)
|
|
241
309
|
});
|
|
242
310
|
break;
|
|
243
311
|
}
|
|
244
|
-
case "
|
|
312
|
+
case "reasoning":
|
|
245
313
|
{
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
314
|
+
const options = part.options.bedrock;
|
|
315
|
+
if (Predicate.isNotUndefined(options)) {
|
|
316
|
+
if (options.type === "thinking") {
|
|
317
|
+
content.push({
|
|
318
|
+
reasoningContent: {
|
|
319
|
+
reasoningText: {
|
|
320
|
+
// Amazon Bedrock does not allow trailing whitespace in
|
|
321
|
+
// assistant content blocks
|
|
322
|
+
text: trimIfLast(isLastGroup, isLastMessage, isLastPart, part.text),
|
|
323
|
+
signature: options.signature
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
});
|
|
252
327
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
content.push({
|
|
260
|
-
reasoningContent: {
|
|
261
|
-
redactedContent
|
|
328
|
+
if (options.type === "redacted_thinking") {
|
|
329
|
+
content.push({
|
|
330
|
+
reasoningContent: {
|
|
331
|
+
redactedContent: options.redactedData
|
|
332
|
+
}
|
|
333
|
+
});
|
|
262
334
|
}
|
|
263
|
-
}
|
|
335
|
+
}
|
|
264
336
|
break;
|
|
265
337
|
}
|
|
266
|
-
case "
|
|
338
|
+
case "tool-call":
|
|
267
339
|
{
|
|
268
340
|
content.push({
|
|
269
341
|
toolUse: {
|
|
@@ -276,6 +348,9 @@ const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
|
|
|
276
348
|
}
|
|
277
349
|
}
|
|
278
350
|
}
|
|
351
|
+
if (getCachePoint(message)) {
|
|
352
|
+
content.push(BEDROCK_CACHE_POINT);
|
|
353
|
+
}
|
|
279
354
|
}
|
|
280
355
|
messages.push({
|
|
281
356
|
role: "assistant",
|
|
@@ -283,189 +358,517 @@ const makeMessages = /*#__PURE__*/Effect.fnUntraced(function* (method, prompt) {
|
|
|
283
358
|
});
|
|
284
359
|
break;
|
|
285
360
|
}
|
|
286
|
-
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
system,
|
|
365
|
+
messages
|
|
366
|
+
};
|
|
367
|
+
});
|
|
368
|
+
// =============================================================================
|
|
369
|
+
// Response Conversion
|
|
370
|
+
// =============================================================================
|
|
371
|
+
const makeResponse = /*#__PURE__*/Effect.fnUntraced(function* (request, response, options) {
|
|
372
|
+
const parts = [];
|
|
373
|
+
parts.push({
|
|
374
|
+
type: "response-metadata",
|
|
375
|
+
modelId: request.modelId,
|
|
376
|
+
timestamp: DateTime.formatIso(yield* DateTime.now)
|
|
377
|
+
});
|
|
378
|
+
for (const part of response.output.message.content) {
|
|
379
|
+
switch (part.type) {
|
|
380
|
+
case "text":
|
|
287
381
|
{
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
382
|
+
// The text parts should only be added to the response here if the
|
|
383
|
+
// response format is `"text"`. If the response format is `"json"`,
|
|
384
|
+
// then the text parts must instead be added to the response when a
|
|
385
|
+
// tool call is received.
|
|
386
|
+
if (options.responseFormat.type === "text") {
|
|
387
|
+
parts.push({
|
|
388
|
+
type: "text",
|
|
389
|
+
text: part.text
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
case "reasoningContent":
|
|
395
|
+
{
|
|
396
|
+
if (part.reasoningContent.type === "reasoning") {
|
|
397
|
+
const signature = part.reasoningContent.reasoningText.signature;
|
|
398
|
+
parts.push({
|
|
399
|
+
type: "reasoning",
|
|
400
|
+
text: part.reasoningContent.reasoningText.text,
|
|
401
|
+
metadata: Predicate.isNotUndefined(signature) ? {
|
|
402
|
+
bedrock: {
|
|
403
|
+
type: "thinking",
|
|
404
|
+
signature
|
|
405
|
+
}
|
|
406
|
+
} : undefined
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
if (part.reasoningContent.type === "redacted-reasoning") {
|
|
410
|
+
parts.push({
|
|
411
|
+
type: "reasoning",
|
|
412
|
+
text: "",
|
|
413
|
+
metadata: {
|
|
414
|
+
bedrock: {
|
|
415
|
+
type: "redacted_thinking",
|
|
416
|
+
redactedData: part.reasoningContent.redactedContent
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case "toolUse":
|
|
424
|
+
{
|
|
425
|
+
// When a `"json"` response format is requested, the JSON that we need
|
|
426
|
+
// will be returned by the tool call injected into the request
|
|
427
|
+
if (options.responseFormat.type === "json") {
|
|
428
|
+
parts.push({
|
|
429
|
+
type: "text",
|
|
430
|
+
text: JSON.stringify(part.toolUse.input)
|
|
431
|
+
});
|
|
432
|
+
} else {
|
|
433
|
+
const providerTool = AnthropicTool.getProviderDefinedToolName(part.toolUse.name);
|
|
434
|
+
const name = Predicate.isNotUndefined(providerTool) ? providerTool : part.toolUse.name;
|
|
435
|
+
const providerName = Predicate.isNotUndefined(providerTool) ? part.toolUse.name : undefined;
|
|
436
|
+
parts.push({
|
|
437
|
+
type: "tool-call",
|
|
438
|
+
id: part.toolUse.toolUseId,
|
|
439
|
+
name,
|
|
440
|
+
params: part.toolUse.input,
|
|
441
|
+
providerName,
|
|
442
|
+
providerExecuted: false
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const finishReason = InternalUtilities.resolveFinishReason(response.stopReason);
|
|
450
|
+
parts.push({
|
|
451
|
+
type: "finish",
|
|
452
|
+
reason: finishReason,
|
|
453
|
+
usage: {
|
|
454
|
+
inputTokens: response.usage.inputTokens,
|
|
455
|
+
outputTokens: response.usage.outputTokens,
|
|
456
|
+
totalTokens: response.usage.totalTokens,
|
|
457
|
+
cachedInputTokens: response.usage.cacheReadInputTokens
|
|
458
|
+
},
|
|
459
|
+
metadata: {
|
|
460
|
+
bedrock: {
|
|
461
|
+
trace: response.trace,
|
|
462
|
+
usage: {
|
|
463
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
return parts;
|
|
469
|
+
});
|
|
470
|
+
const makeStreamResponse = /*#__PURE__*/Effect.fnUntraced(function* (request, stream, options) {
|
|
471
|
+
const contentBlocks = {};
|
|
472
|
+
let trace = undefined;
|
|
473
|
+
let cacheWriteInputTokens = undefined;
|
|
474
|
+
const usage = {
|
|
475
|
+
inputTokens: undefined,
|
|
476
|
+
outputTokens: undefined,
|
|
477
|
+
totalTokens: undefined
|
|
478
|
+
};
|
|
479
|
+
return stream.pipe(Stream.mapEffect(Effect.fnUntraced(function* (event) {
|
|
480
|
+
const parts = [];
|
|
481
|
+
switch (event.type) {
|
|
482
|
+
case "messageStart":
|
|
483
|
+
{
|
|
484
|
+
parts.push({
|
|
485
|
+
type: "response-metadata",
|
|
486
|
+
modelId: request.modelId,
|
|
487
|
+
timestamp: DateTime.formatIso(yield* DateTime.now)
|
|
488
|
+
});
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
case "messageStop":
|
|
492
|
+
{
|
|
493
|
+
const reason = InternalUtilities.resolveFinishReason(event.messageStop.stopReason);
|
|
494
|
+
parts.push({
|
|
495
|
+
type: "finish",
|
|
496
|
+
reason,
|
|
497
|
+
usage,
|
|
498
|
+
metadata: {
|
|
499
|
+
bedrock: {
|
|
500
|
+
trace,
|
|
501
|
+
usage: {
|
|
502
|
+
cacheWriteInputTokens
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
case "contentBlockStart":
|
|
510
|
+
{
|
|
511
|
+
const index = event.contentBlockStart.contentBlockIndex;
|
|
512
|
+
const block = event.contentBlockStart;
|
|
513
|
+
if (Predicate.isNotUndefined(block.start.toolUse)) {
|
|
514
|
+
const toolUse = block.start.toolUse;
|
|
515
|
+
const toolName = toolUse.name;
|
|
516
|
+
const providerTool = AnthropicTool.getProviderDefinedToolName(toolName);
|
|
517
|
+
const name = Predicate.isNotUndefined(providerTool) ? providerTool : toolName;
|
|
518
|
+
const providerName = Predicate.isNotUndefined(providerTool) ? toolName : undefined;
|
|
519
|
+
contentBlocks[index] = {
|
|
520
|
+
type: "tool-call",
|
|
521
|
+
id: toolUse.toolUseId,
|
|
522
|
+
name,
|
|
523
|
+
params: "",
|
|
524
|
+
providerName,
|
|
525
|
+
providerExecuted: false
|
|
526
|
+
};
|
|
527
|
+
// Only emit tool param delta events for text responses
|
|
528
|
+
if (options.responseFormat.type === "text") {
|
|
529
|
+
parts.push({
|
|
530
|
+
type: "tool-params-start",
|
|
531
|
+
id: toolUse.toolUseId,
|
|
532
|
+
name: toolUse.name,
|
|
533
|
+
providerExecuted: false
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
} else {
|
|
537
|
+
contentBlocks[index] = {
|
|
538
|
+
type: "text"
|
|
539
|
+
};
|
|
540
|
+
parts.push({
|
|
541
|
+
type: "text-start",
|
|
542
|
+
id: index.toString()
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
case "contentBlockDelta":
|
|
548
|
+
{
|
|
549
|
+
const index = event.contentBlockDelta.contentBlockIndex;
|
|
550
|
+
const delta = event.contentBlockDelta.delta;
|
|
551
|
+
switch (delta.type) {
|
|
552
|
+
case "text":
|
|
553
|
+
{
|
|
554
|
+
const block = contentBlocks[index];
|
|
555
|
+
if (Predicate.isUndefined(block)) {
|
|
556
|
+
contentBlocks[index] = {
|
|
557
|
+
type: "text"
|
|
558
|
+
};
|
|
559
|
+
// Only emit text delta events for text responses
|
|
560
|
+
if (options.responseFormat.type === "text") {
|
|
561
|
+
parts.push({
|
|
562
|
+
type: "text-start",
|
|
563
|
+
id: index.toString()
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
// Only emit text delta events for text responses
|
|
568
|
+
if (options.responseFormat.type === "text") {
|
|
569
|
+
parts.push({
|
|
570
|
+
type: "text-delta",
|
|
571
|
+
id: index.toString(),
|
|
572
|
+
delta: delta.text
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
case "reasoningContent":
|
|
578
|
+
{
|
|
579
|
+
if ("text" in delta.reasoningContent) {
|
|
580
|
+
const block = contentBlocks[index];
|
|
581
|
+
if (Predicate.isUndefined(block)) {
|
|
582
|
+
contentBlocks[index] = {
|
|
583
|
+
type: "reasoning"
|
|
584
|
+
};
|
|
585
|
+
parts.push({
|
|
586
|
+
type: "reasoning-start",
|
|
587
|
+
id: index.toString()
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
parts.push({
|
|
591
|
+
type: "reasoning-delta",
|
|
592
|
+
id: index.toString(),
|
|
593
|
+
delta: delta.reasoningContent.text
|
|
594
|
+
});
|
|
595
|
+
} else if ("signature" in delta.reasoningContent) {
|
|
596
|
+
parts.push({
|
|
597
|
+
type: "reasoning-delta",
|
|
598
|
+
id: index.toString(),
|
|
599
|
+
delta: "",
|
|
600
|
+
metadata: {
|
|
601
|
+
bedrock: {
|
|
602
|
+
type: "thinking",
|
|
603
|
+
signature: delta.reasoningContent.signature
|
|
303
604
|
}
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
} else {
|
|
608
|
+
parts.push({
|
|
609
|
+
type: "reasoning-delta",
|
|
610
|
+
id: index.toString(),
|
|
611
|
+
delta: "",
|
|
612
|
+
metadata: {
|
|
613
|
+
bedrock: {
|
|
614
|
+
type: "redacted_thinking",
|
|
615
|
+
redactedData: delta.reasoningContent.redactedContent
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
case "toolUse":
|
|
623
|
+
{
|
|
624
|
+
const block = contentBlocks[index];
|
|
625
|
+
if (Predicate.isNotUndefined(block) && block.type === "tool-call") {
|
|
626
|
+
const params = delta.toolUse.input;
|
|
627
|
+
// Only emit tool params delta events for text responses
|
|
628
|
+
if (options.responseFormat.type === "text") {
|
|
629
|
+
parts.push({
|
|
630
|
+
type: "tool-params-delta",
|
|
631
|
+
id: block.id,
|
|
632
|
+
delta: params
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
block.params += params;
|
|
636
|
+
}
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
break;
|
|
641
|
+
}
|
|
642
|
+
case "contentBlockStop":
|
|
643
|
+
{
|
|
644
|
+
const index = event.contentBlockStop.contentBlockIndex;
|
|
645
|
+
const block = contentBlocks[index];
|
|
646
|
+
if (Predicate.isNotUndefined(block)) {
|
|
647
|
+
switch (block.type) {
|
|
648
|
+
case "text":
|
|
649
|
+
{
|
|
650
|
+
// Only emit text end events for text responses
|
|
651
|
+
if (options.responseFormat.type === "text") {
|
|
652
|
+
parts.push({
|
|
653
|
+
type: "text-end",
|
|
654
|
+
id: index.toString()
|
|
304
655
|
});
|
|
305
656
|
}
|
|
306
657
|
break;
|
|
307
658
|
}
|
|
308
|
-
case "
|
|
659
|
+
case "reasoning":
|
|
309
660
|
{
|
|
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
|
-
method,
|
|
348
|
-
description: "No media type specified for image"
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
content.push({
|
|
352
|
-
image: {
|
|
353
|
-
format: part.mediaType.split("/")?.[1],
|
|
354
|
-
source: {
|
|
355
|
-
bytes: Encoding.encodeBase64(part.data)
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
break;
|
|
360
|
-
}
|
|
361
|
-
case "ImageUrlPart":
|
|
362
|
-
{
|
|
363
|
-
// TODO: maybe auto-download images from URL
|
|
364
|
-
return yield* new _AiError.AiError({
|
|
365
|
-
module: "AmazonBedrockLanguageModel",
|
|
366
|
-
method,
|
|
367
|
-
description: "Image URLs are not supported at this time"
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
case "TextPart":
|
|
371
|
-
{
|
|
372
|
-
content.push({
|
|
373
|
-
text: part.text
|
|
374
|
-
});
|
|
375
|
-
break;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
661
|
+
parts.push({
|
|
662
|
+
type: "reasoning-end",
|
|
663
|
+
id: index.toString()
|
|
664
|
+
});
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
case "tool-call":
|
|
668
|
+
{
|
|
669
|
+
// Only emit tool param events for text responses - for JSON
|
|
670
|
+
// responses, the structured output will be emitted as text
|
|
671
|
+
if (options.responseFormat.type === "text") {
|
|
672
|
+
parts.push({
|
|
673
|
+
type: "tool-params-end",
|
|
674
|
+
id: block.id
|
|
675
|
+
});
|
|
676
|
+
parts.push({
|
|
677
|
+
type: "tool-call",
|
|
678
|
+
id: block.id,
|
|
679
|
+
name: block.name,
|
|
680
|
+
params: JSON.parse(block.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
|
+
});
|
|
378
698
|
}
|
|
379
699
|
break;
|
|
380
700
|
}
|
|
381
701
|
}
|
|
702
|
+
delete contentBlocks[index];
|
|
382
703
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
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
|
|
386
757
|
});
|
|
387
758
|
break;
|
|
388
759
|
}
|
|
389
760
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
return messages;
|
|
393
|
-
}
|
|
394
|
-
return yield* new _AiError.AiError({
|
|
395
|
-
module: "AmazonBedrockLanguageModel",
|
|
396
|
-
method,
|
|
397
|
-
description: "Prompt contained no messages"
|
|
398
|
-
});
|
|
761
|
+
return parts;
|
|
762
|
+
})), Stream.flattenIterables);
|
|
399
763
|
});
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
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
|
+
};
|
|
411
774
|
}
|
|
412
|
-
|
|
413
|
-
|
|
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
|
+
}
|
|
414
784
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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* (0, _AnthropicLanguageModel.prepareTools)(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
|
+
};
|
|
427
799
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
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
|
+
});
|
|
434
811
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
redactedText: Encoding.encodeBase64(content.redactedContent)
|
|
446
|
-
}, 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
|
+
}
|
|
447
822
|
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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
|
+
};
|
|
455
855
|
}
|
|
456
856
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
857
|
+
const toolConfig = tools.length > 0 ? {
|
|
858
|
+
tools,
|
|
859
|
+
toolChoice
|
|
860
|
+
} : {};
|
|
861
|
+
return {
|
|
862
|
+
additionalTools,
|
|
863
|
+
betas,
|
|
864
|
+
toolConfig
|
|
865
|
+
};
|
|
460
866
|
});
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
* the group.
|
|
465
|
-
*/
|
|
466
|
-
const trimIfLast = (isLastGroup, isLastMessage, isLastPart, text) => isLastGroup && isLastMessage && isLastPart ? text.trim() : text;
|
|
867
|
+
// =============================================================================
|
|
868
|
+
// Telemetry
|
|
869
|
+
// =============================================================================
|
|
467
870
|
const annotateRequest = (span, request) => {
|
|
468
|
-
(0,
|
|
871
|
+
(0, _Telemetry.addGenAIAnnotations)(span, {
|
|
469
872
|
system: "anthropic",
|
|
470
873
|
operation: {
|
|
471
874
|
name: "chat"
|
|
@@ -479,10 +882,10 @@ const annotateRequest = (span, request) => {
|
|
|
479
882
|
}
|
|
480
883
|
});
|
|
481
884
|
};
|
|
482
|
-
const
|
|
483
|
-
(0,
|
|
885
|
+
const annotateResponse = (span, request, response) => {
|
|
886
|
+
(0, _Telemetry.addGenAIAnnotations)(span, {
|
|
484
887
|
response: {
|
|
485
|
-
model: modelId,
|
|
888
|
+
model: request.modelId,
|
|
486
889
|
finishReasons: response.stopReason ? [response.stopReason] : undefined
|
|
487
890
|
},
|
|
488
891
|
usage: {
|
|
@@ -491,19 +894,112 @@ const annotateChatResponse = (span, modelId, response) => {
|
|
|
491
894
|
}
|
|
492
895
|
});
|
|
493
896
|
};
|
|
494
|
-
const annotateStreamResponse = (span,
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
897
|
+
const annotateStreamResponse = (span, part) => {
|
|
898
|
+
if (part.type === "response-metadata") {
|
|
899
|
+
(0, _Telemetry.addGenAIAnnotations)(span, {
|
|
900
|
+
response: {
|
|
901
|
+
id: part.id,
|
|
902
|
+
model: part.modelId
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
if (part.type === "finish") {
|
|
907
|
+
(0, _Telemetry.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
|
+
}
|
|
506
960
|
}
|
|
507
|
-
}
|
|
961
|
+
}
|
|
962
|
+
return messages;
|
|
508
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 = _AmazonBedrockSchema.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: ${_AmazonBedrockSchema.ImageFormat.literals.map(format => `image/${format}`).join(",")}`
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
return format;
|
|
1004
|
+
});
|
|
509
1005
|
//# sourceMappingURL=AmazonBedrockLanguageModel.js.map
|