langchain 0.2.12 → 0.2.14

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.
@@ -1,6 +1,7 @@
1
- import { similarity as ml_distance_similarity } from "ml-distance";
2
- import { VectorStore } from "@langchain/core/vectorstores";
1
+ import { VectorStore, } from "@langchain/core/vectorstores";
3
2
  import { Document } from "@langchain/core/documents";
3
+ import { cosine } from "../util/ml-distance/similarities.js";
4
+ import { maximalMarginalRelevance } from "../util/math.js";
4
5
  /**
5
6
  * Class that extends `VectorStore` to store vectors in memory. Provides
6
7
  * methods for adding documents, performing similarity searches, and
@@ -24,7 +25,7 @@ export class MemoryVectorStore extends VectorStore {
24
25
  writable: true,
25
26
  value: void 0
26
27
  });
27
- this.similarity = similarity ?? ml_distance_similarity.cosine;
28
+ this.similarity = similarity ?? cosine;
28
29
  }
29
30
  /**
30
31
  * Method to add documents to the memory vector store. It extracts the
@@ -53,17 +54,7 @@ export class MemoryVectorStore extends VectorStore {
53
54
  }));
54
55
  this.memoryVectors = this.memoryVectors.concat(memoryVectors);
55
56
  }
56
- /**
57
- * Method to perform a similarity search in the memory vector store. It
58
- * calculates the similarity between the query vector and each vector in
59
- * the store, sorts the results by similarity, and returns the top `k`
60
- * results along with their scores.
61
- * @param query Query vector to compare against the vectors in the store.
62
- * @param k Number of top results to return.
63
- * @param filter Optional filter function to apply to the vectors before performing the search.
64
- * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
65
- */
66
- async similaritySearchVectorWithScore(query, k, filter) {
57
+ async _queryVectors(query, k, filter) {
67
58
  const filterFunction = (memoryVector) => {
68
59
  if (!filter) {
69
60
  return true;
@@ -75,22 +66,48 @@ export class MemoryVectorStore extends VectorStore {
75
66
  return filter(doc);
76
67
  };
77
68
  const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
78
- const searches = filteredMemoryVectors
69
+ return filteredMemoryVectors
79
70
  .map((vector, index) => ({
80
71
  similarity: this.similarity(query, vector.embedding),
81
72
  index,
73
+ metadata: vector.metadata,
74
+ content: vector.content,
75
+ embedding: vector.embedding,
82
76
  }))
83
77
  .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
84
78
  .slice(0, k);
79
+ }
80
+ /**
81
+ * Method to perform a similarity search in the memory vector store. It
82
+ * calculates the similarity between the query vector and each vector in
83
+ * the store, sorts the results by similarity, and returns the top `k`
84
+ * results along with their scores.
85
+ * @param query Query vector to compare against the vectors in the store.
86
+ * @param k Number of top results to return.
87
+ * @param filter Optional filter function to apply to the vectors before performing the search.
88
+ * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
89
+ */
90
+ async similaritySearchVectorWithScore(query, k, filter) {
91
+ const searches = await this._queryVectors(query, k, filter);
85
92
  const result = searches.map((search) => [
86
93
  new Document({
87
- metadata: filteredMemoryVectors[search.index].metadata,
88
- pageContent: filteredMemoryVectors[search.index].content,
94
+ metadata: search.metadata,
95
+ pageContent: search.content,
89
96
  }),
90
97
  search.similarity,
91
98
  ]);
92
99
  return result;
93
100
  }
101
+ async maxMarginalRelevanceSearch(query, options) {
102
+ const queryEmbedding = await this.embeddings.embedQuery(query);
103
+ const searches = await this._queryVectors(queryEmbedding, options.fetchK ?? 20, options.filter);
104
+ const embeddingList = searches.map((searchResp) => searchResp.embedding);
105
+ const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options.lambda, options.k);
106
+ return mmrIndexes.map((idx) => new Document({
107
+ metadata: searches[idx].metadata,
108
+ pageContent: searches[idx].content,
109
+ }));
110
+ }
94
111
  /**
95
112
  * Static method to create a `MemoryVectorStore` instance from an array of
96
113
  * texts. It creates a `Document` for each text and metadata pair, and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -616,8 +616,8 @@
616
616
  "@langchain/anthropic": "^0.2.8",
617
617
  "@langchain/aws": "^0.0.5",
618
618
  "@langchain/cohere": "^0.2.1",
619
- "@langchain/google-genai": "^0.0.23",
620
- "@langchain/google-vertexai": "^0.0.21",
619
+ "@langchain/google-genai": "^0.0.25",
620
+ "@langchain/google-vertexai": "^0.0.25",
621
621
  "@langchain/groq": "^0.0.15",
622
622
  "@langchain/mistralai": "^0.0.26",
623
623
  "@langchain/ollama": "^0.0.2",
@@ -625,12 +625,11 @@
625
625
  "@mendable/firecrawl-js": "^0.0.13",
626
626
  "@notionhq/client": "^2.2.10",
627
627
  "@pinecone-database/pinecone": "^1.1.0",
628
- "@supabase/supabase-js": "^2.10.0",
628
+ "@supabase/supabase-js": "^2.45.0",
629
629
  "@swc/core": "^1.3.90",
630
630
  "@swc/jest": "^0.2.29",
631
631
  "@tsconfig/recommended": "^1.0.2",
632
632
  "@types/d3-dsv": "^2",
633
- "@types/decamelize": "^1.2.0",
634
633
  "@types/handlebars": "^4.1.0",
635
634
  "@types/html-to-text": "^9",
636
635
  "@types/js-yaml": "^4",
@@ -660,7 +659,7 @@
660
659
  "eslint-plugin-jest": "^27.6.0",
661
660
  "eslint-plugin-no-instanceof": "^1.0.1",
662
661
  "eslint-plugin-prettier": "^4.2.1",
663
- "fast-xml-parser": "^4.2.7",
662
+ "fast-xml-parser": "^4.4.1",
664
663
  "handlebars": "^4.7.8",
665
664
  "html-to-text": "^9.0.5",
666
665
  "ignore": "^5.2.0",
@@ -681,7 +680,7 @@
681
680
  "puppeteer": "^22.0.0",
682
681
  "pyodide": "^0.24.1",
683
682
  "redis": "^4.6.6",
684
- "release-it": "^15.10.1",
683
+ "release-it": "^17.6.0",
685
684
  "rimraf": "^5.0.1",
686
685
  "rollup": "^3.19.1",
687
686
  "sonix-speech-recognition": "^2.1.1",
@@ -935,16 +934,14 @@
935
934
  }
936
935
  },
937
936
  "dependencies": {
938
- "@langchain/core": ">=0.2.11 <0.3.0",
937
+ "@langchain/core": ">=0.2.21 <0.3.0",
939
938
  "@langchain/openai": ">=0.1.0 <0.3.0",
940
939
  "@langchain/textsplitters": "~0.0.0",
941
940
  "binary-extensions": "^2.2.0",
942
941
  "js-tiktoken": "^1.0.12",
943
942
  "js-yaml": "^4.1.0",
944
943
  "jsonpointer": "^5.0.1",
945
- "langchainhub": "~0.0.8",
946
- "langsmith": "~0.1.30",
947
- "ml-distance": "^4.0.0",
944
+ "langsmith": "~0.1.40",
948
945
  "openapi-types": "^12.1.3",
949
946
  "p-retry": "4",
950
947
  "uuid": "^10.0.0",