@langchain/core 0.2.18 → 0.2.20

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 (155) 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/language_models/base.d.ts +0 -10
  5. package/dist/language_models/chat_models.cjs +5 -6
  6. package/dist/language_models/chat_models.d.ts +9 -10
  7. package/dist/language_models/chat_models.js +5 -6
  8. package/dist/language_models/llms.cjs +5 -6
  9. package/dist/language_models/llms.d.ts +10 -12
  10. package/dist/language_models/llms.js +5 -6
  11. package/dist/messages/base.cjs +16 -1
  12. package/dist/messages/base.d.ts +9 -0
  13. package/dist/messages/base.js +14 -0
  14. package/dist/messages/tool.cjs +23 -0
  15. package/dist/messages/tool.d.ts +15 -0
  16. package/dist/messages/tool.js +24 -1
  17. package/dist/messages/utils.cjs +1 -1
  18. package/dist/messages/utils.js +1 -1
  19. package/dist/output_parsers/string.cjs +1 -0
  20. package/dist/output_parsers/string.js +1 -0
  21. package/dist/runnables/base.cjs +35 -12
  22. package/dist/runnables/base.js +35 -12
  23. package/dist/runnables/config.cjs +41 -0
  24. package/dist/runnables/config.js +41 -0
  25. package/dist/runnables/remote.cjs +17 -14
  26. package/dist/runnables/remote.js +17 -14
  27. package/dist/runnables/types.d.ts +10 -0
  28. package/dist/tools/index.cjs +15 -6
  29. package/dist/tools/index.d.ts +21 -12
  30. package/dist/tools/index.js +15 -6
  31. package/dist/utils/math.cjs +6 -4
  32. package/dist/utils/math.js +6 -4
  33. package/dist/utils/ml-distance/distances.cjs +18 -0
  34. package/dist/utils/ml-distance/distances.d.ts +8 -0
  35. package/dist/utils/ml-distance/distances.js +14 -0
  36. package/dist/utils/ml-distance/similarities.cjs +21 -0
  37. package/dist/utils/ml-distance/similarities.d.ts +7 -0
  38. package/dist/utils/ml-distance/similarities.js +17 -0
  39. package/dist/utils/ml-distance-euclidean/euclidean.cjs +15 -0
  40. package/dist/utils/ml-distance-euclidean/euclidean.d.ts +2 -0
  41. package/dist/utils/ml-distance-euclidean/euclidean.js +10 -0
  42. package/dist/utils/signal.cjs +28 -0
  43. package/dist/utils/signal.d.ts +1 -0
  44. package/dist/utils/signal.js +24 -0
  45. package/dist/utils/stream.cjs +19 -4
  46. package/dist/utils/stream.d.ts +3 -1
  47. package/dist/utils/stream.js +19 -4
  48. package/dist/utils/testing/index.cjs +168 -4
  49. package/dist/utils/testing/index.d.ts +100 -6
  50. package/dist/utils/testing/index.js +167 -4
  51. package/package.json +4 -4
  52. package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
  53. package/dist/caches/tests/in_memory_cache.test.js +0 -33
  54. package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
  55. package/dist/callbacks/tests/callbacks.test.js +0 -495
  56. package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
  57. package/dist/callbacks/tests/manager.int.test.js +0 -29
  58. package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
  59. package/dist/callbacks/tests/run_collector.test.js +0 -58
  60. package/dist/language_models/tests/chat_models.test.d.ts +0 -1
  61. package/dist/language_models/tests/chat_models.test.js +0 -204
  62. package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
  63. package/dist/language_models/tests/count_tokens.test.js +0 -19
  64. package/dist/language_models/tests/llms.test.d.ts +0 -1
  65. package/dist/language_models/tests/llms.test.js +0 -52
  66. package/dist/messages/tests/base_message.test.d.ts +0 -1
  67. package/dist/messages/tests/base_message.test.js +0 -245
  68. package/dist/messages/tests/message_utils.test.d.ts +0 -1
  69. package/dist/messages/tests/message_utils.test.js +0 -434
  70. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
  71. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
  72. package/dist/output_parsers/tests/json.test.d.ts +0 -1
  73. package/dist/output_parsers/tests/json.test.js +0 -427
  74. package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
  75. package/dist/output_parsers/tests/output_parser.test.js +0 -78
  76. package/dist/output_parsers/tests/string.test.d.ts +0 -1
  77. package/dist/output_parsers/tests/string.test.js +0 -68
  78. package/dist/output_parsers/tests/structured.test.d.ts +0 -1
  79. package/dist/output_parsers/tests/structured.test.js +0 -166
  80. package/dist/output_parsers/tests/xml.test.d.ts +0 -1
  81. package/dist/output_parsers/tests/xml.test.js +0 -81
  82. package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
  83. package/dist/prompts/tests/chat.mustache.test.js +0 -129
  84. package/dist/prompts/tests/chat.test.d.ts +0 -1
  85. package/dist/prompts/tests/chat.test.js +0 -557
  86. package/dist/prompts/tests/few_shot.test.d.ts +0 -1
  87. package/dist/prompts/tests/few_shot.test.js +0 -224
  88. package/dist/prompts/tests/pipeline.test.d.ts +0 -1
  89. package/dist/prompts/tests/pipeline.test.js +0 -101
  90. package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
  91. package/dist/prompts/tests/prompt.mustache.test.js +0 -105
  92. package/dist/prompts/tests/prompt.test.d.ts +0 -1
  93. package/dist/prompts/tests/prompt.test.js +0 -78
  94. package/dist/prompts/tests/structured.test.d.ts +0 -1
  95. package/dist/prompts/tests/structured.test.js +0 -37
  96. package/dist/prompts/tests/template.test.d.ts +0 -1
  97. package/dist/prompts/tests/template.test.js +0 -24
  98. package/dist/runnables/tests/runnable.test.d.ts +0 -1
  99. package/dist/runnables/tests/runnable.test.js +0 -491
  100. package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
  101. package/dist/runnables/tests/runnable_binding.test.js +0 -46
  102. package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
  103. package/dist/runnables/tests/runnable_branch.test.js +0 -116
  104. package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
  105. package/dist/runnables/tests/runnable_graph.test.js +0 -100
  106. package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
  107. package/dist/runnables/tests/runnable_history.test.js +0 -177
  108. package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
  109. package/dist/runnables/tests/runnable_interface.test.js +0 -209
  110. package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
  111. package/dist/runnables/tests/runnable_map.test.js +0 -238
  112. package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
  113. package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
  114. package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
  115. package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
  116. package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
  117. package/dist/runnables/tests/runnable_remote.test.js +0 -200
  118. package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
  119. package/dist/runnables/tests/runnable_retry.test.js +0 -125
  120. package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
  121. package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
  122. package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
  123. package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
  124. package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
  125. package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
  126. package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
  127. package/dist/runnables/tests/runnable_tools.test.js +0 -149
  128. package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
  129. package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
  130. package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
  131. package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
  132. package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
  133. package/dist/singletons/tests/async_local_storage.test.js +0 -153
  134. package/dist/structured_query/tests/utils.test.d.ts +0 -1
  135. package/dist/structured_query/tests/utils.test.js +0 -47
  136. package/dist/tools/tests/tools.test.d.ts +0 -1
  137. package/dist/tools/tests/tools.test.js +0 -85
  138. package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
  139. package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
  140. package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
  141. package/dist/tracers/tests/langsmith_interop.test.js +0 -551
  142. package/dist/tracers/tests/tracer.test.d.ts +0 -1
  143. package/dist/tracers/tests/tracer.test.js +0 -378
  144. package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
  145. package/dist/utils/testing/tests/chatfake.test.js +0 -112
  146. package/dist/utils/tests/async_caller.test.d.ts +0 -1
  147. package/dist/utils/tests/async_caller.test.js +0 -27
  148. package/dist/utils/tests/enviroment.test.d.ts +0 -1
  149. package/dist/utils/tests/enviroment.test.js +0 -6
  150. package/dist/utils/tests/function_calling.test.d.ts +0 -1
  151. package/dist/utils/tests/function_calling.test.js +0 -107
  152. package/dist/utils/tests/math_utils.test.d.ts +0 -1
  153. package/dist/utils/tests/math_utils.test.js +0 -139
  154. package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
  155. 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 {};