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
@@ -2,15 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Bedrock = void 0;
4
4
  const signature_v4_1 = require("@smithy/signature-v4");
5
- const credential_provider_node_1 = require("@aws-sdk/credential-provider-node");
6
5
  const protocol_http_1 = require("@smithy/protocol-http");
7
6
  const eventstream_codec_1 = require("@smithy/eventstream-codec");
8
7
  const util_utf8_1 = require("@smithy/util-utf8");
9
8
  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");
9
+ const bedrock_js_1 = require("../../util/bedrock.cjs");
10
+ const env_js_1 = require("../../util/env.cjs");
11
+ const base_js_1 = require("../base.cjs");
12
+ const index_js_1 = require("../../schema/index.cjs");
14
13
  /**
15
14
  * A type of Large Language Model (LLM) that interacts with the Bedrock
16
15
  * service. It extends the base `LLM` class and implements the
@@ -21,8 +20,20 @@ const index_js_1 = require("../schema/index.cjs");
21
20
  * region, and the maximum number of tokens to generate.
22
21
  */
23
22
  class Bedrock extends base_js_1.LLM {
23
+ get lc_aliases() {
24
+ return {
25
+ model: "model_id",
26
+ region: "region_name",
27
+ };
28
+ }
24
29
  get lc_secrets() {
25
- return {};
30
+ return {
31
+ "credentials.accessKeyId": "BEDROCK_AWS_ACCESS_KEY_ID",
32
+ "credentials.secretAccessKey": "BEDROCK_AWS_SECRET_ACCESS_KEY",
33
+ };
34
+ }
35
+ get lc_attributes() {
36
+ return { region: this.region };
26
37
  }
27
38
  _llmType() {
28
39
  return "bedrock";
@@ -74,6 +85,7 @@ class Bedrock extends base_js_1.LLM {
74
85
  writable: true,
75
86
  value: void 0
76
87
  });
88
+ /** @deprecated */
77
89
  Object.defineProperty(this, "stopSequences", {
78
90
  enumerable: true,
79
91
  configurable: true,
@@ -98,8 +110,14 @@ class Bedrock extends base_js_1.LLM {
98
110
  writable: true,
99
111
  value: false
100
112
  });
113
+ Object.defineProperty(this, "lc_serializable", {
114
+ enumerable: true,
115
+ configurable: true,
116
+ writable: true,
117
+ value: true
118
+ });
101
119
  this.model = fields?.model ?? this.model;
102
- const allowedModels = ["ai21", "anthropic", "amazon"];
120
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
103
121
  if (!allowedModels.includes(this.model.split(".")[0])) {
104
122
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
105
123
  }
@@ -108,7 +126,11 @@ class Bedrock extends base_js_1.LLM {
108
126
  throw new Error("Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field.");
109
127
  }
110
128
  this.region = region;
111
- this.credentials = fields?.credentials ?? (0, credential_provider_node_1.defaultProvider)();
129
+ const credentials = fields?.credentials;
130
+ if (!credentials) {
131
+ throw new Error("Please set the AWS credentials in the 'credentials' field.");
132
+ }
133
+ this.credentials = credentials;
112
134
  this.temperature = fields?.temperature ?? this.temperature;
113
135
  this.maxTokens = fields?.maxTokens ?? this.maxTokens;
114
136
  this.fetchFn = fields?.fetchFn ?? fetch;
@@ -158,7 +180,7 @@ class Bedrock extends base_js_1.LLM {
158
180
  }
159
181
  async _signedFetch(prompt, options, fields) {
160
182
  const { bedrockMethod, endpointHost, provider } = fields;
161
- const inputBody = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareInput(provider, prompt, this.maxTokens, this.temperature, this.stopSequences, this.modelKwargs);
183
+ const inputBody = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareInput(provider, prompt, this.maxTokens, this.temperature, options.stop ?? this.stopSequences, this.modelKwargs, fields.bedrockMethod);
162
184
  const url = new URL(`https://${endpointHost}/model/${this.model}/${bedrockMethod}`);
163
185
  const request = new protocol_http_1.HttpRequest({
164
186
  hostname: url.hostname,
@@ -189,9 +211,21 @@ class Bedrock extends base_js_1.LLM {
189
211
  }));
190
212
  return response;
191
213
  }
214
+ invocationParams(options) {
215
+ return {
216
+ model: this.model,
217
+ region: this.region,
218
+ temperature: this.temperature,
219
+ maxTokens: this.maxTokens,
220
+ stop: options?.stop ?? this.stopSequences,
221
+ modelKwargs: this.modelKwargs,
222
+ };
223
+ }
192
224
  async *_streamResponseChunks(prompt, options, runManager) {
193
225
  const provider = this.model.split(".")[0];
194
- const bedrockMethod = provider === "anthropic" ? "invoke-with-response-stream" : "invoke";
226
+ const bedrockMethod = provider === "anthropic" || provider === "cohere"
227
+ ? "invoke-with-response-stream"
228
+ : "invoke";
195
229
  const service = "bedrock-runtime";
196
230
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
197
231
  // Send request to AWS using the low-level fetch API
@@ -203,7 +237,7 @@ class Bedrock extends base_js_1.LLM {
203
237
  if (response.status < 200 || response.status >= 300) {
204
238
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
205
239
  }
206
- if (provider === "anthropic") {
240
+ if (provider === "anthropic" || provider === "cohere") {
207
241
  const reader = response.body?.getReader();
208
242
  const decoder = new TextDecoder();
209
243
  for await (const chunk of this._readChunks(reader)) {
@@ -218,7 +252,7 @@ class Bedrock extends base_js_1.LLM {
218
252
  throw new Error(body.message);
219
253
  }
220
254
  if (body.bytes !== undefined) {
221
- const chunkResult = JSON.parse(Buffer.from(body.bytes, "base64").toString());
255
+ const chunkResult = JSON.parse(decoder.decode(Uint8Array.from(atob(body.bytes), (m) => m.codePointAt(0) ?? 0)));
222
256
  const text = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareOutput(provider, chunkResult);
223
257
  yield new index_js_1.GenerationChunk({
224
258
  text,
@@ -1,8 +1,9 @@
1
1
  import { EventStreamCodec } from "@smithy/eventstream-codec";
2
- import { BaseBedrockInput, type CredentialType } from "../util/bedrock.js";
3
- import { LLM, BaseLLMParams } from "./base.js";
4
- import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
5
- import { GenerationChunk } from "../schema/index.js";
2
+ import { BaseBedrockInput, type CredentialType } from "../../util/bedrock.js";
3
+ import { LLM, BaseLLMParams } from "../base.js";
4
+ import { CallbackManagerForLLMRun } from "../../callbacks/manager.js";
5
+ import { GenerationChunk } from "../../schema/index.js";
6
+ import { SerializedFields } from "../../load/map_keys.js";
6
7
  /**
7
8
  * A type of Large Language Model (LLM) that interacts with the Bedrock
8
9
  * service. It extends the base `LLM` class and implements the
@@ -20,13 +21,17 @@ export declare class Bedrock extends LLM implements BaseBedrockInput {
20
21
  maxTokens?: number | undefined;
21
22
  fetchFn: typeof fetch;
22
23
  endpointHost?: string;
24
+ /** @deprecated */
23
25
  stopSequences?: string[];
24
26
  modelKwargs?: Record<string, unknown>;
25
27
  codec: EventStreamCodec;
26
28
  streaming: boolean;
29
+ lc_serializable: boolean;
30
+ get lc_aliases(): Record<string, string>;
27
31
  get lc_secrets(): {
28
32
  [key: string]: string;
29
33
  } | undefined;
34
+ get lc_attributes(): SerializedFields | undefined;
30
35
  _llmType(): string;
31
36
  static lc_name(): string;
32
37
  constructor(fields?: Partial<BaseBedrockInput> & BaseLLMParams);
@@ -46,6 +51,14 @@ export declare class Bedrock extends LLM implements BaseBedrockInput {
46
51
  endpointHost: string;
47
52
  provider: string;
48
53
  }): Promise<Response>;
54
+ invocationParams(options?: this["ParsedCallOptions"]): {
55
+ model: string;
56
+ region: string;
57
+ temperature: number | undefined;
58
+ maxTokens: number | undefined;
59
+ stop: string[] | undefined;
60
+ modelKwargs: Record<string, unknown> | undefined;
61
+ };
49
62
  _streamResponseChunks(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
50
63
  _readChunks(reader: any): {
51
64
  [Symbol.asyncIterator](): AsyncGenerator<any, void, unknown>;
@@ -1,13 +1,12 @@
1
1
  import { SignatureV4 } from "@smithy/signature-v4";
2
- import { defaultProvider } from "@aws-sdk/credential-provider-node";
3
2
  import { HttpRequest } from "@smithy/protocol-http";
4
3
  import { EventStreamCodec } from "@smithy/eventstream-codec";
5
4
  import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
6
5
  import { Sha256 } from "@aws-crypto/sha256-js";
7
- import { BedrockLLMInputOutputAdapter, } from "../util/bedrock.js";
8
- import { getEnvironmentVariable } from "../util/env.js";
9
- import { LLM } from "./base.js";
10
- import { GenerationChunk } from "../schema/index.js";
6
+ import { BedrockLLMInputOutputAdapter, } from "../../util/bedrock.js";
7
+ import { getEnvironmentVariable } from "../../util/env.js";
8
+ import { LLM } from "../base.js";
9
+ import { GenerationChunk } from "../../schema/index.js";
11
10
  /**
12
11
  * A type of Large Language Model (LLM) that interacts with the Bedrock
13
12
  * service. It extends the base `LLM` class and implements the
@@ -18,8 +17,20 @@ import { GenerationChunk } from "../schema/index.js";
18
17
  * region, and the maximum number of tokens to generate.
19
18
  */
20
19
  export class Bedrock extends LLM {
20
+ get lc_aliases() {
21
+ return {
22
+ model: "model_id",
23
+ region: "region_name",
24
+ };
25
+ }
21
26
  get lc_secrets() {
22
- return {};
27
+ return {
28
+ "credentials.accessKeyId": "BEDROCK_AWS_ACCESS_KEY_ID",
29
+ "credentials.secretAccessKey": "BEDROCK_AWS_SECRET_ACCESS_KEY",
30
+ };
31
+ }
32
+ get lc_attributes() {
33
+ return { region: this.region };
23
34
  }
24
35
  _llmType() {
25
36
  return "bedrock";
@@ -71,6 +82,7 @@ export class Bedrock extends LLM {
71
82
  writable: true,
72
83
  value: void 0
73
84
  });
85
+ /** @deprecated */
74
86
  Object.defineProperty(this, "stopSequences", {
75
87
  enumerable: true,
76
88
  configurable: true,
@@ -95,8 +107,14 @@ export class Bedrock extends LLM {
95
107
  writable: true,
96
108
  value: false
97
109
  });
110
+ Object.defineProperty(this, "lc_serializable", {
111
+ enumerable: true,
112
+ configurable: true,
113
+ writable: true,
114
+ value: true
115
+ });
98
116
  this.model = fields?.model ?? this.model;
99
- const allowedModels = ["ai21", "anthropic", "amazon"];
117
+ const allowedModels = ["ai21", "anthropic", "amazon", "cohere"];
100
118
  if (!allowedModels.includes(this.model.split(".")[0])) {
101
119
  throw new Error(`Unknown model: '${this.model}', only these are supported: ${allowedModels}`);
102
120
  }
@@ -105,7 +123,11 @@ export class Bedrock extends LLM {
105
123
  throw new Error("Please set the AWS_DEFAULT_REGION environment variable or pass it to the constructor as the region field.");
106
124
  }
107
125
  this.region = region;
108
- this.credentials = fields?.credentials ?? defaultProvider();
126
+ const credentials = fields?.credentials;
127
+ if (!credentials) {
128
+ throw new Error("Please set the AWS credentials in the 'credentials' field.");
129
+ }
130
+ this.credentials = credentials;
109
131
  this.temperature = fields?.temperature ?? this.temperature;
110
132
  this.maxTokens = fields?.maxTokens ?? this.maxTokens;
111
133
  this.fetchFn = fields?.fetchFn ?? fetch;
@@ -155,7 +177,7 @@ export class Bedrock extends LLM {
155
177
  }
156
178
  async _signedFetch(prompt, options, fields) {
157
179
  const { bedrockMethod, endpointHost, provider } = fields;
158
- const inputBody = BedrockLLMInputOutputAdapter.prepareInput(provider, prompt, this.maxTokens, this.temperature, this.stopSequences, this.modelKwargs);
180
+ const inputBody = BedrockLLMInputOutputAdapter.prepareInput(provider, prompt, this.maxTokens, this.temperature, options.stop ?? this.stopSequences, this.modelKwargs, fields.bedrockMethod);
159
181
  const url = new URL(`https://${endpointHost}/model/${this.model}/${bedrockMethod}`);
160
182
  const request = new HttpRequest({
161
183
  hostname: url.hostname,
@@ -186,9 +208,21 @@ export class Bedrock extends LLM {
186
208
  }));
187
209
  return response;
188
210
  }
211
+ invocationParams(options) {
212
+ return {
213
+ model: this.model,
214
+ region: this.region,
215
+ temperature: this.temperature,
216
+ maxTokens: this.maxTokens,
217
+ stop: options?.stop ?? this.stopSequences,
218
+ modelKwargs: this.modelKwargs,
219
+ };
220
+ }
189
221
  async *_streamResponseChunks(prompt, options, runManager) {
190
222
  const provider = this.model.split(".")[0];
191
- const bedrockMethod = provider === "anthropic" ? "invoke-with-response-stream" : "invoke";
223
+ const bedrockMethod = provider === "anthropic" || provider === "cohere"
224
+ ? "invoke-with-response-stream"
225
+ : "invoke";
192
226
  const service = "bedrock-runtime";
193
227
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
194
228
  // Send request to AWS using the low-level fetch API
@@ -200,7 +234,7 @@ export class Bedrock extends LLM {
200
234
  if (response.status < 200 || response.status >= 300) {
201
235
  throw Error(`Failed to access underlying url '${endpointHost}': got ${response.status} ${response.statusText}: ${await response.text()}`);
202
236
  }
203
- if (provider === "anthropic") {
237
+ if (provider === "anthropic" || provider === "cohere") {
204
238
  const reader = response.body?.getReader();
205
239
  const decoder = new TextDecoder();
206
240
  for await (const chunk of this._readChunks(reader)) {
@@ -215,7 +249,7 @@ export class Bedrock extends LLM {
215
249
  throw new Error(body.message);
216
250
  }
217
251
  if (body.bytes !== undefined) {
218
- const chunkResult = JSON.parse(Buffer.from(body.bytes, "base64").toString());
252
+ const chunkResult = JSON.parse(decoder.decode(Uint8Array.from(atob(body.bytes), (m) => m.codePointAt(0) ?? 0)));
219
253
  const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, chunkResult);
220
254
  yield new GenerationChunk({
221
255
  text,
@@ -36,6 +36,7 @@ exports.optionalImportEntrypoints = [
36
36
  "langchain/llms/googlepalm",
37
37
  "langchain/llms/sagemaker_endpoint",
38
38
  "langchain/llms/bedrock",
39
+ "langchain/llms/bedrock/web",
39
40
  "langchain/llms/llama_cpp",
40
41
  "langchain/llms/writer",
41
42
  "langchain/llms/portkey",
@@ -111,6 +112,7 @@ exports.optionalImportEntrypoints = [
111
112
  "langchain/document_transformers/mozilla_readability",
112
113
  "langchain/chat_models/portkey",
113
114
  "langchain/chat_models/bedrock",
115
+ "langchain/chat_models/bedrock/web",
114
116
  "langchain/chat_models/googlevertexai",
115
117
  "langchain/chat_models/googlevertexai/web",
116
118
  "langchain/chat_models/googlepalm",
@@ -137,6 +139,7 @@ exports.optionalImportEntrypoints = [
137
139
  "langchain/cache/upstash_redis",
138
140
  "langchain/stores/doc/gcs",
139
141
  "langchain/stores/file/node",
142
+ "langchain/stores/message/cassandra",
140
143
  "langchain/stores/message/convex",
141
144
  "langchain/stores/message/cloudflare_d1",
142
145
  "langchain/stores/message/dynamodb",
@@ -33,6 +33,7 @@ export const optionalImportEntrypoints = [
33
33
  "langchain/llms/googlepalm",
34
34
  "langchain/llms/sagemaker_endpoint",
35
35
  "langchain/llms/bedrock",
36
+ "langchain/llms/bedrock/web",
36
37
  "langchain/llms/llama_cpp",
37
38
  "langchain/llms/writer",
38
39
  "langchain/llms/portkey",
@@ -108,6 +109,7 @@ export const optionalImportEntrypoints = [
108
109
  "langchain/document_transformers/mozilla_readability",
109
110
  "langchain/chat_models/portkey",
110
111
  "langchain/chat_models/bedrock",
112
+ "langchain/chat_models/bedrock/web",
111
113
  "langchain/chat_models/googlevertexai",
112
114
  "langchain/chat_models/googlevertexai/web",
113
115
  "langchain/chat_models/googlepalm",
@@ -134,6 +136,7 @@ export const optionalImportEntrypoints = [
134
136
  "langchain/cache/upstash_redis",
135
137
  "langchain/stores/doc/gcs",
136
138
  "langchain/stores/file/node",
139
+ "langchain/stores/message/cassandra",
137
140
  "langchain/stores/message/convex",
138
141
  "langchain/stores/message/cloudflare_d1",
139
142
  "langchain/stores/message/dynamodb",
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CassandraChatMessageHistory = void 0;
4
+ const cassandra_driver_1 = require("cassandra-driver");
5
+ const index_js_1 = require("../../schema/index.cjs");
6
+ const utils_js_1 = require("./utils.cjs");
7
+ /**
8
+ * Class for storing chat message history within Cassandra. It extends the
9
+ * BaseListChatMessageHistory class and provides methods to get, add, and
10
+ * clear messages.
11
+ */
12
+ class CassandraChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
13
+ constructor(options) {
14
+ super();
15
+ Object.defineProperty(this, "lc_namespace", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: ["langchain", "stores", "message", "cassandra"]
20
+ });
21
+ Object.defineProperty(this, "keyspace", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "table", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "client", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "sessionId", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "tableExists", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "options", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "queries", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ this.client = new cassandra_driver_1.Client(options);
64
+ this.keyspace = options.keyspace;
65
+ this.table = options.table;
66
+ this.sessionId = options.sessionId;
67
+ this.tableExists = false;
68
+ this.options = options;
69
+ }
70
+ /**
71
+ * Method to get all the messages stored in the Cassandra database.
72
+ * @returns Array of stored BaseMessage instances.
73
+ */
74
+ async getMessages() {
75
+ await this.ensureTable();
76
+ const resultSet = await this.client.execute(this.queries.select, [this.sessionId], { prepare: true });
77
+ const storedMessages = resultSet.rows.map((row) => ({
78
+ type: row.message_type,
79
+ data: JSON.parse(row.data),
80
+ }));
81
+ const baseMessages = (0, utils_js_1.mapStoredMessagesToChatMessages)(storedMessages);
82
+ return baseMessages;
83
+ }
84
+ /**
85
+ * Method to add a new message to the Cassandra database.
86
+ * @param message The BaseMessage instance to add.
87
+ * @returns A promise that resolves when the message has been added.
88
+ */
89
+ async addMessage(message) {
90
+ await this.ensureTable();
91
+ const messages = (0, utils_js_1.mapChatMessagesToStoredMessages)([message]);
92
+ const { type, data } = messages[0];
93
+ return this.client
94
+ .execute(this.queries.insert, [this.sessionId, type, JSON.stringify(data)], { prepare: true, ...this.options })
95
+ .then(() => { });
96
+ }
97
+ /**
98
+ * Method to clear all the messages from the Cassandra database.
99
+ * @returns A promise that resolves when all messages have been cleared.
100
+ */
101
+ async clear() {
102
+ await this.ensureTable();
103
+ return this.client
104
+ .execute(this.queries.delete, [this.sessionId], {
105
+ prepare: true,
106
+ ...this.options,
107
+ })
108
+ .then(() => { });
109
+ }
110
+ /**
111
+ * Method to initialize the Cassandra database.
112
+ * @returns Promise that resolves when the database has been initialized.
113
+ */
114
+ async ensureTable() {
115
+ if (this.tableExists) {
116
+ return;
117
+ }
118
+ await this.client.execute(`
119
+ CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
120
+ session_id text,
121
+ message_ts timestamp,
122
+ message_type text,
123
+ data text,
124
+ PRIMARY KEY ((session_id), message_ts)
125
+ );
126
+ `);
127
+ this.queries = {
128
+ insert: `INSERT INTO ${this.keyspace}.${this.table} (session_id, message_ts, message_type, data) VALUES (?, toTimestamp(now()), ?, ?);`,
129
+ select: `SELECT message_type, data FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
130
+ delete: `DELETE FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
131
+ };
132
+ this.tableExists = true;
133
+ }
134
+ }
135
+ exports.CassandraChatMessageHistory = CassandraChatMessageHistory;
@@ -0,0 +1,44 @@
1
+ import { DseClientOptions } from "cassandra-driver";
2
+ import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
3
+ export interface CassandraChatMessageHistoryOptions extends DseClientOptions {
4
+ keyspace: string;
5
+ table: string;
6
+ sessionId: string;
7
+ }
8
+ /**
9
+ * Class for storing chat message history within Cassandra. It extends the
10
+ * BaseListChatMessageHistory class and provides methods to get, add, and
11
+ * clear messages.
12
+ */
13
+ export declare class CassandraChatMessageHistory extends BaseListChatMessageHistory {
14
+ lc_namespace: string[];
15
+ private keyspace;
16
+ private table;
17
+ private client;
18
+ private sessionId;
19
+ private tableExists;
20
+ private options;
21
+ private queries;
22
+ constructor(options: CassandraChatMessageHistoryOptions);
23
+ /**
24
+ * Method to get all the messages stored in the Cassandra database.
25
+ * @returns Array of stored BaseMessage instances.
26
+ */
27
+ getMessages(): Promise<BaseMessage[]>;
28
+ /**
29
+ * Method to add a new message to the Cassandra database.
30
+ * @param message The BaseMessage instance to add.
31
+ * @returns A promise that resolves when the message has been added.
32
+ */
33
+ addMessage(message: BaseMessage): Promise<void>;
34
+ /**
35
+ * Method to clear all the messages from the Cassandra database.
36
+ * @returns A promise that resolves when all messages have been cleared.
37
+ */
38
+ clear(): Promise<void>;
39
+ /**
40
+ * Method to initialize the Cassandra database.
41
+ * @returns Promise that resolves when the database has been initialized.
42
+ */
43
+ private ensureTable;
44
+ }
@@ -0,0 +1,131 @@
1
+ import { Client } from "cassandra-driver";
2
+ import { BaseListChatMessageHistory, } from "../../schema/index.js";
3
+ import { mapChatMessagesToStoredMessages, mapStoredMessagesToChatMessages, } from "./utils.js";
4
+ /**
5
+ * Class for storing chat message history within Cassandra. It extends the
6
+ * BaseListChatMessageHistory class and provides methods to get, add, and
7
+ * clear messages.
8
+ */
9
+ export class CassandraChatMessageHistory extends BaseListChatMessageHistory {
10
+ constructor(options) {
11
+ super();
12
+ Object.defineProperty(this, "lc_namespace", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: ["langchain", "stores", "message", "cassandra"]
17
+ });
18
+ Object.defineProperty(this, "keyspace", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "table", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "client", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "sessionId", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "tableExists", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "options", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ Object.defineProperty(this, "queries", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ this.client = new Client(options);
61
+ this.keyspace = options.keyspace;
62
+ this.table = options.table;
63
+ this.sessionId = options.sessionId;
64
+ this.tableExists = false;
65
+ this.options = options;
66
+ }
67
+ /**
68
+ * Method to get all the messages stored in the Cassandra database.
69
+ * @returns Array of stored BaseMessage instances.
70
+ */
71
+ async getMessages() {
72
+ await this.ensureTable();
73
+ const resultSet = await this.client.execute(this.queries.select, [this.sessionId], { prepare: true });
74
+ const storedMessages = resultSet.rows.map((row) => ({
75
+ type: row.message_type,
76
+ data: JSON.parse(row.data),
77
+ }));
78
+ const baseMessages = mapStoredMessagesToChatMessages(storedMessages);
79
+ return baseMessages;
80
+ }
81
+ /**
82
+ * Method to add a new message to the Cassandra database.
83
+ * @param message The BaseMessage instance to add.
84
+ * @returns A promise that resolves when the message has been added.
85
+ */
86
+ async addMessage(message) {
87
+ await this.ensureTable();
88
+ const messages = mapChatMessagesToStoredMessages([message]);
89
+ const { type, data } = messages[0];
90
+ return this.client
91
+ .execute(this.queries.insert, [this.sessionId, type, JSON.stringify(data)], { prepare: true, ...this.options })
92
+ .then(() => { });
93
+ }
94
+ /**
95
+ * Method to clear all the messages from the Cassandra database.
96
+ * @returns A promise that resolves when all messages have been cleared.
97
+ */
98
+ async clear() {
99
+ await this.ensureTable();
100
+ return this.client
101
+ .execute(this.queries.delete, [this.sessionId], {
102
+ prepare: true,
103
+ ...this.options,
104
+ })
105
+ .then(() => { });
106
+ }
107
+ /**
108
+ * Method to initialize the Cassandra database.
109
+ * @returns Promise that resolves when the database has been initialized.
110
+ */
111
+ async ensureTable() {
112
+ if (this.tableExists) {
113
+ return;
114
+ }
115
+ await this.client.execute(`
116
+ CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
117
+ session_id text,
118
+ message_ts timestamp,
119
+ message_type text,
120
+ data text,
121
+ PRIMARY KEY ((session_id), message_ts)
122
+ );
123
+ `);
124
+ this.queries = {
125
+ insert: `INSERT INTO ${this.keyspace}.${this.table} (session_id, message_ts, message_type, data) VALUES (?, toTimestamp(now()), ?, ?);`,
126
+ select: `SELECT message_type, data FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
127
+ delete: `DELETE FROM ${this.keyspace}.${this.table} WHERE session_id = ?;`,
128
+ };
129
+ this.tableExists = true;
130
+ }
131
+ }