langchain 0.2.12 → 0.2.13
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.
- package/dist/agents/openai_tools/index.cjs +3 -1
- package/dist/agents/openai_tools/index.js +3 -1
- package/dist/agents/openai_tools/output_parser.cjs +1 -1
- package/dist/agents/openai_tools/output_parser.d.ts +1 -1
- package/dist/agents/openai_tools/output_parser.js +1 -1
- package/dist/chat_models/universal.d.ts +2 -2
- package/dist/evaluation/embedding_distance/base.cjs +7 -5
- package/dist/evaluation/embedding_distance/base.js +7 -5
- package/dist/hub.cjs +15 -5
- package/dist/hub.d.ts +18 -3
- package/dist/hub.js +13 -3
- package/dist/tools/sql.cjs +1 -1
- package/dist/tools/sql.js +1 -1
- package/dist/util/ml-distance/distances.cjs +52 -0
- package/dist/util/ml-distance/distances.d.ts +24 -0
- package/dist/util/ml-distance/distances.js +46 -0
- package/dist/util/ml-distance/similarities.cjs +21 -0
- package/dist/util/ml-distance/similarities.d.ts +7 -0
- package/dist/util/ml-distance/similarities.js +17 -0
- package/dist/util/ml-distance-euclidean/euclidean.cjs +15 -0
- package/dist/util/ml-distance-euclidean/euclidean.d.ts +2 -0
- package/dist/util/ml-distance-euclidean/euclidean.js +10 -0
- package/dist/vectorstores/memory.cjs +2 -2
- package/dist/vectorstores/memory.d.ts +3 -3
- package/dist/vectorstores/memory.js +2 -2
- package/package.json +6 -9
|
@@ -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({
|
|
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({
|
|
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:
|
|
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 -
|
|
15
|
-
euclidean:
|
|
16
|
-
manhattan:
|
|
17
|
-
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 -
|
|
12
|
-
euclidean
|
|
13
|
-
manhattan
|
|
14
|
-
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
|
|
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
|
|
16
|
-
|
|
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
|
|
27
|
-
const result = await client.
|
|
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?:
|
|
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?:
|
|
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 "
|
|
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
|
-
|
|
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.
|
|
31
|
+
const result = await client._pullPrompt(ownerRepoCommit, {
|
|
32
|
+
includeModel: options?.includeModel,
|
|
33
|
+
});
|
|
24
34
|
return load(result);
|
|
25
35
|
}
|
package/dist/tools/sql.cjs
CHANGED
|
@@ -172,7 +172,7 @@ class QueryCheckerTool extends tools_1.Tool {
|
|
|
172
172
|
writable: true,
|
|
173
173
|
value: `
|
|
174
174
|
{query}
|
|
175
|
-
Double check the
|
|
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
|
|
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,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;
|
|
@@ -1,9 +1,9 @@
|
|
|
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
7
|
/**
|
|
8
8
|
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
9
9
|
* methods for adding documents, performing similarity searches, and
|
|
@@ -27,7 +27,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
|
|
|
27
27
|
writable: true,
|
|
28
28
|
value: void 0
|
|
29
29
|
});
|
|
30
|
-
this.similarity = similarity ??
|
|
30
|
+
this.similarity = similarity ?? similarities_js_1.cosine;
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* Method to add documents to the memory vector store. It extracts the
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { similarity as ml_distance_similarity } from "ml-distance";
|
|
2
1
|
import { VectorStore } from "@langchain/core/vectorstores";
|
|
3
2
|
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
|
|
4
3
|
import { Document } 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
|
|
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
|
|
31
|
+
similarity: typeof cosine;
|
|
32
32
|
_vectorstoreType(): string;
|
|
33
33
|
constructor(embeddings: EmbeddingsInterface, { similarity, ...rest }?: MemoryVectorStoreArgs);
|
|
34
34
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { similarity as ml_distance_similarity } from "ml-distance";
|
|
2
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
4
|
/**
|
|
5
5
|
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
6
6
|
* methods for adding documents, performing similarity searches, and
|
|
@@ -24,7 +24,7 @@ export class MemoryVectorStore extends VectorStore {
|
|
|
24
24
|
writable: true,
|
|
25
25
|
value: void 0
|
|
26
26
|
});
|
|
27
|
-
this.similarity = similarity ??
|
|
27
|
+
this.similarity = similarity ?? cosine;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Method to add documents to the memory vector store. It extracts the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langchain",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"description": "Typescript bindings for langchain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -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.
|
|
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.
|
|
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": "^
|
|
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.
|
|
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
|
-
"
|
|
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",
|