langchain 0.0.179 → 0.0.180

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.
Files changed (46) hide show
  1. package/chat_models/bedrock/web.cjs +1 -0
  2. package/chat_models/bedrock/web.d.ts +1 -0
  3. package/chat_models/bedrock/web.js +1 -0
  4. package/chat_models/bedrock.cjs +1 -1
  5. package/chat_models/bedrock.d.ts +1 -1
  6. package/chat_models/bedrock.js +1 -1
  7. package/dist/chains/combine_documents/reduce.cjs +3 -1
  8. package/dist/chains/combine_documents/reduce.js +3 -1
  9. package/dist/chat_models/baiduwenxin.cjs +16 -2
  10. package/dist/chat_models/baiduwenxin.js +16 -2
  11. package/dist/chat_models/bedrock/index.cjs +24 -0
  12. package/dist/chat_models/bedrock/index.d.ts +12 -0
  13. package/dist/chat_models/bedrock/index.js +18 -0
  14. package/dist/chat_models/{bedrock.cjs → bedrock/web.cjs} +44 -15
  15. package/dist/chat_models/{bedrock.d.ts → bedrock/web.d.ts} +14 -5
  16. package/dist/chat_models/{bedrock.js → bedrock/web.js} +42 -13
  17. package/dist/llms/bedrock/index.cjs +17 -0
  18. package/dist/llms/bedrock/index.d.ts +7 -0
  19. package/dist/llms/bedrock/index.js +13 -0
  20. package/dist/llms/{bedrock.cjs → bedrock/web.cjs} +46 -12
  21. package/dist/llms/{bedrock.d.ts → bedrock/web.d.ts} +17 -4
  22. package/dist/llms/{bedrock.js → bedrock/web.js} +46 -12
  23. package/dist/load/import_constants.cjs +3 -0
  24. package/dist/load/import_constants.js +3 -0
  25. package/dist/stores/message/cassandra.cjs +135 -0
  26. package/dist/stores/message/cassandra.d.ts +44 -0
  27. package/dist/stores/message/cassandra.js +131 -0
  28. package/dist/util/bedrock.cjs +13 -1
  29. package/dist/util/bedrock.d.ts +5 -2
  30. package/dist/util/bedrock.js +13 -1
  31. package/dist/vectorstores/cassandra.cjs +197 -47
  32. package/dist/vectorstores/cassandra.d.ts +47 -4
  33. package/dist/vectorstores/cassandra.js +197 -47
  34. package/llms/bedrock/web.cjs +1 -0
  35. package/llms/bedrock/web.d.ts +1 -0
  36. package/llms/bedrock/web.js +1 -0
  37. package/llms/bedrock.cjs +1 -1
  38. package/llms/bedrock.d.ts +1 -1
  39. package/llms/bedrock.js +1 -1
  40. package/package.json +25 -1
  41. package/stores/message/cassandra.cjs +1 -0
  42. package/stores/message/cassandra.d.ts +1 -0
  43. package/stores/message/cassandra.js +1 -0
  44. package/dist/schema/runnable/remote.cjs +0 -225
  45. package/dist/schema/runnable/remote.d.ts +0 -28
  46. package/dist/schema/runnable/remote.js +0 -221
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/chat_models/bedrock/web.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/chat_models/bedrock/web.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/chat_models/bedrock/web.js'
@@ -1 +1 @@
1
- module.exports = require('../dist/chat_models/bedrock.cjs');
1
+ module.exports = require('../dist/chat_models/bedrock/index.cjs');
@@ -1 +1 @@
1
- export * from '../dist/chat_models/bedrock.js'
1
+ export * from '../dist/chat_models/bedrock/index.js'
@@ -1 +1 @@
1
- export * from '../dist/chat_models/bedrock.js'
1
+ export * from '../dist/chat_models/bedrock/index.js'
@@ -12,7 +12,9 @@ exports.collapseDocs = exports.splitListOfDocs = void 0;
12
12
  *
13
13
  * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
14
14
  */
15
- function splitListOfDocs(docs, lengthFunc, tokenMax) {
15
+ function splitListOfDocs(docs,
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ lengthFunc, tokenMax) {
16
18
  const newResultDocList = [];
17
19
  let subResultDocs = [];
18
20
  for (const doc of docs) {
@@ -9,7 +9,9 @@
9
9
  *
10
10
  * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
11
11
  */
12
- export function splitListOfDocs(docs, lengthFunc, tokenMax) {
12
+ export function splitListOfDocs(docs,
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ lengthFunc, tokenMax) {
13
15
  const newResultDocList = [];
14
16
  let subResultDocs = [];
15
17
  for (const doc of docs) {
@@ -249,7 +249,7 @@ class ChatBaiduWenxin extends base_js_1.BaseChatModel {
249
249
  return;
250
250
  }
251
251
  rejected = true;
252
- reject(data);
252
+ reject(new Error(data?.error_msg));
253
253
  return;
254
254
  }
255
255
  const message = data;
@@ -291,7 +291,12 @@ class ChatBaiduWenxin extends base_js_1.BaseChatModel {
291
291
  : await this.completionWithRetry({
292
292
  ...params,
293
293
  messages: messagesMapped,
294
- }, false, options?.signal);
294
+ }, false, options?.signal).then((data) => {
295
+ if (data?.error_code) {
296
+ throw new Error(data?.error_msg);
297
+ }
298
+ return data;
299
+ });
295
300
  const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data.usage ?? {};
296
301
  if (completionTokens) {
297
302
  tokenUsage.completionTokens =
@@ -335,6 +340,15 @@ class ChatBaiduWenxin extends base_js_1.BaseChatModel {
335
340
  }
336
341
  else {
337
342
  if (response.body) {
343
+ // response will not be a stream if an error occurred
344
+ if (!response.headers
345
+ .get("content-type")
346
+ ?.startsWith("text/event-stream")) {
347
+ onmessage?.(new MessageEvent("message", {
348
+ data: await response.text(),
349
+ }));
350
+ return;
351
+ }
338
352
  const reader = response.body.getReader();
339
353
  const decoder = new TextDecoder("utf-8");
340
354
  let data = "";
@@ -246,7 +246,7 @@ export class ChatBaiduWenxin extends BaseChatModel {
246
246
  return;
247
247
  }
248
248
  rejected = true;
249
- reject(data);
249
+ reject(new Error(data?.error_msg));
250
250
  return;
251
251
  }
252
252
  const message = data;
@@ -288,7 +288,12 @@ export class ChatBaiduWenxin extends BaseChatModel {
288
288
  : await this.completionWithRetry({
289
289
  ...params,
290
290
  messages: messagesMapped,
291
- }, false, options?.signal);
291
+ }, false, options?.signal).then((data) => {
292
+ if (data?.error_code) {
293
+ throw new Error(data?.error_msg);
294
+ }
295
+ return data;
296
+ });
292
297
  const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data.usage ?? {};
293
298
  if (completionTokens) {
294
299
  tokenUsage.completionTokens =
@@ -332,6 +337,15 @@ export class ChatBaiduWenxin extends BaseChatModel {
332
337
  }
333
338
  else {
334
339
  if (response.body) {
340
+ // response will not be a stream if an error occurred
341
+ if (!response.headers
342
+ .get("content-type")
343
+ ?.startsWith("text/event-stream")) {
344
+ onmessage?.(new MessageEvent("message", {
345
+ data: await response.text(),
346
+ }));
347
+ return;
348
+ }
335
349
  const reader = response.body.getReader();
336
350
  const decoder = new TextDecoder("utf-8");
337
351
  let data = "";
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatBedrock = exports.convertMessagesToPrompt = exports.convertMessagesToPromptAnthropic = exports.BedrockChat = void 0;
4
+ const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
5
+ const web_js_1 = require("./web.cjs");
6
+ class BedrockChat extends web_js_1.BedrockChat {
7
+ static lc_name() {
8
+ return "BedrockChat";
9
+ }
10
+ constructor(fields) {
11
+ super({
12
+ ...fields,
13
+ credentials: fields?.credentials ?? (0, credential_provider_node_1.defaultProvider)(),
14
+ });
15
+ }
16
+ }
17
+ exports.BedrockChat = BedrockChat;
18
+ var web_js_2 = require("./web.cjs");
19
+ Object.defineProperty(exports, "convertMessagesToPromptAnthropic", { enumerable: true, get: function () { return web_js_2.convertMessagesToPromptAnthropic; } });
20
+ Object.defineProperty(exports, "convertMessagesToPrompt", { enumerable: true, get: function () { return web_js_2.convertMessagesToPrompt; } });
21
+ /**
22
+ * @deprecated Use `BedrockChat` instead.
23
+ */
24
+ exports.ChatBedrock = BedrockChat;
@@ -0,0 +1,12 @@
1
+ import { BaseBedrockInput } from "../../util/bedrock.js";
2
+ import { BedrockChat as BaseBedrockChat } from "./web.js";
3
+ import { BaseChatModelParams } from "../base.js";
4
+ export declare class BedrockChat extends BaseBedrockChat {
5
+ static lc_name(): string;
6
+ constructor(fields?: Partial<BaseBedrockInput> & BaseChatModelParams);
7
+ }
8
+ export { convertMessagesToPromptAnthropic, convertMessagesToPrompt, } from "./web.js";
9
+ /**
10
+ * @deprecated Use `BedrockChat` instead.
11
+ */
12
+ export declare const ChatBedrock: typeof BedrockChat;
@@ -0,0 +1,18 @@
1
+ import { defaultProvider } from "@aws-sdk/credential-provider-node";
2
+ import { BedrockChat as BaseBedrockChat } from "./web.js";
3
+ export class BedrockChat extends BaseBedrockChat {
4
+ static lc_name() {
5
+ return "BedrockChat";
6
+ }
7
+ constructor(fields) {
8
+ super({
9
+ ...fields,
10
+ credentials: fields?.credentials ?? defaultProvider(),
11
+ });
12
+ }
13
+ }
14
+ export { convertMessagesToPromptAnthropic, convertMessagesToPrompt, } from "./web.js";
15
+ /**
16
+ * @deprecated Use `BedrockChat` instead.
17
+ */
18
+ export const ChatBedrock = BedrockChat;
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatBedrock = exports.convertMessagesToPrompt = exports.convertMessagesToPromptAnthropic = void 0;
3
+ exports.ChatBedrock = exports.BedrockChat = exports.convertMessagesToPrompt = exports.convertMessagesToPromptAnthropic = void 0;
4
4
  const signature_v4_1 = require("@smithy/signature-v4");
5
5
  const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
6
6
  const protocol_http_1 = require("@smithy/protocol-http");
7
7
  const eventstream_codec_1 = require("@smithy/eventstream-codec");
8
8
  const util_utf8_1 = require("@smithy/util-utf8");
9
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");
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
14
  function convertOneMessageToText(message, humanPrompt, aiPrompt) {
15
15
  if (message._getType() === "human") {
16
16
  return `${humanPrompt} ${message.content}`;
@@ -61,15 +61,27 @@ exports.convertMessagesToPrompt = convertMessagesToPrompt;
61
61
  * configured with various parameters such as the model to use, the AWS
62
62
  * region, and the maximum number of tokens to generate.
63
63
  */
64
- class ChatBedrock extends base_js_1.SimpleChatModel {
64
+ class BedrockChat extends base_js_1.SimpleChatModel {
65
+ get lc_aliases() {
66
+ return {
67
+ model: "model_id",
68
+ region: "region_name",
69
+ };
70
+ }
65
71
  get lc_secrets() {
66
- return {};
72
+ return {
73
+ "credentials.accessKeyId": "BEDROCK_AWS_ACCESS_KEY_ID",
74
+ "credentials.secretAccessKey": "BEDROCK_AWS_SECRET_ACCESS_KEY",
75
+ };
76
+ }
77
+ get lc_attributes() {
78
+ return { region: this.region };
67
79
  }
68
80
  _llmType() {
69
81
  return "bedrock";
70
82
  }
71
83
  static lc_name() {
72
- return "ChatBedrock";
84
+ return "BedrockChat";
73
85
  }
74
86
  constructor(fields) {
75
87
  super(fields ?? {});
@@ -115,6 +127,7 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
115
127
  writable: true,
116
128
  value: void 0
117
129
  });
130
+ /** @deprecated */
118
131
  Object.defineProperty(this, "stopSequences", {
119
132
  enumerable: true,
120
133
  configurable: true,
@@ -139,8 +152,14 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
139
152
  writable: true,
140
153
  value: false
141
154
  });
155
+ Object.defineProperty(this, "lc_serializable", {
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ value: true
160
+ });
142
161
  this.model = fields?.model ?? this.model;
143
- const allowedModels = ["ai21", "anthropic", "amazon"];
162
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
144
163
  if (!allowedModels.includes(this.model.split(".")[0])) {
145
164
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
146
165
  }
@@ -149,7 +168,11 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
149
168
  throw new Error("Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field.");
150
169
  }
151
170
  this.region = region;
152
- this.credentials = fields?.credentials ?? (0, credential_provider_node_1.defaultProvider)();
171
+ const credentials = fields?.credentials ?? (0, credential_provider_node_1.defaultProvider)();
172
+ if (!credentials) {
173
+ throw new Error("Please set the AWS credentials in the 'credentials' field.");
174
+ }
175
+ this.credentials = credentials;
153
176
  this.temperature = fields?.temperature ?? this.temperature;
154
177
  this.maxTokens = fields?.maxTokens ?? this.maxTokens;
155
178
  this.fetchFn = fields?.fetchFn ?? fetch;
@@ -199,7 +222,7 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
199
222
  }
200
223
  async _signedFetch(messages, options, fields) {
201
224
  const { bedrockMethod, endpointHost, provider } = fields;
202
- const inputBody = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareInput(provider, convertMessagesToPromptAnthropic(messages), this.maxTokens, this.temperature, this.stopSequences, this.modelKwargs);
225
+ const inputBody = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareInput(provider, convertMessagesToPromptAnthropic(messages), this.maxTokens, this.temperature, options.stop ?? this.stopSequences, this.modelKwargs, fields.bedrockMethod);
203
226
  const url = new URL(`https://${endpointHost}/model/${this.model}/${bedrockMethod}`);
204
227
  const request = new protocol_http_1.HttpRequest({
205
228
  hostname: url.hostname,
@@ -234,7 +257,9 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
234
257
  const provider = this.model.split(".")[0];
235
258
  const service = "bedrock-runtime";
236
259
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
237
- const bedrockMethod = provider === "anthropic" ? "invoke-with-response-stream" : "invoke";
260
+ const bedrockMethod = provider === "anthropic" || provider === "cohere"
261
+ ? "invoke-with-response-stream"
262
+ : "invoke";
238
263
  const response = await this._signedFetch(messages, options, {
239
264
  bedrockMethod,
240
265
  endpointHost,
@@ -243,7 +268,7 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
243
268
  if (response.status < 200 || response.status >= 300) {
244
269
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
245
270
  }
246
- if (provider === "anthropic") {
271
+ if (provider === "anthropic" || provider === "cohere") {
247
272
  const reader = response.body?.getReader();
248
273
  const decoder = new TextDecoder();
249
274
  for await (const chunk of this._readChunks(reader)) {
@@ -258,7 +283,7 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
258
283
  throw new Error(body.message);
259
284
  }
260
285
  if (body.bytes !== undefined) {
261
- const chunkResult = JSON.parse(Buffer.from(body.bytes, "base64").toString());
286
+ const chunkResult = JSON.parse(decoder.decode(Uint8Array.from(atob(body.bytes), (m) => m.codePointAt(0) ?? 0)));
262
287
  const text = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareOutput(provider, chunkResult);
263
288
  yield new index_js_1.ChatGenerationChunk({
264
289
  text,
@@ -296,4 +321,8 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
296
321
  return {};
297
322
  }
298
323
  }
299
- exports.ChatBedrock = ChatBedrock;
324
+ exports.BedrockChat = BedrockChat;
325
+ /**
326
+ * @deprecated Use `BedrockChat` instead.
327
+ */
328
+ exports.ChatBedrock = BedrockChat;
@@ -1,8 +1,9 @@
1
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";
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
+ import { SerializedFields } from "../../load/map_keys.js";
6
7
  export declare function convertMessagesToPromptAnthropic(messages: BaseMessage[], humanPrompt?: string, aiPrompt?: string): string;
7
8
  /**
8
9
  * Function that converts an array of messages into a single string prompt
@@ -22,7 +23,7 @@ export declare function convertMessagesToPrompt(messages: BaseMessage[], provide
22
23
  * configured with various parameters such as the model to use, the AWS
23
24
  * region, and the maximum number of tokens to generate.
24
25
  */
25
- export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockInput {
26
+ export declare class BedrockChat extends SimpleChatModel implements BaseBedrockInput {
26
27
  model: string;
27
28
  region: string;
28
29
  credentials: CredentialType;
@@ -30,13 +31,17 @@ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockI
30
31
  maxTokens?: number | undefined;
31
32
  fetchFn: typeof fetch;
32
33
  endpointHost?: string;
34
+ /** @deprecated */
33
35
  stopSequences?: string[];
34
36
  modelKwargs?: Record<string, unknown>;
35
37
  codec: EventStreamCodec;
36
38
  streaming: boolean;
39
+ lc_serializable: boolean;
40
+ get lc_aliases(): Record<string, string>;
37
41
  get lc_secrets(): {
38
42
  [key: string]: string;
39
43
  } | undefined;
44
+ get lc_attributes(): SerializedFields | undefined;
40
45
  _llmType(): string;
41
46
  static lc_name(): string;
42
47
  constructor(fields?: Partial<BaseBedrockInput> & BaseChatModelParams);
@@ -62,3 +67,7 @@ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockI
62
67
  };
63
68
  _combineLLMOutput(): {};
64
69
  }
70
+ /**
71
+ * @deprecated Use `BedrockChat` instead.
72
+ */
73
+ export declare const ChatBedrock: typeof BedrockChat;
@@ -4,10 +4,10 @@ import { HttpRequest } from "@smithy/protocol-http";
4
4
  import { EventStreamCodec } from "@smithy/eventstream-codec";
5
5
  import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
6
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";
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
11
  function convertOneMessageToText(message, humanPrompt, aiPrompt) {
12
12
  if (message._getType() === "human") {
13
13
  return `${humanPrompt} ${message.content}`;
@@ -56,15 +56,27 @@ export function convertMessagesToPrompt(messages, provider) {
56
56
  * configured with various parameters such as the model to use, the AWS
57
57
  * region, and the maximum number of tokens to generate.
58
58
  */
59
- export class ChatBedrock extends SimpleChatModel {
59
+ export class BedrockChat extends SimpleChatModel {
60
+ get lc_aliases() {
61
+ return {
62
+ model: "model_id",
63
+ region: "region_name",
64
+ };
65
+ }
60
66
  get lc_secrets() {
61
- return {};
67
+ return {
68
+ "credentials.accessKeyId": "BEDROCK_AWS_ACCESS_KEY_ID",
69
+ "credentials.secretAccessKey": "BEDROCK_AWS_SECRET_ACCESS_KEY",
70
+ };
71
+ }
72
+ get lc_attributes() {
73
+ return { region: this.region };
62
74
  }
63
75
  _llmType() {
64
76
  return "bedrock";
65
77
  }
66
78
  static lc_name() {
67
- return "ChatBedrock";
79
+ return "BedrockChat";
68
80
  }
69
81
  constructor(fields) {
70
82
  super(fields ?? {});
@@ -110,6 +122,7 @@ export class ChatBedrock extends SimpleChatModel {
110
122
  writable: true,
111
123
  value: void 0
112
124
  });
125
+ /** @deprecated */
113
126
  Object.defineProperty(this, "stopSequences", {
114
127
  enumerable: true,
115
128
  configurable: true,
@@ -134,8 +147,14 @@ export class ChatBedrock extends SimpleChatModel {
134
147
  writable: true,
135
148
  value: false
136
149
  });
150
+ Object.defineProperty(this, "lc_serializable", {
151
+ enumerable: true,
152
+ configurable: true,
153
+ writable: true,
154
+ value: true
155
+ });
137
156
  this.model = fields?.model ?? this.model;
138
- const allowedModels = ["ai21", "anthropic", "amazon"];
157
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
139
158
  if (!allowedModels.includes(this.model.split(".")[0])) {
140
159
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
141
160
  }
@@ -144,7 +163,11 @@ export class ChatBedrock extends SimpleChatModel {
144
163
  throw new Error("Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field.");
145
164
  }
146
165
  this.region = region;
147
- this.credentials = fields?.credentials ?? defaultProvider();
166
+ const credentials = fields?.credentials ?? defaultProvider();
167
+ if (!credentials) {
168
+ throw new Error("Please set the AWS credentials in the 'credentials' field.");
169
+ }
170
+ this.credentials = credentials;
148
171
  this.temperature = fields?.temperature ?? this.temperature;
149
172
  this.maxTokens = fields?.maxTokens ?? this.maxTokens;
150
173
  this.fetchFn = fields?.fetchFn ?? fetch;
@@ -194,7 +217,7 @@ export class ChatBedrock extends SimpleChatModel {
194
217
  }
195
218
  async _signedFetch(messages, options, fields) {
196
219
  const { bedrockMethod, endpointHost, provider } = fields;
197
- const inputBody = BedrockLLMInputOutputAdapter.prepareInput(provider, convertMessagesToPromptAnthropic(messages), this.maxTokens, this.temperature, this.stopSequences, this.modelKwargs);
220
+ const inputBody = BedrockLLMInputOutputAdapter.prepareInput(provider, convertMessagesToPromptAnthropic(messages), this.maxTokens, this.temperature, options.stop ?? this.stopSequences, this.modelKwargs, fields.bedrockMethod);
198
221
  const url = new URL(`https://${endpointHost}/model/${this.model}/${bedrockMethod}`);
199
222
  const request = new HttpRequest({
200
223
  hostname: url.hostname,
@@ -229,7 +252,9 @@ export class ChatBedrock extends SimpleChatModel {
229
252
  const provider = this.model.split(".")[0];
230
253
  const service = "bedrock-runtime";
231
254
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
232
- const bedrockMethod = provider === "anthropic" ? "invoke-with-response-stream" : "invoke";
255
+ const bedrockMethod = provider === "anthropic" || provider === "cohere"
256
+ ? "invoke-with-response-stream"
257
+ : "invoke";
233
258
  const response = await this._signedFetch(messages, options, {
234
259
  bedrockMethod,
235
260
  endpointHost,
@@ -238,7 +263,7 @@ export class ChatBedrock extends SimpleChatModel {
238
263
  if (response.status < 200 || response.status >= 300) {
239
264
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
240
265
  }
241
- if (provider === "anthropic") {
266
+ if (provider === "anthropic" || provider === "cohere") {
242
267
  const reader = response.body?.getReader();
243
268
  const decoder = new TextDecoder();
244
269
  for await (const chunk of this._readChunks(reader)) {
@@ -253,7 +278,7 @@ export class ChatBedrock extends SimpleChatModel {
253
278
  throw new Error(body.message);
254
279
  }
255
280
  if (body.bytes !== undefined) {
256
- const chunkResult = JSON.parse(Buffer.from(body.bytes, "base64").toString());
281
+ const chunkResult = JSON.parse(decoder.decode(Uint8Array.from(atob(body.bytes), (m) => m.codePointAt(0) ?? 0)));
257
282
  const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, chunkResult);
258
283
  yield new ChatGenerationChunk({
259
284
  text,
@@ -291,3 +316,7 @@ export class ChatBedrock extends SimpleChatModel {
291
316
  return {};
292
317
  }
293
318
  }
319
+ /**
320
+ * @deprecated Use `BedrockChat` instead.
321
+ */
322
+ export const ChatBedrock = BedrockChat;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Bedrock = void 0;
4
+ const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
5
+ const web_js_1 = require("./web.cjs");
6
+ class Bedrock extends web_js_1.Bedrock {
7
+ static lc_name() {
8
+ return "Bedrock";
9
+ }
10
+ constructor(fields) {
11
+ super({
12
+ ...fields,
13
+ credentials: fields?.credentials ?? (0, credential_provider_node_1.defaultProvider)(),
14
+ });
15
+ }
16
+ }
17
+ exports.Bedrock = Bedrock;
@@ -0,0 +1,7 @@
1
+ import { BaseBedrockInput } from "../../util/bedrock.js";
2
+ import { BaseLLMParams } from "../base.js";
3
+ import { Bedrock as BaseBedrock } from "./web.js";
4
+ export declare class Bedrock extends BaseBedrock {
5
+ static lc_name(): string;
6
+ constructor(fields?: Partial<BaseBedrockInput> & BaseLLMParams);
7
+ }
@@ -0,0 +1,13 @@
1
+ import { defaultProvider } from "@aws-sdk/credential-provider-node";
2
+ import { Bedrock as BaseBedrock } from "./web.js";
3
+ export class Bedrock extends BaseBedrock {
4
+ static lc_name() {
5
+ return "Bedrock";
6
+ }
7
+ constructor(fields) {
8
+ super({
9
+ ...fields,
10
+ credentials: fields?.credentials ?? defaultProvider(),
11
+ });
12
+ }
13
+ }