@langchain/core 0.2.0 → 0.2.1
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/callbacks/tests/callbacks.test.d.ts +1 -0
- package/dist/callbacks/tests/callbacks.test.js +492 -0
- package/dist/callbacks/tests/manager.int.test.d.ts +1 -0
- package/dist/callbacks/tests/manager.int.test.js +29 -0
- package/dist/callbacks/tests/run_collector.test.d.ts +1 -0
- package/dist/callbacks/tests/run_collector.test.js +58 -0
- package/dist/chat_history.cjs +13 -0
- package/dist/chat_history.d.ts +9 -0
- package/dist/chat_history.js +13 -0
- package/dist/language_models/chat_models.d.ts +2 -2
- package/dist/language_models/tests/chat_models.test.d.ts +1 -0
- package/dist/language_models/tests/chat_models.test.js +154 -0
- package/dist/language_models/tests/count_tokens.test.d.ts +1 -0
- package/dist/language_models/tests/count_tokens.test.js +19 -0
- package/dist/language_models/tests/llms.test.d.ts +1 -0
- package/dist/language_models/tests/llms.test.js +39 -0
- package/dist/messages/tests/base_message.test.d.ts +1 -0
- package/dist/messages/tests/base_message.test.js +97 -0
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +1 -0
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +81 -0
- package/dist/output_parsers/tests/json.test.d.ts +1 -0
- package/dist/output_parsers/tests/json.test.js +427 -0
- package/dist/output_parsers/tests/output_parser.test.d.ts +1 -0
- package/dist/output_parsers/tests/output_parser.test.js +78 -0
- package/dist/output_parsers/tests/string.test.d.ts +1 -0
- package/dist/output_parsers/tests/string.test.js +68 -0
- package/dist/output_parsers/tests/structured.test.d.ts +1 -0
- package/dist/output_parsers/tests/structured.test.js +166 -0
- package/dist/output_parsers/tests/xml.test.d.ts +1 -0
- package/dist/output_parsers/tests/xml.test.js +81 -0
- package/dist/prompts/tests/chat.mustache.test.d.ts +1 -0
- package/dist/prompts/tests/chat.mustache.test.js +61 -0
- package/dist/prompts/tests/chat.test.d.ts +1 -0
- package/dist/prompts/tests/chat.test.js +507 -0
- package/dist/prompts/tests/few_shot.test.d.ts +1 -0
- package/dist/prompts/tests/few_shot.test.js +224 -0
- package/dist/prompts/tests/pipeline.test.d.ts +1 -0
- package/dist/prompts/tests/pipeline.test.js +101 -0
- package/dist/prompts/tests/prompt.mustache.test.d.ts +1 -0
- package/dist/prompts/tests/prompt.mustache.test.js +85 -0
- package/dist/prompts/tests/prompt.test.d.ts +1 -0
- package/dist/prompts/tests/prompt.test.js +78 -0
- package/dist/prompts/tests/structured.test.d.ts +1 -0
- package/dist/prompts/tests/structured.test.js +37 -0
- package/dist/prompts/tests/template.test.d.ts +1 -0
- package/dist/prompts/tests/template.test.js +24 -0
- package/dist/runnables/base.cjs +87 -6
- package/dist/runnables/base.d.ts +45 -26
- package/dist/runnables/base.js +87 -6
- package/dist/runnables/history.cjs +87 -32
- package/dist/runnables/history.d.ts +1 -1
- package/dist/runnables/history.js +87 -32
- package/dist/runnables/remote.cjs +2 -2
- package/dist/runnables/remote.d.ts +3 -3
- package/dist/runnables/remote.js +2 -2
- package/dist/runnables/tests/runnable.test.d.ts +1 -0
- package/dist/runnables/tests/runnable.test.js +491 -0
- package/dist/runnables/tests/runnable_binding.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_binding.test.js +46 -0
- package/dist/runnables/tests/runnable_branch.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_branch.test.js +116 -0
- package/dist/runnables/tests/runnable_graph.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_graph.test.js +84 -0
- package/dist/runnables/tests/runnable_history.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_history.test.js +177 -0
- package/dist/runnables/tests/runnable_interface.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_interface.test.js +209 -0
- package/dist/runnables/tests/runnable_map.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_map.test.js +238 -0
- package/dist/runnables/tests/runnable_passthrough.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_passthrough.test.js +96 -0
- package/dist/runnables/tests/runnable_remote.int.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_remote.int.test.js +138 -0
- package/dist/runnables/tests/runnable_remote.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_remote.test.js +200 -0
- package/dist/runnables/tests/runnable_retry.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_retry.test.js +125 -0
- package/dist/runnables/tests/runnable_stream_events.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_stream_events.test.js +1013 -0
- package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +973 -0
- package/dist/runnables/tests/runnable_stream_log.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_stream_log.test.js +282 -0
- package/dist/runnables/tests/runnable_tracing.int.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_tracing.int.test.js +37 -0
- package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_with_fallbacks.test.js +36 -0
- package/dist/runnables/utils.d.ts +1 -1
- package/dist/singletons/tests/async_local_storage.test.d.ts +1 -0
- package/dist/singletons/tests/async_local_storage.test.js +120 -0
- package/dist/structured_query/tests/utils.test.d.ts +1 -0
- package/dist/structured_query/tests/utils.test.js +47 -0
- package/dist/tracers/event_stream.cjs +493 -0
- package/dist/tracers/event_stream.d.ts +137 -0
- package/dist/tracers/event_stream.js +489 -0
- package/dist/tracers/log_stream.d.ts +2 -77
- package/dist/tracers/tests/langchain_tracer.int.test.d.ts +1 -0
- package/dist/tracers/tests/langchain_tracer.int.test.js +74 -0
- package/dist/tracers/tests/tracer.test.d.ts +1 -0
- package/dist/tracers/tests/tracer.test.js +378 -0
- package/dist/utils/testing/tests/chatfake.test.d.ts +1 -0
- package/dist/utils/testing/tests/chatfake.test.js +112 -0
- package/dist/utils/tests/async_caller.test.d.ts +1 -0
- package/dist/utils/tests/async_caller.test.js +27 -0
- package/dist/utils/tests/enviroment.test.d.ts +1 -0
- package/dist/utils/tests/enviroment.test.js +6 -0
- package/dist/utils/tests/function_calling.test.d.ts +1 -0
- package/dist/utils/tests/function_calling.test.js +107 -0
- package/dist/utils/tests/math_utils.test.d.ts +1 -0
- package/dist/utils/tests/math_utils.test.js +139 -0
- package/dist/utils/tests/polyfill_stream.test.d.ts +1 -0
- package/dist/utils/tests/polyfill_stream.test.js +15 -0
- package/package.json +6 -6
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { test, expect } from "@jest/globals";
|
|
2
|
+
import { Matrix } from "ml-matrix";
|
|
3
|
+
import { cosineSimilarity, euclideanDistance, innerProduct, maximalMarginalRelevance, normalize, } from "../math.js";
|
|
4
|
+
test("Test cosine similarity zero", async () => {
|
|
5
|
+
const X = Matrix.rand(3, 3).to2DArray();
|
|
6
|
+
const Y = Matrix.zeros(3, 3).to2DArray();
|
|
7
|
+
const expected = Matrix.zeros(3, 3).to2DArray();
|
|
8
|
+
const actual = cosineSimilarity(X, Y);
|
|
9
|
+
expect(actual).toEqual(expected);
|
|
10
|
+
});
|
|
11
|
+
test("Test cosine similarity identity", async () => {
|
|
12
|
+
const X = Matrix.rand(4, 4).to2DArray();
|
|
13
|
+
const actual = cosineSimilarity(X, X);
|
|
14
|
+
// Diagonal is expected to be [1, 1, 1, 1]
|
|
15
|
+
for (let i = 0; i < 4; i += 1) {
|
|
16
|
+
expect(actual[i][i]).toBeCloseTo(1);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
test("Test cosine similarity", async () => {
|
|
20
|
+
const X = [
|
|
21
|
+
[1.0, 2.0, 3.0],
|
|
22
|
+
[0.0, 1.0, 0.0],
|
|
23
|
+
[1.0, 2.0, 0.0],
|
|
24
|
+
];
|
|
25
|
+
const Y = [
|
|
26
|
+
[0.5, 1.0, 1.5],
|
|
27
|
+
[1.0, 0.0, 0.0],
|
|
28
|
+
[2.0, 5.0, 2.0],
|
|
29
|
+
[0.0, 0.0, 0.0],
|
|
30
|
+
];
|
|
31
|
+
const expected = [
|
|
32
|
+
[1, 0.2672612419124244, 0.8374357893586237, 0],
|
|
33
|
+
[0.5345224838248488, 0, 0.8703882797784892, 0],
|
|
34
|
+
[0.5976143046671968, 0.4472135954999579, 0.9341987329938275, 0],
|
|
35
|
+
];
|
|
36
|
+
const actual = cosineSimilarity(X, Y);
|
|
37
|
+
expect(actual).toEqual(expected);
|
|
38
|
+
});
|
|
39
|
+
test("Test cosine similarity empty", async () => {
|
|
40
|
+
const X = [[]];
|
|
41
|
+
const Y = Matrix.rand(3, 3).to2DArray();
|
|
42
|
+
expect(cosineSimilarity(X, X)).toEqual([[]]);
|
|
43
|
+
expect(cosineSimilarity(X, Y)).toEqual([[]]);
|
|
44
|
+
});
|
|
45
|
+
test("Test cosine similarity wrong shape", async () => {
|
|
46
|
+
const X = Matrix.rand(2, 2).to2DArray();
|
|
47
|
+
const Y = Matrix.rand(2, 4).to2DArray();
|
|
48
|
+
expect(() => cosineSimilarity(X, Y)).toThrowError();
|
|
49
|
+
});
|
|
50
|
+
test("Test cosine similarity different shape", async () => {
|
|
51
|
+
const X = Matrix.rand(2, 2).to2DArray();
|
|
52
|
+
const Y = Matrix.rand(4, 2).to2DArray();
|
|
53
|
+
expect(() => cosineSimilarity(X, Y)).not.toThrowError();
|
|
54
|
+
});
|
|
55
|
+
test("Test maximal marginal relevance lambda zero", async () => {
|
|
56
|
+
const queryEmbedding = Matrix.rand(5, 1).to1DArray();
|
|
57
|
+
const zeros = Matrix.zeros(5, 1).to1DArray();
|
|
58
|
+
const embeddingList = [queryEmbedding, queryEmbedding, zeros];
|
|
59
|
+
const expected = [0, 2];
|
|
60
|
+
const actual = maximalMarginalRelevance(queryEmbedding, embeddingList, 0, 2);
|
|
61
|
+
expect(actual).toEqual(expected);
|
|
62
|
+
});
|
|
63
|
+
test("Test maximal marginal relevance lambda one", async () => {
|
|
64
|
+
const queryEmbedding = Matrix.rand(5, 1).to1DArray();
|
|
65
|
+
const zeros = Matrix.zeros(5, 1).to1DArray();
|
|
66
|
+
const embeddingList = [queryEmbedding, queryEmbedding, zeros];
|
|
67
|
+
const expected = [0, 1];
|
|
68
|
+
const actual = maximalMarginalRelevance(queryEmbedding, embeddingList, 1, 2);
|
|
69
|
+
expect(actual).toEqual(expected);
|
|
70
|
+
});
|
|
71
|
+
test("Test maximal marginal relevance", async () => {
|
|
72
|
+
// Vectors that are 30, 45 and 75 degrees from query vector (cosine similarity of
|
|
73
|
+
// 0.87, 0.71, 0.26) and the latter two are 15 and 60 degree from the first
|
|
74
|
+
// (cosine similarity 0.97 and 0.71). So for 3rd vector be chosen, must be case that
|
|
75
|
+
// 0.71lambda - 0.97(1 - lambda) < 0.26lambda - 0.71(1-lambda) -> lambda ~< .26 / .71
|
|
76
|
+
const queryEmbedding = [1, 0];
|
|
77
|
+
const embeddingList = [
|
|
78
|
+
[3 ** 0.5, 1],
|
|
79
|
+
[1, 1],
|
|
80
|
+
[1, 2 + 3 ** 0.5],
|
|
81
|
+
];
|
|
82
|
+
let expected = [0, 2];
|
|
83
|
+
let actual = maximalMarginalRelevance(queryEmbedding, embeddingList, 25 / 71, 2);
|
|
84
|
+
expect(actual).toEqual(expected);
|
|
85
|
+
expected = [0, 1];
|
|
86
|
+
actual = maximalMarginalRelevance(queryEmbedding, embeddingList, 27 / 71, 2);
|
|
87
|
+
expect(actual).toEqual(expected);
|
|
88
|
+
});
|
|
89
|
+
test("Test maximal marginal relevance query dim", async () => {
|
|
90
|
+
const randomVector = Matrix.rand(5, 1);
|
|
91
|
+
const queryEmbedding = randomVector.to1DArray();
|
|
92
|
+
const queryEmbedding2D = randomVector.transpose().to2DArray();
|
|
93
|
+
const embeddingList = Matrix.rand(4, 5).to2DArray();
|
|
94
|
+
const first = maximalMarginalRelevance(queryEmbedding, embeddingList, 1, 2);
|
|
95
|
+
const second = maximalMarginalRelevance(queryEmbedding2D, embeddingList, 1, 2);
|
|
96
|
+
expect(first).toEqual(second);
|
|
97
|
+
});
|
|
98
|
+
test("Test maximal marginal relevance has no duplicates", async () => {
|
|
99
|
+
const queryEmbedding = Matrix.rand(1, 1536).to1DArray();
|
|
100
|
+
const embeddingList = Matrix.rand(200, 1536).to2DArray();
|
|
101
|
+
const actual = maximalMarginalRelevance(queryEmbedding, embeddingList, 0.5, 200);
|
|
102
|
+
const expected = new Set(actual).size;
|
|
103
|
+
expect(actual).toHaveLength(expected);
|
|
104
|
+
});
|
|
105
|
+
test("Test normalize", async () => {
|
|
106
|
+
const input = [
|
|
107
|
+
[1, 2],
|
|
108
|
+
[3, 4],
|
|
109
|
+
];
|
|
110
|
+
const expected = [
|
|
111
|
+
[0.25, 0.5],
|
|
112
|
+
[0.75, 1],
|
|
113
|
+
];
|
|
114
|
+
const actual = normalize(input);
|
|
115
|
+
expect(actual).toEqual(expected);
|
|
116
|
+
});
|
|
117
|
+
test("Test innerProduct", async () => {
|
|
118
|
+
const x = [
|
|
119
|
+
[1, 2],
|
|
120
|
+
[5, 6],
|
|
121
|
+
];
|
|
122
|
+
const y = [
|
|
123
|
+
[3, 4],
|
|
124
|
+
[7, 8],
|
|
125
|
+
];
|
|
126
|
+
const expected = [
|
|
127
|
+
[11, 23],
|
|
128
|
+
[39, 83],
|
|
129
|
+
];
|
|
130
|
+
const actual = innerProduct(x, y);
|
|
131
|
+
expect(actual).toEqual(expected);
|
|
132
|
+
});
|
|
133
|
+
test("Test distance", async () => {
|
|
134
|
+
const x = [[1, 2]];
|
|
135
|
+
const y = [[2, 4]];
|
|
136
|
+
const expected = [[2.23606797749979]];
|
|
137
|
+
const actual = euclideanDistance(x, y);
|
|
138
|
+
expect(actual[0][0]).toBeCloseTo(expected[0][0]);
|
|
139
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "web-streams-polyfill";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "web-streams-polyfill";
|
|
2
|
+
import { test, expect } from "@jest/globals";
|
|
3
|
+
import { FakeStreamingLLM } from "../testing/index.js";
|
|
4
|
+
import { StringOutputParser } from "../../output_parsers/string.js";
|
|
5
|
+
test("Stream the entire way through", async () => {
|
|
6
|
+
const llm = new FakeStreamingLLM({});
|
|
7
|
+
const stream = await llm.pipe(new StringOutputParser()).stream("Hi there!");
|
|
8
|
+
const chunks = [];
|
|
9
|
+
for await (const chunk of stream) {
|
|
10
|
+
chunks.push(chunk);
|
|
11
|
+
console.log(chunk);
|
|
12
|
+
}
|
|
13
|
+
expect(chunks.length).toEqual("Hi there!".length);
|
|
14
|
+
expect(chunks.join("")).toEqual("Hi there!");
|
|
15
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -14,8 +14,10 @@
|
|
|
14
14
|
},
|
|
15
15
|
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/langchain-core/",
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "yarn
|
|
18
|
-
"build:
|
|
17
|
+
"build": "yarn turbo:command build:internal --filter=@langchain/core",
|
|
18
|
+
"build:internal": "yarn lc-build:v2 --create-entrypoints --pre --tree-shaking",
|
|
19
|
+
"clean": "rm -rf .turbo dist/",
|
|
20
|
+
"build:deps": "yarn turbo build",
|
|
19
21
|
"build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests",
|
|
20
22
|
"build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs",
|
|
21
23
|
"build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch",
|
|
@@ -24,7 +26,6 @@
|
|
|
24
26
|
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
25
27
|
"lint": "yarn lint:eslint && yarn lint:dpdm",
|
|
26
28
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
|
|
27
|
-
"clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre",
|
|
28
29
|
"prepack": "yarn build",
|
|
29
30
|
"release": "release-it --only-version --config .release-it.json",
|
|
30
31
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
|
|
@@ -55,7 +56,7 @@
|
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
58
|
"@jest/globals": "^29.5.0",
|
|
58
|
-
"@langchain/scripts": "~0.0",
|
|
59
|
+
"@langchain/scripts": "~0.0.14",
|
|
59
60
|
"@swc/core": "^1.3.90",
|
|
60
61
|
"@swc/jest": "^0.2.29",
|
|
61
62
|
"@types/mustache": "^4",
|
|
@@ -73,7 +74,6 @@
|
|
|
73
74
|
"prettier": "^2.8.3",
|
|
74
75
|
"release-it": "^15.10.1",
|
|
75
76
|
"rimraf": "^5.0.1",
|
|
76
|
-
"turbo": "latest",
|
|
77
77
|
"typescript": "~5.1.6",
|
|
78
78
|
"web-streams-polyfill": "^3.3.3"
|
|
79
79
|
},
|