@langchain/core 0.2.18-rc.0 → 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.
Files changed (128) hide show
  1. package/dist/callbacks/manager.cjs +94 -19
  2. package/dist/callbacks/manager.d.ts +3 -1
  3. package/dist/callbacks/manager.js +94 -19
  4. package/dist/messages/base.cjs +16 -1
  5. package/dist/messages/base.d.ts +9 -0
  6. package/dist/messages/base.js +14 -0
  7. package/dist/messages/tool.cjs +23 -0
  8. package/dist/messages/tool.d.ts +15 -0
  9. package/dist/messages/tool.js +24 -1
  10. package/dist/messages/utils.cjs +1 -1
  11. package/dist/messages/utils.js +1 -1
  12. package/dist/output_parsers/string.cjs +1 -0
  13. package/dist/output_parsers/string.js +1 -0
  14. package/dist/runnables/base.cjs +1 -3
  15. package/dist/runnables/base.js +1 -3
  16. package/dist/runnables/remote.cjs +3 -1
  17. package/dist/runnables/remote.js +3 -1
  18. package/dist/tools/index.cjs +15 -6
  19. package/dist/tools/index.d.ts +21 -12
  20. package/dist/tools/index.js +15 -6
  21. package/dist/utils/testing/index.cjs +161 -3
  22. package/dist/utils/testing/index.d.ts +94 -3
  23. package/dist/utils/testing/index.js +160 -3
  24. package/package.json +4 -3
  25. package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
  26. package/dist/caches/tests/in_memory_cache.test.js +0 -33
  27. package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
  28. package/dist/callbacks/tests/callbacks.test.js +0 -495
  29. package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
  30. package/dist/callbacks/tests/manager.int.test.js +0 -29
  31. package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
  32. package/dist/callbacks/tests/run_collector.test.js +0 -58
  33. package/dist/language_models/tests/chat_models.test.d.ts +0 -1
  34. package/dist/language_models/tests/chat_models.test.js +0 -204
  35. package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
  36. package/dist/language_models/tests/count_tokens.test.js +0 -19
  37. package/dist/language_models/tests/llms.test.d.ts +0 -1
  38. package/dist/language_models/tests/llms.test.js +0 -52
  39. package/dist/messages/tests/base_message.test.d.ts +0 -1
  40. package/dist/messages/tests/base_message.test.js +0 -245
  41. package/dist/messages/tests/message_utils.test.d.ts +0 -1
  42. package/dist/messages/tests/message_utils.test.js +0 -434
  43. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
  44. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
  45. package/dist/output_parsers/tests/json.test.d.ts +0 -1
  46. package/dist/output_parsers/tests/json.test.js +0 -427
  47. package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
  48. package/dist/output_parsers/tests/output_parser.test.js +0 -78
  49. package/dist/output_parsers/tests/string.test.d.ts +0 -1
  50. package/dist/output_parsers/tests/string.test.js +0 -68
  51. package/dist/output_parsers/tests/structured.test.d.ts +0 -1
  52. package/dist/output_parsers/tests/structured.test.js +0 -166
  53. package/dist/output_parsers/tests/xml.test.d.ts +0 -1
  54. package/dist/output_parsers/tests/xml.test.js +0 -81
  55. package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
  56. package/dist/prompts/tests/chat.mustache.test.js +0 -129
  57. package/dist/prompts/tests/chat.test.d.ts +0 -1
  58. package/dist/prompts/tests/chat.test.js +0 -557
  59. package/dist/prompts/tests/few_shot.test.d.ts +0 -1
  60. package/dist/prompts/tests/few_shot.test.js +0 -224
  61. package/dist/prompts/tests/pipeline.test.d.ts +0 -1
  62. package/dist/prompts/tests/pipeline.test.js +0 -101
  63. package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
  64. package/dist/prompts/tests/prompt.mustache.test.js +0 -105
  65. package/dist/prompts/tests/prompt.test.d.ts +0 -1
  66. package/dist/prompts/tests/prompt.test.js +0 -78
  67. package/dist/prompts/tests/structured.test.d.ts +0 -1
  68. package/dist/prompts/tests/structured.test.js +0 -37
  69. package/dist/prompts/tests/template.test.d.ts +0 -1
  70. package/dist/prompts/tests/template.test.js +0 -24
  71. package/dist/runnables/tests/runnable.test.d.ts +0 -1
  72. package/dist/runnables/tests/runnable.test.js +0 -491
  73. package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
  74. package/dist/runnables/tests/runnable_binding.test.js +0 -46
  75. package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
  76. package/dist/runnables/tests/runnable_branch.test.js +0 -116
  77. package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
  78. package/dist/runnables/tests/runnable_graph.test.js +0 -100
  79. package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
  80. package/dist/runnables/tests/runnable_history.test.js +0 -177
  81. package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
  82. package/dist/runnables/tests/runnable_interface.test.js +0 -209
  83. package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
  84. package/dist/runnables/tests/runnable_map.test.js +0 -238
  85. package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
  86. package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
  87. package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
  88. package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
  89. package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
  90. package/dist/runnables/tests/runnable_remote.test.js +0 -200
  91. package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
  92. package/dist/runnables/tests/runnable_retry.test.js +0 -125
  93. package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
  94. package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
  95. package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
  96. package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
  97. package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
  98. package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
  99. package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
  100. package/dist/runnables/tests/runnable_tools.test.js +0 -149
  101. package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
  102. package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
  103. package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
  104. package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
  105. package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
  106. package/dist/singletons/tests/async_local_storage.test.js +0 -153
  107. package/dist/structured_query/tests/utils.test.d.ts +0 -1
  108. package/dist/structured_query/tests/utils.test.js +0 -47
  109. package/dist/tools/tests/tools.test.d.ts +0 -1
  110. package/dist/tools/tests/tools.test.js +0 -85
  111. package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
  112. package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
  113. package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
  114. package/dist/tracers/tests/langsmith_interop.test.js +0 -551
  115. package/dist/tracers/tests/tracer.test.d.ts +0 -1
  116. package/dist/tracers/tests/tracer.test.js +0 -378
  117. package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
  118. package/dist/utils/testing/tests/chatfake.test.js +0 -112
  119. package/dist/utils/tests/async_caller.test.d.ts +0 -1
  120. package/dist/utils/tests/async_caller.test.js +0 -27
  121. package/dist/utils/tests/enviroment.test.d.ts +0 -1
  122. package/dist/utils/tests/enviroment.test.js +0 -6
  123. package/dist/utils/tests/function_calling.test.d.ts +0 -1
  124. package/dist/utils/tests/function_calling.test.js +0 -107
  125. package/dist/utils/tests/math_utils.test.d.ts +0 -1
  126. package/dist/utils/tests/math_utils.test.js +0 -139
  127. package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
  128. package/dist/utils/tests/polyfill_stream.test.js +0 -15
@@ -1,378 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,112 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
1
- import { test, expect, jest } from "@jest/globals";
2
- import { AsyncCaller } from "../async_caller.js";
3
- test("AsyncCaller passes on arguments and returns return value", async () => {
4
- const caller = new AsyncCaller({});
5
- const callable = jest.fn((arg1, arg2) => Promise.resolve([arg2, arg1]));
6
- const resultDirect = await callable(1, 2);
7
- const resultWrapped = await caller.call(callable, 1, 2);
8
- expect(resultDirect).toEqual([2, 1]);
9
- expect(resultWrapped).toEqual([2, 1]);
10
- });
11
- test("AsyncCaller retries on failure", async () => {
12
- const caller = new AsyncCaller({});
13
- // A direct call throws an error.
14
- let callable = jest
15
- .fn()
16
- .mockRejectedValueOnce("error")
17
- .mockResolvedValueOnce([2, 1]);
18
- await expect(() => callable()).rejects.toEqual("error");
19
- // A wrapped call retries and succeeds.
20
- callable = jest
21
- .fn()
22
- .mockRejectedValueOnce("error")
23
- .mockResolvedValueOnce([2, 1]);
24
- const resultWrapped = await caller.call(callable);
25
- expect(resultWrapped).toEqual([2, 1]);
26
- expect(callable.mock.calls).toHaveLength(2);
27
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,6 +0,0 @@
1
- import { getRuntimeEnvironment } from "../env.js";
2
- test("test getRuntimeEnvironment", async () => {
3
- const runtimeEnvironment = await getRuntimeEnvironment();
4
- console.log(runtimeEnvironment);
5
- expect(runtimeEnvironment.runtime).toEqual("node");
6
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,107 +0,0 @@
1
- import { z } from "zod";
2
- import { test, expect } from "@jest/globals";
3
- import { convertToOpenAIFunction, convertToOpenAITool, } from "../function_calling.js";
4
- import { FakeTool } from "../testing/index.js";
5
- test("Can convert tool to OpenAI Functions format", async () => {
6
- const tool = new FakeTool({
7
- name: "faketesttool",
8
- description: "A fake test tool",
9
- schema: z.object({
10
- prop1: z.string(),
11
- prop2: z.number().describe("Some desc"),
12
- optionalProp: z.optional(z.array(z.object({
13
- nestedRequired: z.string(),
14
- nestedOptional: z.optional(z.string()),
15
- }))),
16
- }),
17
- });
18
- const result = convertToOpenAIFunction(tool);
19
- expect(result).toEqual({
20
- name: "faketesttool",
21
- description: "A fake test tool",
22
- parameters: {
23
- type: "object",
24
- properties: {
25
- prop1: {
26
- type: "string",
27
- },
28
- prop2: {
29
- type: "number",
30
- description: "Some desc",
31
- },
32
- optionalProp: {
33
- type: "array",
34
- items: {
35
- type: "object",
36
- properties: {
37
- nestedRequired: {
38
- type: "string",
39
- },
40
- nestedOptional: {
41
- type: "string",
42
- },
43
- },
44
- required: ["nestedRequired"],
45
- additionalProperties: false,
46
- },
47
- },
48
- },
49
- required: ["prop1", "prop2"],
50
- additionalProperties: false,
51
- $schema: "http://json-schema.org/draft-07/schema#",
52
- },
53
- });
54
- });
55
- test("Can convert tool to OpenAI Tool format", async () => {
56
- const tool = new FakeTool({
57
- name: "faketesttool",
58
- description: "A fake test tool",
59
- schema: z.object({
60
- prop1: z.string(),
61
- prop2: z.number().describe("Some desc"),
62
- optionalProp: z.optional(z.array(z.object({
63
- nestedRequired: z.string(),
64
- nestedOptional: z.optional(z.string()),
65
- }))),
66
- }),
67
- });
68
- const result = convertToOpenAITool(tool);
69
- expect(result).toEqual({
70
- type: "function",
71
- function: {
72
- name: "faketesttool",
73
- description: "A fake test tool",
74
- parameters: {
75
- type: "object",
76
- properties: {
77
- prop1: {
78
- type: "string",
79
- },
80
- prop2: {
81
- type: "number",
82
- description: "Some desc",
83
- },
84
- optionalProp: {
85
- type: "array",
86
- items: {
87
- type: "object",
88
- properties: {
89
- nestedRequired: {
90
- type: "string",
91
- },
92
- nestedOptional: {
93
- type: "string",
94
- },
95
- },
96
- required: ["nestedRequired"],
97
- additionalProperties: false,
98
- },
99
- },
100
- },
101
- required: ["prop1", "prop2"],
102
- additionalProperties: false,
103
- $schema: "http://json-schema.org/draft-07/schema#",
104
- },
105
- },
106
- });
107
- });
@@ -1 +0,0 @@
1
- export {};