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.
@@ -70,7 +70,9 @@ async function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }) {
70
70
  `Found ${JSON.stringify(prompt.inputVariables)} instead.`,
71
71
  ].join("\n"));
72
72
  }
73
- const modelWithTools = llm.bind({ tools: tools.map(function_calling_1.convertToOpenAITool) });
73
+ const modelWithTools = llm.bind({
74
+ tools: tools.map((tool) => (0, function_calling_1.convertToOpenAITool)(tool)),
75
+ });
74
76
  const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
75
77
  runnables_1.RunnablePassthrough.assign({
76
78
  agent_scratchpad: (input) => (0, openai_tools_js_1.formatToOpenAIToolMessages)(input.steps),
@@ -67,7 +67,9 @@ export async function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnabl
67
67
  `Found ${JSON.stringify(prompt.inputVariables)} instead.`,
68
68
  ].join("\n"));
69
69
  }
70
- const modelWithTools = llm.bind({ tools: tools.map(convertToOpenAITool) });
70
+ const modelWithTools = llm.bind({
71
+ tools: tools.map((tool) => convertToOpenAITool(tool)),
72
+ });
71
73
  const agent = AgentRunnableSequence.fromRunnables([
72
74
  RunnablePassthrough.assign({
73
75
  agent_scratchpad: (input) => formatToOpenAIToolMessages(input.steps),
@@ -23,7 +23,7 @@ const types_js_1 = require("../types.cjs");
23
23
  * new ChatOpenAI({
24
24
  * modelName: "gpt-3.5-turbo-1106",
25
25
  * temperature: 0,
26
- * }).bind({ tools: tools.map(convertToOpenAITool) }),
26
+ * }).bind({ tools: tools.map((tool) => convertToOpenAITool(tool)) }),
27
27
  * new OpenAIToolsAgentOutputParser(),
28
28
  * ]).withConfig({ runName: "OpenAIToolsAgent" });
29
29
  *
@@ -23,7 +23,7 @@ export type { ToolsAgentAction, ToolsAgentStep };
23
23
  * new ChatOpenAI({
24
24
  * modelName: "gpt-3.5-turbo-1106",
25
25
  * temperature: 0,
26
- * }).bind({ tools: tools.map(convertToOpenAITool) }),
26
+ * }).bind({ tools: tools.map((tool) => convertToOpenAITool(tool)) }),
27
27
  * new OpenAIToolsAgentOutputParser(),
28
28
  * ]).withConfig({ runName: "OpenAIToolsAgent" });
29
29
  *
@@ -20,7 +20,7 @@ import { AgentMultiActionOutputParser } from "../types.js";
20
20
  * new ChatOpenAI({
21
21
  * modelName: "gpt-3.5-turbo-1106",
22
22
  * temperature: 0,
23
- * }).bind({ tools: tools.map(convertToOpenAITool) }),
23
+ * }).bind({ tools: tools.map((tool) => convertToOpenAITool(tool)) }),
24
24
  * new OpenAIToolsAgentOutputParser(),
25
25
  * ]).withConfig({ runName: "OpenAIToolsAgent" });
26
26
  *
@@ -1,5 +1,5 @@
1
1
  import { BaseLanguageModelInput, ToolDefinition } from "@langchain/core/language_models/base";
2
- import { BaseChatModel, BaseChatModelParams, type BaseChatModelCallOptions } from "@langchain/core/language_models/chat_models";
2
+ import { BaseChatModel, BaseChatModelParams, BindToolsInput, type BaseChatModelCallOptions } from "@langchain/core/language_models/chat_models";
3
3
  import { BaseMessage, type AIMessageChunk } from "@langchain/core/messages";
4
4
  import { type RunnableBatchOptions, RunnableBinding, type RunnableConfig, type RunnableToolLike } from "@langchain/core/runnables";
5
5
  import { IterableReadableStream } from "@langchain/core/utils/stream";
@@ -62,7 +62,7 @@ declare class _ConfigurableModel<RunInput extends BaseLanguageModelInput = BaseL
62
62
  constructor(fields: ConfigurableModelFields);
63
63
  _model(config?: RunnableConfig): Promise<BaseChatModel<BaseChatModelCallOptions, import("@langchain/core/messages").BaseMessageChunk>>;
64
64
  _generate(messages: BaseMessage[], options?: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
65
- bindTools(tools: (StructuredToolInterface | Record<string, any> | ToolDefinition | RunnableToolLike)[], params?: Record<string, any>): _ConfigurableModel<RunInput, CallOptions>;
65
+ bindTools(tools: BindToolsInput[], params?: Record<string, any>): _ConfigurableModel<RunInput, CallOptions>;
66
66
  withStructuredOutput: BaseChatModel["withStructuredOutput"];
67
67
  _modelParams(config?: RunnableConfig): Record<string, any>;
68
68
  _removePrefix(str: string, prefix: string): string;
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PairwiseEmbeddingDistanceEvalChain = exports.EmbeddingDistanceEvalChain = exports.computeEvaluationScore = exports.getDistanceCalculationFunction = void 0;
4
- const ml_distance_1 = require("ml-distance");
5
4
  const openai_1 = require("@langchain/openai");
6
5
  const base_js_1 = require("../base.cjs");
6
+ const similarities_js_1 = require("../../util/ml-distance/similarities.cjs");
7
+ const distances_js_1 = require("../../util/ml-distance/distances.cjs");
8
+ const euclidean_js_1 = require("../../util/ml-distance-euclidean/euclidean.cjs");
7
9
  /**
8
10
  * Get the distance function for the given distance type.
9
11
  * @param distance The distance type.
@@ -11,10 +13,10 @@ const base_js_1 = require("../base.cjs");
11
13
  */
12
14
  function getDistanceCalculationFunction(distanceType) {
13
15
  const distanceFunctions = {
14
- cosine: (X, Y) => 1.0 - ml_distance_1.similarity.cosine(X, Y),
15
- euclidean: ml_distance_1.distance.euclidean,
16
- manhattan: ml_distance_1.distance.manhattan,
17
- chebyshev: ml_distance_1.distance.chebyshev,
16
+ cosine: (X, Y) => 1.0 - (0, similarities_js_1.cosine)(X, Y),
17
+ euclidean: euclidean_js_1.euclidean,
18
+ manhattan: distances_js_1.manhattan,
19
+ chebyshev: distances_js_1.chebyshev,
18
20
  };
19
21
  return distanceFunctions[distanceType];
20
22
  }
@@ -1,6 +1,8 @@
1
- import { distance, similarity } from "ml-distance";
2
1
  import { OpenAIEmbeddings } from "@langchain/openai";
3
2
  import { PairwiseStringEvaluator, StringEvaluator, } from "../base.js";
3
+ import { cosine } from "../../util/ml-distance/similarities.js";
4
+ import { chebyshev, manhattan } from "../../util/ml-distance/distances.js";
5
+ import { euclidean } from "../../util/ml-distance-euclidean/euclidean.js";
4
6
  /**
5
7
  * Get the distance function for the given distance type.
6
8
  * @param distance The distance type.
@@ -8,10 +10,10 @@ import { PairwiseStringEvaluator, StringEvaluator, } from "../base.js";
8
10
  */
9
11
  export function getDistanceCalculationFunction(distanceType) {
10
12
  const distanceFunctions = {
11
- cosine: (X, Y) => 1.0 - similarity.cosine(X, Y),
12
- euclidean: distance.euclidean,
13
- manhattan: distance.manhattan,
14
- chebyshev: distance.chebyshev,
13
+ cosine: (X, Y) => 1.0 - cosine(X, Y),
14
+ euclidean,
15
+ manhattan,
16
+ chebyshev,
15
17
  };
16
18
  return distanceFunctions[distanceType];
17
19
  }
package/dist/hub.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pull = exports.push = void 0;
4
- const langchainhub_1 = require("langchainhub");
4
+ const langsmith_1 = require("langsmith");
5
5
  const index_js_1 = require("./load/index.cjs");
6
6
  /**
7
7
  * Push a prompt to the hub.
@@ -12,8 +12,16 @@ const index_js_1 = require("./load/index.cjs");
12
12
  * @returns The URL of the newly pushed prompt in the hub.
13
13
  */
14
14
  async function push(repoFullName, runnable, options) {
15
- const client = new langchainhub_1.Client(options);
16
- return client.push(repoFullName, JSON.stringify(runnable), options);
15
+ const client = new langsmith_1.Client(options);
16
+ const payloadOptions = {
17
+ object: runnable,
18
+ parentCommitHash: options?.parentCommitHash,
19
+ isPublic: options?.isPublic ?? options?.newRepoIsPublic,
20
+ description: options?.description ?? options?.newRepoDescription,
21
+ readme: options?.readme,
22
+ tags: options?.tags,
23
+ };
24
+ return client.pushPrompt(repoFullName, payloadOptions);
17
25
  }
18
26
  exports.push = push;
19
27
  /**
@@ -23,8 +31,10 @@ exports.push = push;
23
31
  * @returns
24
32
  */
25
33
  async function pull(ownerRepoCommit, options) {
26
- const client = new langchainhub_1.Client(options);
27
- const result = await client.pull(ownerRepoCommit);
34
+ const client = new langsmith_1.Client(options);
35
+ const result = await client._pullPrompt(ownerRepoCommit, {
36
+ includeModel: options?.includeModel,
37
+ });
28
38
  return (0, index_js_1.load)(result);
29
39
  }
30
40
  exports.pull = pull;
package/dist/hub.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { ClientConfiguration, HubPushOptions } from "langchainhub";
2
1
  import { Runnable } from "@langchain/core/runnables";
3
2
  /**
4
3
  * Push a prompt to the hub.
@@ -8,11 +7,27 @@ import { Runnable } from "@langchain/core/runnables";
8
7
  * @param options
9
8
  * @returns The URL of the newly pushed prompt in the hub.
10
9
  */
11
- export declare function push(repoFullName: string, runnable: Runnable, options?: HubPushOptions & ClientConfiguration): Promise<string>;
10
+ export declare function push(repoFullName: string, runnable: Runnable, options?: {
11
+ apiKey?: string;
12
+ apiUrl?: string;
13
+ parentCommitHash?: string;
14
+ /** @deprecated Use isPublic instead. */
15
+ newRepoIsPublic?: boolean;
16
+ isPublic?: boolean;
17
+ /** @deprecated Use description instead. */
18
+ newRepoDescription?: string;
19
+ description?: string;
20
+ readme?: string;
21
+ tags?: string[];
22
+ }): Promise<string>;
12
23
  /**
13
24
  * Pull a prompt from the hub.
14
25
  * @param ownerRepoCommit The name of the repo containing the prompt, as well as an optional commit hash separated by a slash.
15
26
  * @param options
16
27
  * @returns
17
28
  */
18
- export declare function pull<T extends Runnable>(ownerRepoCommit: string, options?: ClientConfiguration): Promise<T>;
29
+ export declare function pull<T extends Runnable>(ownerRepoCommit: string, options?: {
30
+ apiKey?: string;
31
+ apiUrl?: string;
32
+ includeModel?: boolean;
33
+ }): Promise<T>;
package/dist/hub.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Client } from "langchainhub";
1
+ import { Client } from "langsmith";
2
2
  import { load } from "./load/index.js";
3
3
  /**
4
4
  * Push a prompt to the hub.
@@ -10,7 +10,15 @@ import { load } from "./load/index.js";
10
10
  */
11
11
  export async function push(repoFullName, runnable, options) {
12
12
  const client = new Client(options);
13
- return client.push(repoFullName, JSON.stringify(runnable), options);
13
+ const payloadOptions = {
14
+ object: runnable,
15
+ parentCommitHash: options?.parentCommitHash,
16
+ isPublic: options?.isPublic ?? options?.newRepoIsPublic,
17
+ description: options?.description ?? options?.newRepoDescription,
18
+ readme: options?.readme,
19
+ tags: options?.tags,
20
+ };
21
+ return client.pushPrompt(repoFullName, payloadOptions);
14
22
  }
15
23
  /**
16
24
  * Pull a prompt from the hub.
@@ -20,6 +28,8 @@ export async function push(repoFullName, runnable, options) {
20
28
  */
21
29
  export async function pull(ownerRepoCommit, options) {
22
30
  const client = new Client(options);
23
- const result = await client.pull(ownerRepoCommit);
31
+ const result = await client._pullPrompt(ownerRepoCommit, {
32
+ includeModel: options?.includeModel,
33
+ });
24
34
  return load(result);
25
35
  }
@@ -172,7 +172,7 @@ class QueryCheckerTool extends tools_1.Tool {
172
172
  writable: true,
173
173
  value: `
174
174
  {query}
175
- Double check the sqlite query above for common mistakes, including:
175
+ Double check the SQL query above for common mistakes, including:
176
176
  - Using NOT IN with NULL values
177
177
  - Using UNION when UNION ALL should have been used
178
178
  - Using BETWEEN for exclusive ranges
package/dist/tools/sql.js CHANGED
@@ -166,7 +166,7 @@ export class QueryCheckerTool extends Tool {
166
166
  writable: true,
167
167
  value: `
168
168
  {query}
169
- Double check the sqlite query above for common mistakes, including:
169
+ Double check the SQL query above for common mistakes, including:
170
170
  - Using NOT IN with NULL values
171
171
  - Using UNION when UNION ALL should have been used
172
172
  - Using BETWEEN for exclusive ranges
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.manhattan = exports.chebyshev = exports.innerProduct = void 0;
4
+ /**
5
+ *Returns the Inner Product similarity between vectors a and b
6
+ * @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
7
+ * @param a - first vector
8
+ * @param b - second vector
9
+ *
10
+ */
11
+ function innerProduct(a, b) {
12
+ let ans = 0;
13
+ for (let i = 0; i < a.length; i++) {
14
+ ans += a[i] * b[i];
15
+ }
16
+ return ans;
17
+ }
18
+ exports.innerProduct = innerProduct;
19
+ /**
20
+ *Returns the Chebyshev distance between vectors a and b
21
+ * @link [Chebyshev algorithm](https://en.wikipedia.org/wiki/Chebyshev_distance)
22
+ * @param a - first vector
23
+ * @param b - second vector
24
+ *
25
+ */
26
+ function chebyshev(a, b) {
27
+ let max = 0;
28
+ let aux = 0;
29
+ for (let i = 0; i < a.length; i++) {
30
+ aux = Math.abs(a[i] - b[i]);
31
+ if (max < aux) {
32
+ max = aux;
33
+ }
34
+ }
35
+ return max;
36
+ }
37
+ exports.chebyshev = chebyshev;
38
+ /**
39
+ *Returns the Manhattan distance between vectors a and b
40
+ * @link [Manhattan algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
41
+ * @param a - first vector
42
+ * @param b - second vector
43
+ *
44
+ */
45
+ function manhattan(a, b) {
46
+ let d = 0;
47
+ for (let i = 0; i < a.length; i++) {
48
+ d += Math.abs(a[i] - b[i]);
49
+ }
50
+ return d;
51
+ }
52
+ exports.manhattan = manhattan;
@@ -0,0 +1,24 @@
1
+ /**
2
+ *Returns the Inner Product similarity between vectors a and b
3
+ * @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
4
+ * @param a - first vector
5
+ * @param b - second vector
6
+ *
7
+ */
8
+ export declare function innerProduct(a: number[], b: number[]): number;
9
+ /**
10
+ *Returns the Chebyshev distance between vectors a and b
11
+ * @link [Chebyshev algorithm](https://en.wikipedia.org/wiki/Chebyshev_distance)
12
+ * @param a - first vector
13
+ * @param b - second vector
14
+ *
15
+ */
16
+ export declare function chebyshev(a: number[], b: number[]): number;
17
+ /**
18
+ *Returns the Manhattan distance between vectors a and b
19
+ * @link [Manhattan algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
20
+ * @param a - first vector
21
+ * @param b - second vector
22
+ *
23
+ */
24
+ export declare function manhattan(a: number[], b: number[]): number;
@@ -0,0 +1,46 @@
1
+ /**
2
+ *Returns the Inner Product similarity between vectors a and b
3
+ * @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
4
+ * @param a - first vector
5
+ * @param b - second vector
6
+ *
7
+ */
8
+ export function innerProduct(a, b) {
9
+ let ans = 0;
10
+ for (let i = 0; i < a.length; i++) {
11
+ ans += a[i] * b[i];
12
+ }
13
+ return ans;
14
+ }
15
+ /**
16
+ *Returns the Chebyshev distance between vectors a and b
17
+ * @link [Chebyshev algorithm](https://en.wikipedia.org/wiki/Chebyshev_distance)
18
+ * @param a - first vector
19
+ * @param b - second vector
20
+ *
21
+ */
22
+ export function chebyshev(a, b) {
23
+ let max = 0;
24
+ let aux = 0;
25
+ for (let i = 0; i < a.length; i++) {
26
+ aux = Math.abs(a[i] - b[i]);
27
+ if (max < aux) {
28
+ max = aux;
29
+ }
30
+ }
31
+ return max;
32
+ }
33
+ /**
34
+ *Returns the Manhattan distance between vectors a and b
35
+ * @link [Manhattan algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
36
+ * @param a - first vector
37
+ * @param b - second vector
38
+ *
39
+ */
40
+ export function manhattan(a, b) {
41
+ let d = 0;
42
+ for (let i = 0; i < a.length; i++) {
43
+ d += Math.abs(a[i] - b[i]);
44
+ }
45
+ return d;
46
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cosine = void 0;
4
+ /**
5
+ * Returns the average of cosine distances between vectors a and b
6
+ * @param a - first vector
7
+ * @param b - second vector
8
+ *
9
+ */
10
+ function cosine(a, b) {
11
+ let p = 0;
12
+ let p2 = 0;
13
+ let q2 = 0;
14
+ for (let i = 0; i < a.length; i++) {
15
+ p += a[i] * b[i];
16
+ p2 += a[i] * a[i];
17
+ q2 += b[i] * b[i];
18
+ }
19
+ return p / (Math.sqrt(p2) * Math.sqrt(q2));
20
+ }
21
+ exports.cosine = cosine;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns the average of cosine distances between vectors a and b
3
+ * @param a - first vector
4
+ * @param b - second vector
5
+ *
6
+ */
7
+ export declare function cosine(a: number[], b: number[]): number;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Returns the average of cosine distances between vectors a and b
3
+ * @param a - first vector
4
+ * @param b - second vector
5
+ *
6
+ */
7
+ export function cosine(a, b) {
8
+ let p = 0;
9
+ let p2 = 0;
10
+ let q2 = 0;
11
+ for (let i = 0; i < a.length; i++) {
12
+ p += a[i] * b[i];
13
+ p2 += a[i] * a[i];
14
+ q2 += b[i] * b[i];
15
+ }
16
+ return p / (Math.sqrt(p2) * Math.sqrt(q2));
17
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.euclidean = exports.squaredEuclidean = void 0;
4
+ function squaredEuclidean(p, q) {
5
+ let d = 0;
6
+ for (let i = 0; i < p.length; i++) {
7
+ d += (p[i] - q[i]) * (p[i] - q[i]);
8
+ }
9
+ return d;
10
+ }
11
+ exports.squaredEuclidean = squaredEuclidean;
12
+ function euclidean(p, q) {
13
+ return Math.sqrt(squaredEuclidean(p, q));
14
+ }
15
+ exports.euclidean = euclidean;
@@ -0,0 +1,2 @@
1
+ export declare function squaredEuclidean(p: number[], q: number[]): number;
2
+ export declare function euclidean(p: number[], q: number[]): number;
@@ -0,0 +1,10 @@
1
+ export function squaredEuclidean(p, q) {
2
+ let d = 0;
3
+ for (let i = 0; i < p.length; i++) {
4
+ d += (p[i] - q[i]) * (p[i] - q[i]);
5
+ }
6
+ return d;
7
+ }
8
+ export function euclidean(p, q) {
9
+ return Math.sqrt(squaredEuclidean(p, q));
10
+ }
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MemoryVectorStore = void 0;
4
- const ml_distance_1 = require("ml-distance");
5
4
  const vectorstores_1 = require("@langchain/core/vectorstores");
6
5
  const documents_1 = require("@langchain/core/documents");
6
+ const similarities_js_1 = require("../util/ml-distance/similarities.cjs");
7
+ const math_js_1 = require("../util/math.cjs");
7
8
  /**
8
9
  * Class that extends `VectorStore` to store vectors in memory. Provides
9
10
  * methods for adding documents, performing similarity searches, and
@@ -27,7 +28,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
27
28
  writable: true,
28
29
  value: void 0
29
30
  });
30
- this.similarity = similarity ?? ml_distance_1.similarity.cosine;
31
+ this.similarity = similarity ?? similarities_js_1.cosine;
31
32
  }
32
33
  /**
33
34
  * Method to add documents to the memory vector store. It extracts the
@@ -56,17 +57,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
56
57
  }));
57
58
  this.memoryVectors = this.memoryVectors.concat(memoryVectors);
58
59
  }
59
- /**
60
- * Method to perform a similarity search in the memory vector store. It
61
- * calculates the similarity between the query vector and each vector in
62
- * the store, sorts the results by similarity, and returns the top `k`
63
- * results along with their scores.
64
- * @param query Query vector to compare against the vectors in the store.
65
- * @param k Number of top results to return.
66
- * @param filter Optional filter function to apply to the vectors before performing the search.
67
- * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
68
- */
69
- async similaritySearchVectorWithScore(query, k, filter) {
60
+ async _queryVectors(query, k, filter) {
70
61
  const filterFunction = (memoryVector) => {
71
62
  if (!filter) {
72
63
  return true;
@@ -78,22 +69,48 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
78
69
  return filter(doc);
79
70
  };
80
71
  const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
81
- const searches = filteredMemoryVectors
72
+ return filteredMemoryVectors
82
73
  .map((vector, index) => ({
83
74
  similarity: this.similarity(query, vector.embedding),
84
75
  index,
76
+ metadata: vector.metadata,
77
+ content: vector.content,
78
+ embedding: vector.embedding,
85
79
  }))
86
80
  .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
87
81
  .slice(0, k);
82
+ }
83
+ /**
84
+ * Method to perform a similarity search in the memory vector store. It
85
+ * calculates the similarity between the query vector and each vector in
86
+ * the store, sorts the results by similarity, and returns the top `k`
87
+ * results along with their scores.
88
+ * @param query Query vector to compare against the vectors in the store.
89
+ * @param k Number of top results to return.
90
+ * @param filter Optional filter function to apply to the vectors before performing the search.
91
+ * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
92
+ */
93
+ async similaritySearchVectorWithScore(query, k, filter) {
94
+ const searches = await this._queryVectors(query, k, filter);
88
95
  const result = searches.map((search) => [
89
96
  new documents_1.Document({
90
- metadata: filteredMemoryVectors[search.index].metadata,
91
- pageContent: filteredMemoryVectors[search.index].content,
97
+ metadata: search.metadata,
98
+ pageContent: search.content,
92
99
  }),
93
100
  search.similarity,
94
101
  ]);
95
102
  return result;
96
103
  }
104
+ async maxMarginalRelevanceSearch(query, options) {
105
+ const queryEmbedding = await this.embeddings.embedQuery(query);
106
+ const searches = await this._queryVectors(queryEmbedding, options.fetchK ?? 20, options.filter);
107
+ const embeddingList = searches.map((searchResp) => searchResp.embedding);
108
+ const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, options.lambda, options.k);
109
+ return mmrIndexes.map((idx) => new documents_1.Document({
110
+ metadata: searches[idx].metadata,
111
+ pageContent: searches[idx].content,
112
+ }));
113
+ }
97
114
  /**
98
115
  * Static method to create a `MemoryVectorStore` instance from an array of
99
116
  * texts. It creates a `Document` for each text and metadata pair, and
@@ -1,7 +1,7 @@
1
- import { similarity as ml_distance_similarity } from "ml-distance";
2
- import { VectorStore } from "@langchain/core/vectorstores";
1
+ import { MaxMarginalRelevanceSearchOptions, VectorStore } from "@langchain/core/vectorstores";
3
2
  import type { EmbeddingsInterface } from "@langchain/core/embeddings";
4
- import { Document } from "@langchain/core/documents";
3
+ import { Document, DocumentInterface } from "@langchain/core/documents";
4
+ import { cosine } from "../util/ml-distance/similarities.js";
5
5
  /**
6
6
  * Interface representing a vector in memory. It includes the content
7
7
  * (text), the corresponding embedding (vector), and any associated
@@ -18,7 +18,7 @@ interface MemoryVector {
18
18
  * function.
19
19
  */
20
20
  export interface MemoryVectorStoreArgs {
21
- similarity?: typeof ml_distance_similarity.cosine;
21
+ similarity?: typeof cosine;
22
22
  }
23
23
  /**
24
24
  * Class that extends `VectorStore` to store vectors in memory. Provides
@@ -28,7 +28,7 @@ export interface MemoryVectorStoreArgs {
28
28
  export declare class MemoryVectorStore extends VectorStore {
29
29
  FilterType: (doc: Document) => boolean;
30
30
  memoryVectors: MemoryVector[];
31
- similarity: typeof ml_distance_similarity.cosine;
31
+ similarity: typeof cosine;
32
32
  _vectorstoreType(): string;
33
33
  constructor(embeddings: EmbeddingsInterface, { similarity, ...rest }?: MemoryVectorStoreArgs);
34
34
  /**
@@ -48,6 +48,13 @@ export declare class MemoryVectorStore extends VectorStore {
48
48
  * @returns Promise that resolves when all vectors have been added.
49
49
  */
50
50
  addVectors(vectors: number[][], documents: Document[]): Promise<void>;
51
+ protected _queryVectors(query: number[], k: number, filter?: this["FilterType"]): Promise<{
52
+ similarity: number;
53
+ index: number;
54
+ metadata: Record<string, any>;
55
+ content: string;
56
+ embedding: number[];
57
+ }[]>;
51
58
  /**
52
59
  * Method to perform a similarity search in the memory vector store. It
53
60
  * calculates the similarity between the query vector and each vector in
@@ -59,6 +66,7 @@ export declare class MemoryVectorStore extends VectorStore {
59
66
  * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
60
67
  */
61
68
  similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
69
+ maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>): Promise<DocumentInterface[]>;
62
70
  /**
63
71
  * Static method to create a `MemoryVectorStore` instance from an array of
64
72
  * texts. It creates a `Document` for each text and metadata pair, and