@langchain/core 0.2.0-rc.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/base.cjs +9 -1
- package/dist/callbacks/base.d.ts +3 -0
- package/dist/callbacks/base.js +9 -1
- package/dist/callbacks/manager.cjs +51 -0
- package/dist/callbacks/manager.js +51 -0
- 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/base.cjs +3 -0
- package/dist/language_models/base.js +3 -0
- package/dist/language_models/chat_models.cjs +21 -3
- package/dist/language_models/chat_models.d.ts +11 -2
- package/dist/language_models/chat_models.js +21 -3
- 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 +174 -19
- package/dist/runnables/base.d.ts +47 -28
- package/dist/runnables/base.js +174 -19
- 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/iter.cjs +46 -0
- package/dist/runnables/iter.d.ts +5 -0
- package/dist/runnables/iter.js +39 -0
- package/dist/runnables/passthrough.cjs +1 -0
- package/dist/runnables/passthrough.d.ts +1 -1
- package/dist/runnables/passthrough.js +1 -0
- package/dist/runnables/remote.cjs +60 -48
- package/dist/runnables/remote.d.ts +8 -4
- package/dist/runnables/remote.js +61 -49
- 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/index.cjs +1 -1
- package/dist/singletons/index.d.ts +2 -2
- package/dist/singletons/index.js +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/stream.cjs +27 -11
- package/dist/utils/stream.d.ts +6 -1
- package/dist/utils/stream.js +27 -11
- 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 +7 -7
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { test, expect, jest } from "@jest/globals";
|
|
2
|
+
import * as uuid from "uuid";
|
|
3
|
+
import { Document } from "../../documents/document.js";
|
|
4
|
+
import { HumanMessage } from "../../messages/index.js";
|
|
5
|
+
import { FakeTracer } from "../../utils/testing/index.js";
|
|
6
|
+
const _DATE = 1620000000000;
|
|
7
|
+
Date.now = jest.fn(() => _DATE);
|
|
8
|
+
const serialized = {
|
|
9
|
+
lc: 1,
|
|
10
|
+
type: "constructor",
|
|
11
|
+
id: ["test"],
|
|
12
|
+
kwargs: {},
|
|
13
|
+
};
|
|
14
|
+
test("Test LLMRun", async () => {
|
|
15
|
+
const tracer = new FakeTracer();
|
|
16
|
+
const runId = uuid.v4();
|
|
17
|
+
await tracer.handleLLMStart(serialized, ["test"], runId);
|
|
18
|
+
await tracer.handleLLMEnd({ generations: [] }, runId);
|
|
19
|
+
expect(tracer.runs.length).toBe(1);
|
|
20
|
+
const run = tracer.runs[0];
|
|
21
|
+
const compareRun = {
|
|
22
|
+
id: runId,
|
|
23
|
+
name: "test",
|
|
24
|
+
start_time: _DATE,
|
|
25
|
+
end_time: _DATE,
|
|
26
|
+
execution_order: 1,
|
|
27
|
+
child_execution_order: 1,
|
|
28
|
+
serialized,
|
|
29
|
+
events: [
|
|
30
|
+
{
|
|
31
|
+
name: "start",
|
|
32
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "end",
|
|
36
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
inputs: { prompts: ["test"] },
|
|
40
|
+
run_type: "llm",
|
|
41
|
+
outputs: { generations: [] },
|
|
42
|
+
child_runs: [],
|
|
43
|
+
extra: {},
|
|
44
|
+
tags: [],
|
|
45
|
+
dotted_order: `20210503T000000000001Z${runId}`,
|
|
46
|
+
trace_id: runId,
|
|
47
|
+
};
|
|
48
|
+
expect(run).toEqual(compareRun);
|
|
49
|
+
});
|
|
50
|
+
test("Test Chat Model Run", async () => {
|
|
51
|
+
const tracer = new FakeTracer();
|
|
52
|
+
const runId = uuid.v4();
|
|
53
|
+
const messages = [[new HumanMessage("Avast")]];
|
|
54
|
+
await tracer.handleChatModelStart(serialized, messages, runId);
|
|
55
|
+
await tracer.handleLLMEnd({ generations: [] }, runId);
|
|
56
|
+
expect(tracer.runs.length).toBe(1);
|
|
57
|
+
const run = tracer.runs[0];
|
|
58
|
+
expect(run).toMatchInlineSnapshot({
|
|
59
|
+
id: expect.any(String),
|
|
60
|
+
}, `
|
|
61
|
+
{
|
|
62
|
+
"child_execution_order": 1,
|
|
63
|
+
"child_runs": [],
|
|
64
|
+
"dotted_order": "20210503T000000000001Z${runId}",
|
|
65
|
+
"end_time": 1620000000000,
|
|
66
|
+
"events": [
|
|
67
|
+
{
|
|
68
|
+
"name": "start",
|
|
69
|
+
"time": "2021-05-03T00:00:00.000Z",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"name": "end",
|
|
73
|
+
"time": "2021-05-03T00:00:00.000Z",
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
"execution_order": 1,
|
|
77
|
+
"extra": {},
|
|
78
|
+
"id": Any<String>,
|
|
79
|
+
"inputs": {
|
|
80
|
+
"messages": [
|
|
81
|
+
[
|
|
82
|
+
{
|
|
83
|
+
"id": [
|
|
84
|
+
"langchain_core",
|
|
85
|
+
"messages",
|
|
86
|
+
"HumanMessage",
|
|
87
|
+
],
|
|
88
|
+
"kwargs": {
|
|
89
|
+
"additional_kwargs": {},
|
|
90
|
+
"content": "Avast",
|
|
91
|
+
"response_metadata": {},
|
|
92
|
+
},
|
|
93
|
+
"lc": 1,
|
|
94
|
+
"type": "constructor",
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
"name": "test",
|
|
100
|
+
"outputs": {
|
|
101
|
+
"generations": [],
|
|
102
|
+
},
|
|
103
|
+
"parent_run_id": undefined,
|
|
104
|
+
"run_type": "llm",
|
|
105
|
+
"serialized": {
|
|
106
|
+
"id": [
|
|
107
|
+
"test",
|
|
108
|
+
],
|
|
109
|
+
"kwargs": {},
|
|
110
|
+
"lc": 1,
|
|
111
|
+
"type": "constructor",
|
|
112
|
+
},
|
|
113
|
+
"start_time": 1620000000000,
|
|
114
|
+
"tags": [],
|
|
115
|
+
"trace_id": "${runId}",
|
|
116
|
+
}
|
|
117
|
+
`);
|
|
118
|
+
});
|
|
119
|
+
test("Test LLM Run no start", async () => {
|
|
120
|
+
const tracer = new FakeTracer();
|
|
121
|
+
const runId = uuid.v4();
|
|
122
|
+
await expect(tracer.handleLLMEnd({ generations: [] }, runId)).rejects.toThrow("No LLM run to end");
|
|
123
|
+
});
|
|
124
|
+
test("Test Chain Run", async () => {
|
|
125
|
+
const tracer = new FakeTracer();
|
|
126
|
+
const runId = uuid.v4();
|
|
127
|
+
const compareRun = {
|
|
128
|
+
id: runId,
|
|
129
|
+
name: "test",
|
|
130
|
+
start_time: _DATE,
|
|
131
|
+
end_time: _DATE,
|
|
132
|
+
execution_order: 1,
|
|
133
|
+
child_execution_order: 1,
|
|
134
|
+
serialized,
|
|
135
|
+
events: [
|
|
136
|
+
{
|
|
137
|
+
name: "start",
|
|
138
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
name: "end",
|
|
142
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
inputs: { foo: "bar" },
|
|
146
|
+
outputs: { foo: "bar" },
|
|
147
|
+
run_type: "chain",
|
|
148
|
+
child_runs: [],
|
|
149
|
+
extra: {},
|
|
150
|
+
tags: [],
|
|
151
|
+
dotted_order: `20210503T000000000001Z${runId}`,
|
|
152
|
+
trace_id: runId,
|
|
153
|
+
};
|
|
154
|
+
await tracer.handleChainStart(serialized, { foo: "bar" }, runId);
|
|
155
|
+
await tracer.handleChainEnd({ foo: "bar" }, runId);
|
|
156
|
+
expect(tracer.runs.length).toBe(1);
|
|
157
|
+
const run = tracer.runs[0];
|
|
158
|
+
expect(run).toEqual(compareRun);
|
|
159
|
+
});
|
|
160
|
+
test("Test Tool Run", async () => {
|
|
161
|
+
const tracer = new FakeTracer();
|
|
162
|
+
const runId = uuid.v4();
|
|
163
|
+
const compareRun = {
|
|
164
|
+
id: runId,
|
|
165
|
+
name: "test",
|
|
166
|
+
start_time: _DATE,
|
|
167
|
+
end_time: _DATE,
|
|
168
|
+
execution_order: 1,
|
|
169
|
+
child_execution_order: 1,
|
|
170
|
+
serialized,
|
|
171
|
+
events: [
|
|
172
|
+
{
|
|
173
|
+
name: "start",
|
|
174
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "end",
|
|
178
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
inputs: { input: "test" },
|
|
182
|
+
outputs: { output: "output" },
|
|
183
|
+
run_type: "tool",
|
|
184
|
+
child_runs: [],
|
|
185
|
+
extra: {},
|
|
186
|
+
tags: [],
|
|
187
|
+
dotted_order: `20210503T000000000001Z${runId}`,
|
|
188
|
+
trace_id: runId,
|
|
189
|
+
};
|
|
190
|
+
await tracer.handleToolStart(serialized, "test", runId);
|
|
191
|
+
await tracer.handleToolEnd("output", runId);
|
|
192
|
+
expect(tracer.runs.length).toBe(1);
|
|
193
|
+
const run = tracer.runs[0];
|
|
194
|
+
expect(run).toEqual(compareRun);
|
|
195
|
+
});
|
|
196
|
+
test("Test Retriever Run", async () => {
|
|
197
|
+
const tracer = new FakeTracer();
|
|
198
|
+
const runId = uuid.v4();
|
|
199
|
+
const document = new Document({
|
|
200
|
+
pageContent: "test",
|
|
201
|
+
metadata: { test: "test" },
|
|
202
|
+
});
|
|
203
|
+
const compareRun = {
|
|
204
|
+
id: runId,
|
|
205
|
+
name: "test",
|
|
206
|
+
start_time: _DATE,
|
|
207
|
+
end_time: _DATE,
|
|
208
|
+
execution_order: 1,
|
|
209
|
+
child_execution_order: 1,
|
|
210
|
+
serialized,
|
|
211
|
+
events: [
|
|
212
|
+
{
|
|
213
|
+
name: "start",
|
|
214
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
name: "end",
|
|
218
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
inputs: { query: "bar" },
|
|
222
|
+
outputs: { documents: [document] },
|
|
223
|
+
run_type: "retriever",
|
|
224
|
+
child_runs: [],
|
|
225
|
+
extra: {},
|
|
226
|
+
tags: [],
|
|
227
|
+
dotted_order: `20210503T000000000001Z${runId}`,
|
|
228
|
+
trace_id: runId,
|
|
229
|
+
};
|
|
230
|
+
await tracer.handleRetrieverStart(serialized, "bar", runId);
|
|
231
|
+
await tracer.handleRetrieverEnd([document], runId);
|
|
232
|
+
expect(tracer.runs.length).toBe(1);
|
|
233
|
+
const run = tracer.runs[0];
|
|
234
|
+
expect(run).toEqual(compareRun);
|
|
235
|
+
});
|
|
236
|
+
test("Test nested runs", async () => {
|
|
237
|
+
const tracer = new FakeTracer();
|
|
238
|
+
const chainRunId = uuid.v4();
|
|
239
|
+
const toolRunId = uuid.v4();
|
|
240
|
+
const llmRunId = uuid.v4();
|
|
241
|
+
await tracer.handleChainStart(serialized, { foo: "bar" }, chainRunId);
|
|
242
|
+
await tracer.handleToolStart({ ...serialized, id: ["test_tool"] }, "test", toolRunId, chainRunId);
|
|
243
|
+
await tracer.handleLLMStart({ ...serialized, id: ["test_llm_child_run"] }, ["test"], llmRunId, toolRunId);
|
|
244
|
+
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId);
|
|
245
|
+
await tracer.handleToolEnd("output", toolRunId);
|
|
246
|
+
const llmRunId2 = uuid.v4();
|
|
247
|
+
await tracer.handleLLMStart({ ...serialized, id: ["test_llm2"] }, ["test"], llmRunId2, chainRunId);
|
|
248
|
+
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId2);
|
|
249
|
+
await tracer.handleChainEnd({ foo: "bar" }, chainRunId);
|
|
250
|
+
const compareRun = {
|
|
251
|
+
child_runs: [
|
|
252
|
+
{
|
|
253
|
+
id: toolRunId,
|
|
254
|
+
name: "test_tool",
|
|
255
|
+
parent_run_id: chainRunId,
|
|
256
|
+
child_runs: [
|
|
257
|
+
{
|
|
258
|
+
id: llmRunId,
|
|
259
|
+
name: "test_llm_child_run",
|
|
260
|
+
parent_run_id: toolRunId,
|
|
261
|
+
end_time: 1620000000000,
|
|
262
|
+
execution_order: 3,
|
|
263
|
+
child_execution_order: 3,
|
|
264
|
+
inputs: { prompts: ["test"] },
|
|
265
|
+
outputs: {
|
|
266
|
+
generations: [[]],
|
|
267
|
+
},
|
|
268
|
+
serialized: { ...serialized, id: ["test_llm_child_run"] },
|
|
269
|
+
events: [
|
|
270
|
+
{
|
|
271
|
+
name: "start",
|
|
272
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
name: "end",
|
|
276
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
277
|
+
},
|
|
278
|
+
],
|
|
279
|
+
start_time: 1620000000000,
|
|
280
|
+
run_type: "llm",
|
|
281
|
+
child_runs: [],
|
|
282
|
+
extra: {},
|
|
283
|
+
tags: [],
|
|
284
|
+
dotted_order: `20210503T000000000001Z${chainRunId}.20210503T000000000002Z${toolRunId}.20210503T000000000003Z${llmRunId}`,
|
|
285
|
+
trace_id: chainRunId,
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
end_time: 1620000000000,
|
|
289
|
+
execution_order: 2,
|
|
290
|
+
child_execution_order: 3,
|
|
291
|
+
outputs: { output: "output" },
|
|
292
|
+
serialized: { ...serialized, id: ["test_tool"] },
|
|
293
|
+
events: [
|
|
294
|
+
{
|
|
295
|
+
name: "start",
|
|
296
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
name: "end",
|
|
300
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
start_time: 1620000000000,
|
|
304
|
+
inputs: { input: "test" },
|
|
305
|
+
run_type: "tool",
|
|
306
|
+
extra: {},
|
|
307
|
+
tags: [],
|
|
308
|
+
dotted_order: `20210503T000000000001Z${chainRunId}.20210503T000000000002Z${toolRunId}`,
|
|
309
|
+
trace_id: chainRunId,
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
id: llmRunId2,
|
|
313
|
+
name: "test_llm2",
|
|
314
|
+
parent_run_id: chainRunId,
|
|
315
|
+
end_time: 1620000000000,
|
|
316
|
+
execution_order: 4,
|
|
317
|
+
child_execution_order: 4,
|
|
318
|
+
inputs: { prompts: ["test"] },
|
|
319
|
+
outputs: {
|
|
320
|
+
generations: [[]],
|
|
321
|
+
},
|
|
322
|
+
serialized: { ...serialized, id: ["test_llm2"] },
|
|
323
|
+
events: [
|
|
324
|
+
{
|
|
325
|
+
name: "start",
|
|
326
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
name: "end",
|
|
330
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
start_time: 1620000000000,
|
|
334
|
+
run_type: "llm",
|
|
335
|
+
child_runs: [],
|
|
336
|
+
extra: {},
|
|
337
|
+
tags: [],
|
|
338
|
+
dotted_order: `20210503T000000000001Z${chainRunId}.20210503T000000000004Z${llmRunId2}`,
|
|
339
|
+
trace_id: chainRunId,
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
id: chainRunId,
|
|
343
|
+
end_time: 1620000000000,
|
|
344
|
+
execution_order: 1,
|
|
345
|
+
child_execution_order: 4,
|
|
346
|
+
inputs: {
|
|
347
|
+
foo: "bar",
|
|
348
|
+
},
|
|
349
|
+
outputs: {
|
|
350
|
+
foo: "bar",
|
|
351
|
+
},
|
|
352
|
+
events: [
|
|
353
|
+
{
|
|
354
|
+
name: "start",
|
|
355
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
name: "end",
|
|
359
|
+
time: "2021-05-03T00:00:00.000Z",
|
|
360
|
+
},
|
|
361
|
+
],
|
|
362
|
+
name: "test",
|
|
363
|
+
serialized,
|
|
364
|
+
start_time: 1620000000000,
|
|
365
|
+
run_type: "chain",
|
|
366
|
+
extra: {},
|
|
367
|
+
tags: [],
|
|
368
|
+
parent_run_id: undefined,
|
|
369
|
+
dotted_order: `20210503T000000000001Z${chainRunId}`,
|
|
370
|
+
trace_id: chainRunId,
|
|
371
|
+
};
|
|
372
|
+
expect(tracer.runs.length).toBe(1);
|
|
373
|
+
expect(tracer.runs[0]).toEqual(compareRun);
|
|
374
|
+
const llmRunId3 = uuid.v4();
|
|
375
|
+
await tracer.handleLLMStart(serialized, ["test"], llmRunId3);
|
|
376
|
+
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId3);
|
|
377
|
+
expect(tracer.runs.length).toBe(2);
|
|
378
|
+
});
|
package/dist/utils/stream.cjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.pipeGeneratorWithSetup = exports.AsyncGeneratorWithSetup = exports.concat = exports.atee = exports.IterableReadableStream = void 0;
|
|
4
|
+
// Make this a type to override ReadableStream's async iterator type in case
|
|
5
|
+
// the popular web-streams-polyfill is imported - the supplied types
|
|
6
|
+
const index_js_1 = require("../singletons/index.cjs");
|
|
4
7
|
/*
|
|
5
8
|
* Support async iterator syntax for ReadableStreams in all environments.
|
|
6
9
|
* Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
@@ -167,7 +170,7 @@ function concat(first, second) {
|
|
|
167
170
|
}
|
|
168
171
|
exports.concat = concat;
|
|
169
172
|
class AsyncGeneratorWithSetup {
|
|
170
|
-
constructor(
|
|
173
|
+
constructor(params) {
|
|
171
174
|
Object.defineProperty(this, "generator", {
|
|
172
175
|
enumerable: true,
|
|
173
176
|
configurable: true,
|
|
@@ -180,6 +183,12 @@ class AsyncGeneratorWithSetup {
|
|
|
180
183
|
writable: true,
|
|
181
184
|
value: void 0
|
|
182
185
|
});
|
|
186
|
+
Object.defineProperty(this, "config", {
|
|
187
|
+
enumerable: true,
|
|
188
|
+
configurable: true,
|
|
189
|
+
writable: true,
|
|
190
|
+
value: void 0
|
|
191
|
+
});
|
|
183
192
|
Object.defineProperty(this, "firstResult", {
|
|
184
193
|
enumerable: true,
|
|
185
194
|
configurable: true,
|
|
@@ -192,19 +201,23 @@ class AsyncGeneratorWithSetup {
|
|
|
192
201
|
writable: true,
|
|
193
202
|
value: false
|
|
194
203
|
});
|
|
195
|
-
this.generator = generator;
|
|
204
|
+
this.generator = params.generator;
|
|
205
|
+
this.config = params.config;
|
|
196
206
|
// setup is a promise that resolves only after the first iterator value
|
|
197
207
|
// is available. this is useful when setup of several piped generators
|
|
198
208
|
// needs to happen in logical order, ie. in the order in which input to
|
|
199
209
|
// to each generator is available.
|
|
200
210
|
this.setup = new Promise((resolve, reject) => {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
this.firstResult.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
211
|
+
const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
|
|
212
|
+
void storage.run(params.config, async () => {
|
|
213
|
+
this.firstResult = params.generator.next();
|
|
214
|
+
if (params.startSetup) {
|
|
215
|
+
this.firstResult.then(params.startSetup).then(resolve, reject);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
this.firstResult.then((_result) => resolve(undefined), reject);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
208
221
|
});
|
|
209
222
|
}
|
|
210
223
|
async next(...args) {
|
|
@@ -212,7 +225,10 @@ class AsyncGeneratorWithSetup {
|
|
|
212
225
|
this.firstResultUsed = true;
|
|
213
226
|
return this.firstResult;
|
|
214
227
|
}
|
|
215
|
-
|
|
228
|
+
const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
|
|
229
|
+
return storage.run(this.config, async () => {
|
|
230
|
+
return this.generator.next(...args);
|
|
231
|
+
});
|
|
216
232
|
}
|
|
217
233
|
async return(value) {
|
|
218
234
|
return this.generator.return(value);
|
|
@@ -226,7 +242,7 @@ class AsyncGeneratorWithSetup {
|
|
|
226
242
|
}
|
|
227
243
|
exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
|
|
228
244
|
async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
|
|
229
|
-
const gen = new AsyncGeneratorWithSetup(generator, startSetup);
|
|
245
|
+
const gen = new AsyncGeneratorWithSetup({ generator, startSetup });
|
|
230
246
|
const setup = await gen.setup;
|
|
231
247
|
return { output: to(gen, setup, ...args), setup };
|
|
232
248
|
}
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -14,9 +14,14 @@ export declare function concat<T extends Array<any> | string | number | Record<s
|
|
|
14
14
|
export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn = unknown, TNext = unknown> implements AsyncGenerator<T, TReturn, TNext> {
|
|
15
15
|
private generator;
|
|
16
16
|
setup: Promise<S>;
|
|
17
|
+
config?: unknown;
|
|
17
18
|
private firstResult;
|
|
18
19
|
private firstResultUsed;
|
|
19
|
-
constructor(
|
|
20
|
+
constructor(params: {
|
|
21
|
+
generator: AsyncGenerator<T>;
|
|
22
|
+
startSetup?: () => Promise<S>;
|
|
23
|
+
config?: unknown;
|
|
24
|
+
});
|
|
20
25
|
next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
|
|
21
26
|
return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
|
|
22
27
|
throw(e: Error): Promise<IteratorResult<T>>;
|
package/dist/utils/stream.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// Make this a type to override ReadableStream's async iterator type in case
|
|
2
|
+
// the popular web-streams-polyfill is imported - the supplied types
|
|
3
|
+
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
1
4
|
/*
|
|
2
5
|
* Support async iterator syntax for ReadableStreams in all environments.
|
|
3
6
|
* Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
@@ -161,7 +164,7 @@ export function concat(first, second) {
|
|
|
161
164
|
}
|
|
162
165
|
}
|
|
163
166
|
export class AsyncGeneratorWithSetup {
|
|
164
|
-
constructor(
|
|
167
|
+
constructor(params) {
|
|
165
168
|
Object.defineProperty(this, "generator", {
|
|
166
169
|
enumerable: true,
|
|
167
170
|
configurable: true,
|
|
@@ -174,6 +177,12 @@ export class AsyncGeneratorWithSetup {
|
|
|
174
177
|
writable: true,
|
|
175
178
|
value: void 0
|
|
176
179
|
});
|
|
180
|
+
Object.defineProperty(this, "config", {
|
|
181
|
+
enumerable: true,
|
|
182
|
+
configurable: true,
|
|
183
|
+
writable: true,
|
|
184
|
+
value: void 0
|
|
185
|
+
});
|
|
177
186
|
Object.defineProperty(this, "firstResult", {
|
|
178
187
|
enumerable: true,
|
|
179
188
|
configurable: true,
|
|
@@ -186,19 +195,23 @@ export class AsyncGeneratorWithSetup {
|
|
|
186
195
|
writable: true,
|
|
187
196
|
value: false
|
|
188
197
|
});
|
|
189
|
-
this.generator = generator;
|
|
198
|
+
this.generator = params.generator;
|
|
199
|
+
this.config = params.config;
|
|
190
200
|
// setup is a promise that resolves only after the first iterator value
|
|
191
201
|
// is available. this is useful when setup of several piped generators
|
|
192
202
|
// needs to happen in logical order, ie. in the order in which input to
|
|
193
203
|
// to each generator is available.
|
|
194
204
|
this.setup = new Promise((resolve, reject) => {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
this.firstResult.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
205
|
+
const storage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
206
|
+
void storage.run(params.config, async () => {
|
|
207
|
+
this.firstResult = params.generator.next();
|
|
208
|
+
if (params.startSetup) {
|
|
209
|
+
this.firstResult.then(params.startSetup).then(resolve, reject);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
this.firstResult.then((_result) => resolve(undefined), reject);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
202
215
|
});
|
|
203
216
|
}
|
|
204
217
|
async next(...args) {
|
|
@@ -206,7 +219,10 @@ export class AsyncGeneratorWithSetup {
|
|
|
206
219
|
this.firstResultUsed = true;
|
|
207
220
|
return this.firstResult;
|
|
208
221
|
}
|
|
209
|
-
|
|
222
|
+
const storage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
223
|
+
return storage.run(this.config, async () => {
|
|
224
|
+
return this.generator.next(...args);
|
|
225
|
+
});
|
|
210
226
|
}
|
|
211
227
|
async return(value) {
|
|
212
228
|
return this.generator.return(value);
|
|
@@ -219,7 +235,7 @@ export class AsyncGeneratorWithSetup {
|
|
|
219
235
|
}
|
|
220
236
|
}
|
|
221
237
|
export async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
|
|
222
|
-
const gen = new AsyncGeneratorWithSetup(generator, startSetup);
|
|
238
|
+
const gen = new AsyncGeneratorWithSetup({ generator, startSetup });
|
|
223
239
|
const setup = await gen.setup;
|
|
224
240
|
return { output: to(gen, setup, ...args), setup };
|
|
225
241
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, test, expect, jest } from "@jest/globals";
|
|
2
|
+
import { HumanMessage } from "../../../messages/index.js";
|
|
3
|
+
import { StringOutputParser } from "../../../output_parsers/string.js";
|
|
4
|
+
import { FakeListChatModel } from "../index.js";
|
|
5
|
+
describe("Test FakeListChatLLM", () => {
|
|
6
|
+
test("Should exist", async () => {
|
|
7
|
+
const chat = new FakeListChatModel({ responses: ["test response"] });
|
|
8
|
+
const message = new HumanMessage("test message");
|
|
9
|
+
const response = await chat.invoke([message]);
|
|
10
|
+
expect(typeof response.content).toBe("string");
|
|
11
|
+
});
|
|
12
|
+
test("Should return responses in order", async () => {
|
|
13
|
+
const chat = new FakeListChatModel({
|
|
14
|
+
responses: ["test response 1", "test response 2"],
|
|
15
|
+
});
|
|
16
|
+
const message = new HumanMessage("test message");
|
|
17
|
+
const response1 = await chat.invoke([message]);
|
|
18
|
+
const response2 = await chat.invoke([message]);
|
|
19
|
+
expect(response1.content).toBe("test response 1");
|
|
20
|
+
expect(response2.content).toBe("test response 2");
|
|
21
|
+
});
|
|
22
|
+
test("Should reset index when all responses have been returned", async () => {
|
|
23
|
+
const chat = new FakeListChatModel({
|
|
24
|
+
responses: ["test response 1", "test response 2"],
|
|
25
|
+
});
|
|
26
|
+
const message = new HumanMessage("test message");
|
|
27
|
+
const first_response = await chat.invoke([message]);
|
|
28
|
+
const second_response = await chat.invoke([message]);
|
|
29
|
+
const third_response = await chat.invoke([message]);
|
|
30
|
+
expect(first_response.content).toBe("test response 1");
|
|
31
|
+
expect(second_response.content).toBe("test response 2");
|
|
32
|
+
expect(third_response.content).toBe("test response 1");
|
|
33
|
+
});
|
|
34
|
+
test("Should return stop value as response when provided", async () => {
|
|
35
|
+
const chat = new FakeListChatModel({
|
|
36
|
+
responses: ["test response 1", "test response 2"],
|
|
37
|
+
});
|
|
38
|
+
const message = new HumanMessage("test message");
|
|
39
|
+
const response = await chat.invoke([message], { stop: ["stop"] });
|
|
40
|
+
expect(response.content).toBe("stop");
|
|
41
|
+
});
|
|
42
|
+
test("Should not increment index when stop value is provided", async () => {
|
|
43
|
+
const chat = new FakeListChatModel({
|
|
44
|
+
responses: ["test response 1", "test response 2"],
|
|
45
|
+
});
|
|
46
|
+
const message = new HumanMessage("test message");
|
|
47
|
+
const first_response = await chat.invoke([message], { stop: ["stop"] });
|
|
48
|
+
const second_response = await chat.invoke([message]);
|
|
49
|
+
expect(first_response.content).toBe("stop");
|
|
50
|
+
expect(second_response.content).toBe("test response 1");
|
|
51
|
+
});
|
|
52
|
+
test("Should return responses after sleep if requested", async () => {
|
|
53
|
+
const chat = new FakeListChatModel({
|
|
54
|
+
responses: ["test response 1", "test response 2"],
|
|
55
|
+
sleep: 10,
|
|
56
|
+
});
|
|
57
|
+
const sleepSpy = jest.spyOn(chat, "_sleep");
|
|
58
|
+
const message = new HumanMessage("test message");
|
|
59
|
+
await chat.invoke([message]);
|
|
60
|
+
expect(sleepSpy).toHaveBeenCalledTimes(1);
|
|
61
|
+
}, 30000);
|
|
62
|
+
test("Should stream responses if requested", async () => {
|
|
63
|
+
const chat = new FakeListChatModel({
|
|
64
|
+
responses: ["test response 1", "test response 2"],
|
|
65
|
+
});
|
|
66
|
+
const chunks = [];
|
|
67
|
+
const response = await chat
|
|
68
|
+
.pipe(new StringOutputParser())
|
|
69
|
+
.stream("Test message");
|
|
70
|
+
for await (const chunk of response) {
|
|
71
|
+
chunks.push(chunk);
|
|
72
|
+
}
|
|
73
|
+
expect(chunks.length).toBeGreaterThan(1);
|
|
74
|
+
expect(chunks.join("")).toBe("test response 1");
|
|
75
|
+
});
|
|
76
|
+
test("Should return responses in order when streaming", async () => {
|
|
77
|
+
const chat = new FakeListChatModel({
|
|
78
|
+
responses: ["test response 1", "test response 2"],
|
|
79
|
+
});
|
|
80
|
+
const chunks1 = [];
|
|
81
|
+
const chunks2 = [];
|
|
82
|
+
const response1 = await chat
|
|
83
|
+
.pipe(new StringOutputParser())
|
|
84
|
+
.stream("Test message");
|
|
85
|
+
for await (const chunk of response1) {
|
|
86
|
+
chunks1.push(chunk);
|
|
87
|
+
}
|
|
88
|
+
const response2 = await chat
|
|
89
|
+
.pipe(new StringOutputParser())
|
|
90
|
+
.stream("Test message");
|
|
91
|
+
for await (const chunk of response2) {
|
|
92
|
+
chunks2.push(chunk);
|
|
93
|
+
}
|
|
94
|
+
expect(chunks1.join("")).toBe("test response 1");
|
|
95
|
+
expect(chunks2.join("")).toBe("test response 2");
|
|
96
|
+
});
|
|
97
|
+
test("Should stream responses after sleep if requested", async () => {
|
|
98
|
+
const chat = new FakeListChatModel({
|
|
99
|
+
responses: ["test response 1", "test response 2"],
|
|
100
|
+
sleep: 10,
|
|
101
|
+
});
|
|
102
|
+
const sleepSpy = jest.spyOn(chat, "_sleep");
|
|
103
|
+
const chunks = [];
|
|
104
|
+
const response = await chat
|
|
105
|
+
.pipe(new StringOutputParser())
|
|
106
|
+
.stream("Test message");
|
|
107
|
+
for await (const chunk of response) {
|
|
108
|
+
chunks.push(chunk);
|
|
109
|
+
}
|
|
110
|
+
expect(sleepSpy).toHaveBeenCalledTimes(chunks.length);
|
|
111
|
+
}, 30000);
|
|
112
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|