@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,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import { EventStreamCodec } from "@smithy/eventstream-codec"
|
|
5
|
+
import { fromUtf8, toUtf8 } from "@smithy/util-utf8"
|
|
6
|
+
import * as Channel from "effect/Channel"
|
|
7
|
+
import type * as Chunk from "effect/Chunk"
|
|
8
|
+
import * as Effect from "effect/Effect"
|
|
9
|
+
import * as Mailbox from "effect/Mailbox"
|
|
10
|
+
import type { ParseError } from "effect/ParseResult"
|
|
11
|
+
import * as Schema from "effect/Schema"
|
|
12
|
+
import type * as AsyncInput from "effect/SingleProducerAsyncInput"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An event stream encoding parser.
|
|
16
|
+
*
|
|
17
|
+
* See the [AWS Documentation](https://docs.aws.amazon.com/lexv2/latest/dg/event-stream-encoding.html)
|
|
18
|
+
* for more information.
|
|
19
|
+
*
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
export const makeChannel = <A, I, IE, Done>(schema: Schema.Schema<A, I>, options?: {
|
|
23
|
+
readonly bufferSize?: number
|
|
24
|
+
}): Channel.Channel<
|
|
25
|
+
Chunk.Chunk<A>,
|
|
26
|
+
Chunk.Chunk<Uint8Array<ArrayBufferLike>>,
|
|
27
|
+
IE | ParseError,
|
|
28
|
+
IE,
|
|
29
|
+
void,
|
|
30
|
+
Done,
|
|
31
|
+
never
|
|
32
|
+
> =>
|
|
33
|
+
Mailbox.make<A, IE | ParseError>(options?.bufferSize ?? 16).pipe(
|
|
34
|
+
Effect.map((mailbox) => {
|
|
35
|
+
const codec = new EventStreamCodec(toUtf8, fromUtf8)
|
|
36
|
+
const decodeMessage = Schema.decodeUnknown(schema, {
|
|
37
|
+
onExcessProperty: "ignore"
|
|
38
|
+
})
|
|
39
|
+
const textDecoder = new TextDecoder()
|
|
40
|
+
|
|
41
|
+
let messages: Array<A> = []
|
|
42
|
+
let buffer = new Uint8Array(0)
|
|
43
|
+
const input: AsyncInput.AsyncInputProducer<
|
|
44
|
+
IE | ParseError,
|
|
45
|
+
Chunk.Chunk<Uint8Array>,
|
|
46
|
+
Done
|
|
47
|
+
> = {
|
|
48
|
+
awaitRead() {
|
|
49
|
+
return Effect.void
|
|
50
|
+
},
|
|
51
|
+
emit(chunks) {
|
|
52
|
+
return Effect.forEach(
|
|
53
|
+
chunks,
|
|
54
|
+
Effect.fnUntraced(function*(chunk) {
|
|
55
|
+
// Append new chunk to buffer
|
|
56
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length)
|
|
57
|
+
newBuffer.set(buffer)
|
|
58
|
+
newBuffer.set(chunk, buffer.length)
|
|
59
|
+
buffer = newBuffer
|
|
60
|
+
|
|
61
|
+
// Try to decode messages from the buffer
|
|
62
|
+
while (buffer.length >= 4) {
|
|
63
|
+
// The first four bytes are the total length of the message (big-endian)
|
|
64
|
+
const totalLength = new DataView(
|
|
65
|
+
buffer.buffer,
|
|
66
|
+
buffer.byteOffset,
|
|
67
|
+
buffer.byteLength
|
|
68
|
+
).getUint32(0, false)
|
|
69
|
+
|
|
70
|
+
// If we don't have the full message yet, keep looping
|
|
71
|
+
if (buffer.length < totalLength) {
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Decode exactly the sub-slice for this event
|
|
76
|
+
const subView = buffer.subarray(0, totalLength)
|
|
77
|
+
const decoded = codec.decode(subView)
|
|
78
|
+
|
|
79
|
+
// Slice the used bytes off the buffer, removing this message
|
|
80
|
+
buffer = buffer.slice(totalLength)
|
|
81
|
+
|
|
82
|
+
// Process the message
|
|
83
|
+
if (decoded.headers[":message-type"]?.value === "event") {
|
|
84
|
+
const data = textDecoder.decode(decoded.body)
|
|
85
|
+
|
|
86
|
+
// Wrap the data in the `":event-type"` field to match the
|
|
87
|
+
// expected schema
|
|
88
|
+
const message = yield* decodeMessage({
|
|
89
|
+
[decoded.headers[":event-type"]?.value as string]: JSON.parse(data)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
messages.push(message)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
yield* mailbox.offerAll(messages)
|
|
96
|
+
messages = []
|
|
97
|
+
}),
|
|
98
|
+
{ discard: true }
|
|
99
|
+
).pipe(Effect.catchAll((error) => mailbox.fail(error)))
|
|
100
|
+
},
|
|
101
|
+
error(cause) {
|
|
102
|
+
return mailbox.failCause(cause)
|
|
103
|
+
},
|
|
104
|
+
done() {
|
|
105
|
+
return mailbox.end
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return Channel.embedInput(Mailbox.toChannel(mailbox), input)
|
|
109
|
+
}),
|
|
110
|
+
Channel.unwrap
|
|
111
|
+
)
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
export * as AmazonBedrockClient from "./AmazonBedrockClient.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
export * as AmazonBedrockConfig from "./AmazonBedrockConfig.js"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @since 1.0.0
|
|
13
|
+
*/
|
|
14
|
+
export * as AmazonBedrockLanguageModel from "./AmazonBedrockLanguageModel.js"
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*/
|
|
19
|
+
export * as AmazonBedrockSchema from "./AmazonBedrockSchema.js"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export * as EventStreamEncoding from "./EventStreamEncoding.js"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type * as AiResponse from "@effect/ai/AiResponse"
|
|
2
|
+
import * as Predicate from "effect/Predicate"
|
|
3
|
+
import type { StopReason } from "../AmazonBedrockSchema.js"
|
|
4
|
+
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const ProviderMetadataKey = "@effect/ai-amazon-bedrock/AmazonBedrockLanguageModel/ProviderMetadata"
|
|
7
|
+
|
|
8
|
+
const finishReasonMap: Record<StopReason, AiResponse.FinishReason> = {
|
|
9
|
+
content_filtered: "content-filter",
|
|
10
|
+
end_turn: "stop",
|
|
11
|
+
guardrail_intervened: "content-filter",
|
|
12
|
+
max_tokens: "length",
|
|
13
|
+
stop_sequence: "stop",
|
|
14
|
+
tool_use: "tool-calls"
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
export const resolveFinishReason = (stopReason: StopReason): AiResponse.FinishReason => {
|
|
19
|
+
const reason = finishReasonMap[stopReason]
|
|
20
|
+
return Predicate.isUndefined(reason) ? "unknown" : reason
|
|
21
|
+
}
|