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
@@ -14,19 +14,19 @@ export class CassandraStore extends VectorStore {
14
14
  return "cassandra";
15
15
  }
16
16
  constructor(embeddings, args) {
17
- const argsWithDefaults = {
18
- indices: [],
19
- maxConcurrency: 25,
20
- batchSize: 1,
21
- ...args,
22
- };
23
- super(embeddings, argsWithDefaults);
17
+ super(embeddings, args);
24
18
  Object.defineProperty(this, "client", {
25
19
  enumerable: true,
26
20
  configurable: true,
27
21
  writable: true,
28
22
  value: void 0
29
23
  });
24
+ Object.defineProperty(this, "vectorType", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
30
  Object.defineProperty(this, "dimensions", {
31
31
  enumerable: true,
32
32
  configurable: true,
@@ -51,6 +51,18 @@ export class CassandraStore extends VectorStore {
51
51
  writable: true,
52
52
  value: void 0
53
53
  });
54
+ Object.defineProperty(this, "withClause", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ Object.defineProperty(this, "selectColumns", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
54
66
  Object.defineProperty(this, "table", {
55
67
  enumerable: true,
56
68
  configurable: true,
@@ -81,19 +93,27 @@ export class CassandraStore extends VectorStore {
81
93
  writable: true,
82
94
  value: void 0
83
95
  });
84
- this.asyncCaller = new AsyncCaller(argsWithDefaults ?? {});
96
+ const { indices = [], maxConcurrency = 25, withClause = "", batchSize = 1, vectorType = "cosine", dimensions, keyspace, table, primaryKey, metadataColumns, } = args;
97
+ const argsWithDefaults = {
98
+ ...args,
99
+ indices,
100
+ maxConcurrency,
101
+ withClause,
102
+ batchSize,
103
+ vectorType,
104
+ };
105
+ this.asyncCaller = new AsyncCaller(argsWithDefaults);
85
106
  this.client = new CassandraClient(argsWithDefaults);
86
- this.dimensions = argsWithDefaults.dimensions;
87
- this.keyspace = argsWithDefaults.keyspace;
88
- this.table = argsWithDefaults.table;
89
- this.primaryKey = argsWithDefaults.primaryKey;
90
- this.metadataColumns = argsWithDefaults.metadataColumns;
91
- this.indices = argsWithDefaults.indices;
92
- this.batchSize = argsWithDefaults.batchSize;
93
- if (this.batchSize < 1) {
94
- console.warn("batchSize must be greater than or equal to 1, defaulting to 1");
95
- this.batchSize = 1;
96
- }
107
+ // Assign properties
108
+ this.vectorType = vectorType;
109
+ this.dimensions = dimensions;
110
+ this.keyspace = keyspace;
111
+ this.table = table;
112
+ this.primaryKey = Array.isArray(primaryKey) ? primaryKey : [primaryKey];
113
+ this.metadataColumns = metadataColumns;
114
+ this.withClause = withClause.trim().replace(/^with\s*/i, "");
115
+ this.indices = indices;
116
+ this.batchSize = batchSize >= 1 ? batchSize : 1;
97
117
  }
98
118
  /**
99
119
  * Method to save vectors to the Cassandra database.
@@ -129,16 +149,45 @@ export class CassandraStore extends VectorStore {
129
149
  if (!this.isInitialized) {
130
150
  await this.initialize();
131
151
  }
132
- const queryStr = this.buildSearchQuery(query, k, filter);
133
- const queryResultSet = await this.client.execute(queryStr);
134
- return queryResultSet?.rows.map((row, index) => {
152
+ // Ensure we have an array of Filter from the public interface
153
+ const filters = this.asFilters(filter);
154
+ const queryStr = this.buildSearchQuery(filters);
155
+ // Search query will be of format:
156
+ // SELECT ..., text, similarity_x(?) AS similarity_score
157
+ // FROM ...
158
+ // <WHERE ...>
159
+ // ORDER BY vector ANN OF ?
160
+ // LIMIT ?
161
+ // If any filter values are specified, they will be in the WHERE clause as
162
+ // filter.name filter.operator ?
163
+ // queryParams is a list of bind variables sent with the prepared statement
164
+ const queryParams = [];
165
+ const vectorAsFloat32Array = new Float32Array(query);
166
+ queryParams.push(vectorAsFloat32Array);
167
+ if (filters) {
168
+ const values = filters.map(({ value }) => value);
169
+ queryParams.push(...values);
170
+ }
171
+ queryParams.push(vectorAsFloat32Array);
172
+ queryParams.push(k);
173
+ const queryResultSet = await this.client.execute(queryStr, queryParams, {
174
+ prepare: true,
175
+ });
176
+ return queryResultSet?.rows.map((row) => {
135
177
  const textContent = row.text;
136
- const sanitizedRow = Object.assign(row, {});
137
- delete sanitizedRow.vector;
178
+ const sanitizedRow = { ...row };
138
179
  delete sanitizedRow.text;
180
+ delete sanitizedRow.similarity_score;
181
+ // A null value in Cassandra evaluates to a deleted column
182
+ // as this is treated as a tombstone record for the cell.
183
+ Object.keys(sanitizedRow).forEach((key) => {
184
+ if (sanitizedRow[key] === null) {
185
+ delete sanitizedRow[key];
186
+ }
187
+ });
139
188
  return [
140
189
  new Document({ pageContent: textContent, metadata: sanitizedRow }),
141
- index,
190
+ row.similarity_score,
142
191
  ];
143
192
  });
144
193
  }
@@ -191,41 +240,142 @@ export class CassandraStore extends VectorStore {
191
240
  * @returns Promise that resolves when the database has been initialized.
192
241
  */
193
242
  async initialize() {
194
- await this.client.execute(`CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
195
- ${this.primaryKey.name} ${this.primaryKey.type} PRIMARY KEY,
196
- text TEXT,
243
+ let cql = "";
244
+ cql = `CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
245
+ ${this.primaryKey.map((col) => `${col.name} ${col.type}`).join(", ")}
246
+ , text TEXT
197
247
  ${this.metadataColumns.length > 0
198
- ? this.metadataColumns.map((col) => `${col.name} ${col.type},`)
248
+ ? ", " +
249
+ this.metadataColumns
250
+ .map((col) => `${col.name} ${col.type}`)
251
+ .join(", ")
199
252
  : ""}
200
- vector VECTOR<FLOAT, ${this.dimensions}>
201
- );`);
202
- await this.client
203
- .execute(`CREATE CUSTOM INDEX IF NOT EXISTS idx_vector_${this.table}
204
- ON ${this.keyspace}.${this.table}(vector) USING 'StorageAttachedIndex';`);
253
+ , vector VECTOR<FLOAT, ${this.dimensions}>
254
+ , ${this.buildPrimaryKey(this.primaryKey)}
255
+ ) ${this.withClause ? `WITH ${this.withClause}` : ""};`;
256
+ await this.client.execute(cql);
257
+ this.selectColumns = `${this.primaryKey
258
+ .map((col) => `${col.name}`)
259
+ .join(", ")}
260
+ ${this.metadataColumns.length > 0
261
+ ? ", " +
262
+ this.metadataColumns
263
+ .map((col) => `${col.name}`)
264
+ .join(", ")
265
+ : ""}`;
266
+ cql = `CREATE CUSTOM INDEX IF NOT EXISTS idx_vector_${this.table}
267
+ ON ${this.keyspace}.${this.table}(vector) USING 'StorageAttachedIndex' WITH OPTIONS = {'similarity_function': '${this.vectorType.toUpperCase()}'};`;
268
+ await this.client.execute(cql);
205
269
  for await (const { name, value } of this.indices) {
206
- await this.client
207
- .execute(`CREATE CUSTOM INDEX IF NOT EXISTS idx_${this.table}_${name}
208
- ON ${this.keyspace}.${this.table} ${value} USING 'StorageAttachedIndex';`);
270
+ cql = `CREATE CUSTOM INDEX IF NOT EXISTS idx_${this.table}_${name}
271
+ ON ${this.keyspace}.${this.table} ${value} USING 'StorageAttachedIndex';`;
272
+ await this.client.execute(cql);
209
273
  }
210
274
  this.isInitialized = true;
211
275
  }
212
- buildWhereClause(filter) {
213
- const whereClause = Object.entries(filter)
214
- .map(([key, value]) => `${key} = '${value}'`)
215
- .join(" AND ");
216
- return `WHERE ${whereClause}`;
276
+ /**
277
+ * Method to build the PRIMARY KEY clause for CREATE TABLE.
278
+ * @param columns: list of Column to include in the key
279
+ * @returns The clause, including PRIMARY KEY
280
+ */
281
+ buildPrimaryKey(columns) {
282
+ // Partition columns may be specified with optional attribute col.partition
283
+ const partitionColumns = columns
284
+ .filter((col) => col.partition)
285
+ .map((col) => col.name)
286
+ .join(", ");
287
+ // All columns not part of the partition key are clustering columns
288
+ const clusteringColumns = columns
289
+ .filter((col) => !col.partition)
290
+ .map((col) => col.name)
291
+ .join(", ");
292
+ let primaryKey = "";
293
+ // If partition columns are specified, they are included in a () wrapper
294
+ // If not, the clustering columns are used, and the first clustering column
295
+ // is the partition key per normal Cassandra behaviour.
296
+ if (partitionColumns) {
297
+ primaryKey = `PRIMARY KEY ((${partitionColumns}), ${clusteringColumns})`;
298
+ }
299
+ else {
300
+ primaryKey = `PRIMARY KEY (${clusteringColumns})`;
301
+ }
302
+ return primaryKey;
303
+ }
304
+ /**
305
+ * Type guard to check if an object is a Filter.
306
+ * @param obj: the object to check
307
+ * @returns boolean indicating if the object is a Filter
308
+ */
309
+ isFilter(obj) {
310
+ return (typeof obj === "object" && obj !== null && "name" in obj && "value" in obj);
311
+ }
312
+ /**
313
+ * Helper to convert Record<string,unknown> to a Filter[]
314
+ * @param record: a key-value Record collection
315
+ * @returns Record as a Filter[]
316
+ */
317
+ convertToFilters(record) {
318
+ return Object.entries(record).map(([name, value]) => ({
319
+ name,
320
+ value,
321
+ operator: "=",
322
+ }));
323
+ }
324
+ /**
325
+ * Input santisation method for filters, as FilterType is not required to be
326
+ * Filter[], but we want to use Filter[] internally.
327
+ * @param record: the proposed filter
328
+ * @returns A Filter[], which may be empty
329
+ */
330
+ asFilters(record) {
331
+ if (!record) {
332
+ return [];
333
+ }
334
+ // If record is already an array
335
+ if (Array.isArray(record)) {
336
+ return record.flatMap((item) => {
337
+ // Check if item is a Filter before passing it to convertToFilters
338
+ if (this.isFilter(item)) {
339
+ return [item];
340
+ }
341
+ else {
342
+ // Here item is treated as Record<string, unknown>
343
+ return this.convertToFilters(item);
344
+ }
345
+ });
346
+ }
347
+ // If record is a single Filter object, return it in an array
348
+ if (this.isFilter(record)) {
349
+ return [record];
350
+ }
351
+ // If record is a Record<string, unknown>, convert it to an array of Filter
352
+ return this.convertToFilters(record);
353
+ }
354
+ /**
355
+ * Method to build the WHERE clause of a CQL query, using bind variable ?
356
+ * @param filters list of filters to include in the WHERE clause
357
+ * @returns The WHERE clause
358
+ */
359
+ buildWhereClause(filters) {
360
+ if (!filters || filters.length === 0) {
361
+ return "";
362
+ }
363
+ const whereConditions = filters.map(({ name, operator = "=" }) => `${name} ${operator} ?`);
364
+ return `WHERE ${whereConditions.join(" AND ")}`;
217
365
  }
218
366
  /**
219
367
  * Method to build an CQL query for searching for similar vectors in the
220
368
  * Cassandra database.
221
369
  * @param query The query vector.
222
370
  * @param k The number of similar vectors to return.
223
- * @param filter
371
+ * @param filters
224
372
  * @returns The CQL query string.
225
373
  */
226
- buildSearchQuery(query, k = 1, filter = undefined) {
227
- const whereClause = filter ? this.buildWhereClause(filter) : "";
228
- return `SELECT * FROM ${this.keyspace}.${this.table} ${whereClause} ORDER BY vector ANN OF [${query}] LIMIT ${k}`;
374
+ buildSearchQuery(filters) {
375
+ const whereClause = filters ? this.buildWhereClause(filters) : "";
376
+ const cqlQuery = `SELECT ${this.selectColumns}, text, similarity_${this.vectorType}(vector, ?) AS similarity_score
377
+ FROM ${this.keyspace}.${this.table} ${whereClause} ORDER BY vector ANN OF ? LIMIT ?`;
378
+ return cqlQuery;
229
379
  }
230
380
  /**
231
381
  * Method for inserting vectors and documents into the Cassandra database in a batch.
@@ -253,7 +403,7 @@ export class CassandraStore extends VectorStore {
253
403
  const metadataInsert = metadataColNames.length > 0 ? ", " + metadataColNames.join(", ") : "";
254
404
  // Construct the query string and parameters
255
405
  const query = {
256
- query: `INSERT INTO ${this.keyspace}.${this.table} (vector, text${metadataInsert})
406
+ query: `INSERT INTO ${this.keyspace}.${this.table} (vector, text${metadataInsert})
257
407
  VALUES (?, ?${", ?".repeat(metadataColNames.length)})`,
258
408
  params: [preparedVector, document.pageContent, ...metadataVals],
259
409
  };
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/llms/bedrock/web.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/llms/bedrock/web.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/llms/bedrock/web.js'
package/llms/bedrock.cjs CHANGED
@@ -1 +1 @@
1
- module.exports = require('../dist/llms/bedrock.cjs');
1
+ module.exports = require('../dist/llms/bedrock/index.cjs');
package/llms/bedrock.d.ts CHANGED
@@ -1 +1 @@
1
- export * from '../dist/llms/bedrock.js'
1
+ export * from '../dist/llms/bedrock/index.js'
package/llms/bedrock.js CHANGED
@@ -1 +1 @@
1
- export * from '../dist/llms/bedrock.js'
1
+ export * from '../dist/llms/bedrock/index.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.179",
3
+ "version": "0.0.180",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -199,6 +199,9 @@
199
199
  "llms/bedrock.cjs",
200
200
  "llms/bedrock.js",
201
201
  "llms/bedrock.d.ts",
202
+ "llms/bedrock/web.cjs",
203
+ "llms/bedrock/web.js",
204
+ "llms/bedrock/web.d.ts",
202
205
  "llms/llama_cpp.cjs",
203
206
  "llms/llama_cpp.js",
204
207
  "llms/llama_cpp.d.ts",
@@ -481,6 +484,9 @@
481
484
  "chat_models/bedrock.cjs",
482
485
  "chat_models/bedrock.js",
483
486
  "chat_models/bedrock.d.ts",
487
+ "chat_models/bedrock/web.cjs",
488
+ "chat_models/bedrock/web.js",
489
+ "chat_models/bedrock/web.d.ts",
484
490
  "chat_models/cloudflare_workersai.cjs",
485
491
  "chat_models/cloudflare_workersai.js",
486
492
  "chat_models/cloudflare_workersai.d.ts",
@@ -664,6 +670,9 @@
664
670
  "stores/file/node.cjs",
665
671
  "stores/file/node.js",
666
672
  "stores/file/node.d.ts",
673
+ "stores/message/cassandra.cjs",
674
+ "stores/message/cassandra.js",
675
+ "stores/message/cassandra.d.ts",
667
676
  "stores/message/convex.cjs",
668
677
  "stores/message/convex.js",
669
678
  "stores/message/convex.d.ts",
@@ -1692,6 +1701,11 @@
1692
1701
  "import": "./llms/bedrock.js",
1693
1702
  "require": "./llms/bedrock.cjs"
1694
1703
  },
1704
+ "./llms/bedrock/web": {
1705
+ "types": "./llms/bedrock/web.d.ts",
1706
+ "import": "./llms/bedrock/web.js",
1707
+ "require": "./llms/bedrock/web.cjs"
1708
+ },
1695
1709
  "./llms/llama_cpp": {
1696
1710
  "types": "./llms/llama_cpp.d.ts",
1697
1711
  "import": "./llms/llama_cpp.js",
@@ -2162,6 +2176,11 @@
2162
2176
  "import": "./chat_models/bedrock.js",
2163
2177
  "require": "./chat_models/bedrock.cjs"
2164
2178
  },
2179
+ "./chat_models/bedrock/web": {
2180
+ "types": "./chat_models/bedrock/web.d.ts",
2181
+ "import": "./chat_models/bedrock/web.js",
2182
+ "require": "./chat_models/bedrock/web.cjs"
2183
+ },
2165
2184
  "./chat_models/cloudflare_workersai": {
2166
2185
  "types": "./chat_models/cloudflare_workersai.d.ts",
2167
2186
  "import": "./chat_models/cloudflare_workersai.js",
@@ -2467,6 +2486,11 @@
2467
2486
  "import": "./stores/file/node.js",
2468
2487
  "require": "./stores/file/node.cjs"
2469
2488
  },
2489
+ "./stores/message/cassandra": {
2490
+ "types": "./stores/message/cassandra.d.ts",
2491
+ "import": "./stores/message/cassandra.js",
2492
+ "require": "./stores/message/cassandra.cjs"
2493
+ },
2470
2494
  "./stores/message/convex": {
2471
2495
  "types": "./stores/message/convex.d.ts",
2472
2496
  "import": "./stores/message/convex.js",
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/stores/message/cassandra.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/stores/message/cassandra.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/stores/message/cassandra.js'
@@ -1,225 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RemoteRunnable = void 0;
4
- const base_js_1 = require("./base.cjs");
5
- const stream_js_1 = require("../../util/stream.cjs");
6
- const event_source_parse_js_1 = require("../../util/event-source-parse.cjs");
7
- const document_js_1 = require("../../document.cjs");
8
- const index_js_1 = require("../index.cjs");
9
- const base_js_2 = require("../../prompts/base.cjs");
10
- const chat_js_1 = require("../../prompts/chat.cjs");
11
- function isSuperset(set, subset) {
12
- for (const elem of subset) {
13
- if (!set.has(elem)) {
14
- return false;
15
- }
16
- }
17
- return true;
18
- }
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- function revive(obj) {
21
- if (Array.isArray(obj))
22
- return obj.map(revive);
23
- if (typeof obj === "object") {
24
- const keysArr = Object.keys(obj);
25
- const keys = new Set(keysArr);
26
- if (isSuperset(keys, new Set(["page_content", "metadata"])))
27
- return new document_js_1.Document({
28
- pageContent: obj.page_content,
29
- metadata: obj.metadata,
30
- });
31
- if (isSuperset(keys, new Set(["content", "type", "is_chunk"]))) {
32
- if (!obj.is_chunk) {
33
- if (obj.type === "human") {
34
- return new index_js_1.HumanMessage({
35
- content: obj.content,
36
- });
37
- }
38
- if (obj.type === "system") {
39
- return new index_js_1.SystemMessage({
40
- content: obj.content,
41
- });
42
- }
43
- if (obj.type === "chat") {
44
- return new index_js_1.ChatMessage({
45
- content: obj.content,
46
- role: obj.role,
47
- });
48
- }
49
- if (obj.type === "function") {
50
- return new index_js_1.FunctionMessage({
51
- content: obj.content,
52
- name: obj.name,
53
- });
54
- }
55
- if (obj.type === "ai") {
56
- return new index_js_1.AIMessage({
57
- content: obj.content,
58
- });
59
- }
60
- }
61
- else {
62
- if (obj.type === "human") {
63
- return new index_js_1.HumanMessageChunk({
64
- content: obj.content,
65
- });
66
- }
67
- if (obj.type === "system") {
68
- return new index_js_1.SystemMessageChunk({
69
- content: obj.content,
70
- });
71
- }
72
- if (obj.type === "chat") {
73
- return new index_js_1.ChatMessageChunk({
74
- content: obj.content,
75
- role: obj.role,
76
- });
77
- }
78
- if (obj.type === "function") {
79
- return new index_js_1.FunctionMessageChunk({
80
- content: obj.content,
81
- name: obj.name,
82
- });
83
- }
84
- if (obj.type === "ai") {
85
- return new index_js_1.AIMessageChunk({
86
- content: obj.content,
87
- });
88
- }
89
- }
90
- }
91
- if (isSuperset(keys, new Set(["text"]))) {
92
- return new base_js_2.StringPromptValue(obj.text);
93
- }
94
- if (isSuperset(keys, new Set(["messages"]))) {
95
- return new chat_js_1.ChatPromptValue({
96
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
- messages: obj.messages.map((msg) => revive(msg)),
98
- });
99
- }
100
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
- const innerRevive = (key) => [
102
- key,
103
- revive(obj[key]),
104
- ];
105
- const rtn = Object.fromEntries(keysArr.map(innerRevive));
106
- return rtn;
107
- }
108
- return obj;
109
- }
110
- function deserialize(str) {
111
- const obj = JSON.parse(str);
112
- return revive(obj);
113
- }
114
- function removeCallbacks(options) {
115
- const rest = { ...options };
116
- delete rest.callbacks;
117
- return rest;
118
- }
119
- class RemoteRunnable extends base_js_1.Runnable {
120
- constructor(fields) {
121
- super(fields);
122
- Object.defineProperty(this, "url", {
123
- enumerable: true,
124
- configurable: true,
125
- writable: true,
126
- value: void 0
127
- });
128
- Object.defineProperty(this, "options", {
129
- enumerable: true,
130
- configurable: true,
131
- writable: true,
132
- value: void 0
133
- });
134
- Object.defineProperty(this, "lc_namespace", {
135
- enumerable: true,
136
- configurable: true,
137
- writable: true,
138
- value: ["langchain", "schema", "runnable", "remote"]
139
- });
140
- const { url, options } = fields;
141
- this.url = url.replace(/\/$/, ""); // remove trailing slash
142
- this.options = options;
143
- }
144
- async post(path, body) {
145
- return await fetch(`${this.url}${path}`, {
146
- method: "POST",
147
- body: JSON.stringify(body),
148
- headers: {
149
- "Content-Type": "application/json",
150
- },
151
- signal: AbortSignal.timeout(this.options?.timeout ?? 5000),
152
- });
153
- }
154
- async invoke(input, options) {
155
- const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
156
- const response = await this.post("/invoke", {
157
- input,
158
- config: removeCallbacks(config),
159
- kwargs: kwargs ?? {},
160
- });
161
- return revive((await response.json()).output);
162
- }
163
- async _batch(inputs, options, _, batchOptions) {
164
- if (batchOptions?.returnExceptions) {
165
- throw new Error("returnExceptions is not supported for remote clients");
166
- }
167
- const configsAndKwargsArray = options?.map((opts) => this._separateRunnableConfigFromCallOptions(opts));
168
- const [configs, kwargs] = configsAndKwargsArray?.reduce(([pc, pk], [c, k]) => [
169
- [...pc, c],
170
- [...pk, k],
171
- ], [[], []]) ?? [[], []];
172
- const response = await this.post("/batch", {
173
- inputs,
174
- config: (configs ?? [])
175
- .map(removeCallbacks)
176
- .map((config) => ({ ...config, ...batchOptions })),
177
- kwargs,
178
- });
179
- const body = await response.json();
180
- if (!body.output)
181
- throw new Error("Invalid response from remote runnable");
182
- return revive(body.output);
183
- }
184
- async batch(inputs, options, batchOptions) {
185
- if (batchOptions?.returnExceptions) {
186
- throw Error("returnExceptions is not supported for remote clients");
187
- }
188
- return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
189
- }
190
- async stream(input, options) {
191
- const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
192
- const response = await this.post("/stream", {
193
- input,
194
- config,
195
- kwargs,
196
- });
197
- if (!response.ok) {
198
- const json = await response.json();
199
- const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
200
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
201
- error.response = response;
202
- throw error;
203
- }
204
- const { body } = response;
205
- if (!body) {
206
- throw new Error("Could not begin remote stream. Please check the given URL and try again.");
207
- }
208
- const stream = new ReadableStream({
209
- async start(controller) {
210
- const enqueueLine = (0, event_source_parse_js_1.getMessages)((msg) => {
211
- if (msg.data)
212
- controller.enqueue(deserialize(msg.data));
213
- });
214
- const onLine = (line, fieldLength, flush) => {
215
- enqueueLine(line, fieldLength, flush);
216
- if (flush)
217
- controller.close();
218
- };
219
- await (0, event_source_parse_js_1.getBytes)(body, (0, event_source_parse_js_1.getLines)(onLine));
220
- },
221
- });
222
- return stream_js_1.IterableReadableStream.fromReadableStream(stream);
223
- }
224
- }
225
- exports.RemoteRunnable = RemoteRunnable;
@@ -1,28 +0,0 @@
1
- import { Runnable, RunnableBatchOptions } from "./base.js";
2
- import { RunnableConfig } from "./config.js";
3
- import { IterableReadableStream } from "../../util/stream.js";
4
- import { CallbackManagerForChainRun } from "../../callbacks/manager.js";
5
- type RemoteRunnableOptions = {
6
- timeout?: number;
7
- };
8
- export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
9
- private url;
10
- private options?;
11
- lc_namespace: string[];
12
- constructor(fields: {
13
- url: string;
14
- options?: RemoteRunnableOptions;
15
- });
16
- private post;
17
- invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
18
- _batch(inputs: RunInput[], options?: Partial<CallOptions>[], _?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
19
- batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
20
- returnExceptions?: false;
21
- }): Promise<RunOutput[]>;
22
- batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
23
- returnExceptions: true;
24
- }): Promise<(RunOutput | Error)[]>;
25
- batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
26
- stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
27
- }
28
- export {};