langchain 0.0.155 → 0.0.156

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.
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/chat_models/bedrock.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/bedrock.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/bedrock.js'
@@ -0,0 +1,260 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatBedrock = exports.convertMessagesToPrompt = exports.convertMessagesToPromptAnthropic = void 0;
4
+ const signature_v4_1 = require("@smithy/signature-v4");
5
+ const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
6
+ const protocol_http_1 = require("@smithy/protocol-http");
7
+ const eventstream_codec_1 = require("@smithy/eventstream-codec");
8
+ const util_utf8_1 = require("@smithy/util-utf8");
9
+ const sha256_js_1 = require("@aws-crypto/sha256-js");
10
+ const bedrock_js_1 = require("../util/bedrock.cjs");
11
+ const env_js_1 = require("../util/env.cjs");
12
+ const base_js_1 = require("./base.cjs");
13
+ const index_js_1 = require("../schema/index.cjs");
14
+ function convertOneMessageToText(message, humanPrompt, aiPrompt) {
15
+ if (message._getType() === "human") {
16
+ return `${humanPrompt} ${message.content}`;
17
+ }
18
+ else if (message._getType() === "ai") {
19
+ return `${aiPrompt} ${message.content}`;
20
+ }
21
+ else if (message._getType() === "system") {
22
+ return `${humanPrompt} <admin>${message.content}</admin>`;
23
+ }
24
+ else if (index_js_1.ChatMessage.isInstance(message)) {
25
+ return `\n\n${message.role[0].toUpperCase() + message.role.slice(1)}: {message.content}`;
26
+ }
27
+ throw new Error(`Unknown role: ${message._getType()}`);
28
+ }
29
+ function convertMessagesToPromptAnthropic(messages, humanPrompt = "\n\nHuman:", aiPrompt = "\n\nAssistant:") {
30
+ const messagesCopy = [...messages];
31
+ if (messagesCopy.length === 0 ||
32
+ messagesCopy[messagesCopy.length - 1]._getType() !== "ai") {
33
+ messagesCopy.push(new index_js_1.AIMessage({ content: "" }));
34
+ }
35
+ return messagesCopy
36
+ .map((message) => convertOneMessageToText(message, humanPrompt, aiPrompt))
37
+ .join("");
38
+ }
39
+ exports.convertMessagesToPromptAnthropic = convertMessagesToPromptAnthropic;
40
+ /**
41
+ * Function that converts an array of messages into a single string prompt
42
+ * that can be used as input for a chat model. It delegates the conversion
43
+ * logic to the appropriate provider-specific function.
44
+ * @param messages Array of messages to be converted.
45
+ * @param options Options to be used during the conversion.
46
+ * @returns A string prompt that can be used as input for a chat model.
47
+ */
48
+ function convertMessagesToPrompt(messages, provider) {
49
+ if (provider === "anthropic") {
50
+ return convertMessagesToPromptAnthropic(messages);
51
+ }
52
+ throw new Error(`Provider ${provider} does not support chat.`);
53
+ }
54
+ exports.convertMessagesToPrompt = convertMessagesToPrompt;
55
+ /**
56
+ * A type of Large Language Model (LLM) that interacts with the Bedrock
57
+ * service. It extends the base `LLM` class and implements the
58
+ * `BaseBedrockInput` interface. The class is designed to authenticate and
59
+ * interact with the Bedrock service, which is a part of Amazon Web
60
+ * Services (AWS). It uses AWS credentials for authentication and can be
61
+ * configured with various parameters such as the model to use, the AWS
62
+ * region, and the maximum number of tokens to generate.
63
+ */
64
+ class ChatBedrock extends base_js_1.SimpleChatModel {
65
+ get lc_secrets() {
66
+ return {};
67
+ }
68
+ _llmType() {
69
+ return "bedrock";
70
+ }
71
+ static lc_name() {
72
+ return "ChatBedrock";
73
+ }
74
+ constructor(fields) {
75
+ super(fields ?? {});
76
+ Object.defineProperty(this, "model", {
77
+ enumerable: true,
78
+ configurable: true,
79
+ writable: true,
80
+ value: "amazon.titan-tg1-large"
81
+ });
82
+ Object.defineProperty(this, "region", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: void 0
87
+ });
88
+ Object.defineProperty(this, "credentials", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: void 0
93
+ });
94
+ Object.defineProperty(this, "temperature", {
95
+ enumerable: true,
96
+ configurable: true,
97
+ writable: true,
98
+ value: undefined
99
+ });
100
+ Object.defineProperty(this, "maxTokens", {
101
+ enumerable: true,
102
+ configurable: true,
103
+ writable: true,
104
+ value: undefined
105
+ });
106
+ Object.defineProperty(this, "fetchFn", {
107
+ enumerable: true,
108
+ configurable: true,
109
+ writable: true,
110
+ value: void 0
111
+ });
112
+ Object.defineProperty(this, "endpointHost", {
113
+ enumerable: true,
114
+ configurable: true,
115
+ writable: true,
116
+ value: void 0
117
+ });
118
+ Object.defineProperty(this, "stopSequences", {
119
+ enumerable: true,
120
+ configurable: true,
121
+ writable: true,
122
+ value: void 0
123
+ });
124
+ Object.defineProperty(this, "modelKwargs", {
125
+ enumerable: true,
126
+ configurable: true,
127
+ writable: true,
128
+ value: void 0
129
+ });
130
+ Object.defineProperty(this, "codec", {
131
+ enumerable: true,
132
+ configurable: true,
133
+ writable: true,
134
+ value: new eventstream_codec_1.EventStreamCodec(util_utf8_1.toUtf8, util_utf8_1.fromUtf8)
135
+ });
136
+ this.model = fields?.model ?? this.model;
137
+ const allowedModels = ["ai21", "anthropic", "amazon"];
138
+ if (!allowedModels.includes(this.model.split(".")[0])) {
139
+ throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
140
+ }
141
+ const region = fields?.region ?? (0, env_js_1.getEnvironmentVariable)("AWS_DEFAULT_REGION");
142
+ if (!region) {
143
+ throw new Error("Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field.");
144
+ }
145
+ this.region = region;
146
+ this.credentials = fields?.credentials ?? (0, credential_provider_node_1.defaultProvider)();
147
+ this.temperature = fields?.temperature ?? this.temperature;
148
+ this.maxTokens = fields?.maxTokens ?? this.maxTokens;
149
+ this.fetchFn = fields?.fetchFn ?? fetch;
150
+ this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
151
+ this.stopSequences = fields?.stopSequences;
152
+ this.modelKwargs = fields?.modelKwargs;
153
+ }
154
+ /** Call out to Bedrock service model.
155
+ Arguments:
156
+ prompt: The prompt to pass into the model.
157
+
158
+ Returns:
159
+ The string generated by the model.
160
+
161
+ Example:
162
+ response = model.call("Tell me a joke.")
163
+ */
164
+ async _call(messages, options, runManager) {
165
+ const chunks = [];
166
+ for await (const chunk of this._streamResponseChunks(messages, options, runManager)) {
167
+ chunks.push(chunk);
168
+ }
169
+ return chunks.map((chunk) => chunk.text).join("");
170
+ }
171
+ async *_streamResponseChunks(messages, options, runManager) {
172
+ const provider = this.model.split(".")[0];
173
+ const service = "bedrock-runtime";
174
+ const inputBody = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareInput(provider, convertMessagesToPromptAnthropic(messages), this.maxTokens, this.temperature, this.stopSequences, this.modelKwargs);
175
+ const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
176
+ const amazonMethod = provider === "anthropic" ? "invoke-with-response-stream" : "invoke";
177
+ const url = new URL(`https://${endpointHost}/model/${this.model}/${amazonMethod}`);
178
+ const request = new protocol_http_1.HttpRequest({
179
+ hostname: url.hostname,
180
+ path: url.pathname,
181
+ protocol: url.protocol,
182
+ method: "POST",
183
+ body: JSON.stringify(inputBody),
184
+ query: Object.fromEntries(url.searchParams.entries()),
185
+ headers: {
186
+ // host is required by AWS Signature V4: https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
187
+ host: url.host,
188
+ accept: "application/json",
189
+ "content-type": "application/json",
190
+ },
191
+ });
192
+ const signer = new signature_v4_1.SignatureV4({
193
+ credentials: this.credentials,
194
+ service: "bedrock",
195
+ region: this.region,
196
+ sha256: sha256_js_1.Sha256,
197
+ });
198
+ const signedRequest = await signer.sign(request);
199
+ // Send request to AWS using the low-level fetch API
200
+ const response = await this.caller.callWithOptions({ signal: options.signal }, async () => this.fetchFn(url, {
201
+ headers: signedRequest.headers,
202
+ body: signedRequest.body,
203
+ method: signedRequest.method,
204
+ }));
205
+ if (response.status < 200 || response.status >= 300) {
206
+ throw Error(`Failed to access underlying url '${url}': got ${response.status} ${response.statusText}: ${await response.text()}`);
207
+ }
208
+ if (provider === "anthropic") {
209
+ const reader = response.body?.getReader();
210
+ const decoder = new TextDecoder();
211
+ for await (const chunk of this._readChunks(reader)) {
212
+ const event = this.codec.decode(chunk);
213
+ if ((event.headers[":event-type"] !== undefined &&
214
+ event.headers[":event-type"].value !== "chunk") ||
215
+ event.headers[":content-type"].value !== "application/json") {
216
+ throw Error(`Failed to get event chunk: got ${chunk}`);
217
+ }
218
+ // console.log(decoder.decode(event.body));
219
+ const body = JSON.parse(decoder.decode(event.body));
220
+ if (body.message) {
221
+ throw new Error(body.message);
222
+ }
223
+ if (body.bytes !== undefined) {
224
+ const chunkResult = JSON.parse(Buffer.from(body.bytes, "base64").toString());
225
+ const text = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareOutput(provider, chunkResult);
226
+ yield new index_js_1.ChatGenerationChunk({
227
+ text,
228
+ message: new index_js_1.AIMessageChunk({ content: text }),
229
+ });
230
+ await runManager?.handleLLMNewToken(text);
231
+ }
232
+ }
233
+ }
234
+ else {
235
+ const json = await response.json();
236
+ const text = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
237
+ yield new index_js_1.ChatGenerationChunk({
238
+ text,
239
+ message: new index_js_1.AIMessageChunk({ content: text }),
240
+ });
241
+ await runManager?.handleLLMNewToken(text);
242
+ }
243
+ }
244
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
245
+ _readChunks(reader) {
246
+ return {
247
+ async *[Symbol.asyncIterator]() {
248
+ let readResult = await reader.read();
249
+ while (!readResult.done) {
250
+ yield readResult.value;
251
+ readResult = await reader.read();
252
+ }
253
+ },
254
+ };
255
+ }
256
+ _combineLLMOutput() {
257
+ return {};
258
+ }
259
+ }
260
+ exports.ChatBedrock = ChatBedrock;
@@ -0,0 +1,58 @@
1
+ import { EventStreamCodec } from "@smithy/eventstream-codec";
2
+ import { BaseBedrockInput, type CredentialType } from "../util/bedrock.js";
3
+ import { SimpleChatModel, BaseChatModelParams } from "./base.js";
4
+ import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
5
+ import { BaseMessage, ChatGenerationChunk } from "../schema/index.js";
6
+ export declare function convertMessagesToPromptAnthropic(messages: BaseMessage[], humanPrompt?: string, aiPrompt?: string): string;
7
+ /**
8
+ * Function that converts an array of messages into a single string prompt
9
+ * that can be used as input for a chat model. It delegates the conversion
10
+ * logic to the appropriate provider-specific function.
11
+ * @param messages Array of messages to be converted.
12
+ * @param options Options to be used during the conversion.
13
+ * @returns A string prompt that can be used as input for a chat model.
14
+ */
15
+ export declare function convertMessagesToPrompt(messages: BaseMessage[], provider: string): string;
16
+ /**
17
+ * A type of Large Language Model (LLM) that interacts with the Bedrock
18
+ * service. It extends the base `LLM` class and implements the
19
+ * `BaseBedrockInput` interface. The class is designed to authenticate and
20
+ * interact with the Bedrock service, which is a part of Amazon Web
21
+ * Services (AWS). It uses AWS credentials for authentication and can be
22
+ * configured with various parameters such as the model to use, the AWS
23
+ * region, and the maximum number of tokens to generate.
24
+ */
25
+ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockInput {
26
+ model: string;
27
+ region: string;
28
+ credentials: CredentialType;
29
+ temperature?: number | undefined;
30
+ maxTokens?: number | undefined;
31
+ fetchFn: typeof fetch;
32
+ endpointHost?: string;
33
+ stopSequences?: string[];
34
+ modelKwargs?: Record<string, unknown>;
35
+ codec: EventStreamCodec;
36
+ get lc_secrets(): {
37
+ [key: string]: string;
38
+ } | undefined;
39
+ _llmType(): string;
40
+ static lc_name(): string;
41
+ constructor(fields?: Partial<BaseBedrockInput> & BaseChatModelParams);
42
+ /** Call out to Bedrock service model.
43
+ Arguments:
44
+ prompt: The prompt to pass into the model.
45
+
46
+ Returns:
47
+ The string generated by the model.
48
+
49
+ Example:
50
+ response = model.call("Tell me a joke.")
51
+ */
52
+ _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
53
+ _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
54
+ _readChunks(reader: any): {
55
+ [Symbol.asyncIterator](): AsyncGenerator<any, void, unknown>;
56
+ };
57
+ _combineLLMOutput(): {};
58
+ }
@@ -0,0 +1,254 @@
1
+ import { SignatureV4 } from "@smithy/signature-v4";
2
+ import { defaultProvider } from "@aws-sdk/credential-provider-node";
3
+ import { HttpRequest } from "@smithy/protocol-http";
4
+ import { EventStreamCodec } from "@smithy/eventstream-codec";
5
+ import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
6
+ import { Sha256 } from "@aws-crypto/sha256-js";
7
+ import { BedrockLLMInputOutputAdapter, } from "../util/bedrock.js";
8
+ import { getEnvironmentVariable } from "../util/env.js";
9
+ import { SimpleChatModel } from "./base.js";
10
+ import { AIMessageChunk, AIMessage, ChatGenerationChunk, ChatMessage, } from "../schema/index.js";
11
+ function convertOneMessageToText(message, humanPrompt, aiPrompt) {
12
+ if (message._getType() === "human") {
13
+ return `${humanPrompt} ${message.content}`;
14
+ }
15
+ else if (message._getType() === "ai") {
16
+ return `${aiPrompt} ${message.content}`;
17
+ }
18
+ else if (message._getType() === "system") {
19
+ return `${humanPrompt} <admin>${message.content}</admin>`;
20
+ }
21
+ else if (ChatMessage.isInstance(message)) {
22
+ return `\n\n${message.role[0].toUpperCase() + message.role.slice(1)}: {message.content}`;
23
+ }
24
+ throw new Error(`Unknown role: ${message._getType()}`);
25
+ }
26
+ export function convertMessagesToPromptAnthropic(messages, humanPrompt = "\n\nHuman:", aiPrompt = "\n\nAssistant:") {
27
+ const messagesCopy = [...messages];
28
+ if (messagesCopy.length === 0 ||
29
+ messagesCopy[messagesCopy.length - 1]._getType() !== "ai") {
30
+ messagesCopy.push(new AIMessage({ content: "" }));
31
+ }
32
+ return messagesCopy
33
+ .map((message) => convertOneMessageToText(message, humanPrompt, aiPrompt))
34
+ .join("");
35
+ }
36
+ /**
37
+ * Function that converts an array of messages into a single string prompt
38
+ * that can be used as input for a chat model. It delegates the conversion
39
+ * logic to the appropriate provider-specific function.
40
+ * @param messages Array of messages to be converted.
41
+ * @param options Options to be used during the conversion.
42
+ * @returns A string prompt that can be used as input for a chat model.
43
+ */
44
+ export function convertMessagesToPrompt(messages, provider) {
45
+ if (provider === "anthropic") {
46
+ return convertMessagesToPromptAnthropic(messages);
47
+ }
48
+ throw new Error(`Provider ${provider} does not support chat.`);
49
+ }
50
+ /**
51
+ * A type of Large Language Model (LLM) that interacts with the Bedrock
52
+ * service. It extends the base `LLM` class and implements the
53
+ * `BaseBedrockInput` interface. The class is designed to authenticate and
54
+ * interact with the Bedrock service, which is a part of Amazon Web
55
+ * Services (AWS). It uses AWS credentials for authentication and can be
56
+ * configured with various parameters such as the model to use, the AWS
57
+ * region, and the maximum number of tokens to generate.
58
+ */
59
+ export class ChatBedrock extends SimpleChatModel {
60
+ get lc_secrets() {
61
+ return {};
62
+ }
63
+ _llmType() {
64
+ return "bedrock";
65
+ }
66
+ static lc_name() {
67
+ return "ChatBedrock";
68
+ }
69
+ constructor(fields) {
70
+ super(fields ?? {});
71
+ Object.defineProperty(this, "model", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: "amazon.titan-tg1-large"
76
+ });
77
+ Object.defineProperty(this, "region", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "credentials", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "temperature", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: undefined
94
+ });
95
+ Object.defineProperty(this, "maxTokens", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: undefined
100
+ });
101
+ Object.defineProperty(this, "fetchFn", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ Object.defineProperty(this, "endpointHost", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ Object.defineProperty(this, "stopSequences", {
114
+ enumerable: true,
115
+ configurable: true,
116
+ writable: true,
117
+ value: void 0
118
+ });
119
+ Object.defineProperty(this, "modelKwargs", {
120
+ enumerable: true,
121
+ configurable: true,
122
+ writable: true,
123
+ value: void 0
124
+ });
125
+ Object.defineProperty(this, "codec", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: new EventStreamCodec(toUtf8, fromUtf8)
130
+ });
131
+ this.model = fields?.model ?? this.model;
132
+ const allowedModels = ["ai21", "anthropic", "amazon"];
133
+ if (!allowedModels.includes(this.model.split(".")[0])) {
134
+ throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
135
+ }
136
+ const region = fields?.region ?? getEnvironmentVariable("AWS_DEFAULT_REGION");
137
+ if (!region) {
138
+ throw new Error("Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field.");
139
+ }
140
+ this.region = region;
141
+ this.credentials = fields?.credentials ?? defaultProvider();
142
+ this.temperature = fields?.temperature ?? this.temperature;
143
+ this.maxTokens = fields?.maxTokens ?? this.maxTokens;
144
+ this.fetchFn = fields?.fetchFn ?? fetch;
145
+ this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
146
+ this.stopSequences = fields?.stopSequences;
147
+ this.modelKwargs = fields?.modelKwargs;
148
+ }
149
+ /** Call out to Bedrock service model.
150
+ Arguments:
151
+ prompt: The prompt to pass into the model.
152
+
153
+ Returns:
154
+ The string generated by the model.
155
+
156
+ Example:
157
+ response = model.call("Tell me a joke.")
158
+ */
159
+ async _call(messages, options, runManager) {
160
+ const chunks = [];
161
+ for await (const chunk of this._streamResponseChunks(messages, options, runManager)) {
162
+ chunks.push(chunk);
163
+ }
164
+ return chunks.map((chunk) => chunk.text).join("");
165
+ }
166
+ async *_streamResponseChunks(messages, options, runManager) {
167
+ const provider = this.model.split(".")[0];
168
+ const service = "bedrock-runtime";
169
+ const inputBody = BedrockLLMInputOutputAdapter.prepareInput(provider, convertMessagesToPromptAnthropic(messages), this.maxTokens, this.temperature, this.stopSequences, this.modelKwargs);
170
+ const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
171
+ const amazonMethod = provider === "anthropic" ? "invoke-with-response-stream" : "invoke";
172
+ const url = new URL(`https://${endpointHost}/model/${this.model}/${amazonMethod}`);
173
+ const request = new HttpRequest({
174
+ hostname: url.hostname,
175
+ path: url.pathname,
176
+ protocol: url.protocol,
177
+ method: "POST",
178
+ body: JSON.stringify(inputBody),
179
+ query: Object.fromEntries(url.searchParams.entries()),
180
+ headers: {
181
+ // host is required by AWS Signature V4: https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
182
+ host: url.host,
183
+ accept: "application/json",
184
+ "content-type": "application/json",
185
+ },
186
+ });
187
+ const signer = new SignatureV4({
188
+ credentials: this.credentials,
189
+ service: "bedrock",
190
+ region: this.region,
191
+ sha256: Sha256,
192
+ });
193
+ const signedRequest = await signer.sign(request);
194
+ // Send request to AWS using the low-level fetch API
195
+ const response = await this.caller.callWithOptions({ signal: options.signal }, async () => this.fetchFn(url, {
196
+ headers: signedRequest.headers,
197
+ body: signedRequest.body,
198
+ method: signedRequest.method,
199
+ }));
200
+ if (response.status < 200 || response.status >= 300) {
201
+ throw Error(`Failed to access underlying url '${url}': got ${response.status} ${response.statusText}: ${await response.text()}`);
202
+ }
203
+ if (provider === "anthropic") {
204
+ const reader = response.body?.getReader();
205
+ const decoder = new TextDecoder();
206
+ for await (const chunk of this._readChunks(reader)) {
207
+ const event = this.codec.decode(chunk);
208
+ if ((event.headers[":event-type"] !== undefined &&
209
+ event.headers[":event-type"].value !== "chunk") ||
210
+ event.headers[":content-type"].value !== "application/json") {
211
+ throw Error(`Failed to get event chunk: got ${chunk}`);
212
+ }
213
+ // console.log(decoder.decode(event.body));
214
+ const body = JSON.parse(decoder.decode(event.body));
215
+ if (body.message) {
216
+ throw new Error(body.message);
217
+ }
218
+ if (body.bytes !== undefined) {
219
+ const chunkResult = JSON.parse(Buffer.from(body.bytes, "base64").toString());
220
+ const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, chunkResult);
221
+ yield new ChatGenerationChunk({
222
+ text,
223
+ message: new AIMessageChunk({ content: text }),
224
+ });
225
+ await runManager?.handleLLMNewToken(text);
226
+ }
227
+ }
228
+ }
229
+ else {
230
+ const json = await response.json();
231
+ const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
232
+ yield new ChatGenerationChunk({
233
+ text,
234
+ message: new AIMessageChunk({ content: text }),
235
+ });
236
+ await runManager?.handleLLMNewToken(text);
237
+ }
238
+ }
239
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
240
+ _readChunks(reader) {
241
+ return {
242
+ async *[Symbol.asyncIterator]() {
243
+ let readResult = await reader.read();
244
+ while (!readResult.done) {
245
+ yield readResult.value;
246
+ readResult = await reader.read();
247
+ }
248
+ },
249
+ };
250
+ }
251
+ _combineLLMOutput() {
252
+ return {};
253
+ }
254
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudflareWorkersAIEmbeddings = void 0;
4
+ const ai_1 = require("@cloudflare/ai");
5
+ const chunk_js_1 = require("../util/chunk.cjs");
6
+ const base_js_1 = require("./base.cjs");
7
+ class CloudflareWorkersAIEmbeddings extends base_js_1.Embeddings {
8
+ constructor(fields) {
9
+ super(fields);
10
+ Object.defineProperty(this, "modelName", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: "@cf/baai/bge-base-en-v1.5"
15
+ });
16
+ Object.defineProperty(this, "batchSize", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: 50
21
+ });
22
+ Object.defineProperty(this, "stripNewLines", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: true
27
+ });
28
+ Object.defineProperty(this, "ai", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ if (!fields.binding) {
35
+ throw new Error("Must supply a Workers AI binding, eg { binding: env.AI }");
36
+ }
37
+ this.ai = new ai_1.Ai(fields.binding);
38
+ this.modelName = fields.modelName ?? this.modelName;
39
+ this.stripNewLines = fields.stripNewLines ?? this.stripNewLines;
40
+ }
41
+ async embedDocuments(texts) {
42
+ const batches = (0, chunk_js_1.chunkArray)(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
43
+ const batchRequests = batches.map((batch) => this.runEmbedding(batch));
44
+ const batchResponses = await Promise.all(batchRequests);
45
+ const embeddings = [];
46
+ for (let i = 0; i < batchResponses.length; i += 1) {
47
+ const batchResponse = batchResponses[i];
48
+ for (let j = 0; j < batchResponse.length; j += 1) {
49
+ embeddings.push(batchResponse[j]);
50
+ }
51
+ }
52
+ return embeddings;
53
+ }
54
+ async embedQuery(text) {
55
+ const data = await this.runEmbedding([
56
+ this.stripNewLines ? text.replace(/\n/g, " ") : text,
57
+ ]);
58
+ return data[0];
59
+ }
60
+ async runEmbedding(texts) {
61
+ return this.caller.call(async () => {
62
+ const response = await this.ai.run(this.modelName, {
63
+ text: texts,
64
+ });
65
+ return response.data;
66
+ });
67
+ }
68
+ }
69
+ exports.CloudflareWorkersAIEmbeddings = CloudflareWorkersAIEmbeddings;
@@ -0,0 +1,28 @@
1
+ import { Ai } from "@cloudflare/ai";
2
+ import { Fetcher } from "@cloudflare/workers-types";
3
+ import { Embeddings, EmbeddingsParams } from "./base.js";
4
+ export interface CloudflareWorkersAIEmbeddingsParams extends EmbeddingsParams {
5
+ /** Binding */
6
+ binding: Fetcher;
7
+ /** Model name to use */
8
+ modelName?: string;
9
+ /**
10
+ * The maximum number of documents to embed in a single request.
11
+ */
12
+ batchSize?: number;
13
+ /**
14
+ * Whether to strip new lines from the input text. This is recommended by
15
+ * OpenAI, but may not be suitable for all use cases.
16
+ */
17
+ stripNewLines?: boolean;
18
+ }
19
+ export declare class CloudflareWorkersAIEmbeddings extends Embeddings {
20
+ modelName: string;
21
+ batchSize: number;
22
+ stripNewLines: boolean;
23
+ ai: Ai;
24
+ constructor(fields: CloudflareWorkersAIEmbeddingsParams);
25
+ embedDocuments(texts: string[]): Promise<number[][]>;
26
+ embedQuery(text: string): Promise<number[]>;
27
+ private runEmbedding;
28
+ }