@langchain/core 0.2.18 → 0.2.19
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/manager.cjs +94 -19
- package/dist/callbacks/manager.d.ts +3 -1
- package/dist/callbacks/manager.js +94 -19
- package/dist/messages/base.cjs +16 -1
- package/dist/messages/base.d.ts +9 -0
- package/dist/messages/base.js +14 -0
- package/dist/messages/tool.cjs +23 -0
- package/dist/messages/tool.d.ts +15 -0
- package/dist/messages/tool.js +24 -1
- package/dist/messages/utils.cjs +1 -1
- package/dist/messages/utils.js +1 -1
- package/dist/output_parsers/string.cjs +1 -0
- package/dist/output_parsers/string.js +1 -0
- package/dist/runnables/base.cjs +1 -3
- package/dist/runnables/base.js +1 -3
- package/dist/runnables/remote.cjs +3 -1
- package/dist/runnables/remote.js +3 -1
- package/dist/tools/index.cjs +15 -6
- package/dist/tools/index.d.ts +21 -12
- package/dist/tools/index.js +15 -6
- package/dist/utils/testing/index.cjs +161 -3
- package/dist/utils/testing/index.d.ts +94 -3
- package/dist/utils/testing/index.js +160 -3
- package/package.json +4 -3
- package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
- package/dist/caches/tests/in_memory_cache.test.js +0 -33
- package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
- package/dist/callbacks/tests/callbacks.test.js +0 -495
- package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
- package/dist/callbacks/tests/manager.int.test.js +0 -29
- package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
- package/dist/callbacks/tests/run_collector.test.js +0 -58
- package/dist/language_models/tests/chat_models.test.d.ts +0 -1
- package/dist/language_models/tests/chat_models.test.js +0 -204
- package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
- package/dist/language_models/tests/count_tokens.test.js +0 -19
- package/dist/language_models/tests/llms.test.d.ts +0 -1
- package/dist/language_models/tests/llms.test.js +0 -52
- package/dist/messages/tests/base_message.test.d.ts +0 -1
- package/dist/messages/tests/base_message.test.js +0 -245
- package/dist/messages/tests/message_utils.test.d.ts +0 -1
- package/dist/messages/tests/message_utils.test.js +0 -434
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
- package/dist/output_parsers/tests/json.test.d.ts +0 -1
- package/dist/output_parsers/tests/json.test.js +0 -427
- package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
- package/dist/output_parsers/tests/output_parser.test.js +0 -78
- package/dist/output_parsers/tests/string.test.d.ts +0 -1
- package/dist/output_parsers/tests/string.test.js +0 -68
- package/dist/output_parsers/tests/structured.test.d.ts +0 -1
- package/dist/output_parsers/tests/structured.test.js +0 -166
- package/dist/output_parsers/tests/xml.test.d.ts +0 -1
- package/dist/output_parsers/tests/xml.test.js +0 -81
- package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
- package/dist/prompts/tests/chat.mustache.test.js +0 -129
- package/dist/prompts/tests/chat.test.d.ts +0 -1
- package/dist/prompts/tests/chat.test.js +0 -557
- package/dist/prompts/tests/few_shot.test.d.ts +0 -1
- package/dist/prompts/tests/few_shot.test.js +0 -224
- package/dist/prompts/tests/pipeline.test.d.ts +0 -1
- package/dist/prompts/tests/pipeline.test.js +0 -101
- package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
- package/dist/prompts/tests/prompt.mustache.test.js +0 -105
- package/dist/prompts/tests/prompt.test.d.ts +0 -1
- package/dist/prompts/tests/prompt.test.js +0 -78
- package/dist/prompts/tests/structured.test.d.ts +0 -1
- package/dist/prompts/tests/structured.test.js +0 -37
- package/dist/prompts/tests/template.test.d.ts +0 -1
- package/dist/prompts/tests/template.test.js +0 -24
- package/dist/runnables/tests/runnable.test.d.ts +0 -1
- package/dist/runnables/tests/runnable.test.js +0 -491
- package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_binding.test.js +0 -46
- package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_branch.test.js +0 -116
- package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_graph.test.js +0 -100
- package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_history.test.js +0 -177
- package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_interface.test.js +0 -209
- package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_map.test.js +0 -238
- package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
- package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
- package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_remote.test.js +0 -200
- package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_retry.test.js +0 -125
- package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
- package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
- package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
- package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_tools.test.js +0 -149
- package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
- package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
- package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
- package/dist/singletons/tests/async_local_storage.test.js +0 -153
- package/dist/structured_query/tests/utils.test.d.ts +0 -1
- package/dist/structured_query/tests/utils.test.js +0 -47
- package/dist/tools/tests/tools.test.d.ts +0 -1
- package/dist/tools/tests/tools.test.js +0 -85
- package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
- package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
- package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
- package/dist/tracers/tests/langsmith_interop.test.js +0 -551
- package/dist/tracers/tests/tracer.test.d.ts +0 -1
- package/dist/tracers/tests/tracer.test.js +0 -378
- package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
- package/dist/utils/testing/tests/chatfake.test.js +0 -112
- package/dist/utils/tests/async_caller.test.d.ts +0 -1
- package/dist/utils/tests/async_caller.test.js +0 -27
- package/dist/utils/tests/enviroment.test.d.ts +0 -1
- package/dist/utils/tests/enviroment.test.js +0 -6
- package/dist/utils/tests/function_calling.test.d.ts +0 -1
- package/dist/utils/tests/function_calling.test.js +0 -107
- package/dist/utils/tests/math_utils.test.d.ts +0 -1
- package/dist/utils/tests/math_utils.test.js +0 -139
- package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
- package/dist/utils/tests/polyfill_stream.test.js +0 -15
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
import { ChatPromptTemplate } from "../../prompts/chat.js";
|
|
2
|
-
import { RunnableSequence } from "../../runnables/base.js";
|
|
3
|
-
import { RunnablePassthrough } from "../../runnables/passthrough.js";
|
|
4
|
-
import { FakeStreamingLLM } from "../../utils/testing/index.js";
|
|
5
|
-
import { JsonOutputParser } from "../json.js";
|
|
6
|
-
const STREAMED_TOKENS = `
|
|
7
|
-
{
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
setup
|
|
11
|
-
":
|
|
12
|
-
"
|
|
13
|
-
Why
|
|
14
|
-
did
|
|
15
|
-
the
|
|
16
|
-
bears
|
|
17
|
-
start
|
|
18
|
-
a
|
|
19
|
-
band
|
|
20
|
-
called
|
|
21
|
-
Bears
|
|
22
|
-
Bears
|
|
23
|
-
Bears
|
|
24
|
-
?
|
|
25
|
-
"
|
|
26
|
-
,
|
|
27
|
-
"
|
|
28
|
-
punchline
|
|
29
|
-
":
|
|
30
|
-
"
|
|
31
|
-
Because
|
|
32
|
-
they
|
|
33
|
-
wanted
|
|
34
|
-
to
|
|
35
|
-
play
|
|
36
|
-
bear
|
|
37
|
-
-y
|
|
38
|
-
good
|
|
39
|
-
music
|
|
40
|
-
!
|
|
41
|
-
"
|
|
42
|
-
,
|
|
43
|
-
"
|
|
44
|
-
audience
|
|
45
|
-
":
|
|
46
|
-
[
|
|
47
|
-
"
|
|
48
|
-
Haha
|
|
49
|
-
"
|
|
50
|
-
,
|
|
51
|
-
"
|
|
52
|
-
So
|
|
53
|
-
funny
|
|
54
|
-
"
|
|
55
|
-
]
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
`.split("\n");
|
|
59
|
-
const EXPECTED_STREAMED_JSON = [
|
|
60
|
-
{},
|
|
61
|
-
{ setup: "" },
|
|
62
|
-
{ setup: "Why" },
|
|
63
|
-
{ setup: "Why did" },
|
|
64
|
-
{ setup: "Why did the" },
|
|
65
|
-
{ setup: "Why did the bears" },
|
|
66
|
-
{ setup: "Why did the bears start" },
|
|
67
|
-
{ setup: "Why did the bears start a" },
|
|
68
|
-
{ setup: "Why did the bears start a band" },
|
|
69
|
-
{ setup: "Why did the bears start a band called" },
|
|
70
|
-
{ setup: "Why did the bears start a band called Bears" },
|
|
71
|
-
{ setup: "Why did the bears start a band called Bears Bears" },
|
|
72
|
-
{ setup: "Why did the bears start a band called Bears Bears Bears" },
|
|
73
|
-
{ setup: "Why did the bears start a band called Bears Bears Bears ?" },
|
|
74
|
-
{
|
|
75
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
76
|
-
punchline: "",
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
80
|
-
punchline: "Because",
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
84
|
-
punchline: "Because they",
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
88
|
-
punchline: "Because they wanted",
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
92
|
-
punchline: "Because they wanted to",
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
96
|
-
punchline: "Because they wanted to play",
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
100
|
-
punchline: "Because they wanted to play bear",
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
104
|
-
punchline: "Because they wanted to play bear -y",
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
108
|
-
punchline: "Because they wanted to play bear -y good",
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
112
|
-
punchline: "Because they wanted to play bear -y good music",
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
116
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
120
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
121
|
-
audience: [],
|
|
122
|
-
},
|
|
123
|
-
{
|
|
124
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
125
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
126
|
-
audience: [""],
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
130
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
131
|
-
audience: ["Haha"],
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
135
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
136
|
-
audience: ["Haha", ""],
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
140
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
141
|
-
audience: ["Haha", "So"],
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
punchline: "Because they wanted to play bear -y good music !",
|
|
145
|
-
setup: "Why did the bears start a band called Bears Bears Bears ?",
|
|
146
|
-
audience: ["Haha", "So funny"],
|
|
147
|
-
},
|
|
148
|
-
];
|
|
149
|
-
const EXPECTED_STREAMED_JSON_DIFF = [
|
|
150
|
-
[{ op: "replace", path: "", value: {} }],
|
|
151
|
-
[{ op: "add", path: "/setup", value: "" }],
|
|
152
|
-
[{ op: "replace", path: "/setup", value: "Why" }],
|
|
153
|
-
[{ op: "replace", path: "/setup", value: "Why did" }],
|
|
154
|
-
[{ op: "replace", path: "/setup", value: "Why did the" }],
|
|
155
|
-
[{ op: "replace", path: "/setup", value: "Why did the bears" }],
|
|
156
|
-
[{ op: "replace", path: "/setup", value: "Why did the bears start" }],
|
|
157
|
-
[{ op: "replace", path: "/setup", value: "Why did the bears start a" }],
|
|
158
|
-
[{ op: "replace", path: "/setup", value: "Why did the bears start a band" }],
|
|
159
|
-
[
|
|
160
|
-
{
|
|
161
|
-
op: "replace",
|
|
162
|
-
path: "/setup",
|
|
163
|
-
value: "Why did the bears start a band called",
|
|
164
|
-
},
|
|
165
|
-
],
|
|
166
|
-
[
|
|
167
|
-
{
|
|
168
|
-
op: "replace",
|
|
169
|
-
path: "/setup",
|
|
170
|
-
value: "Why did the bears start a band called Bears",
|
|
171
|
-
},
|
|
172
|
-
],
|
|
173
|
-
[
|
|
174
|
-
{
|
|
175
|
-
op: "replace",
|
|
176
|
-
path: "/setup",
|
|
177
|
-
value: "Why did the bears start a band called Bears Bears",
|
|
178
|
-
},
|
|
179
|
-
],
|
|
180
|
-
[
|
|
181
|
-
{
|
|
182
|
-
op: "replace",
|
|
183
|
-
path: "/setup",
|
|
184
|
-
value: "Why did the bears start a band called Bears Bears Bears",
|
|
185
|
-
},
|
|
186
|
-
],
|
|
187
|
-
[
|
|
188
|
-
{
|
|
189
|
-
op: "replace",
|
|
190
|
-
path: "/setup",
|
|
191
|
-
value: "Why did the bears start a band called Bears Bears Bears ?",
|
|
192
|
-
},
|
|
193
|
-
],
|
|
194
|
-
[{ op: "add", path: "/punchline", value: "" }],
|
|
195
|
-
[{ op: "replace", path: "/punchline", value: "Because" }],
|
|
196
|
-
[{ op: "replace", path: "/punchline", value: "Because they" }],
|
|
197
|
-
[{ op: "replace", path: "/punchline", value: "Because they wanted" }],
|
|
198
|
-
[{ op: "replace", path: "/punchline", value: "Because they wanted to" }],
|
|
199
|
-
[{ op: "replace", path: "/punchline", value: "Because they wanted to play" }],
|
|
200
|
-
[
|
|
201
|
-
{
|
|
202
|
-
op: "replace",
|
|
203
|
-
path: "/punchline",
|
|
204
|
-
value: "Because they wanted to play bear",
|
|
205
|
-
},
|
|
206
|
-
],
|
|
207
|
-
[
|
|
208
|
-
{
|
|
209
|
-
op: "replace",
|
|
210
|
-
path: "/punchline",
|
|
211
|
-
value: "Because they wanted to play bear -y",
|
|
212
|
-
},
|
|
213
|
-
],
|
|
214
|
-
[
|
|
215
|
-
{
|
|
216
|
-
op: "replace",
|
|
217
|
-
path: "/punchline",
|
|
218
|
-
value: "Because they wanted to play bear -y good",
|
|
219
|
-
},
|
|
220
|
-
],
|
|
221
|
-
[
|
|
222
|
-
{
|
|
223
|
-
op: "replace",
|
|
224
|
-
path: "/punchline",
|
|
225
|
-
value: "Because they wanted to play bear -y good music",
|
|
226
|
-
},
|
|
227
|
-
],
|
|
228
|
-
[
|
|
229
|
-
{
|
|
230
|
-
op: "replace",
|
|
231
|
-
path: "/punchline",
|
|
232
|
-
value: "Because they wanted to play bear -y good music !",
|
|
233
|
-
},
|
|
234
|
-
],
|
|
235
|
-
[{ op: "add", path: "/audience", value: [] }],
|
|
236
|
-
[{ op: "add", path: "/audience/0", value: "" }],
|
|
237
|
-
[{ op: "replace", path: "/audience/0", value: "Haha" }],
|
|
238
|
-
[{ op: "add", path: "/audience/1", value: "" }],
|
|
239
|
-
[{ op: "replace", path: "/audience/1", value: "So" }],
|
|
240
|
-
[{ op: "replace", path: "/audience/1", value: "So funny" }],
|
|
241
|
-
];
|
|
242
|
-
async function acc(iter) {
|
|
243
|
-
const acc = [];
|
|
244
|
-
for await (const chunk of iter) {
|
|
245
|
-
acc.push(chunk);
|
|
246
|
-
}
|
|
247
|
-
return acc;
|
|
248
|
-
}
|
|
249
|
-
test("JSONOutputParser parses streamed JSON", async () => {
|
|
250
|
-
async function* generator() {
|
|
251
|
-
for (const token of STREAMED_TOKENS) {
|
|
252
|
-
yield token;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
const parser = new JsonOutputParser();
|
|
256
|
-
const result = await acc(parser.transform(generator(), {}));
|
|
257
|
-
expect(result).toEqual(EXPECTED_STREAMED_JSON);
|
|
258
|
-
await expect(parser.parse(STREAMED_TOKENS.join(""))).resolves.toEqual(EXPECTED_STREAMED_JSON[EXPECTED_STREAMED_JSON.length - 1]);
|
|
259
|
-
});
|
|
260
|
-
test("JSONOutputParser parses streamed JSON diff", async () => {
|
|
261
|
-
async function* generator() {
|
|
262
|
-
for (const token of STREAMED_TOKENS) {
|
|
263
|
-
yield token;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
const parser = new JsonOutputParser({ diff: true });
|
|
267
|
-
const result = await acc(parser.transform(generator(), {}));
|
|
268
|
-
expect(result).toEqual(EXPECTED_STREAMED_JSON_DIFF);
|
|
269
|
-
});
|
|
270
|
-
test("JsonOutputParser supports a type param", async () => {
|
|
271
|
-
const prompt = ChatPromptTemplate.fromTemplate(`{errors} {question} {cypher} {schema}`);
|
|
272
|
-
const llm = new FakeStreamingLLM({
|
|
273
|
-
responses: [`{"cypher":"testoutput","errors":["testerror"]}`],
|
|
274
|
-
});
|
|
275
|
-
const chain = RunnableSequence.from([
|
|
276
|
-
RunnablePassthrough.assign({
|
|
277
|
-
// Convert array of strings into single string
|
|
278
|
-
errors: ({ errors }) => Array.isArray(errors) ? errors.join("\n") : errors,
|
|
279
|
-
}),
|
|
280
|
-
prompt,
|
|
281
|
-
llm,
|
|
282
|
-
new JsonOutputParser(),
|
|
283
|
-
]);
|
|
284
|
-
const result = await chain.invoke({
|
|
285
|
-
question: "test",
|
|
286
|
-
cypher: "test",
|
|
287
|
-
schema: "test",
|
|
288
|
-
errors: ["test"],
|
|
289
|
-
});
|
|
290
|
-
expect(result).toEqual({
|
|
291
|
-
cypher: "testoutput",
|
|
292
|
-
errors: ["testerror"],
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
const GOOD_JSON = `\`\`\`json
|
|
296
|
-
{
|
|
297
|
-
"foo": "bar"
|
|
298
|
-
}
|
|
299
|
-
\`\`\``;
|
|
300
|
-
const JSON_WITH_NEW_LINES = `
|
|
301
|
-
|
|
302
|
-
\`\`\`json
|
|
303
|
-
{
|
|
304
|
-
"foo": "bar"
|
|
305
|
-
}
|
|
306
|
-
\`\`\`
|
|
307
|
-
|
|
308
|
-
`;
|
|
309
|
-
const JSON_WITH_NEW_LINES_INSIDE = `\`\`\`json
|
|
310
|
-
{
|
|
311
|
-
|
|
312
|
-
"foo": "bar"
|
|
313
|
-
|
|
314
|
-
}
|
|
315
|
-
\`\`\``;
|
|
316
|
-
const JSON_WITH_NEW_LINES_EVERYWHERE = `
|
|
317
|
-
|
|
318
|
-
\`\`\`json
|
|
319
|
-
|
|
320
|
-
{
|
|
321
|
-
|
|
322
|
-
"foo": "bar"
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
\`\`\`
|
|
327
|
-
|
|
328
|
-
`;
|
|
329
|
-
const TICKS_WITH_NEW_LINES_EVERYWHERE = `
|
|
330
|
-
|
|
331
|
-
\`\`\`
|
|
332
|
-
|
|
333
|
-
{
|
|
334
|
-
|
|
335
|
-
"foo": "bar"
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
\`\`\`
|
|
340
|
-
|
|
341
|
-
`;
|
|
342
|
-
const JSON_WITH_ESCAPED_DOUBLE_QUOTES_IN_NESTED_JSON = `\`\`\`json
|
|
343
|
-
{
|
|
344
|
-
"action": "Final Answer",
|
|
345
|
-
"action_input": "{\\"foo\\": \\"bar\\", \\"bar\\": \\"foo\\"}"
|
|
346
|
-
}
|
|
347
|
-
\`\`\``;
|
|
348
|
-
const NO_TICKS = `{
|
|
349
|
-
"foo": "bar"
|
|
350
|
-
}`;
|
|
351
|
-
const NO_TICKS_WHITE_SPACE = `
|
|
352
|
-
{
|
|
353
|
-
"foo": "bar"
|
|
354
|
-
}
|
|
355
|
-
`;
|
|
356
|
-
const TEXT_BEFORE = `Thought: I need to use the search tool
|
|
357
|
-
|
|
358
|
-
Action:
|
|
359
|
-
\`\`\`
|
|
360
|
-
{
|
|
361
|
-
"foo": "bar"
|
|
362
|
-
}
|
|
363
|
-
\`\`\``;
|
|
364
|
-
const TEXT_AFTER = `\`\`\`
|
|
365
|
-
{
|
|
366
|
-
"foo": "bar"
|
|
367
|
-
}
|
|
368
|
-
\`\`\`
|
|
369
|
-
This should do the trick`;
|
|
370
|
-
const TEXT_BEFORE_AND_AFTER = `Action: Testing
|
|
371
|
-
|
|
372
|
-
\`\`\`
|
|
373
|
-
{
|
|
374
|
-
"foo": "bar"
|
|
375
|
-
}
|
|
376
|
-
\`\`\`
|
|
377
|
-
This should do the trick`;
|
|
378
|
-
const TEST_CASES = [
|
|
379
|
-
GOOD_JSON,
|
|
380
|
-
JSON_WITH_NEW_LINES,
|
|
381
|
-
JSON_WITH_NEW_LINES_INSIDE,
|
|
382
|
-
JSON_WITH_NEW_LINES_EVERYWHERE,
|
|
383
|
-
TICKS_WITH_NEW_LINES_EVERYWHERE,
|
|
384
|
-
NO_TICKS,
|
|
385
|
-
NO_TICKS_WHITE_SPACE,
|
|
386
|
-
TEXT_BEFORE,
|
|
387
|
-
TEXT_AFTER,
|
|
388
|
-
TEXT_BEFORE_AND_AFTER,
|
|
389
|
-
];
|
|
390
|
-
const EXPECTED_JSON = {
|
|
391
|
-
foo: "bar",
|
|
392
|
-
};
|
|
393
|
-
for (const test_case of TEST_CASES) {
|
|
394
|
-
// eslint-disable-next-line no-loop-func
|
|
395
|
-
test(`JSONOutputParser parses ${test_case}`, async () => {
|
|
396
|
-
async function* generator() {
|
|
397
|
-
for (const token of test_case) {
|
|
398
|
-
yield token;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
const parser = new JsonOutputParser();
|
|
402
|
-
const result = await acc(parser.transform(generator(), {}));
|
|
403
|
-
expect(result[result.length - 1]).toEqual(EXPECTED_JSON);
|
|
404
|
-
await expect(parser.parse(test_case)).resolves.toEqual(EXPECTED_JSON);
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
const TEST_CASES_ESCAPED_QUOTES = [
|
|
408
|
-
JSON_WITH_ESCAPED_DOUBLE_QUOTES_IN_NESTED_JSON,
|
|
409
|
-
];
|
|
410
|
-
const EXPECTED_JSON_ESCAPED_QUOTES = {
|
|
411
|
-
action: "Final Answer",
|
|
412
|
-
action_input: '{"foo": "bar", "bar": "foo"}',
|
|
413
|
-
};
|
|
414
|
-
for (const test_case of TEST_CASES_ESCAPED_QUOTES) {
|
|
415
|
-
// eslint-disable-next-line no-loop-func
|
|
416
|
-
test(`JSONOutputParser parses ${test_case}`, async () => {
|
|
417
|
-
async function* generator() {
|
|
418
|
-
for (const token of test_case) {
|
|
419
|
-
yield token;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
const parser = new JsonOutputParser();
|
|
423
|
-
const result = await acc(parser.transform(generator(), {}));
|
|
424
|
-
expect(result[result.length - 1]).toEqual(EXPECTED_JSON_ESCAPED_QUOTES);
|
|
425
|
-
await expect(parser.parse(test_case)).resolves.toEqual(EXPECTED_JSON_ESCAPED_QUOTES);
|
|
426
|
-
});
|
|
427
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-loop-func */
|
|
2
|
-
/* eslint-disable no-promise-executor-return */
|
|
3
|
-
import { test } from "@jest/globals";
|
|
4
|
-
import { FakeStreamingLLM } from "../../utils/testing/index.js";
|
|
5
|
-
import { BytesOutputParser } from "../bytes.js";
|
|
6
|
-
import { CommaSeparatedListOutputParser, MarkdownListOutputParser, NumberedListOutputParser, } from "../list.js";
|
|
7
|
-
test("BytesOutputParser", async () => {
|
|
8
|
-
const llm = new FakeStreamingLLM({});
|
|
9
|
-
const stream = await llm.pipe(new BytesOutputParser()).stream("Hi there!");
|
|
10
|
-
const chunks = [];
|
|
11
|
-
const decoder = new TextDecoder();
|
|
12
|
-
for await (const chunk of stream) {
|
|
13
|
-
chunks.push(decoder.decode(chunk));
|
|
14
|
-
}
|
|
15
|
-
expect(chunks.length).toEqual("Hi there!".length);
|
|
16
|
-
expect(chunks.join("")).toEqual("Hi there!");
|
|
17
|
-
});
|
|
18
|
-
async function acc(iter) {
|
|
19
|
-
const acc = [];
|
|
20
|
-
for await (const chunk of iter) {
|
|
21
|
-
acc.push(chunk);
|
|
22
|
-
}
|
|
23
|
-
return acc;
|
|
24
|
-
}
|
|
25
|
-
const listTestCases = [
|
|
26
|
-
[CommaSeparatedListOutputParser, "a,b,c", ["a", "b", "c"]],
|
|
27
|
-
[CommaSeparatedListOutputParser, "a,b,c,", ["a", "b", "c", ""]],
|
|
28
|
-
[CommaSeparatedListOutputParser, "a", ["a"]],
|
|
29
|
-
[NumberedListOutputParser, "1. a\n2. b\n3. c", ["a", "b", "c"]],
|
|
30
|
-
[
|
|
31
|
-
NumberedListOutputParser,
|
|
32
|
-
"Items:\n\n1. apple\n\n2. banana\n\n3. cherry",
|
|
33
|
-
["apple", "banana", "cherry"],
|
|
34
|
-
],
|
|
35
|
-
[
|
|
36
|
-
NumberedListOutputParser,
|
|
37
|
-
`Your response should be a numbered list with each item on a new line. For example: \n\n1. foo\n\n2. bar\n\n3. baz`,
|
|
38
|
-
["foo", "bar", "baz"],
|
|
39
|
-
],
|
|
40
|
-
[NumberedListOutputParser, "No items in the list.", []],
|
|
41
|
-
[MarkdownListOutputParser, "- a\n - b\n- c", ["a", "b", "c"]],
|
|
42
|
-
[
|
|
43
|
-
MarkdownListOutputParser,
|
|
44
|
-
"Items:\n\n- apple\n\n- banana\n\n- cherry",
|
|
45
|
-
["apple", "banana", "cherry"],
|
|
46
|
-
],
|
|
47
|
-
[
|
|
48
|
-
MarkdownListOutputParser,
|
|
49
|
-
`Your response should be a numbered - not an item - list with each item on a new line. For example: \n\n- foo\n\n- bar\n\n- baz`,
|
|
50
|
-
["foo", "bar", "baz"],
|
|
51
|
-
],
|
|
52
|
-
[MarkdownListOutputParser, "No items in the list.", []],
|
|
53
|
-
[MarkdownListOutputParser, "* a\n * b\n* c", ["a", "b", "c"]],
|
|
54
|
-
[
|
|
55
|
-
MarkdownListOutputParser,
|
|
56
|
-
"Items:\n\n* apple\n\n* banana\n\n* cherry",
|
|
57
|
-
["apple", "banana", "cherry"],
|
|
58
|
-
],
|
|
59
|
-
[
|
|
60
|
-
MarkdownListOutputParser,
|
|
61
|
-
`Your response should be a numbered list with each item on a new line. For example: \n\n* foo\n\n* bar\n\n* baz`,
|
|
62
|
-
["foo", "bar", "baz"],
|
|
63
|
-
],
|
|
64
|
-
[MarkdownListOutputParser, "No items in the list.", []],
|
|
65
|
-
];
|
|
66
|
-
for (const [Parser, input, output] of listTestCases) {
|
|
67
|
-
test(`${Parser.name} parses ${input} to ${output}`, async () => {
|
|
68
|
-
async function* generator() {
|
|
69
|
-
for (const char of input) {
|
|
70
|
-
yield char;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const parser = new Parser();
|
|
74
|
-
const chunks = await acc(parser.transform(generator(), {}));
|
|
75
|
-
expect(chunks).toEqual(output.map((x) => [x]));
|
|
76
|
-
await expect(parser.parse(input)).resolves.toEqual(output);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { describe } from "@jest/globals";
|
|
2
|
-
import { StringOutputParser } from "../string.js";
|
|
3
|
-
import { AIMessage, } from "../../messages/index.js";
|
|
4
|
-
describe("StringOutputParser", () => {
|
|
5
|
-
test("string input", async () => {
|
|
6
|
-
const msg = "hello";
|
|
7
|
-
const parser = new StringOutputParser();
|
|
8
|
-
const result = await parser.invoke(msg);
|
|
9
|
-
expect(result).toEqual("hello");
|
|
10
|
-
});
|
|
11
|
-
test("BaseMessage string content", async () => {
|
|
12
|
-
const msg = new AIMessage({ content: "hello" });
|
|
13
|
-
const parser = new StringOutputParser();
|
|
14
|
-
const result = await parser.invoke(msg);
|
|
15
|
-
expect(result).toEqual("hello");
|
|
16
|
-
});
|
|
17
|
-
test("BaseMessage complex text type", async () => {
|
|
18
|
-
const parser = new StringOutputParser();
|
|
19
|
-
const content = [
|
|
20
|
-
{
|
|
21
|
-
type: "text",
|
|
22
|
-
text: "hello",
|
|
23
|
-
},
|
|
24
|
-
];
|
|
25
|
-
const msg = new AIMessage({
|
|
26
|
-
content,
|
|
27
|
-
});
|
|
28
|
-
const result = await parser.invoke(msg);
|
|
29
|
-
expect(result).toEqual("hello");
|
|
30
|
-
});
|
|
31
|
-
test("BaseMessage multiple complex text type", async () => {
|
|
32
|
-
const parser = new StringOutputParser();
|
|
33
|
-
const content = [
|
|
34
|
-
{
|
|
35
|
-
type: "text",
|
|
36
|
-
text: "hello",
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
type: "text",
|
|
40
|
-
text: "there",
|
|
41
|
-
},
|
|
42
|
-
];
|
|
43
|
-
const msg = new AIMessage({
|
|
44
|
-
content,
|
|
45
|
-
});
|
|
46
|
-
const result = await parser.invoke(msg);
|
|
47
|
-
expect(result).toEqual("hellothere");
|
|
48
|
-
});
|
|
49
|
-
test("BaseMessage complex text and image type fails", async () => {
|
|
50
|
-
const parser = new StringOutputParser();
|
|
51
|
-
const content = [
|
|
52
|
-
{
|
|
53
|
-
type: "text",
|
|
54
|
-
text: "hello",
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
type: "image_url",
|
|
58
|
-
image_url: "https://example.com/example.png",
|
|
59
|
-
},
|
|
60
|
-
];
|
|
61
|
-
const msg = new AIMessage({
|
|
62
|
-
content,
|
|
63
|
-
});
|
|
64
|
-
await expect(async () => {
|
|
65
|
-
await parser.invoke(msg);
|
|
66
|
-
}).rejects.toThrowError();
|
|
67
|
-
});
|
|
68
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|