@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.
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,434 +0,0 @@
1
- import { it, expect } from "@jest/globals";
2
- import { filterMessages, mergeMessageRuns, trimMessages, } from "../transformers.js";
3
- import { AIMessage } from "../ai.js";
4
- import { HumanMessage } from "../human.js";
5
- import { SystemMessage } from "../system.js";
6
- import { getBufferString } from "../utils.js";
7
- describe("filterMessage", () => {
8
- const getMessages = () => [
9
- new SystemMessage("you're a good assistant."),
10
- new HumanMessage({
11
- content: "what's your name",
12
- id: "foo",
13
- name: "example_user",
14
- }),
15
- new AIMessage({ content: "steve-o", id: "bar", name: "example_assistant" }),
16
- new HumanMessage({ content: "what's your favorite color", id: "baz" }),
17
- new AIMessage({ content: "silicon blue", id: "blah" }),
18
- ];
19
- it("works", () => {
20
- const messages = getMessages();
21
- const filteredMessages = filterMessages(messages, {
22
- includeNames: ["example_user", "example_assistant"],
23
- includeTypes: ["system"],
24
- excludeIds: ["bar"],
25
- });
26
- expect(filteredMessages).toEqual([
27
- new SystemMessage("you're a good assistant."),
28
- new HumanMessage({
29
- content: "what's your name",
30
- id: "foo",
31
- name: "example_user",
32
- }),
33
- ]);
34
- });
35
- it("can filter messages based on class types", () => {
36
- const messages = getMessages();
37
- const filteredMessages = filterMessages(messages, {
38
- includeTypes: [HumanMessage, AIMessage],
39
- });
40
- expect(filteredMessages).toHaveLength(4);
41
- expect(filteredMessages).toEqual([
42
- new HumanMessage({
43
- content: "what's your name",
44
- id: "foo",
45
- name: "example_user",
46
- }),
47
- new AIMessage({
48
- content: "steve-o",
49
- id: "bar",
50
- name: "example_assistant",
51
- }),
52
- new HumanMessage({ content: "what's your favorite color", id: "baz" }),
53
- new AIMessage({ content: "silicon blue", id: "blah" }),
54
- ]);
55
- });
56
- it("returns a runnable if no messages are passed", () => {
57
- const filteredMessagesRunnable = filterMessages();
58
- expect(filteredMessagesRunnable).toBeDefined();
59
- expect(filteredMessagesRunnable.lc_namespace).toEqual([
60
- "langchain_core",
61
- "runnables",
62
- ]);
63
- expect("func" in filteredMessagesRunnable).toBeTruthy();
64
- // `func` is protected, so we need to cast it to any to access it
65
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
- expect(typeof filteredMessagesRunnable.func).toBe("function");
67
- });
68
- });
69
- describe("mergeMessageRuns", () => {
70
- const getMessages = () => [
71
- new SystemMessage("you're a good assistant."),
72
- new HumanMessage({ content: "what's your favorite color", id: "foo" }),
73
- new HumanMessage({ content: "wait your favorite food", id: "bar" }),
74
- new AIMessage({
75
- content: "my favorite colo",
76
- tool_calls: [{ name: "blah_tool", args: { x: 2 }, id: "123" }],
77
- id: "baz",
78
- }),
79
- new AIMessage({
80
- content: [{ type: "text", text: "my favorite dish is lasagna" }],
81
- tool_calls: [{ name: "blah_tool", args: { x: -10 }, id: "456" }],
82
- id: "blur",
83
- }),
84
- ];
85
- it("works", () => {
86
- const messages = getMessages();
87
- const mergedMessages = mergeMessageRuns(messages);
88
- expect(mergedMessages).toHaveLength(3);
89
- expect(mergedMessages).toEqual([
90
- new SystemMessage("you're a good assistant."),
91
- new HumanMessage({
92
- content: "what's your favorite color\nwait your favorite food",
93
- id: "foo",
94
- }),
95
- new AIMessage({
96
- content: [
97
- { type: "text", text: "my favorite colo" },
98
- { type: "text", text: "my favorite dish is lasagna" },
99
- ],
100
- tool_calls: [
101
- { name: "blah_tool", args: { x: 2 }, id: "123", type: "tool_call" },
102
- { name: "blah_tool", args: { x: -10 }, id: "456", type: "tool_call" },
103
- ],
104
- id: "baz",
105
- }),
106
- ]);
107
- });
108
- it("returns a runnable if no messages are passed", () => {
109
- const mergedMessages = mergeMessageRuns();
110
- expect(mergedMessages).toBeDefined();
111
- expect(mergedMessages.lc_namespace).toEqual([
112
- "langchain_core",
113
- "runnables",
114
- ]);
115
- expect("func" in mergedMessages).toBeTruthy();
116
- // `func` is protected, so we need to cast it to any to access it
117
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
118
- expect(typeof mergedMessages.func).toBe("function");
119
- });
120
- });
121
- describe("trimMessages can trim", () => {
122
- const messagesAndTokenCounterFactory = () => {
123
- const messages = [
124
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
125
- new HumanMessage({
126
- content: "This is a 4 token text. The full message is 10 tokens.",
127
- id: "first",
128
- }),
129
- new AIMessage({
130
- content: [
131
- { type: "text", text: "This is the FIRST 4 token block." },
132
- { type: "text", text: "This is the SECOND 4 token block." },
133
- ],
134
- id: "second",
135
- }),
136
- new HumanMessage({
137
- content: "This is a 4 token text. The full message is 10 tokens.",
138
- id: "third",
139
- }),
140
- new AIMessage({
141
- content: "This is a 4 token text. The full message is 10 tokens.",
142
- id: "fourth",
143
- }),
144
- ];
145
- const dummyTokenCounter = (messages) => {
146
- // treat each message like it adds 3 default tokens at the beginning
147
- // of the message and at the end of the message. 3 + 4 + 3 = 10 tokens
148
- // per message.
149
- const defaultContentLen = 4;
150
- const defaultMsgPrefixLen = 3;
151
- const defaultMsgSuffixLen = 3;
152
- let count = 0;
153
- for (const msg of messages) {
154
- if (typeof msg.content === "string") {
155
- count +=
156
- defaultMsgPrefixLen + defaultContentLen + defaultMsgSuffixLen;
157
- }
158
- if (Array.isArray(msg.content)) {
159
- count +=
160
- defaultMsgPrefixLen +
161
- msg.content.length * defaultContentLen +
162
- defaultMsgSuffixLen;
163
- }
164
- }
165
- return count;
166
- };
167
- return {
168
- messages,
169
- dummyTokenCounter,
170
- };
171
- };
172
- it("First 30 tokens, not allowing partial messages", async () => {
173
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
174
- const trimmedMessages = await trimMessages(messages, {
175
- maxTokens: 30,
176
- tokenCounter: dummyTokenCounter,
177
- strategy: "first",
178
- });
179
- expect(trimmedMessages).toHaveLength(2);
180
- expect(trimmedMessages).toEqual([
181
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
182
- new HumanMessage({
183
- content: "This is a 4 token text. The full message is 10 tokens.",
184
- id: "first",
185
- }),
186
- ]);
187
- });
188
- it("First 30 tokens, allowing partial messages", async () => {
189
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
190
- const trimmedMessages = await trimMessages(messages, {
191
- maxTokens: 30,
192
- tokenCounter: dummyTokenCounter,
193
- strategy: "first",
194
- allowPartial: true,
195
- });
196
- expect(trimmedMessages).toHaveLength(3);
197
- expect(trimmedMessages).toEqual([
198
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
199
- new HumanMessage({
200
- content: "This is a 4 token text. The full message is 10 tokens.",
201
- id: "first",
202
- }),
203
- new AIMessage({
204
- content: [{ type: "text", text: "This is the FIRST 4 token block." }],
205
- id: "second",
206
- }),
207
- ]);
208
- });
209
- it("First 30 tokens, allowing partial messages, have to end on HumanMessage", async () => {
210
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
211
- const trimmedMessages = await trimMessages(messages, {
212
- maxTokens: 30,
213
- tokenCounter: dummyTokenCounter,
214
- strategy: "first",
215
- allowPartial: true,
216
- endOn: "human",
217
- });
218
- expect(trimmedMessages).toHaveLength(2);
219
- expect(trimmedMessages).toEqual([
220
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
221
- new HumanMessage({
222
- content: "This is a 4 token text. The full message is 10 tokens.",
223
- id: "first",
224
- }),
225
- ]);
226
- });
227
- it("Last 30 tokens, including system message, not allowing partial messages", async () => {
228
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
229
- const trimmedMessages = await trimMessages(messages, {
230
- maxTokens: 30,
231
- includeSystem: true,
232
- tokenCounter: dummyTokenCounter,
233
- strategy: "last",
234
- });
235
- expect(trimmedMessages).toHaveLength(3);
236
- expect(trimmedMessages).toEqual([
237
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
238
- new HumanMessage({
239
- content: "This is a 4 token text. The full message is 10 tokens.",
240
- id: "third",
241
- }),
242
- new AIMessage({
243
- content: "This is a 4 token text. The full message is 10 tokens.",
244
- id: "fourth",
245
- }),
246
- ]);
247
- });
248
- it("Last 40 tokens, including system message, allowing partial messages", async () => {
249
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
250
- const trimmedMessages = await trimMessages(messages, {
251
- maxTokens: 40,
252
- tokenCounter: dummyTokenCounter,
253
- strategy: "last",
254
- allowPartial: true,
255
- includeSystem: true,
256
- });
257
- expect(trimmedMessages).toHaveLength(4);
258
- expect(trimmedMessages).toEqual([
259
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
260
- new AIMessage({
261
- content: [{ type: "text", text: "This is the FIRST 4 token block." }],
262
- id: "second",
263
- }),
264
- new HumanMessage({
265
- content: "This is a 4 token text. The full message is 10 tokens.",
266
- id: "third",
267
- }),
268
- new AIMessage({
269
- content: "This is a 4 token text. The full message is 10 tokens.",
270
- id: "fourth",
271
- }),
272
- ]);
273
- });
274
- it("Last 30 tokens, including system message, allowing partial messages, end on HumanMessage", async () => {
275
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
276
- const trimmedMessages = await trimMessages(messages, {
277
- maxTokens: 30,
278
- tokenCounter: dummyTokenCounter,
279
- strategy: "last",
280
- endOn: "human",
281
- includeSystem: true,
282
- allowPartial: true,
283
- });
284
- expect(trimmedMessages).toHaveLength(3);
285
- expect(trimmedMessages).toEqual([
286
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
287
- new AIMessage({
288
- content: [{ type: "text", text: "This is the FIRST 4 token block." }],
289
- id: "second",
290
- }),
291
- new HumanMessage({
292
- content: "This is a 4 token text. The full message is 10 tokens.",
293
- id: "third",
294
- }),
295
- ]);
296
- });
297
- it("Last 40 tokens, including system message, allowing partial messages, start on HumanMessage", async () => {
298
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
299
- const trimmedMessages = await trimMessages(messages, {
300
- maxTokens: 40,
301
- tokenCounter: dummyTokenCounter,
302
- strategy: "last",
303
- includeSystem: true,
304
- allowPartial: true,
305
- startOn: "human",
306
- });
307
- expect(trimmedMessages).toHaveLength(3);
308
- console.log(trimmedMessages);
309
- expect(trimmedMessages).toEqual([
310
- new SystemMessage("This is a 4 token text. The full message is 10 tokens."),
311
- new HumanMessage({
312
- content: "This is a 4 token text. The full message is 10 tokens.",
313
- id: "third",
314
- }),
315
- new AIMessage({
316
- content: "This is a 4 token text. The full message is 10 tokens.",
317
- id: "fourth",
318
- }),
319
- ]);
320
- });
321
- it("can filter (startOn) with message classes", async () => {
322
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
323
- const trimmedMessages = await trimMessages(messages, {
324
- maxTokens: 40,
325
- tokenCounter: dummyTokenCounter,
326
- startOn: [HumanMessage],
327
- });
328
- expect(trimmedMessages).toHaveLength(2);
329
- expect(trimmedMessages).toEqual([
330
- new HumanMessage({
331
- content: "This is a 4 token text. The full message is 10 tokens.",
332
- id: "third",
333
- }),
334
- new AIMessage({
335
- content: "This is a 4 token text. The full message is 10 tokens.",
336
- id: "fourth",
337
- }),
338
- ]);
339
- });
340
- it("can filter (endOn) with message classes", async () => {
341
- const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
342
- const trimmedMessages = await trimMessages(messages, {
343
- maxTokens: 40,
344
- tokenCounter: dummyTokenCounter,
345
- endOn: [HumanMessage],
346
- });
347
- console.log(trimmedMessages);
348
- expect(trimmedMessages).toHaveLength(3);
349
- expect(trimmedMessages).toEqual([
350
- new HumanMessage({
351
- content: "This is a 4 token text. The full message is 10 tokens.",
352
- id: "first",
353
- }),
354
- new AIMessage({
355
- content: [
356
- { type: "text", text: "This is the FIRST 4 token block." },
357
- { type: "text", text: "This is the SECOND 4 token block." },
358
- ],
359
- id: "second",
360
- }),
361
- new HumanMessage({
362
- content: "This is a 4 token text. The full message is 10 tokens.",
363
- id: "third",
364
- }),
365
- ]);
366
- });
367
- it("can return a runnable if empty array is passed", () => {
368
- const { dummyTokenCounter } = messagesAndTokenCounterFactory();
369
- const trimmedMessages = trimMessages({
370
- maxTokens: 40,
371
- tokenCounter: dummyTokenCounter,
372
- });
373
- expect(trimmedMessages).toBeDefined();
374
- expect(trimmedMessages.lc_namespace).toEqual([
375
- "langchain_core",
376
- "runnables",
377
- ]);
378
- expect("func" in trimmedMessages).toBeTruthy();
379
- // `func` is protected, so we need to cast it to any to access it
380
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
381
- expect(typeof trimmedMessages.func).toBe("function");
382
- });
383
- });
384
- test("getBufferString can handle complex messages", () => {
385
- const messageArr1 = [new HumanMessage("Hello there!")];
386
- const messageArr2 = [
387
- new AIMessage({
388
- content: [
389
- {
390
- type: "text",
391
- text: "Hello there!",
392
- },
393
- ],
394
- }),
395
- ];
396
- const messageArr3 = [
397
- new HumanMessage({
398
- content: [
399
- {
400
- type: "image_url",
401
- image_url: {
402
- url: "https://example.com/image.jpg",
403
- },
404
- },
405
- {
406
- type: "image_url",
407
- image_url: "https://example.com/image.jpg",
408
- },
409
- ],
410
- }),
411
- ];
412
- const bufferString1 = getBufferString(messageArr1);
413
- expect(bufferString1).toBe("Human: Hello there!");
414
- const bufferString2 = getBufferString(messageArr2);
415
- expect(bufferString2).toBe(`AI: ${JSON.stringify([
416
- {
417
- type: "text",
418
- text: "Hello there!",
419
- },
420
- ], null, 2)}`);
421
- const bufferString3 = getBufferString(messageArr3);
422
- expect(bufferString3).toBe(`Human: ${JSON.stringify([
423
- {
424
- type: "image_url",
425
- image_url: {
426
- url: "https://example.com/image.jpg",
427
- },
428
- },
429
- {
430
- type: "image_url",
431
- image_url: "https://example.com/image.jpg",
432
- },
433
- ], null, 2)}`);
434
- });
@@ -1,81 +0,0 @@
1
- import { test } from "@jest/globals";
2
- import { z } from "zod";
3
- import { JsonOutputKeyToolsParser } from "../json_output_tools_parsers.js";
4
- import { AIMessage } from "../../../messages/index.js";
5
- import { OutputParserException } from "../../base.js";
6
- test("JSONOutputKeyToolsParser invoke", async () => {
7
- const outputParser = new JsonOutputKeyToolsParser({
8
- keyName: "testing",
9
- returnSingle: true,
10
- });
11
- const result = await outputParser.invoke(new AIMessage({
12
- content: "",
13
- additional_kwargs: {
14
- tool_calls: [
15
- {
16
- id: "test",
17
- type: "function",
18
- function: {
19
- name: "testing",
20
- arguments: JSON.stringify({ testKey: 9 }),
21
- },
22
- },
23
- ],
24
- },
25
- }));
26
- expect(result).toEqual({ testKey: 9 });
27
- });
28
- test("JSONOutputKeyToolsParser with a passed schema throws", async () => {
29
- const outputParser = new JsonOutputKeyToolsParser({
30
- keyName: "testing",
31
- returnSingle: true,
32
- zodSchema: z.object({
33
- testKey: z.string(),
34
- }),
35
- });
36
- try {
37
- await outputParser.invoke(new AIMessage({
38
- content: "",
39
- additional_kwargs: {
40
- tool_calls: [
41
- {
42
- id: "test",
43
- type: "function",
44
- function: {
45
- name: "testing",
46
- arguments: JSON.stringify({ testKey: 9 }),
47
- },
48
- },
49
- ],
50
- },
51
- }));
52
- }
53
- catch (e) {
54
- expect(e).toBeInstanceOf(OutputParserException);
55
- }
56
- });
57
- test("JSONOutputKeyToolsParser can validate a proper input", async () => {
58
- const outputParser = new JsonOutputKeyToolsParser({
59
- keyName: "testing",
60
- returnSingle: true,
61
- zodSchema: z.object({
62
- testKey: z.string(),
63
- }),
64
- });
65
- const result = await outputParser.invoke(new AIMessage({
66
- content: "",
67
- additional_kwargs: {
68
- tool_calls: [
69
- {
70
- id: "test",
71
- type: "function",
72
- function: {
73
- name: "testing",
74
- arguments: JSON.stringify({ testKey: "testval" }),
75
- },
76
- },
77
- ],
78
- },
79
- }));
80
- expect(result).toEqual({ testKey: "testval" });
81
- });
@@ -1 +0,0 @@
1
- export {};