langchain 0.0.172 → 0.0.174

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 (67) hide show
  1. package/agents/openai/output_parser.cjs +1 -0
  2. package/agents/openai/output_parser.d.ts +1 -0
  3. package/agents/openai/output_parser.js +1 -0
  4. package/agents/xml/output_parser.cjs +1 -0
  5. package/agents/xml/output_parser.d.ts +1 -0
  6. package/agents/xml/output_parser.js +1 -0
  7. package/dist/agents/index.cjs +3 -1
  8. package/dist/agents/index.d.ts +1 -0
  9. package/dist/agents/index.js +1 -0
  10. package/dist/agents/openai/index.cjs +8 -31
  11. package/dist/agents/openai/index.d.ts +2 -0
  12. package/dist/agents/openai/index.js +8 -31
  13. package/dist/agents/openai/output_parser.cjs +65 -0
  14. package/dist/agents/openai/output_parser.d.ts +22 -0
  15. package/dist/agents/openai/output_parser.js +61 -0
  16. package/dist/agents/toolkits/conversational_retrieval/tool.cjs +2 -1
  17. package/dist/agents/toolkits/conversational_retrieval/tool.js +2 -1
  18. package/dist/agents/xml/index.cjs +9 -25
  19. package/dist/agents/xml/index.d.ts +2 -7
  20. package/dist/agents/xml/index.js +8 -23
  21. package/dist/agents/xml/output_parser.cjs +44 -0
  22. package/dist/agents/xml/output_parser.d.ts +14 -0
  23. package/dist/agents/xml/output_parser.js +40 -0
  24. package/dist/callbacks/manager.cjs +2 -1
  25. package/dist/callbacks/manager.js +2 -1
  26. package/dist/document_loaders/fs/pdf.cjs +2 -1
  27. package/dist/document_loaders/fs/pdf.js +2 -1
  28. package/dist/document_loaders/web/pdf.cjs +2 -1
  29. package/dist/document_loaders/web/pdf.js +2 -1
  30. package/dist/load/import_map.cjs +5 -2
  31. package/dist/load/import_map.d.ts +3 -0
  32. package/dist/load/import_map.js +3 -0
  33. package/dist/memory/index.cjs +2 -1
  34. package/dist/memory/index.d.ts +1 -1
  35. package/dist/memory/index.js +1 -1
  36. package/dist/memory/vector_store.cjs +2 -1
  37. package/dist/memory/vector_store.js +2 -1
  38. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -18
  39. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +12 -6
  40. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +15 -18
  41. package/dist/storage/file_system.cjs +31 -11
  42. package/dist/storage/file_system.js +9 -9
  43. package/dist/tools/index.cjs +3 -1
  44. package/dist/tools/index.d.ts +1 -0
  45. package/dist/tools/index.js +1 -0
  46. package/dist/tools/render.cjs +19 -1
  47. package/dist/tools/render.d.ts +12 -0
  48. package/dist/tools/render.js +17 -0
  49. package/dist/tools/serpapi.d.ts +2 -2
  50. package/dist/tools/webbrowser.cjs +2 -1
  51. package/dist/tools/webbrowser.js +2 -1
  52. package/dist/util/document.cjs +12 -0
  53. package/dist/util/document.d.ts +9 -0
  54. package/dist/util/document.js +8 -0
  55. package/dist/vectorstores/cassandra.cjs +130 -35
  56. package/dist/vectorstores/cassandra.d.ts +21 -10
  57. package/dist/vectorstores/cassandra.js +130 -35
  58. package/dist/vectorstores/pgvector.cjs +13 -7
  59. package/dist/vectorstores/pgvector.d.ts +7 -0
  60. package/dist/vectorstores/pgvector.js +13 -7
  61. package/dist/vectorstores/pinecone.cjs +46 -9
  62. package/dist/vectorstores/pinecone.d.ts +20 -2
  63. package/dist/vectorstores/pinecone.js +46 -9
  64. package/package.json +27 -3
  65. package/util/document.cjs +1 -0
  66. package/util/document.d.ts +1 -0
  67. package/util/document.js +1 -0
@@ -68,6 +68,12 @@ export class PGVectorStore extends VectorStore {
68
68
  writable: true,
69
69
  value: void 0
70
70
  });
71
+ Object.defineProperty(this, "chunkSize", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: 500
76
+ });
71
77
  this.tableName = config.tableName;
72
78
  this.filter = config.filter;
73
79
  this.vectorColumnName = config.columns?.vectorColumnName ?? "embedding";
@@ -76,6 +82,7 @@ export class PGVectorStore extends VectorStore {
76
82
  this.metadataColumnName = config.columns?.metadataColumnName ?? "metadata";
77
83
  const pool = new pg.Pool(config.postgresConnectionOptions);
78
84
  this.pool = pool;
85
+ this.chunkSize = config.chunkSize ?? 500;
79
86
  this._verbose =
80
87
  getEnvironmentVariable("LANGCHAIN_VERBOSE") === "true" ??
81
88
  !!config.verbose;
@@ -126,9 +133,9 @@ export class PGVectorStore extends VectorStore {
126
133
  * @param chunkIndex - The starting index for generating query placeholders based on chunk positioning.
127
134
  * @returns The complete SQL INSERT INTO query string.
128
135
  */
129
- buildInsertQuery(rows, chunkIndex) {
136
+ buildInsertQuery(rows) {
130
137
  const valuesPlaceholders = rows
131
- .map((_, j) => this.generatePlaceholderForRowAt(chunkIndex + j))
138
+ .map((_, j) => this.generatePlaceholderForRowAt(j))
132
139
  .join(", ");
133
140
  const text = `
134
141
  INSERT INTO ${this.tableName}(
@@ -157,10 +164,9 @@ export class PGVectorStore extends VectorStore {
157
164
  documents[idx].metadata,
158
165
  ];
159
166
  });
160
- const chunkSize = 500;
161
- for (let i = 0; i < rows.length; i += chunkSize) {
162
- const chunk = rows.slice(i, i + chunkSize);
163
- const insertQuery = this.buildInsertQuery(chunk, i);
167
+ for (let i = 0; i < rows.length; i += this.chunkSize) {
168
+ const chunk = rows.slice(i, i + this.chunkSize);
169
+ const insertQuery = this.buildInsertQuery(chunk);
164
170
  const flatValues = chunk.flat();
165
171
  try {
166
172
  await this.pool.query(insertQuery, flatValues);
@@ -264,7 +270,7 @@ export class PGVectorStore extends VectorStore {
264
270
  * @returns Promise that resolves when all clients are closed and the pool is terminated.
265
271
  */
266
272
  async end() {
267
- await this.client?.release();
273
+ this.client?.release();
268
274
  return this.pool.end();
269
275
  }
270
276
  }
@@ -33,6 +33,7 @@ const flat_1 = __importDefault(require("flat"));
33
33
  const base_js_1 = require("./base.cjs");
34
34
  const document_js_1 = require("../document.cjs");
35
35
  const async_caller_js_1 = require("../util/async_caller.cjs");
36
+ const math_js_1 = require("../util/math.cjs");
36
37
  /**
37
38
  * Class that extends the VectorStore class and provides methods to
38
39
  * interact with the Pinecone vector database.
@@ -166,15 +167,7 @@ class PineconeStore extends base_js_1.VectorStore {
166
167
  throw new Error("Either ids or delete_all must be provided.");
167
168
  }
168
169
  }
169
- /**
170
- * Method that performs a similarity search in the Pinecone database and
171
- * returns the results along with their scores.
172
- * @param query Query vector for the similarity search.
173
- * @param k Number of top results to return.
174
- * @param filter Optional filter to apply to the search.
175
- * @returns Promise that resolves with an array of documents and their scores.
176
- */
177
- async similaritySearchVectorWithScore(query, k, filter) {
170
+ async _runPineconeQuery(query, k, filter, options) {
178
171
  if (filter && this.filter) {
179
172
  throw new Error("cannot provide both `filter` and `this.filter`");
180
173
  }
@@ -185,7 +178,20 @@ class PineconeStore extends base_js_1.VectorStore {
185
178
  topK: k,
186
179
  vector: query,
187
180
  filter: _filter,
181
+ ...options,
188
182
  });
183
+ return results;
184
+ }
185
+ /**
186
+ * Method that performs a similarity search in the Pinecone database and
187
+ * returns the results along with their scores.
188
+ * @param query Query vector for the similarity search.
189
+ * @param k Number of top results to return.
190
+ * @param filter Optional filter to apply to the search.
191
+ * @returns Promise that resolves with an array of documents and their scores.
192
+ */
193
+ async similaritySearchVectorWithScore(query, k, filter) {
194
+ const results = await this._runPineconeQuery(query, k, filter);
189
195
  const result = [];
190
196
  if (results.matches) {
191
197
  for (const res of results.matches) {
@@ -198,6 +204,37 @@ class PineconeStore extends base_js_1.VectorStore {
198
204
  }
199
205
  return result;
200
206
  }
207
+ /**
208
+ * Return documents selected using the maximal marginal relevance.
209
+ * Maximal marginal relevance optimizes for similarity to the query AND diversity
210
+ * among selected documents.
211
+ *
212
+ * @param {string} query - Text to look up documents similar to.
213
+ * @param {number} options.k - Number of documents to return.
214
+ * @param {number} options.fetchK=20 - Number of documents to fetch before passing to the MMR algorithm.
215
+ * @param {number} options.lambda=0.5 - Number between 0 and 1 that determines the degree of diversity among the results,
216
+ * where 0 corresponds to maximum diversity and 1 to minimum diversity.
217
+ * @param {PineconeMetadata} options.filter - Optional filter to apply to the search.
218
+ *
219
+ * @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
220
+ */
221
+ async maxMarginalRelevanceSearch(query, options) {
222
+ const queryEmbedding = await this.embeddings.embedQuery(query);
223
+ const results = await this._runPineconeQuery(queryEmbedding, options.fetchK ?? 20, options.filter, { includeValues: true });
224
+ const matches = results?.matches ?? [];
225
+ const embeddingList = matches.map((match) => match.values);
226
+ const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, options.lambda, options.k);
227
+ const topMmrMatches = mmrIndexes.map((idx) => matches[idx]);
228
+ const finalResult = [];
229
+ for (const res of topMmrMatches) {
230
+ const { [this.textKey]: pageContent, ...metadata } = (res.metadata ??
231
+ {});
232
+ if (res.score) {
233
+ finalResult.push(new document_js_1.Document({ metadata, pageContent }));
234
+ }
235
+ }
236
+ return finalResult;
237
+ }
201
238
  /**
202
239
  * Static method that creates a new instance of the PineconeStore class
203
240
  * from texts.
@@ -1,5 +1,5 @@
1
- import { Index as PineconeIndex } from "@pinecone-database/pinecone";
2
- import { VectorStore } from "./base.js";
1
+ import { RecordMetadata, Index as PineconeIndex } from "@pinecone-database/pinecone";
2
+ import { MaxMarginalRelevanceSearchOptions, VectorStore } from "./base.js";
3
3
  import { Embeddings } from "../embeddings/base.js";
4
4
  import { Document } from "../document.js";
5
5
  import { AsyncCaller } from "../util/async_caller.js";
@@ -57,6 +57,9 @@ export declare class PineconeStore extends VectorStore {
57
57
  * @returns Promise that resolves when the delete operation is complete.
58
58
  */
59
59
  delete(params: PineconeDeleteParams): Promise<void>;
60
+ protected _runPineconeQuery(query: number[], k: number, filter?: PineconeMetadata, options?: {
61
+ includeValues: boolean;
62
+ }): Promise<import("@pinecone-database/pinecone").QueryResponse<RecordMetadata>>;
60
63
  /**
61
64
  * Method that performs a similarity search in the Pinecone database and
62
65
  * returns the results along with their scores.
@@ -66,6 +69,21 @@ export declare class PineconeStore extends VectorStore {
66
69
  * @returns Promise that resolves with an array of documents and their scores.
67
70
  */
68
71
  similaritySearchVectorWithScore(query: number[], k: number, filter?: PineconeMetadata): Promise<[Document, number][]>;
72
+ /**
73
+ * Return documents selected using the maximal marginal relevance.
74
+ * Maximal marginal relevance optimizes for similarity to the query AND diversity
75
+ * among selected documents.
76
+ *
77
+ * @param {string} query - Text to look up documents similar to.
78
+ * @param {number} options.k - Number of documents to return.
79
+ * @param {number} options.fetchK=20 - Number of documents to fetch before passing to the MMR algorithm.
80
+ * @param {number} options.lambda=0.5 - Number between 0 and 1 that determines the degree of diversity among the results,
81
+ * where 0 corresponds to maximum diversity and 1 to minimum diversity.
82
+ * @param {PineconeMetadata} options.filter - Optional filter to apply to the search.
83
+ *
84
+ * @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
85
+ */
86
+ maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>): Promise<Document[]>;
69
87
  /**
70
88
  * Static method that creates a new instance of the PineconeStore class
71
89
  * from texts.
@@ -4,6 +4,7 @@ import flatten from "flat";
4
4
  import { VectorStore } from "./base.js";
5
5
  import { Document } from "../document.js";
6
6
  import { AsyncCaller } from "../util/async_caller.js";
7
+ import { maximalMarginalRelevance } from "../util/math.js";
7
8
  /**
8
9
  * Class that extends the VectorStore class and provides methods to
9
10
  * interact with the Pinecone vector database.
@@ -137,15 +138,7 @@ export class PineconeStore extends VectorStore {
137
138
  throw new Error("Either ids or delete_all must be provided.");
138
139
  }
139
140
  }
140
- /**
141
- * Method that performs a similarity search in the Pinecone database and
142
- * returns the results along with their scores.
143
- * @param query Query vector for the similarity search.
144
- * @param k Number of top results to return.
145
- * @param filter Optional filter to apply to the search.
146
- * @returns Promise that resolves with an array of documents and their scores.
147
- */
148
- async similaritySearchVectorWithScore(query, k, filter) {
141
+ async _runPineconeQuery(query, k, filter, options) {
149
142
  if (filter && this.filter) {
150
143
  throw new Error("cannot provide both `filter` and `this.filter`");
151
144
  }
@@ -156,7 +149,20 @@ export class PineconeStore extends VectorStore {
156
149
  topK: k,
157
150
  vector: query,
158
151
  filter: _filter,
152
+ ...options,
159
153
  });
154
+ return results;
155
+ }
156
+ /**
157
+ * Method that performs a similarity search in the Pinecone database and
158
+ * returns the results along with their scores.
159
+ * @param query Query vector for the similarity search.
160
+ * @param k Number of top results to return.
161
+ * @param filter Optional filter to apply to the search.
162
+ * @returns Promise that resolves with an array of documents and their scores.
163
+ */
164
+ async similaritySearchVectorWithScore(query, k, filter) {
165
+ const results = await this._runPineconeQuery(query, k, filter);
160
166
  const result = [];
161
167
  if (results.matches) {
162
168
  for (const res of results.matches) {
@@ -169,6 +175,37 @@ export class PineconeStore extends VectorStore {
169
175
  }
170
176
  return result;
171
177
  }
178
+ /**
179
+ * Return documents selected using the maximal marginal relevance.
180
+ * Maximal marginal relevance optimizes for similarity to the query AND diversity
181
+ * among selected documents.
182
+ *
183
+ * @param {string} query - Text to look up documents similar to.
184
+ * @param {number} options.k - Number of documents to return.
185
+ * @param {number} options.fetchK=20 - Number of documents to fetch before passing to the MMR algorithm.
186
+ * @param {number} options.lambda=0.5 - Number between 0 and 1 that determines the degree of diversity among the results,
187
+ * where 0 corresponds to maximum diversity and 1 to minimum diversity.
188
+ * @param {PineconeMetadata} options.filter - Optional filter to apply to the search.
189
+ *
190
+ * @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
191
+ */
192
+ async maxMarginalRelevanceSearch(query, options) {
193
+ const queryEmbedding = await this.embeddings.embedQuery(query);
194
+ const results = await this._runPineconeQuery(queryEmbedding, options.fetchK ?? 20, options.filter, { includeValues: true });
195
+ const matches = results?.matches ?? [];
196
+ const embeddingList = matches.map((match) => match.values);
197
+ const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options.lambda, options.k);
198
+ const topMmrMatches = mmrIndexes.map((idx) => matches[idx]);
199
+ const finalResult = [];
200
+ for (const res of topMmrMatches) {
201
+ const { [this.textKey]: pageContent, ...metadata } = (res.metadata ??
202
+ {});
203
+ if (res.score) {
204
+ finalResult.push(new Document({ metadata, pageContent }));
205
+ }
206
+ }
207
+ return finalResult;
208
+ }
172
209
  /**
173
210
  * Static method that creates a new instance of the PineconeStore class
174
211
  * from texts.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.172",
3
+ "version": "0.0.174",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -46,6 +46,12 @@
46
46
  "agents/react/output_parser.cjs",
47
47
  "agents/react/output_parser.js",
48
48
  "agents/react/output_parser.d.ts",
49
+ "agents/xml/output_parser.cjs",
50
+ "agents/xml/output_parser.js",
51
+ "agents/xml/output_parser.d.ts",
52
+ "agents/openai/output_parser.cjs",
53
+ "agents/openai/output_parser.js",
54
+ "agents/openai/output_parser.d.ts",
49
55
  "base_language.cjs",
50
56
  "base_language.js",
51
57
  "base_language.d.ts",
@@ -697,6 +703,9 @@
697
703
  "hub.cjs",
698
704
  "hub.js",
699
705
  "hub.d.ts",
706
+ "util/document.cjs",
707
+ "util/document.js",
708
+ "util/document.d.ts",
700
709
  "util/math.cjs",
701
710
  "util/math.js",
702
711
  "util/math.d.ts",
@@ -837,7 +846,7 @@
837
846
  "apify-client": "^2.7.1",
838
847
  "assemblyai": "^2.0.2",
839
848
  "axios": "^0.26.0",
840
- "cassandra-driver": "^4.6.4",
849
+ "cassandra-driver": "^4.7.2",
841
850
  "cheerio": "^1.0.0-rc.12",
842
851
  "chromadb": "^1.5.3",
843
852
  "closevector-common": "0.1.0-alpha.1",
@@ -953,7 +962,7 @@
953
962
  "apify-client": "^2.7.1",
954
963
  "assemblyai": "^2.0.2",
955
964
  "axios": "*",
956
- "cassandra-driver": "^4.6.4",
965
+ "cassandra-driver": "^4.7.2",
957
966
  "cheerio": "^1.0.0-rc.12",
958
967
  "chromadb": "*",
959
968
  "closevector-common": "0.1.0-alpha.1",
@@ -1391,6 +1400,16 @@
1391
1400
  "import": "./agents/react/output_parser.js",
1392
1401
  "require": "./agents/react/output_parser.cjs"
1393
1402
  },
1403
+ "./agents/xml/output_parser": {
1404
+ "types": "./agents/xml/output_parser.d.ts",
1405
+ "import": "./agents/xml/output_parser.js",
1406
+ "require": "./agents/xml/output_parser.cjs"
1407
+ },
1408
+ "./agents/openai/output_parser": {
1409
+ "types": "./agents/openai/output_parser.d.ts",
1410
+ "import": "./agents/openai/output_parser.js",
1411
+ "require": "./agents/openai/output_parser.cjs"
1412
+ },
1394
1413
  "./base_language": {
1395
1414
  "types": "./base_language.d.ts",
1396
1415
  "import": "./base_language.js",
@@ -2476,6 +2495,11 @@
2476
2495
  "import": "./hub.js",
2477
2496
  "require": "./hub.cjs"
2478
2497
  },
2498
+ "./util/document": {
2499
+ "types": "./util/document.d.ts",
2500
+ "import": "./util/document.js",
2501
+ "require": "./util/document.cjs"
2502
+ },
2479
2503
  "./util/math": {
2480
2504
  "types": "./util/math.d.ts",
2481
2505
  "import": "./util/math.js",
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/util/document.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/util/document.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/util/document.js'