@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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/* eslint-disable no-promise-executor-return */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
import { test } from "@jest/globals";
|
|
4
|
+
import { ChatPromptTemplate, HumanMessagePromptTemplate, PromptTemplate, SystemMessagePromptTemplate, } from "../../prompts/index.js";
|
|
5
|
+
import { Document } from "../../documents/document.js";
|
|
6
|
+
import { RunnableSequence, RunnableMap } from "../base.js";
|
|
7
|
+
import { FakeLLM, FakeStreamingLLM, FakeChatModel, FakeRetriever, } from "../../utils/testing/index.js";
|
|
8
|
+
import { SystemMessage, HumanMessage } from "../../messages/index.js";
|
|
9
|
+
import { CommaSeparatedListOutputParser } from "../../output_parsers/list.js";
|
|
10
|
+
import { ChatPromptValue } from "../../prompt_values.js";
|
|
11
|
+
test("Runnable streamLog method", async () => {
|
|
12
|
+
const promptTemplate = PromptTemplate.fromTemplate("{input}");
|
|
13
|
+
const llm = new FakeLLM({});
|
|
14
|
+
const runnable = promptTemplate.pipe(llm);
|
|
15
|
+
const result = await runnable.streamLog({ input: "Hello world!" });
|
|
16
|
+
let finalState;
|
|
17
|
+
for await (const chunk of result) {
|
|
18
|
+
if (finalState === undefined) {
|
|
19
|
+
finalState = chunk;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
finalState = finalState.concat(chunk);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
expect(finalState.state.final_output).toEqual({
|
|
26
|
+
output: "Hello world!",
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
test("Runnable streamLog method with a more complicated sequence", async () => {
|
|
30
|
+
const promptTemplate = ChatPromptTemplate.fromMessages([
|
|
31
|
+
SystemMessagePromptTemplate.fromTemplate(`You are a nice assistant.`),
|
|
32
|
+
HumanMessagePromptTemplate.fromTemplate(`Context:\n{documents}\n\nQuestion:\n{question}`),
|
|
33
|
+
]);
|
|
34
|
+
const llm = new FakeChatModel({});
|
|
35
|
+
const retrieverOutputDocs = [
|
|
36
|
+
new Document({ pageContent: "foo" }),
|
|
37
|
+
new Document({ pageContent: "bar" }),
|
|
38
|
+
];
|
|
39
|
+
const inputs = {
|
|
40
|
+
question: (input) => input,
|
|
41
|
+
documents: RunnableSequence.from([
|
|
42
|
+
new FakeRetriever({
|
|
43
|
+
output: retrieverOutputDocs,
|
|
44
|
+
}),
|
|
45
|
+
(docs) => JSON.stringify(docs),
|
|
46
|
+
]).withConfig({ runName: "CUSTOM_NAME" }),
|
|
47
|
+
extraField: new FakeLLM({
|
|
48
|
+
response: "testing",
|
|
49
|
+
}).withConfig({ tags: ["only_one"] }),
|
|
50
|
+
};
|
|
51
|
+
const runnable = new RunnableMap({
|
|
52
|
+
steps: inputs,
|
|
53
|
+
})
|
|
54
|
+
.pipe(promptTemplate)
|
|
55
|
+
.pipe(llm);
|
|
56
|
+
const stream = await runnable.streamLog("Do you know the Muffin Man?", {}, {
|
|
57
|
+
includeTags: ["only_one"],
|
|
58
|
+
includeNames: ["CUSTOM_NAME"],
|
|
59
|
+
});
|
|
60
|
+
let finalState;
|
|
61
|
+
for await (const chunk of stream) {
|
|
62
|
+
if (finalState === undefined) {
|
|
63
|
+
finalState = chunk;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
finalState = finalState.concat(chunk);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
expect(finalState.state.logs.FakeLLM).toBeDefined();
|
|
70
|
+
expect(finalState.state.logs.FakeLLM.final_output.generations[0][0]
|
|
71
|
+
.text).toEqual("testing");
|
|
72
|
+
expect(finalState.state.logs.CUSTOM_NAME).toBeDefined();
|
|
73
|
+
expect(finalState.state.logs.CUSTOM_NAME.final_output.output).toEqual(JSON.stringify(retrieverOutputDocs));
|
|
74
|
+
});
|
|
75
|
+
test("Test stream log aggregation", async () => {
|
|
76
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
77
|
+
["system", "You are a nice assistant"],
|
|
78
|
+
["human", "{question}"],
|
|
79
|
+
]);
|
|
80
|
+
const llm = new FakeStreamingLLM({
|
|
81
|
+
responses: ["tomato, lettuce, onion"],
|
|
82
|
+
});
|
|
83
|
+
const parser = new CommaSeparatedListOutputParser({});
|
|
84
|
+
const chain = prompt.pipe(llm).pipe(parser);
|
|
85
|
+
const logStream = await chain.streamLog({
|
|
86
|
+
question: "what is up?",
|
|
87
|
+
});
|
|
88
|
+
const chunks = [];
|
|
89
|
+
for await (const chunk of logStream) {
|
|
90
|
+
chunks.push(chunk);
|
|
91
|
+
}
|
|
92
|
+
expect(chunks).toMatchObject([
|
|
93
|
+
{
|
|
94
|
+
ops: [
|
|
95
|
+
{
|
|
96
|
+
op: "replace",
|
|
97
|
+
path: "",
|
|
98
|
+
value: {
|
|
99
|
+
id: expect.any(String),
|
|
100
|
+
streamed_output: [],
|
|
101
|
+
logs: {},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
ops: [
|
|
108
|
+
{
|
|
109
|
+
op: "add",
|
|
110
|
+
path: "/logs/ChatPromptTemplate",
|
|
111
|
+
value: {
|
|
112
|
+
id: expect.any(String),
|
|
113
|
+
name: "ChatPromptTemplate",
|
|
114
|
+
type: "prompt",
|
|
115
|
+
tags: ["seq:step:1"],
|
|
116
|
+
metadata: {},
|
|
117
|
+
start_time: expect.any(String),
|
|
118
|
+
streamed_output: [],
|
|
119
|
+
streamed_output_str: [],
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
ops: [
|
|
126
|
+
{
|
|
127
|
+
op: "add",
|
|
128
|
+
path: "/logs/ChatPromptTemplate/final_output",
|
|
129
|
+
value: new ChatPromptValue([
|
|
130
|
+
new SystemMessage("You are a nice assistant"),
|
|
131
|
+
new HumanMessage("what is up?"),
|
|
132
|
+
]),
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
op: "add",
|
|
136
|
+
path: "/logs/ChatPromptTemplate/end_time",
|
|
137
|
+
value: expect.any(String),
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
ops: [
|
|
143
|
+
{
|
|
144
|
+
op: "add",
|
|
145
|
+
path: "/logs/FakeStreamingLLM",
|
|
146
|
+
value: {
|
|
147
|
+
id: expect.any(String),
|
|
148
|
+
name: "FakeStreamingLLM",
|
|
149
|
+
type: "llm",
|
|
150
|
+
tags: ["seq:step:2"],
|
|
151
|
+
metadata: {},
|
|
152
|
+
start_time: expect.any(String),
|
|
153
|
+
streamed_output: [],
|
|
154
|
+
streamed_output_str: [],
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
ops: [
|
|
161
|
+
{
|
|
162
|
+
op: "add",
|
|
163
|
+
path: "/logs/CommaSeparatedListOutputParser",
|
|
164
|
+
value: {
|
|
165
|
+
id: expect.any(String),
|
|
166
|
+
name: "CommaSeparatedListOutputParser",
|
|
167
|
+
type: "parser",
|
|
168
|
+
tags: ["seq:step:3"],
|
|
169
|
+
metadata: {},
|
|
170
|
+
start_time: expect.any(String),
|
|
171
|
+
streamed_output: [],
|
|
172
|
+
streamed_output_str: [],
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
ops: [
|
|
179
|
+
{
|
|
180
|
+
op: "add",
|
|
181
|
+
path: "/logs/CommaSeparatedListOutputParser/streamed_output/-",
|
|
182
|
+
value: ["tomato"],
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
ops: [
|
|
188
|
+
{
|
|
189
|
+
op: "add",
|
|
190
|
+
path: "/streamed_output/-",
|
|
191
|
+
value: ["tomato"],
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
ops: [
|
|
197
|
+
{
|
|
198
|
+
op: "add",
|
|
199
|
+
path: "/logs/CommaSeparatedListOutputParser/streamed_output/-",
|
|
200
|
+
value: ["lettuce"],
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
ops: [
|
|
206
|
+
{
|
|
207
|
+
op: "add",
|
|
208
|
+
path: "/streamed_output/-",
|
|
209
|
+
value: ["lettuce"],
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
ops: [
|
|
215
|
+
{
|
|
216
|
+
op: "add",
|
|
217
|
+
path: "/logs/FakeStreamingLLM/final_output",
|
|
218
|
+
value: {
|
|
219
|
+
generations: [
|
|
220
|
+
[
|
|
221
|
+
{
|
|
222
|
+
text: "tomato, lettuce, onion",
|
|
223
|
+
generationInfo: {},
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
],
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
op: "add",
|
|
231
|
+
path: "/logs/FakeStreamingLLM/end_time",
|
|
232
|
+
value: expect.any(String),
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
ops: [
|
|
238
|
+
{
|
|
239
|
+
op: "add",
|
|
240
|
+
path: "/logs/CommaSeparatedListOutputParser/streamed_output/-",
|
|
241
|
+
value: ["onion"],
|
|
242
|
+
},
|
|
243
|
+
],
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
ops: [
|
|
247
|
+
{
|
|
248
|
+
op: "add",
|
|
249
|
+
path: "/streamed_output/-",
|
|
250
|
+
value: ["onion"],
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
ops: [
|
|
256
|
+
{
|
|
257
|
+
op: "add",
|
|
258
|
+
path: "/logs/CommaSeparatedListOutputParser/final_output",
|
|
259
|
+
value: {
|
|
260
|
+
output: ["tomato", "lettuce", "onion"],
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
op: "add",
|
|
265
|
+
path: "/logs/CommaSeparatedListOutputParser/end_time",
|
|
266
|
+
value: expect.any(String),
|
|
267
|
+
},
|
|
268
|
+
],
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
ops: [
|
|
272
|
+
{
|
|
273
|
+
op: "replace",
|
|
274
|
+
path: "/final_output",
|
|
275
|
+
value: {
|
|
276
|
+
output: ["tomato", "lettuce", "onion"],
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
],
|
|
280
|
+
},
|
|
281
|
+
]);
|
|
282
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Client } from "langsmith";
|
|
2
|
+
import { LangChainTracer } from "../../tracers/tracer_langchain.js";
|
|
3
|
+
import { BaseOutputParser, } from "../../output_parsers/base.js";
|
|
4
|
+
import { FakeChatModel } from "../../utils/testing/index.js";
|
|
5
|
+
import { getEnvironmentVariable } from "../../utils/env.js";
|
|
6
|
+
class FakeDateOutputParser extends BaseOutputParser {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: ["langchain_core", "output_parsers", "testing"]
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
async parse(_text) {
|
|
17
|
+
return new Date();
|
|
18
|
+
}
|
|
19
|
+
getFormatInstructions(_options) {
|
|
20
|
+
return "";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
test("Should handle tracing with a date output", async () => {
|
|
24
|
+
const client = new Client({
|
|
25
|
+
apiUrl: getEnvironmentVariable("LANGCHAIN_ENDPOINT"),
|
|
26
|
+
apiKey: getEnvironmentVariable("LANGCHAIN_API_KEY"),
|
|
27
|
+
});
|
|
28
|
+
const tracer = new LangChainTracer({
|
|
29
|
+
projectName: getEnvironmentVariable("LANGCHAIN_SESSION"),
|
|
30
|
+
client,
|
|
31
|
+
});
|
|
32
|
+
const model = new FakeChatModel({});
|
|
33
|
+
const parser = new FakeDateOutputParser();
|
|
34
|
+
const chain = model.pipe(parser);
|
|
35
|
+
const result = await chain.invoke("test", { callbacks: [tracer] });
|
|
36
|
+
console.log(result);
|
|
37
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* eslint-disable no-promise-executor-return */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
import { test } from "@jest/globals";
|
|
4
|
+
import { FakeLLM } from "../../utils/testing/index.js";
|
|
5
|
+
test("RunnableWithFallbacks", async () => {
|
|
6
|
+
const llm = new FakeLLM({
|
|
7
|
+
thrownErrorString: "Bad error!",
|
|
8
|
+
});
|
|
9
|
+
await expect(async () => {
|
|
10
|
+
const result1 = await llm.invoke("What up");
|
|
11
|
+
console.log(result1);
|
|
12
|
+
}).rejects.toThrow();
|
|
13
|
+
const llmWithFallbacks = llm.withFallbacks({
|
|
14
|
+
fallbacks: [new FakeLLM({})],
|
|
15
|
+
});
|
|
16
|
+
const result2 = await llmWithFallbacks.invoke("What up");
|
|
17
|
+
expect(result2).toEqual("What up");
|
|
18
|
+
});
|
|
19
|
+
test("RunnableWithFallbacks batch", async () => {
|
|
20
|
+
const llm = new FakeLLM({
|
|
21
|
+
thrownErrorString: "Bad error!",
|
|
22
|
+
});
|
|
23
|
+
await expect(async () => {
|
|
24
|
+
const result1 = await llm.batch(["What up"]);
|
|
25
|
+
console.log(result1);
|
|
26
|
+
}).rejects.toThrow();
|
|
27
|
+
const llmWithFallbacks = llm.withFallbacks({
|
|
28
|
+
fallbacks: [new FakeLLM({})],
|
|
29
|
+
});
|
|
30
|
+
const result2 = await llmWithFallbacks.batch([
|
|
31
|
+
"What up 1",
|
|
32
|
+
"What up 2",
|
|
33
|
+
"What up 3",
|
|
34
|
+
]);
|
|
35
|
+
expect(result2).toEqual(["What up 1", "What up 2", "What up 3"]);
|
|
36
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { test, expect } from "@jest/globals";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import { AsyncLocalStorageProviderSingleton } from "../index.js";
|
|
4
|
+
import { RunnableLambda } from "../../runnables/base.js";
|
|
5
|
+
import { FakeListChatModel } from "../../utils/testing/index.js";
|
|
6
|
+
test("Config should be automatically populated after setting global async local storage", async () => {
|
|
7
|
+
const inner = RunnableLambda.from((_, config) => config);
|
|
8
|
+
const outer = RunnableLambda.from(async (input) => {
|
|
9
|
+
const res = await inner.invoke(input);
|
|
10
|
+
return res;
|
|
11
|
+
});
|
|
12
|
+
const res1 = await outer.invoke({ hi: true }, {
|
|
13
|
+
configurable: {
|
|
14
|
+
sampleKey: "sampleValue",
|
|
15
|
+
},
|
|
16
|
+
tags: ["tester"],
|
|
17
|
+
});
|
|
18
|
+
expect(res1?.tags).toEqual([]);
|
|
19
|
+
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
20
|
+
const res2 = await outer.invoke({ hi: true }, {
|
|
21
|
+
configurable: {
|
|
22
|
+
sampleKey: "sampleValue",
|
|
23
|
+
},
|
|
24
|
+
tags: ["tester"],
|
|
25
|
+
});
|
|
26
|
+
expect(res2?.tags).toEqual(["tester"]);
|
|
27
|
+
const stream = await outer.stream({ hi2: true }, {
|
|
28
|
+
configurable: {
|
|
29
|
+
sampleKey: "sampleValue",
|
|
30
|
+
},
|
|
31
|
+
tags: ["stream_tester"],
|
|
32
|
+
});
|
|
33
|
+
const chunks = [];
|
|
34
|
+
for await (const chunk of stream) {
|
|
35
|
+
console.log(chunk);
|
|
36
|
+
chunks.push(chunk);
|
|
37
|
+
}
|
|
38
|
+
expect(chunks.length).toEqual(1);
|
|
39
|
+
expect(chunks[0]).toEqual(expect.objectContaining({
|
|
40
|
+
configurable: {
|
|
41
|
+
sampleKey: "sampleValue",
|
|
42
|
+
},
|
|
43
|
+
tags: ["stream_tester"],
|
|
44
|
+
}));
|
|
45
|
+
const outer2 = RunnableLambda.from(async () => inner);
|
|
46
|
+
const res3 = await outer2.invoke({}, {
|
|
47
|
+
configurable: {
|
|
48
|
+
sampleKey: "sampleValue",
|
|
49
|
+
},
|
|
50
|
+
tags: ["test_recursive"],
|
|
51
|
+
});
|
|
52
|
+
expect(res3?.tags).toEqual(["test_recursive"]);
|
|
53
|
+
const stream2 = await outer2.stream({}, {
|
|
54
|
+
configurable: {
|
|
55
|
+
sampleKey: "sampleValue",
|
|
56
|
+
},
|
|
57
|
+
tags: ["stream_test_recursive"],
|
|
58
|
+
});
|
|
59
|
+
const chunks2 = [];
|
|
60
|
+
for await (const chunk of stream2) {
|
|
61
|
+
console.log(chunk);
|
|
62
|
+
chunks2.push(chunk);
|
|
63
|
+
}
|
|
64
|
+
expect(chunks2.length).toEqual(1);
|
|
65
|
+
expect(chunks2[0]).toEqual(expect.objectContaining({
|
|
66
|
+
configurable: {
|
|
67
|
+
sampleKey: "sampleValue",
|
|
68
|
+
},
|
|
69
|
+
tags: ["stream_test_recursive"],
|
|
70
|
+
}));
|
|
71
|
+
const inner2 = RunnableLambda.from((_, config) => config).withConfig({
|
|
72
|
+
runName: "inner_test_run",
|
|
73
|
+
});
|
|
74
|
+
const outer3 = RunnableLambda.from(async (input) => {
|
|
75
|
+
const res = await inner2.invoke(input);
|
|
76
|
+
return res;
|
|
77
|
+
});
|
|
78
|
+
const res4 = await outer3.invoke({ hi: true }, {
|
|
79
|
+
configurable: {
|
|
80
|
+
sampleKey: "sampleValue",
|
|
81
|
+
},
|
|
82
|
+
tags: ["tester_with_config"],
|
|
83
|
+
});
|
|
84
|
+
expect(res4?.tags).toEqual(["tester_with_config"]);
|
|
85
|
+
const chatModel = new FakeListChatModel({ responses: ["test"] });
|
|
86
|
+
const outer4 = RunnableLambda.from(async () => {
|
|
87
|
+
const res = await chatModel.invoke("hey");
|
|
88
|
+
return res;
|
|
89
|
+
});
|
|
90
|
+
const eventStream = await outer4.streamEvents({ hi: true }, { version: "v1" });
|
|
91
|
+
const events = [];
|
|
92
|
+
for await (const event of eventStream) {
|
|
93
|
+
console.log(event);
|
|
94
|
+
events.push(event);
|
|
95
|
+
}
|
|
96
|
+
expect(events.filter((event) => event.event === "on_llm_start").length).toEqual(1);
|
|
97
|
+
});
|
|
98
|
+
test("Runnable streamEvents method with streaming nested in a RunnableLambda", async () => {
|
|
99
|
+
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
100
|
+
const chat = new FakeListChatModel({
|
|
101
|
+
responses: ["Hello"],
|
|
102
|
+
});
|
|
103
|
+
const myFunc = async (input) => {
|
|
104
|
+
for await (const _ of await chat.stream(input)) {
|
|
105
|
+
// no-op
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const myNestedLambda = RunnableLambda.from(myFunc);
|
|
109
|
+
const events = [];
|
|
110
|
+
for await (const event of myNestedLambda.streamEvents("hello", {
|
|
111
|
+
version: "v1",
|
|
112
|
+
})) {
|
|
113
|
+
console.log(event);
|
|
114
|
+
events.push(event);
|
|
115
|
+
}
|
|
116
|
+
const chatModelStreamEvent = events.find((event) => {
|
|
117
|
+
return event.event === "on_llm_stream";
|
|
118
|
+
});
|
|
119
|
+
expect(chatModelStreamEvent).toBeDefined();
|
|
120
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/* eslint-disable no-process-env */
|
|
2
|
+
import { test } from "@jest/globals";
|
|
3
|
+
import { castValue, isFloat, isInt, isString } from "../utils.js";
|
|
4
|
+
test("Casting values correctly", () => {
|
|
5
|
+
const stringString = [
|
|
6
|
+
"string",
|
|
7
|
+
"test",
|
|
8
|
+
"this is a string",
|
|
9
|
+
" ",
|
|
10
|
+
"\n\n\n\n\n\n",
|
|
11
|
+
`asdf
|
|
12
|
+
zxcv`,
|
|
13
|
+
];
|
|
14
|
+
const intString = [
|
|
15
|
+
"1a",
|
|
16
|
+
"2b",
|
|
17
|
+
"3c",
|
|
18
|
+
"a4",
|
|
19
|
+
`123
|
|
20
|
+
asdf`,
|
|
21
|
+
];
|
|
22
|
+
const floatString = ["1.1a", "2.2b", "3.3c", "c4.4"];
|
|
23
|
+
const intInt = ["1", 2, 3];
|
|
24
|
+
const floatFloat = ["1.1", 2.2, 3.3];
|
|
25
|
+
stringString.map(castValue).forEach((value) => {
|
|
26
|
+
expect(typeof value).toBe("string");
|
|
27
|
+
expect(isString(value)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
intString.map(castValue).forEach((value) => {
|
|
30
|
+
expect(typeof value).toBe("string");
|
|
31
|
+
expect(isString(value)).toBe(true);
|
|
32
|
+
expect(isInt(value)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
floatString.map(castValue).forEach((value) => {
|
|
35
|
+
expect(typeof value).toBe("string");
|
|
36
|
+
expect(isString(value)).toBe(true);
|
|
37
|
+
expect(isFloat(value)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
intInt.map(castValue).forEach((value) => {
|
|
40
|
+
expect(typeof value).toBe("number");
|
|
41
|
+
expect(isInt(value)).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
floatFloat.map(castValue).forEach((value) => {
|
|
44
|
+
expect(typeof value).toBe("number");
|
|
45
|
+
expect(isFloat(value)).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
});
|