langchain 0.0.178 → 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 (84) hide show
  1. package/chains/combine_documents/reduce.cjs +1 -0
  2. package/chains/combine_documents/reduce.d.ts +1 -0
  3. package/chains/combine_documents/reduce.js +1 -0
  4. package/chat_models/bedrock/web.cjs +1 -0
  5. package/chat_models/bedrock/web.d.ts +1 -0
  6. package/chat_models/bedrock/web.js +1 -0
  7. package/chat_models/bedrock.cjs +1 -1
  8. package/chat_models/bedrock.d.ts +1 -1
  9. package/chat_models/bedrock.js +1 -1
  10. package/dist/cache/base.d.ts +1 -1
  11. package/dist/callbacks/index.d.ts +1 -1
  12. package/dist/chains/combine_documents/reduce.cjs +69 -0
  13. package/dist/chains/combine_documents/reduce.d.ts +28 -0
  14. package/dist/chains/combine_documents/reduce.js +64 -0
  15. package/dist/chat_models/baiduwenxin.cjs +16 -2
  16. package/dist/chat_models/baiduwenxin.js +16 -2
  17. package/dist/chat_models/bedrock/index.cjs +24 -0
  18. package/dist/chat_models/bedrock/index.d.ts +12 -0
  19. package/dist/chat_models/bedrock/index.js +18 -0
  20. package/dist/chat_models/{bedrock.cjs → bedrock/web.cjs} +44 -15
  21. package/dist/chat_models/{bedrock.d.ts → bedrock/web.d.ts} +14 -5
  22. package/dist/chat_models/{bedrock.js → bedrock/web.js} +42 -13
  23. package/dist/chat_models/iflytek_xinghuo/index.cjs +1 -1
  24. package/dist/chat_models/iflytek_xinghuo/index.js +1 -1
  25. package/dist/llms/bedrock/index.cjs +17 -0
  26. package/dist/llms/bedrock/index.d.ts +7 -0
  27. package/dist/llms/bedrock/index.js +13 -0
  28. package/dist/llms/{bedrock.cjs → bedrock/web.cjs} +46 -12
  29. package/dist/llms/{bedrock.d.ts → bedrock/web.d.ts} +17 -4
  30. package/dist/llms/{bedrock.js → bedrock/web.js} +46 -12
  31. package/dist/load/import_constants.cjs +3 -0
  32. package/dist/load/import_constants.js +3 -0
  33. package/dist/load/import_map.cjs +4 -2
  34. package/dist/load/import_map.d.ts +2 -0
  35. package/dist/load/import_map.js +2 -0
  36. package/dist/output_parsers/json.cjs +77 -0
  37. package/dist/output_parsers/json.d.ts +1 -0
  38. package/dist/output_parsers/json.js +73 -0
  39. package/dist/output_parsers/openai_functions.cjs +37 -2
  40. package/dist/output_parsers/openai_functions.d.ts +10 -5
  41. package/dist/output_parsers/openai_functions.js +38 -3
  42. package/dist/schema/index.cjs +33 -1
  43. package/dist/schema/index.d.ts +3 -1
  44. package/dist/schema/index.js +31 -0
  45. package/dist/schema/output_parser.cjs +63 -3
  46. package/dist/schema/output_parser.d.ts +16 -1
  47. package/dist/schema/output_parser.js +59 -0
  48. package/dist/schema/prompt_template.cjs +33 -0
  49. package/dist/schema/prompt_template.d.ts +12 -0
  50. package/dist/schema/prompt_template.js +29 -0
  51. package/dist/storage/convex.d.ts +21 -0
  52. package/dist/stores/message/cassandra.cjs +135 -0
  53. package/dist/stores/message/cassandra.d.ts +44 -0
  54. package/dist/stores/message/cassandra.js +131 -0
  55. package/dist/stores/message/convex.d.ts +21 -0
  56. package/dist/util/bedrock.cjs +13 -1
  57. package/dist/util/bedrock.d.ts +5 -2
  58. package/dist/util/bedrock.js +13 -1
  59. package/dist/util/fast-json-patch/index.cjs +1 -0
  60. package/dist/util/fast-json-patch/index.d.ts +1 -0
  61. package/dist/util/fast-json-patch/index.js +1 -0
  62. package/dist/util/fast-json-patch/src/duplex.cjs +237 -0
  63. package/dist/util/fast-json-patch/src/duplex.d.ts +23 -0
  64. package/dist/util/fast-json-patch/src/duplex.js +230 -0
  65. package/dist/vectorstores/cassandra.cjs +197 -47
  66. package/dist/vectorstores/cassandra.d.ts +47 -4
  67. package/dist/vectorstores/cassandra.js +197 -47
  68. package/dist/vectorstores/convex.d.ts +21 -0
  69. package/llms/bedrock/web.cjs +1 -0
  70. package/llms/bedrock/web.d.ts +1 -0
  71. package/llms/bedrock/web.js +1 -0
  72. package/llms/bedrock.cjs +1 -1
  73. package/llms/bedrock.d.ts +1 -1
  74. package/llms/bedrock.js +1 -1
  75. package/package.json +41 -1
  76. package/schema/prompt_template.cjs +1 -0
  77. package/schema/prompt_template.d.ts +1 -0
  78. package/schema/prompt_template.js +1 -0
  79. package/stores/message/cassandra.cjs +1 -0
  80. package/stores/message/cassandra.d.ts +1 -0
  81. package/stores/message/cassandra.js +1 -0
  82. package/dist/schema/runnable/remote.cjs +0 -225
  83. package/dist/schema/runnable/remote.d.ts +0 -28
  84. package/dist/schema/runnable/remote.js +0 -221
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/chains/combine_documents/reduce.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/chains/combine_documents/reduce.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/chains/combine_documents/reduce.js'
@@ -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,7 @@ import { Generation, StoredGeneration } from "../schema/index.js";
12
12
  export declare const getCacheKey: (...strings: string[]) => string;
13
13
  export declare function deserializeStoredGeneration(storedGeneration: StoredGeneration): {
14
14
  text: string;
15
- message: import("../schema/index.js").HumanMessage | import("../schema/index.js").AIMessage | import("../schema/index.js").SystemMessage | import("../schema/index.js").FunctionMessage | import("../schema/index.js").ChatMessage;
15
+ message: import("../schema/index.js").ChatMessage | import("../schema/index.js").HumanMessage | import("../schema/index.js").AIMessage | import("../schema/index.js").SystemMessage | import("../schema/index.js").FunctionMessage;
16
16
  } | {
17
17
  text: string;
18
18
  message?: undefined;
@@ -5,5 +5,5 @@ export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
5
5
  export { LangChainTracer } from "./handlers/tracer_langchain.js";
6
6
  export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
7
7
  export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
8
- export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, type CallbackManagerOptions, type Callbacks, TraceGroup, traceAsGroup, } from "./manager.js";
8
+ export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, type CallbackManagerOptions, type Callbacks, type BaseCallbackConfig, TraceGroup, traceAsGroup, } from "./manager.js";
9
9
  export { awaitAllCallbacks, consumeCallback } from "./promises.js";
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.collapseDocs = exports.splitListOfDocs = void 0;
4
+ /**
5
+ * Splits a list of documents into sublists based on a maximum token limit.
6
+ *
7
+ * @param {Document[]} docs - The list of documents to be split.
8
+ * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.
9
+ * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.
10
+ *
11
+ * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.
12
+ *
13
+ * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
14
+ */
15
+ function splitListOfDocs(docs,
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ lengthFunc, tokenMax) {
18
+ const newResultDocList = [];
19
+ let subResultDocs = [];
20
+ for (const doc of docs) {
21
+ subResultDocs.push(doc);
22
+ const numTokens = lengthFunc(subResultDocs);
23
+ if (numTokens > tokenMax) {
24
+ if (subResultDocs.length === 1) {
25
+ throw new Error("A single document was longer than the context length, we cannot handle this.");
26
+ }
27
+ newResultDocList.push(subResultDocs.slice(0, -1));
28
+ subResultDocs = subResultDocs.slice(-1);
29
+ }
30
+ }
31
+ newResultDocList.push(subResultDocs);
32
+ return newResultDocList;
33
+ }
34
+ exports.splitListOfDocs = splitListOfDocs;
35
+ /**
36
+ * Collapses a list of documents into a single document.
37
+ *
38
+ * This function takes a list of documents and a function to combine the content of these documents.
39
+ * It combines the content of the documents using the provided function and merges the metadata of all documents.
40
+ * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.
41
+ *
42
+ * @param {Document[]} docs - The list of documents to be collapsed.
43
+ * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.
44
+ *
45
+ * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.
46
+ *
47
+ * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.
48
+ */
49
+ async function collapseDocs(docs, combineDocumentFunc) {
50
+ const result = await combineDocumentFunc(docs);
51
+ const combinedMetadata = {};
52
+ for (const key in docs[0].metadata) {
53
+ if (key in docs[0].metadata) {
54
+ combinedMetadata[key] = String(docs[0].metadata[key]);
55
+ }
56
+ }
57
+ for (const doc of docs.slice(1)) {
58
+ for (const key in doc.metadata) {
59
+ if (key in combinedMetadata) {
60
+ combinedMetadata[key] += `, ${doc.metadata[key]}`;
61
+ }
62
+ else {
63
+ combinedMetadata[key] = String(doc.metadata[key]);
64
+ }
65
+ }
66
+ }
67
+ return { pageContent: result, metadata: combinedMetadata };
68
+ }
69
+ exports.collapseDocs = collapseDocs;
@@ -0,0 +1,28 @@
1
+ import { Document } from "../../document.js";
2
+ /**
3
+ * Splits a list of documents into sublists based on a maximum token limit.
4
+ *
5
+ * @param {Document[]} docs - The list of documents to be split.
6
+ * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.
7
+ * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.
8
+ *
9
+ * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.
10
+ *
11
+ * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
12
+ */
13
+ export declare function splitListOfDocs(docs: Document[], lengthFunc: (...args: any[]) => any, tokenMax: number): Document[][];
14
+ /**
15
+ * Collapses a list of documents into a single document.
16
+ *
17
+ * This function takes a list of documents and a function to combine the content of these documents.
18
+ * It combines the content of the documents using the provided function and merges the metadata of all documents.
19
+ * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.
20
+ *
21
+ * @param {Document[]} docs - The list of documents to be collapsed.
22
+ * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.
23
+ *
24
+ * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.
25
+ *
26
+ * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.
27
+ */
28
+ export declare function collapseDocs(docs: Document[], combineDocumentFunc: (docs: Document[]) => Promise<string>): Promise<Document>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Splits a list of documents into sublists based on a maximum token limit.
3
+ *
4
+ * @param {Document[]} docs - The list of documents to be split.
5
+ * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.
6
+ * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.
7
+ *
8
+ * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.
9
+ *
10
+ * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
11
+ */
12
+ export function splitListOfDocs(docs,
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ lengthFunc, tokenMax) {
15
+ const newResultDocList = [];
16
+ let subResultDocs = [];
17
+ for (const doc of docs) {
18
+ subResultDocs.push(doc);
19
+ const numTokens = lengthFunc(subResultDocs);
20
+ if (numTokens > tokenMax) {
21
+ if (subResultDocs.length === 1) {
22
+ throw new Error("A single document was longer than the context length, we cannot handle this.");
23
+ }
24
+ newResultDocList.push(subResultDocs.slice(0, -1));
25
+ subResultDocs = subResultDocs.slice(-1);
26
+ }
27
+ }
28
+ newResultDocList.push(subResultDocs);
29
+ return newResultDocList;
30
+ }
31
+ /**
32
+ * Collapses a list of documents into a single document.
33
+ *
34
+ * This function takes a list of documents and a function to combine the content of these documents.
35
+ * It combines the content of the documents using the provided function and merges the metadata of all documents.
36
+ * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.
37
+ *
38
+ * @param {Document[]} docs - The list of documents to be collapsed.
39
+ * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.
40
+ *
41
+ * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.
42
+ *
43
+ * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.
44
+ */
45
+ export async function collapseDocs(docs, combineDocumentFunc) {
46
+ const result = await combineDocumentFunc(docs);
47
+ const combinedMetadata = {};
48
+ for (const key in docs[0].metadata) {
49
+ if (key in docs[0].metadata) {
50
+ combinedMetadata[key] = String(docs[0].metadata[key]);
51
+ }
52
+ }
53
+ for (const doc of docs.slice(1)) {
54
+ for (const key in doc.metadata) {
55
+ if (key in combinedMetadata) {
56
+ combinedMetadata[key] += `, ${doc.metadata[key]}`;
57
+ }
58
+ else {
59
+ combinedMetadata[key] = String(doc.metadata[key]);
60
+ }
61
+ }
62
+ }
63
+ return { pageContent: result, metadata: combinedMetadata };
64
+ }
@@ -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;