@effect/ai-amazon-bedrock 0.1.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/AmazonBedrockClient/package.json +6 -0
- package/AmazonBedrockConfig/package.json +6 -0
- package/AmazonBedrockLanguageModel/package.json +6 -0
- package/AmazonBedrockSchema/package.json +6 -0
- package/EventStreamEncoding/package.json +6 -0
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cjs/AmazonBedrockClient.js +255 -0
- package/dist/cjs/AmazonBedrockClient.js.map +1 -0
- package/dist/cjs/AmazonBedrockConfig.js +30 -0
- package/dist/cjs/AmazonBedrockConfig.js.map +1 -0
- package/dist/cjs/AmazonBedrockLanguageModel.js +507 -0
- package/dist/cjs/AmazonBedrockLanguageModel.js.map +1 -0
- package/dist/cjs/AmazonBedrockSchema.js +745 -0
- package/dist/cjs/AmazonBedrockSchema.js.map +1 -0
- package/dist/cjs/EventStreamEncoding.js +85 -0
- package/dist/cjs/EventStreamEncoding.js.map +1 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/internal/utilities.js +25 -0
- package/dist/cjs/internal/utilities.js.map +1 -0
- package/dist/dts/AmazonBedrockClient.d.ts +95 -0
- package/dist/dts/AmazonBedrockClient.d.ts.map +1 -0
- package/dist/dts/AmazonBedrockConfig.d.ts +47 -0
- package/dist/dts/AmazonBedrockConfig.d.ts.map +1 -0
- package/dist/dts/AmazonBedrockLanguageModel.d.ts +95 -0
- package/dist/dts/AmazonBedrockLanguageModel.d.ts.map +1 -0
- package/dist/dts/AmazonBedrockSchema.d.ts +1434 -0
- package/dist/dts/AmazonBedrockSchema.d.ts.map +1 -0
- package/dist/dts/EventStreamEncoding.d.ts +16 -0
- package/dist/dts/EventStreamEncoding.d.ts.map +1 -0
- package/dist/dts/index.d.ts +21 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/dts/internal/utilities.d.ts +2 -0
- package/dist/dts/internal/utilities.d.ts.map +1 -0
- package/dist/esm/AmazonBedrockClient.js +242 -0
- package/dist/esm/AmazonBedrockClient.js.map +1 -0
- package/dist/esm/AmazonBedrockConfig.js +22 -0
- package/dist/esm/AmazonBedrockConfig.js.map +1 -0
- package/dist/esm/AmazonBedrockLanguageModel.js +495 -0
- package/dist/esm/AmazonBedrockLanguageModel.js.map +1 -0
- package/dist/esm/AmazonBedrockSchema.js +675 -0
- package/dist/esm/AmazonBedrockSchema.js.map +1 -0
- package/dist/esm/EventStreamEncoding.js +76 -0
- package/dist/esm/EventStreamEncoding.js.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/utilities.js +17 -0
- package/dist/esm/internal/utilities.js.map +1 -0
- package/dist/esm/package.json +4 -0
- package/index/package.json +6 -0
- package/package.json +91 -0
- package/src/AmazonBedrockClient.ts +409 -0
- package/src/AmazonBedrockConfig.ts +78 -0
- package/src/AmazonBedrockLanguageModel.ts +623 -0
- package/src/AmazonBedrockSchema.ts +1038 -0
- package/src/EventStreamEncoding.ts +111 -0
- package/src/index.ts +24 -0
- package/src/internal/utilities.ts +21 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import { AiError } from "@effect/ai/AiError"
|
|
5
|
+
import type * as AiInput from "@effect/ai/AiInput"
|
|
6
|
+
import * as AiLanguageModel from "@effect/ai/AiLanguageModel"
|
|
7
|
+
import * as AiModel from "@effect/ai/AiModel"
|
|
8
|
+
import * as AiResponse from "@effect/ai/AiResponse"
|
|
9
|
+
import { addGenAIAnnotations } from "@effect/ai/AiTelemetry"
|
|
10
|
+
import * as Arr from "effect/Array"
|
|
11
|
+
import * as Context from "effect/Context"
|
|
12
|
+
import * as Effect from "effect/Effect"
|
|
13
|
+
import * as Encoding from "effect/Encoding"
|
|
14
|
+
import { dual, identity } from "effect/Function"
|
|
15
|
+
import * as Layer from "effect/Layer"
|
|
16
|
+
import * as Option from "effect/Option"
|
|
17
|
+
import * as Predicate from "effect/Predicate"
|
|
18
|
+
import * as Stream from "effect/Stream"
|
|
19
|
+
import type { Span } from "effect/Tracer"
|
|
20
|
+
import type { Mutable, Simplify } from "effect/Types"
|
|
21
|
+
import { AmazonBedrockClient } from "./AmazonBedrockClient.js"
|
|
22
|
+
import type {
|
|
23
|
+
BedrockFoundationModelId,
|
|
24
|
+
ContentBlock,
|
|
25
|
+
ConverseMetrics,
|
|
26
|
+
ConverseRequest,
|
|
27
|
+
ConverseResponse,
|
|
28
|
+
ConverseTrace,
|
|
29
|
+
DocumentFormat,
|
|
30
|
+
ImageFormat,
|
|
31
|
+
Message,
|
|
32
|
+
PerformanceConfiguration,
|
|
33
|
+
ToolConfiguration
|
|
34
|
+
} from "./AmazonBedrockSchema.js"
|
|
35
|
+
import * as InternalUtilities from "./internal/utilities.js"
|
|
36
|
+
|
|
37
|
+
const constDisableValidation = { disableValidation: true }
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
* @category Models
|
|
42
|
+
*/
|
|
43
|
+
export type Model = typeof BedrockFoundationModelId.Encoded
|
|
44
|
+
|
|
45
|
+
// =============================================================================
|
|
46
|
+
// Configuration
|
|
47
|
+
// =============================================================================
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
* @category Context
|
|
52
|
+
*/
|
|
53
|
+
export class Config extends Context.Tag(
|
|
54
|
+
"@effect/ai-amazon-bedrock/AmazonBedrockLanguageModel/Config"
|
|
55
|
+
)<Config, Config.Service>() {
|
|
56
|
+
/**
|
|
57
|
+
* @since 1.0.0
|
|
58
|
+
*/
|
|
59
|
+
static readonly getOrUndefined: Effect.Effect<typeof Config.Service | undefined> = Effect.map(
|
|
60
|
+
Effect.context<never>(),
|
|
61
|
+
(context) => context.unsafeMap.get(Config.key)
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @since 1.0.0
|
|
67
|
+
*/
|
|
68
|
+
export declare namespace Config {
|
|
69
|
+
/**
|
|
70
|
+
* @since 1.0.0
|
|
71
|
+
* @category Configuration
|
|
72
|
+
*/
|
|
73
|
+
export interface Service extends
|
|
74
|
+
Simplify<
|
|
75
|
+
Partial<
|
|
76
|
+
Omit<
|
|
77
|
+
typeof ConverseRequest.Encoded,
|
|
78
|
+
"messages" | "system" | "toolConfig"
|
|
79
|
+
>
|
|
80
|
+
>
|
|
81
|
+
>
|
|
82
|
+
{}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// =============================================================================
|
|
86
|
+
// Amazon Bedrock Provider Metadata
|
|
87
|
+
// =============================================================================
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @since 1.0.0
|
|
91
|
+
* @category Context
|
|
92
|
+
*/
|
|
93
|
+
export class ProviderMetadata extends Context.Tag(InternalUtilities.ProviderMetadataKey)<
|
|
94
|
+
ProviderMetadata,
|
|
95
|
+
ProviderMetadata.Service
|
|
96
|
+
>() {}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @since 1.0.0
|
|
100
|
+
*/
|
|
101
|
+
export declare namespace ProviderMetadata {
|
|
102
|
+
/**
|
|
103
|
+
* @since 1.0.0
|
|
104
|
+
* @category Provider Metadata
|
|
105
|
+
*/
|
|
106
|
+
export interface Service {
|
|
107
|
+
readonly metrics: ConverseMetrics
|
|
108
|
+
readonly trace?: ConverseTrace
|
|
109
|
+
readonly performanceConfig?: PerformanceConfiguration
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// =============================================================================
|
|
114
|
+
// Amazon Bedrock Language Model
|
|
115
|
+
// =============================================================================
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @since 1.0.0
|
|
119
|
+
* @category AiModels
|
|
120
|
+
*/
|
|
121
|
+
export const model = (
|
|
122
|
+
model: (string & {}) | Model,
|
|
123
|
+
config?: Omit<Config.Service, "model">
|
|
124
|
+
): AiModel.AiModel<AiLanguageModel.AiLanguageModel, AmazonBedrockClient> => AiModel.make(layer({ model, config }))
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @since 1.0.0
|
|
128
|
+
* @category Constructors
|
|
129
|
+
*/
|
|
130
|
+
export const make = Effect.fnUntraced(function*(options: {
|
|
131
|
+
readonly model: (string & {}) | Model
|
|
132
|
+
readonly config?: Omit<Config.Service, "model">
|
|
133
|
+
}) {
|
|
134
|
+
const client = yield* AmazonBedrockClient
|
|
135
|
+
|
|
136
|
+
const makeToolConfig = (options: {
|
|
137
|
+
readonly toolChoice: AiLanguageModel.AiLanguageModelOptions["toolChoice"]
|
|
138
|
+
readonly tools: AiLanguageModel.AiLanguageModelOptions["tools"]
|
|
139
|
+
}): typeof ToolConfiguration.Encoded | undefined => {
|
|
140
|
+
if (options.tools.length === 0) return undefined
|
|
141
|
+
const isStructured = options.tools.length === 1 && options.tools[0].structured
|
|
142
|
+
const tools = options.tools.map((tool) => ({
|
|
143
|
+
toolSpec: {
|
|
144
|
+
name: tool.name,
|
|
145
|
+
description: tool.description,
|
|
146
|
+
inputSchema: { json: tool.parameters }
|
|
147
|
+
}
|
|
148
|
+
}))
|
|
149
|
+
const toolConfig: Mutable<typeof ToolConfiguration.Encoded> = { tools } as any
|
|
150
|
+
if (isStructured) {
|
|
151
|
+
toolConfig.toolChoice = { tool: { name: options.tools[0].name } }
|
|
152
|
+
return toolConfig
|
|
153
|
+
}
|
|
154
|
+
if (options.toolChoice === "auto") {
|
|
155
|
+
toolConfig.toolChoice = { auto: {} }
|
|
156
|
+
} else if (options.toolChoice === "none") {
|
|
157
|
+
toolConfig.toolChoice = undefined
|
|
158
|
+
} else if (options.toolChoice === "required") {
|
|
159
|
+
toolConfig.toolChoice = { any: {} }
|
|
160
|
+
} else {
|
|
161
|
+
toolConfig.toolChoice = { tool: { name: options.toolChoice.tool } }
|
|
162
|
+
}
|
|
163
|
+
return toolConfig
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const makeRequest = Effect.fnUntraced(
|
|
167
|
+
function*(method: string, { prompt, system, ...rest }: AiLanguageModel.AiLanguageModelOptions) {
|
|
168
|
+
const context = yield* Effect.context<never>()
|
|
169
|
+
const messages = yield* makeMessages(method, prompt)
|
|
170
|
+
const toolConfig = makeToolConfig(rest)
|
|
171
|
+
return identity<typeof ConverseRequest.Encoded>({
|
|
172
|
+
modelId: options.model,
|
|
173
|
+
...options.config,
|
|
174
|
+
...context.unsafeMap.get(Config.key),
|
|
175
|
+
// TODO: re-evaluate a better way to do this
|
|
176
|
+
system: Option.getOrUndefined(system),
|
|
177
|
+
messages,
|
|
178
|
+
toolConfig
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
return yield* AiLanguageModel.make({
|
|
184
|
+
generateText: Effect.fnUntraced(
|
|
185
|
+
function*(_options) {
|
|
186
|
+
const request = yield* makeRequest("generateText", _options)
|
|
187
|
+
annotateRequest(_options.span, request)
|
|
188
|
+
const rawResponse = yield* client.client.converse(request)
|
|
189
|
+
const context = yield* Effect.context<never>()
|
|
190
|
+
const model = Option.match(Context.getOption(context, Config), {
|
|
191
|
+
onNone: () => options.model,
|
|
192
|
+
onSome: ({ modelId }) => modelId ?? options.model
|
|
193
|
+
})
|
|
194
|
+
annotateChatResponse(_options.span, model, rawResponse)
|
|
195
|
+
const response = yield* makeResponse(rawResponse, model)
|
|
196
|
+
return response
|
|
197
|
+
},
|
|
198
|
+
Effect.catchAll((cause) =>
|
|
199
|
+
AiError.is(cause) ? cause : new AiError({
|
|
200
|
+
module: "AmazonBedrockLanguageModel",
|
|
201
|
+
method: "generateText",
|
|
202
|
+
description: "An error occurred",
|
|
203
|
+
cause
|
|
204
|
+
})
|
|
205
|
+
)
|
|
206
|
+
),
|
|
207
|
+
streamText(_options) {
|
|
208
|
+
return makeRequest("streamText", _options).pipe(
|
|
209
|
+
Effect.tap((request) => annotateRequest(_options.span, request)),
|
|
210
|
+
Effect.map(client.stream),
|
|
211
|
+
Stream.unwrap,
|
|
212
|
+
Stream.map((response) => {
|
|
213
|
+
annotateStreamResponse(_options.span, response)
|
|
214
|
+
return response
|
|
215
|
+
}),
|
|
216
|
+
Stream.catchAll((cause) =>
|
|
217
|
+
AiError.is(cause) ? Effect.fail(cause) : Effect.fail(
|
|
218
|
+
new AiError({
|
|
219
|
+
module: "AmazonBedrockLanguageModel",
|
|
220
|
+
method: "streamText",
|
|
221
|
+
description: "An error occurred",
|
|
222
|
+
cause
|
|
223
|
+
})
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @since 1.0.0
|
|
233
|
+
* @category Layers
|
|
234
|
+
*/
|
|
235
|
+
export const layer = (options: {
|
|
236
|
+
readonly model: (string & {}) | Model
|
|
237
|
+
readonly config?: Omit<Config.Service, "model">
|
|
238
|
+
}): Layer.Layer<AiLanguageModel.AiLanguageModel, never, AmazonBedrockClient> =>
|
|
239
|
+
Layer.effect(AiLanguageModel.AiLanguageModel, make({ model: options.model, config: options.config }))
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @since 1.0.0
|
|
243
|
+
* @category Configuration
|
|
244
|
+
*/
|
|
245
|
+
export const withConfigOverride: {
|
|
246
|
+
/**
|
|
247
|
+
* @since 1.0.0
|
|
248
|
+
* @category Configuration
|
|
249
|
+
*/
|
|
250
|
+
(config: Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
|
|
251
|
+
/**
|
|
252
|
+
* @since 1.0.0
|
|
253
|
+
* @category Configuration
|
|
254
|
+
*/
|
|
255
|
+
<A, E, R>(self: Effect.Effect<A, E, R>, config: Config.Service): Effect.Effect<A, E, R>
|
|
256
|
+
} = dual<
|
|
257
|
+
/**
|
|
258
|
+
* @since 1.0.0
|
|
259
|
+
* @category Configuration
|
|
260
|
+
*/
|
|
261
|
+
(config: Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
|
|
262
|
+
/**
|
|
263
|
+
* @since 1.0.0
|
|
264
|
+
* @category Configuration
|
|
265
|
+
*/
|
|
266
|
+
<A, E, R>(self: Effect.Effect<A, E, R>, config: Config.Service) => Effect.Effect<A, E, R>
|
|
267
|
+
>(2, (self, overrides) =>
|
|
268
|
+
Effect.flatMap(
|
|
269
|
+
Config.getOrUndefined,
|
|
270
|
+
(config) => Effect.provideService(self, Config, { ...config, ...overrides })
|
|
271
|
+
))
|
|
272
|
+
|
|
273
|
+
// =============================================================================
|
|
274
|
+
// Utilities
|
|
275
|
+
// =============================================================================
|
|
276
|
+
|
|
277
|
+
type MessageGroup = AssistantMessageGroup | UserMessageGroup
|
|
278
|
+
|
|
279
|
+
interface AssistantMessageGroup {
|
|
280
|
+
readonly type: "assistant"
|
|
281
|
+
readonly messages: Array<AiInput.AssistantMessage>
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
interface UserMessageGroup {
|
|
285
|
+
readonly type: "user"
|
|
286
|
+
readonly messages: Array<AiInput.ToolMessage | AiInput.UserMessage>
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const groupMessages = (prompt: AiInput.AiInput): Array<MessageGroup> => {
|
|
290
|
+
const messages: Array<MessageGroup> = []
|
|
291
|
+
let current: MessageGroup | undefined = undefined
|
|
292
|
+
for (const message of prompt.messages) {
|
|
293
|
+
switch (message._tag) {
|
|
294
|
+
case "AssistantMessage": {
|
|
295
|
+
if (current?.type !== "assistant") {
|
|
296
|
+
current = { type: "assistant", messages: [] }
|
|
297
|
+
messages.push(current)
|
|
298
|
+
}
|
|
299
|
+
current.messages.push(message)
|
|
300
|
+
break
|
|
301
|
+
}
|
|
302
|
+
case "ToolMessage":
|
|
303
|
+
case "UserMessage": {
|
|
304
|
+
if (current?.type !== "user") {
|
|
305
|
+
current = { type: "user", messages: [] }
|
|
306
|
+
messages.push(current)
|
|
307
|
+
}
|
|
308
|
+
current.messages.push(message)
|
|
309
|
+
break
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return messages
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
let fileCounter = 0
|
|
317
|
+
|
|
318
|
+
const makeMessages = Effect.fnUntraced(
|
|
319
|
+
function*(method: string, prompt: AiInput.AiInput) {
|
|
320
|
+
const messages: Array<typeof Message.Encoded> = []
|
|
321
|
+
const groups = groupMessages(prompt)
|
|
322
|
+
for (let i = 0; i < groups.length; i++) {
|
|
323
|
+
const group = groups[i]
|
|
324
|
+
const isLastGroup = i === groups.length - 1
|
|
325
|
+
switch (group.type) {
|
|
326
|
+
case "assistant": {
|
|
327
|
+
const content: Array<typeof ContentBlock.Encoded> = []
|
|
328
|
+
for (let j = 0; j < group.messages.length; j++) {
|
|
329
|
+
const message = group.messages[j]
|
|
330
|
+
const isLastMessage = j === group.messages.length - 1
|
|
331
|
+
for (let k = 0; k < message.parts.length; k++) {
|
|
332
|
+
const part = message.parts[k]
|
|
333
|
+
const isLastPart = k === message.parts.length - 1
|
|
334
|
+
switch (part._tag) {
|
|
335
|
+
case "TextPart": {
|
|
336
|
+
content.push({
|
|
337
|
+
text: trimIfLast(
|
|
338
|
+
isLastGroup,
|
|
339
|
+
isLastMessage,
|
|
340
|
+
isLastPart,
|
|
341
|
+
part.text
|
|
342
|
+
)
|
|
343
|
+
})
|
|
344
|
+
break
|
|
345
|
+
}
|
|
346
|
+
case "ReasoningPart": {
|
|
347
|
+
content.push({
|
|
348
|
+
reasoningContent: {
|
|
349
|
+
reasoningText: {
|
|
350
|
+
text: trimIfLast(
|
|
351
|
+
isLastGroup,
|
|
352
|
+
isLastMessage,
|
|
353
|
+
isLastPart,
|
|
354
|
+
part.reasoningText
|
|
355
|
+
),
|
|
356
|
+
signature: part.signature
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
})
|
|
360
|
+
break
|
|
361
|
+
}
|
|
362
|
+
case "RedactedReasoningPart": {
|
|
363
|
+
const redactedContent = Encoding.encodeBase64(part.redactedText)
|
|
364
|
+
content.push({ reasoningContent: { redactedContent } })
|
|
365
|
+
break
|
|
366
|
+
}
|
|
367
|
+
case "ToolCallPart": {
|
|
368
|
+
content.push({
|
|
369
|
+
toolUse: {
|
|
370
|
+
toolUseId: part.id,
|
|
371
|
+
name: part.name,
|
|
372
|
+
input: part.params
|
|
373
|
+
}
|
|
374
|
+
})
|
|
375
|
+
break
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
messages.push({ role: "assistant", content })
|
|
381
|
+
break
|
|
382
|
+
}
|
|
383
|
+
case "user": {
|
|
384
|
+
const content: Array<typeof ContentBlock.Encoded> = []
|
|
385
|
+
for (let j = 0; j < group.messages.length; j++) {
|
|
386
|
+
const message = group.messages[j]
|
|
387
|
+
switch (message._tag) {
|
|
388
|
+
case "ToolMessage": {
|
|
389
|
+
for (let k = 0; k < message.parts.length; k++) {
|
|
390
|
+
const part = message.parts[k]
|
|
391
|
+
// TODO: support advanced tool result content parts
|
|
392
|
+
content.push({
|
|
393
|
+
toolResult: {
|
|
394
|
+
toolUseId: part.id,
|
|
395
|
+
content: [{ text: JSON.stringify(part.result) }]
|
|
396
|
+
}
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
break
|
|
400
|
+
}
|
|
401
|
+
case "UserMessage": {
|
|
402
|
+
for (let k = 0; k < message.parts.length; k++) {
|
|
403
|
+
const part = message.parts[k]
|
|
404
|
+
switch (part._tag) {
|
|
405
|
+
case "FilePart": {
|
|
406
|
+
if (Predicate.isUndefined(part.mediaType)) {
|
|
407
|
+
return yield* new AiError({
|
|
408
|
+
module: "AmazonBedrockLanguageModel",
|
|
409
|
+
method,
|
|
410
|
+
description: "No media type specified for image"
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
content.push({
|
|
414
|
+
document: {
|
|
415
|
+
name: part.name ?? `file-${fileCounter++}`,
|
|
416
|
+
format: part.mediaType.split("/")?.[1] as DocumentFormat,
|
|
417
|
+
source: {
|
|
418
|
+
bytes: Encoding.encodeBase64(part.data)
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
})
|
|
422
|
+
break
|
|
423
|
+
}
|
|
424
|
+
case "FileUrlPart": {
|
|
425
|
+
// TODO: maybe auto-download images from URL
|
|
426
|
+
return yield* new AiError({
|
|
427
|
+
module: "AmazonBedrockLanguageModel",
|
|
428
|
+
method,
|
|
429
|
+
description: "File URLs are not supported at this time"
|
|
430
|
+
})
|
|
431
|
+
}
|
|
432
|
+
case "ImagePart": {
|
|
433
|
+
if (Predicate.isUndefined(part.mediaType)) {
|
|
434
|
+
return yield* new AiError({
|
|
435
|
+
module: "AmazonBedrockLanguageModel",
|
|
436
|
+
method,
|
|
437
|
+
description: "No media type specified for image"
|
|
438
|
+
})
|
|
439
|
+
}
|
|
440
|
+
content.push({
|
|
441
|
+
image: {
|
|
442
|
+
format: part.mediaType.split("/")?.[1] as ImageFormat,
|
|
443
|
+
source: {
|
|
444
|
+
bytes: Encoding.encodeBase64(part.data)
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
})
|
|
448
|
+
break
|
|
449
|
+
}
|
|
450
|
+
case "ImageUrlPart": {
|
|
451
|
+
// TODO: maybe auto-download images from URL
|
|
452
|
+
return yield* new AiError({
|
|
453
|
+
module: "AmazonBedrockLanguageModel",
|
|
454
|
+
method,
|
|
455
|
+
description: "Image URLs are not supported at this time"
|
|
456
|
+
})
|
|
457
|
+
}
|
|
458
|
+
case "TextPart": {
|
|
459
|
+
content.push({
|
|
460
|
+
text: part.text
|
|
461
|
+
})
|
|
462
|
+
break
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
break
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
messages.push({ role: "user", content })
|
|
471
|
+
break
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (Arr.isNonEmptyReadonlyArray(messages)) {
|
|
476
|
+
return messages
|
|
477
|
+
}
|
|
478
|
+
return yield* new AiError({
|
|
479
|
+
module: "AmazonBedrockLanguageModel",
|
|
480
|
+
method,
|
|
481
|
+
description: "Prompt contained no messages"
|
|
482
|
+
})
|
|
483
|
+
}
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
const makeResponse = Effect.fnUntraced(
|
|
487
|
+
function*(response: ConverseResponse, modelId: string) {
|
|
488
|
+
const parts: Array<AiResponse.Part> = []
|
|
489
|
+
parts.push(
|
|
490
|
+
new AiResponse.MetadataPart({
|
|
491
|
+
model: modelId
|
|
492
|
+
}, constDisableValidation)
|
|
493
|
+
)
|
|
494
|
+
const finishReason = InternalUtilities.resolveFinishReason(response.stopReason)
|
|
495
|
+
const metadata: Mutable<ProviderMetadata.Service> = {
|
|
496
|
+
metrics: response.metrics
|
|
497
|
+
}
|
|
498
|
+
if (Predicate.isNotUndefined(response.trace)) {
|
|
499
|
+
metadata.trace = response.trace
|
|
500
|
+
}
|
|
501
|
+
if (Predicate.isNotUndefined(response.performanceConfig)) {
|
|
502
|
+
metadata.performanceConfig = response.performanceConfig
|
|
503
|
+
}
|
|
504
|
+
parts.push(
|
|
505
|
+
new AiResponse.FinishPart({
|
|
506
|
+
reason: finishReason,
|
|
507
|
+
usage: {
|
|
508
|
+
inputTokens: response.usage.inputTokens,
|
|
509
|
+
outputTokens: response.usage.outputTokens,
|
|
510
|
+
totalTokens: response.usage.totalTokens,
|
|
511
|
+
reasoningTokens: 0,
|
|
512
|
+
cacheReadInputTokens: response.usage.cacheReadInputTokens ?? 0,
|
|
513
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens ?? 0
|
|
514
|
+
},
|
|
515
|
+
providerMetadata: { [InternalUtilities.ProviderMetadataKey]: metadata }
|
|
516
|
+
}, constDisableValidation)
|
|
517
|
+
)
|
|
518
|
+
for (const part of response.output.message.content) {
|
|
519
|
+
if ("text" in part) {
|
|
520
|
+
parts.push(
|
|
521
|
+
new AiResponse.TextPart({
|
|
522
|
+
text: part.text
|
|
523
|
+
}, constDisableValidation)
|
|
524
|
+
)
|
|
525
|
+
}
|
|
526
|
+
if ("reasoningContent" in part) {
|
|
527
|
+
const content = part.reasoningContent
|
|
528
|
+
if ("reasoningText" in content) {
|
|
529
|
+
parts.push(
|
|
530
|
+
new AiResponse.ReasoningPart({
|
|
531
|
+
reasoningText: content.reasoningText.text,
|
|
532
|
+
signature: content.reasoningText.signature
|
|
533
|
+
}, constDisableValidation)
|
|
534
|
+
)
|
|
535
|
+
}
|
|
536
|
+
if ("redactedContent" in content) {
|
|
537
|
+
parts.push(
|
|
538
|
+
new AiResponse.RedactedReasoningPart({
|
|
539
|
+
redactedText: Encoding.encodeBase64(content.redactedContent)
|
|
540
|
+
}, constDisableValidation)
|
|
541
|
+
)
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if ("toolUse" in part) {
|
|
545
|
+
parts.push(
|
|
546
|
+
AiResponse.ToolCallPart.fromUnknown({
|
|
547
|
+
id: part.toolUse.toolUseId,
|
|
548
|
+
name: part.toolUse.name,
|
|
549
|
+
params: part.toolUse.input
|
|
550
|
+
})
|
|
551
|
+
)
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return new AiResponse.AiResponse({
|
|
555
|
+
parts
|
|
556
|
+
}, constDisableValidation)
|
|
557
|
+
}
|
|
558
|
+
)
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Amazon Bedrock does not allow trailing whitespace in pre-fillled assistant
|
|
562
|
+
* responses, so we trim the final text part here if it's the last message in
|
|
563
|
+
* the group.
|
|
564
|
+
*/
|
|
565
|
+
const trimIfLast = (
|
|
566
|
+
isLastGroup: boolean,
|
|
567
|
+
isLastMessage: boolean,
|
|
568
|
+
isLastPart: boolean,
|
|
569
|
+
text: string
|
|
570
|
+
) => isLastGroup && isLastMessage && isLastPart ? text.trim() : text
|
|
571
|
+
|
|
572
|
+
const annotateRequest = (
|
|
573
|
+
span: Span,
|
|
574
|
+
request: typeof ConverseRequest.Encoded
|
|
575
|
+
): void => {
|
|
576
|
+
addGenAIAnnotations(span, {
|
|
577
|
+
system: "anthropic",
|
|
578
|
+
operation: { name: "chat" },
|
|
579
|
+
request: {
|
|
580
|
+
model: request.modelId,
|
|
581
|
+
temperature: request.inferenceConfig?.temperature,
|
|
582
|
+
topP: request.inferenceConfig?.topP,
|
|
583
|
+
maxTokens: request.inferenceConfig?.maxTokens,
|
|
584
|
+
stopSequences: request.inferenceConfig?.stopSequences ?? []
|
|
585
|
+
}
|
|
586
|
+
})
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const annotateChatResponse = (
|
|
590
|
+
span: Span,
|
|
591
|
+
modelId: string,
|
|
592
|
+
response: ConverseResponse
|
|
593
|
+
): void => {
|
|
594
|
+
addGenAIAnnotations(span, {
|
|
595
|
+
response: {
|
|
596
|
+
model: modelId,
|
|
597
|
+
finishReasons: response.stopReason ? [response.stopReason] : undefined
|
|
598
|
+
},
|
|
599
|
+
usage: {
|
|
600
|
+
inputTokens: response.usage.inputTokens,
|
|
601
|
+
outputTokens: response.usage.outputTokens
|
|
602
|
+
}
|
|
603
|
+
})
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const annotateStreamResponse = (
|
|
607
|
+
span: Span,
|
|
608
|
+
response: AiResponse.AiResponse
|
|
609
|
+
) => {
|
|
610
|
+
const metadataPart = response.parts.find((part) => part._tag === "MetadataPart")
|
|
611
|
+
const finishPart = response.parts.find((part) => part._tag === "FinishPart")
|
|
612
|
+
addGenAIAnnotations(span, {
|
|
613
|
+
response: {
|
|
614
|
+
id: metadataPart?.id,
|
|
615
|
+
model: metadataPart?.model,
|
|
616
|
+
finishReasons: finishPart?.reason ? [finishPart.reason] : undefined
|
|
617
|
+
},
|
|
618
|
+
usage: {
|
|
619
|
+
inputTokens: finishPart?.usage.inputTokens,
|
|
620
|
+
outputTokens: finishPart?.usage.outputTokens
|
|
621
|
+
}
|
|
622
|
+
})
|
|
623
|
+
}
|