@langchain/core 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/callbacks/tests/callbacks.test.d.ts +1 -0
- package/dist/callbacks/tests/callbacks.test.js +492 -0
- package/dist/callbacks/tests/manager.int.test.d.ts +1 -0
- package/dist/callbacks/tests/manager.int.test.js +29 -0
- package/dist/callbacks/tests/run_collector.test.d.ts +1 -0
- package/dist/callbacks/tests/run_collector.test.js +58 -0
- package/dist/chat_history.cjs +13 -0
- package/dist/chat_history.d.ts +9 -0
- package/dist/chat_history.js +13 -0
- package/dist/language_models/chat_models.d.ts +2 -2
- package/dist/language_models/tests/chat_models.test.d.ts +1 -0
- package/dist/language_models/tests/chat_models.test.js +154 -0
- package/dist/language_models/tests/count_tokens.test.d.ts +1 -0
- package/dist/language_models/tests/count_tokens.test.js +19 -0
- package/dist/language_models/tests/llms.test.d.ts +1 -0
- package/dist/language_models/tests/llms.test.js +39 -0
- package/dist/messages/tests/base_message.test.d.ts +1 -0
- package/dist/messages/tests/base_message.test.js +97 -0
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +1 -0
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +81 -0
- package/dist/output_parsers/tests/json.test.d.ts +1 -0
- package/dist/output_parsers/tests/json.test.js +427 -0
- package/dist/output_parsers/tests/output_parser.test.d.ts +1 -0
- package/dist/output_parsers/tests/output_parser.test.js +78 -0
- package/dist/output_parsers/tests/string.test.d.ts +1 -0
- package/dist/output_parsers/tests/string.test.js +68 -0
- package/dist/output_parsers/tests/structured.test.d.ts +1 -0
- package/dist/output_parsers/tests/structured.test.js +166 -0
- package/dist/output_parsers/tests/xml.test.d.ts +1 -0
- package/dist/output_parsers/tests/xml.test.js +81 -0
- package/dist/prompts/tests/chat.mustache.test.d.ts +1 -0
- package/dist/prompts/tests/chat.mustache.test.js +61 -0
- package/dist/prompts/tests/chat.test.d.ts +1 -0
- package/dist/prompts/tests/chat.test.js +507 -0
- package/dist/prompts/tests/few_shot.test.d.ts +1 -0
- package/dist/prompts/tests/few_shot.test.js +224 -0
- package/dist/prompts/tests/pipeline.test.d.ts +1 -0
- package/dist/prompts/tests/pipeline.test.js +101 -0
- package/dist/prompts/tests/prompt.mustache.test.d.ts +1 -0
- package/dist/prompts/tests/prompt.mustache.test.js +85 -0
- package/dist/prompts/tests/prompt.test.d.ts +1 -0
- package/dist/prompts/tests/prompt.test.js +78 -0
- package/dist/prompts/tests/structured.test.d.ts +1 -0
- package/dist/prompts/tests/structured.test.js +37 -0
- package/dist/prompts/tests/template.test.d.ts +1 -0
- package/dist/prompts/tests/template.test.js +24 -0
- package/dist/runnables/base.cjs +87 -6
- package/dist/runnables/base.d.ts +45 -26
- package/dist/runnables/base.js +87 -6
- package/dist/runnables/history.cjs +87 -32
- package/dist/runnables/history.d.ts +1 -1
- package/dist/runnables/history.js +87 -32
- package/dist/runnables/remote.cjs +2 -2
- package/dist/runnables/remote.d.ts +3 -3
- package/dist/runnables/remote.js +2 -2
- package/dist/runnables/tests/runnable.test.d.ts +1 -0
- package/dist/runnables/tests/runnable.test.js +491 -0
- package/dist/runnables/tests/runnable_binding.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_binding.test.js +46 -0
- package/dist/runnables/tests/runnable_branch.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_branch.test.js +116 -0
- package/dist/runnables/tests/runnable_graph.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_graph.test.js +84 -0
- package/dist/runnables/tests/runnable_history.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_history.test.js +177 -0
- package/dist/runnables/tests/runnable_interface.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_interface.test.js +209 -0
- package/dist/runnables/tests/runnable_map.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_map.test.js +238 -0
- package/dist/runnables/tests/runnable_passthrough.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_passthrough.test.js +96 -0
- package/dist/runnables/tests/runnable_remote.int.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_remote.int.test.js +138 -0
- package/dist/runnables/tests/runnable_remote.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_remote.test.js +200 -0
- package/dist/runnables/tests/runnable_retry.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_retry.test.js +125 -0
- package/dist/runnables/tests/runnable_stream_events.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_stream_events.test.js +1013 -0
- package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +973 -0
- package/dist/runnables/tests/runnable_stream_log.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_stream_log.test.js +282 -0
- package/dist/runnables/tests/runnable_tracing.int.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_tracing.int.test.js +37 -0
- package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_with_fallbacks.test.js +36 -0
- package/dist/runnables/utils.d.ts +1 -1
- package/dist/singletons/tests/async_local_storage.test.d.ts +1 -0
- package/dist/singletons/tests/async_local_storage.test.js +120 -0
- package/dist/structured_query/tests/utils.test.d.ts +1 -0
- package/dist/structured_query/tests/utils.test.js +47 -0
- package/dist/tracers/event_stream.cjs +493 -0
- package/dist/tracers/event_stream.d.ts +137 -0
- package/dist/tracers/event_stream.js +489 -0
- package/dist/tracers/log_stream.d.ts +2 -77
- package/dist/tracers/tests/langchain_tracer.int.test.d.ts +1 -0
- package/dist/tracers/tests/langchain_tracer.int.test.js +74 -0
- package/dist/tracers/tests/tracer.test.d.ts +1 -0
- package/dist/tracers/tests/tracer.test.js +378 -0
- package/dist/utils/testing/tests/chatfake.test.d.ts +1 -0
- package/dist/utils/testing/tests/chatfake.test.js +112 -0
- package/dist/utils/tests/async_caller.test.d.ts +1 -0
- package/dist/utils/tests/async_caller.test.js +27 -0
- package/dist/utils/tests/enviroment.test.d.ts +1 -0
- package/dist/utils/tests/enviroment.test.js +6 -0
- package/dist/utils/tests/function_calling.test.d.ts +1 -0
- package/dist/utils/tests/function_calling.test.js +107 -0
- package/dist/utils/tests/math_utils.test.d.ts +1 -0
- package/dist/utils/tests/math_utils.test.js +139 -0
- package/dist/utils/tests/polyfill_stream.test.d.ts +1 -0
- package/dist/utils/tests/polyfill_stream.test.js +15 -0
- package/package.json +6 -6
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
import { expect, test } from "@jest/globals";
|
|
2
|
+
import { AIMessagePromptTemplate, ChatPromptTemplate, ChatMessagePromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, MessagesPlaceholder, } from "../chat.js";
|
|
3
|
+
import { PromptTemplate } from "../prompt.js";
|
|
4
|
+
import { SystemMessage, HumanMessage, AIMessage, ChatMessage, FunctionMessage, } from "../../messages/index.js";
|
|
5
|
+
function createChatPromptTemplate() {
|
|
6
|
+
const systemPrompt = new PromptTemplate({
|
|
7
|
+
template: "Here's some context: {context}",
|
|
8
|
+
inputVariables: ["context"],
|
|
9
|
+
});
|
|
10
|
+
const userPrompt = new PromptTemplate({
|
|
11
|
+
template: "Hello {foo}, I'm {bar}. Thanks for the {context}",
|
|
12
|
+
inputVariables: ["foo", "bar", "context"],
|
|
13
|
+
});
|
|
14
|
+
const aiPrompt = new PromptTemplate({
|
|
15
|
+
template: "I'm an AI. I'm {foo}. I'm {bar}.",
|
|
16
|
+
inputVariables: ["foo", "bar"],
|
|
17
|
+
});
|
|
18
|
+
const genericPrompt = new PromptTemplate({
|
|
19
|
+
template: "I'm a generic message. I'm {foo}. I'm {bar}.",
|
|
20
|
+
inputVariables: ["foo", "bar"],
|
|
21
|
+
});
|
|
22
|
+
// return new ChatPromptTemplate({
|
|
23
|
+
// promptMessages: [
|
|
24
|
+
// new SystemMessagePromptTemplate(systemPrompt),
|
|
25
|
+
// new HumanMessagePromptTemplate(userPrompt),
|
|
26
|
+
// new AIMessagePromptTemplate({ prompt: aiPrompt }),
|
|
27
|
+
// new ChatMessagePromptTemplate(genericPrompt, "test"),
|
|
28
|
+
// ],
|
|
29
|
+
// inputVariables: ["context", "foo", "bar"],
|
|
30
|
+
// });
|
|
31
|
+
return ChatPromptTemplate.fromMessages([
|
|
32
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
33
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
34
|
+
new AIMessagePromptTemplate({ prompt: aiPrompt }),
|
|
35
|
+
new ChatMessagePromptTemplate(genericPrompt, "test"),
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
test("Test format", async () => {
|
|
39
|
+
const chatPrompt = createChatPromptTemplate();
|
|
40
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
41
|
+
context: "This is a context",
|
|
42
|
+
foo: "Foo",
|
|
43
|
+
bar: "Bar",
|
|
44
|
+
unused: "extra",
|
|
45
|
+
});
|
|
46
|
+
expect(messages.toChatMessages()).toEqual([
|
|
47
|
+
new SystemMessage("Here's some context: This is a context"),
|
|
48
|
+
new HumanMessage("Hello Foo, I'm Bar. Thanks for the This is a context"),
|
|
49
|
+
new AIMessage("I'm an AI. I'm Foo. I'm Bar."),
|
|
50
|
+
new ChatMessage("I'm a generic message. I'm Foo. I'm Bar.", "test"),
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
test("Test format with invalid input values", async () => {
|
|
54
|
+
const chatPrompt = createChatPromptTemplate();
|
|
55
|
+
await expect(
|
|
56
|
+
// @ts-expect-error TS compiler should flag missing input variables
|
|
57
|
+
chatPrompt.formatPromptValue({
|
|
58
|
+
context: "This is a context",
|
|
59
|
+
foo: "Foo",
|
|
60
|
+
})).rejects.toThrow("Missing value for input variable `bar`");
|
|
61
|
+
});
|
|
62
|
+
test("Test format with invalid input variables", async () => {
|
|
63
|
+
const systemPrompt = new PromptTemplate({
|
|
64
|
+
template: "Here's some context: {context}",
|
|
65
|
+
inputVariables: ["context"],
|
|
66
|
+
});
|
|
67
|
+
const userPrompt = new PromptTemplate({
|
|
68
|
+
template: "Hello {foo}, I'm {bar}",
|
|
69
|
+
inputVariables: ["foo", "bar"],
|
|
70
|
+
});
|
|
71
|
+
expect(() => new ChatPromptTemplate({
|
|
72
|
+
promptMessages: [
|
|
73
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
74
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
75
|
+
],
|
|
76
|
+
inputVariables: ["context", "foo", "bar", "baz"],
|
|
77
|
+
})).toThrow("Input variables `baz` are not used in any of the prompt messages.");
|
|
78
|
+
expect(() => new ChatPromptTemplate({
|
|
79
|
+
promptMessages: [
|
|
80
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
81
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
82
|
+
],
|
|
83
|
+
inputVariables: ["context", "foo"],
|
|
84
|
+
})).toThrow("Input variables `bar` are used in prompt messages but not in the prompt template.");
|
|
85
|
+
});
|
|
86
|
+
test("Test fromTemplate", async () => {
|
|
87
|
+
const chatPrompt = ChatPromptTemplate.fromTemplate("Hello {foo}, I'm {bar}");
|
|
88
|
+
expect(chatPrompt.inputVariables).toEqual(["foo", "bar"]);
|
|
89
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
90
|
+
foo: "Foo",
|
|
91
|
+
bar: "Bar",
|
|
92
|
+
});
|
|
93
|
+
expect(messages.toChatMessages()).toEqual([
|
|
94
|
+
new HumanMessage("Hello Foo, I'm Bar"),
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
test("Test fromMessages", async () => {
|
|
98
|
+
const systemPrompt = new PromptTemplate({
|
|
99
|
+
template: "Here's some context: {context}",
|
|
100
|
+
inputVariables: ["context"],
|
|
101
|
+
});
|
|
102
|
+
const userPrompt = new PromptTemplate({
|
|
103
|
+
template: "Hello {foo}, I'm {bar}",
|
|
104
|
+
inputVariables: ["foo", "bar"],
|
|
105
|
+
});
|
|
106
|
+
// TODO: Fix autocomplete for the fromMessages method
|
|
107
|
+
const chatPrompt = ChatPromptTemplate.fromMessages([
|
|
108
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
109
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
110
|
+
]);
|
|
111
|
+
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
|
|
112
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
113
|
+
context: "This is a context",
|
|
114
|
+
foo: "Foo",
|
|
115
|
+
bar: "Bar",
|
|
116
|
+
});
|
|
117
|
+
expect(messages.toChatMessages()).toEqual([
|
|
118
|
+
new SystemMessage("Here's some context: This is a context"),
|
|
119
|
+
new HumanMessage("Hello Foo, I'm Bar"),
|
|
120
|
+
]);
|
|
121
|
+
});
|
|
122
|
+
test("Test fromMessages with a variety of ways to declare prompt messages", async () => {
|
|
123
|
+
const systemPrompt = new PromptTemplate({
|
|
124
|
+
template: "Here's some context: {context}",
|
|
125
|
+
inputVariables: ["context"],
|
|
126
|
+
});
|
|
127
|
+
// TODO: Fix autocomplete for the fromMessages method
|
|
128
|
+
const chatPrompt = ChatPromptTemplate.fromMessages([
|
|
129
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
130
|
+
"Hello {foo}, I'm {bar}",
|
|
131
|
+
["assistant", "Nice to meet you, {bar}!"],
|
|
132
|
+
["human", "Thanks {foo}!!"],
|
|
133
|
+
]);
|
|
134
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
135
|
+
context: "This is a context",
|
|
136
|
+
foo: "Foo",
|
|
137
|
+
bar: "Bar",
|
|
138
|
+
});
|
|
139
|
+
expect(messages.toChatMessages()).toEqual([
|
|
140
|
+
new SystemMessage("Here's some context: This is a context"),
|
|
141
|
+
new HumanMessage("Hello Foo, I'm Bar"),
|
|
142
|
+
new AIMessage("Nice to meet you, Bar!"),
|
|
143
|
+
new HumanMessage("Thanks Foo!!"),
|
|
144
|
+
]);
|
|
145
|
+
});
|
|
146
|
+
test("Test fromMessages with an extra input variable", async () => {
|
|
147
|
+
const systemPrompt = new PromptTemplate({
|
|
148
|
+
template: "Here's some context: {context}",
|
|
149
|
+
inputVariables: ["context"],
|
|
150
|
+
});
|
|
151
|
+
const userPrompt = new PromptTemplate({
|
|
152
|
+
template: "Hello {foo}, I'm {bar}",
|
|
153
|
+
inputVariables: ["foo", "bar"],
|
|
154
|
+
});
|
|
155
|
+
// TODO: Fix autocomplete for the fromMessages method
|
|
156
|
+
const chatPrompt = ChatPromptTemplate.fromMessages([
|
|
157
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
158
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
159
|
+
]);
|
|
160
|
+
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
|
|
161
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
162
|
+
context: "This is a context",
|
|
163
|
+
foo: "Foo",
|
|
164
|
+
bar: "Bar",
|
|
165
|
+
unused: "No problemo!",
|
|
166
|
+
});
|
|
167
|
+
expect(messages.toChatMessages()).toEqual([
|
|
168
|
+
new SystemMessage("Here's some context: This is a context"),
|
|
169
|
+
new HumanMessage("Hello Foo, I'm Bar"),
|
|
170
|
+
]);
|
|
171
|
+
});
|
|
172
|
+
test("Test fromMessages is composable", async () => {
|
|
173
|
+
const systemPrompt = new PromptTemplate({
|
|
174
|
+
template: "Here's some context: {context}",
|
|
175
|
+
inputVariables: ["context"],
|
|
176
|
+
});
|
|
177
|
+
const userPrompt = new PromptTemplate({
|
|
178
|
+
template: "Hello {foo}, I'm {bar}",
|
|
179
|
+
inputVariables: ["foo", "bar"],
|
|
180
|
+
});
|
|
181
|
+
const chatPromptInner = ChatPromptTemplate.fromMessages([
|
|
182
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
183
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
184
|
+
]);
|
|
185
|
+
const chatPrompt = ChatPromptTemplate.fromMessages([
|
|
186
|
+
chatPromptInner,
|
|
187
|
+
AIMessagePromptTemplate.fromTemplate("I'm an AI. I'm {foo}. I'm {bar}."),
|
|
188
|
+
]);
|
|
189
|
+
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
|
|
190
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
191
|
+
context: "This is a context",
|
|
192
|
+
foo: "Foo",
|
|
193
|
+
bar: "Bar",
|
|
194
|
+
});
|
|
195
|
+
expect(messages.toChatMessages()).toEqual([
|
|
196
|
+
new SystemMessage("Here's some context: This is a context"),
|
|
197
|
+
new HumanMessage("Hello Foo, I'm Bar"),
|
|
198
|
+
new AIMessage("I'm an AI. I'm Foo. I'm Bar."),
|
|
199
|
+
]);
|
|
200
|
+
});
|
|
201
|
+
test("Test fromMessages is composable with partial vars", async () => {
|
|
202
|
+
const systemPrompt = new PromptTemplate({
|
|
203
|
+
template: "Here's some context: {context}",
|
|
204
|
+
inputVariables: ["context"],
|
|
205
|
+
});
|
|
206
|
+
const userPrompt = new PromptTemplate({
|
|
207
|
+
template: "Hello {foo}, I'm {bar}",
|
|
208
|
+
inputVariables: ["foo", "bar"],
|
|
209
|
+
});
|
|
210
|
+
const chatPromptInner = ChatPromptTemplate.fromMessages([
|
|
211
|
+
new SystemMessagePromptTemplate(systemPrompt),
|
|
212
|
+
new HumanMessagePromptTemplate(userPrompt),
|
|
213
|
+
]);
|
|
214
|
+
const chatPrompt = ChatPromptTemplate.fromMessages([
|
|
215
|
+
await chatPromptInner.partial({
|
|
216
|
+
context: "This is a context",
|
|
217
|
+
foo: "Foo",
|
|
218
|
+
}),
|
|
219
|
+
AIMessagePromptTemplate.fromTemplate("I'm an AI. I'm {foo}. I'm {bar}."),
|
|
220
|
+
]);
|
|
221
|
+
expect(chatPrompt.inputVariables).toEqual(["bar"]);
|
|
222
|
+
const messages = await chatPrompt.formatPromptValue({
|
|
223
|
+
bar: "Bar",
|
|
224
|
+
});
|
|
225
|
+
expect(messages.toChatMessages()).toEqual([
|
|
226
|
+
new SystemMessage("Here's some context: This is a context"),
|
|
227
|
+
new HumanMessage("Hello Foo, I'm Bar"),
|
|
228
|
+
new AIMessage("I'm an AI. I'm Foo. I'm Bar."),
|
|
229
|
+
]);
|
|
230
|
+
});
|
|
231
|
+
test("Test SimpleMessagePromptTemplate", async () => {
|
|
232
|
+
const prompt = new MessagesPlaceholder("foo");
|
|
233
|
+
const values = { foo: [new HumanMessage("Hello Foo, I'm Bar")] };
|
|
234
|
+
const messages = await prompt.formatMessages(values);
|
|
235
|
+
expect(messages).toEqual([new HumanMessage("Hello Foo, I'm Bar")]);
|
|
236
|
+
});
|
|
237
|
+
test("Test MessagesPlaceholder optional", async () => {
|
|
238
|
+
const prompt = new MessagesPlaceholder({
|
|
239
|
+
variableName: "foo",
|
|
240
|
+
optional: true,
|
|
241
|
+
});
|
|
242
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
243
|
+
const messages = await prompt.formatMessages({});
|
|
244
|
+
expect(messages).toEqual([]);
|
|
245
|
+
});
|
|
246
|
+
test("Test MessagesPlaceholder optional in a chat prompt template", async () => {
|
|
247
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
248
|
+
new MessagesPlaceholder({
|
|
249
|
+
variableName: "foo",
|
|
250
|
+
optional: true,
|
|
251
|
+
}),
|
|
252
|
+
]);
|
|
253
|
+
const messages = await prompt.formatMessages({});
|
|
254
|
+
expect(messages).toEqual([]);
|
|
255
|
+
});
|
|
256
|
+
test("Test MessagesPlaceholder not optional", async () => {
|
|
257
|
+
const prompt = new MessagesPlaceholder({
|
|
258
|
+
variableName: "foo",
|
|
259
|
+
});
|
|
260
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
261
|
+
await expect(prompt.formatMessages({})).rejects.toThrow('Error: Field "foo" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages as an input value. Received: undefined');
|
|
262
|
+
});
|
|
263
|
+
test("Test MessagesPlaceholder shorthand in a chat prompt template should throw for invalid syntax", async () => {
|
|
264
|
+
expect(() => ChatPromptTemplate.fromMessages([["placeholder", "foo"]])).toThrow();
|
|
265
|
+
});
|
|
266
|
+
test("Test MessagesPlaceholder shorthand in a chat prompt template", async () => {
|
|
267
|
+
const prompt = ChatPromptTemplate.fromMessages([["placeholder", "{foo}"]]);
|
|
268
|
+
const messages = await prompt.formatMessages({
|
|
269
|
+
foo: [new HumanMessage("Hi there!"), new AIMessage("how r u")],
|
|
270
|
+
});
|
|
271
|
+
expect(messages).toEqual([
|
|
272
|
+
new HumanMessage("Hi there!"),
|
|
273
|
+
new AIMessage("how r u"),
|
|
274
|
+
]);
|
|
275
|
+
});
|
|
276
|
+
test("Test using partial", async () => {
|
|
277
|
+
const userPrompt = new PromptTemplate({
|
|
278
|
+
template: "{foo}{bar}",
|
|
279
|
+
inputVariables: ["foo", "bar"],
|
|
280
|
+
});
|
|
281
|
+
const prompt = new ChatPromptTemplate({
|
|
282
|
+
promptMessages: [new HumanMessagePromptTemplate(userPrompt)],
|
|
283
|
+
inputVariables: ["foo", "bar"],
|
|
284
|
+
});
|
|
285
|
+
const partialPrompt = await prompt.partial({ foo: "foo" });
|
|
286
|
+
// original prompt is not modified
|
|
287
|
+
expect(prompt.inputVariables).toEqual(["foo", "bar"]);
|
|
288
|
+
// partial prompt has only remaining variables
|
|
289
|
+
expect(partialPrompt.inputVariables).toEqual(["bar"]);
|
|
290
|
+
expect(await partialPrompt.format({ bar: "baz" })).toMatchInlineSnapshot(`"Human: foobaz"`);
|
|
291
|
+
});
|
|
292
|
+
test("Test BaseMessage", async () => {
|
|
293
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
294
|
+
new SystemMessage("You are a chatbot {mock_variable}"),
|
|
295
|
+
AIMessagePromptTemplate.fromTemplate("{name} is my name."),
|
|
296
|
+
new FunctionMessage({ content: "{}", name: "get_weather" }),
|
|
297
|
+
]);
|
|
298
|
+
const messages = await prompt.formatPromptValue({ name: "Bob" });
|
|
299
|
+
expect(prompt.inputVariables).toEqual(["name"]);
|
|
300
|
+
expect(prompt.partialVariables).toEqual({});
|
|
301
|
+
expect(messages.toChatMessages()).toEqual([
|
|
302
|
+
new SystemMessage("You are a chatbot {mock_variable}"),
|
|
303
|
+
new AIMessage("Bob is my name."),
|
|
304
|
+
new FunctionMessage({ content: "{}", name: "get_weather" }),
|
|
305
|
+
]);
|
|
306
|
+
});
|
|
307
|
+
test("Throws if trying to pass non BaseMessage inputs to MessagesPlaceholder", async () => {
|
|
308
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
309
|
+
["system", "some string"],
|
|
310
|
+
new MessagesPlaceholder("chatHistory"),
|
|
311
|
+
["human", "{question}"],
|
|
312
|
+
]);
|
|
313
|
+
const value = "this is not a valid input type!";
|
|
314
|
+
try {
|
|
315
|
+
await prompt.formatMessages({
|
|
316
|
+
chatHistory: value,
|
|
317
|
+
question: "What is the meaning of life?",
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
catch (e) {
|
|
321
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
322
|
+
if (e instanceof Error) {
|
|
323
|
+
expect(e.name).toBe("InputFormatError");
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
throw e;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
test("Does not throws if null or undefined is passed as input to MessagesPlaceholder", async () => {
|
|
331
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
332
|
+
["system", "some string"],
|
|
333
|
+
new MessagesPlaceholder("chatHistory"),
|
|
334
|
+
new MessagesPlaceholder("chatHistory2"),
|
|
335
|
+
["human", "{question}"],
|
|
336
|
+
]);
|
|
337
|
+
const value1 = null;
|
|
338
|
+
const value2 = undefined;
|
|
339
|
+
try {
|
|
340
|
+
await prompt.formatMessages({
|
|
341
|
+
chatHistory: value1,
|
|
342
|
+
chatHistory2: value2,
|
|
343
|
+
question: "What is the meaning of life?",
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
catch (e) {
|
|
347
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
348
|
+
if (e instanceof Error) {
|
|
349
|
+
expect(e.name).toBe("InputFormatError");
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
throw e;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
test("Multi part chat prompt template", async () => {
|
|
357
|
+
const name = "Bob";
|
|
358
|
+
const objectName = "chair";
|
|
359
|
+
const template = ChatPromptTemplate.fromMessages([
|
|
360
|
+
["system", "You are an AI assistant named {name}"],
|
|
361
|
+
[
|
|
362
|
+
"human",
|
|
363
|
+
[
|
|
364
|
+
{
|
|
365
|
+
type: "text",
|
|
366
|
+
text: "What is in this object {objectName}",
|
|
367
|
+
},
|
|
368
|
+
],
|
|
369
|
+
],
|
|
370
|
+
]);
|
|
371
|
+
const messages = await template.formatMessages({
|
|
372
|
+
name,
|
|
373
|
+
objectName,
|
|
374
|
+
});
|
|
375
|
+
expect(messages).toEqual([
|
|
376
|
+
new SystemMessage("You are an AI assistant named Bob"),
|
|
377
|
+
new HumanMessage({
|
|
378
|
+
content: [
|
|
379
|
+
{
|
|
380
|
+
type: "text",
|
|
381
|
+
text: "What is in this object chair",
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
}),
|
|
385
|
+
]);
|
|
386
|
+
});
|
|
387
|
+
test("Multi part chat prompt template with image", async () => {
|
|
388
|
+
const name = "Bob";
|
|
389
|
+
const objectName = "chair";
|
|
390
|
+
const myImage = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAA";
|
|
391
|
+
const myUrl = "https://www.example.com/image.png";
|
|
392
|
+
const template = ChatPromptTemplate.fromMessages([
|
|
393
|
+
["system", "You are an AI assistant named {name}"],
|
|
394
|
+
[
|
|
395
|
+
"human",
|
|
396
|
+
[
|
|
397
|
+
{
|
|
398
|
+
type: "image_url",
|
|
399
|
+
image_url: "data:image/jpeg;base64,{myImage}",
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
type: "text",
|
|
403
|
+
text: "What is in this object {objectName}",
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
type: "image_url",
|
|
407
|
+
image_url: {
|
|
408
|
+
url: "{myUrl}",
|
|
409
|
+
detail: "high",
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
],
|
|
413
|
+
],
|
|
414
|
+
]);
|
|
415
|
+
const messages = await template.formatMessages({
|
|
416
|
+
name,
|
|
417
|
+
objectName,
|
|
418
|
+
myImage,
|
|
419
|
+
myUrl,
|
|
420
|
+
});
|
|
421
|
+
expect(messages).toEqual([
|
|
422
|
+
new SystemMessage("You are an AI assistant named Bob"),
|
|
423
|
+
new HumanMessage({
|
|
424
|
+
content: [
|
|
425
|
+
{
|
|
426
|
+
type: "image_url",
|
|
427
|
+
image_url: {
|
|
428
|
+
url: `data:image/jpeg;base64,${myImage}`,
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
type: "text",
|
|
433
|
+
text: `What is in this object ${objectName}`,
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
type: "image_url",
|
|
437
|
+
image_url: {
|
|
438
|
+
url: `${myUrl}`,
|
|
439
|
+
detail: "high",
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
],
|
|
443
|
+
}),
|
|
444
|
+
]);
|
|
445
|
+
});
|
|
446
|
+
test("Multi-modal, multi part chat prompt works with instances of BaseMessage", async () => {
|
|
447
|
+
const name = "Bob";
|
|
448
|
+
const objectName = "chair";
|
|
449
|
+
const myImage = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAA";
|
|
450
|
+
const myUrl = "https://www.example.com/image.png";
|
|
451
|
+
const inlineImageUrl = new HumanMessage({
|
|
452
|
+
content: [
|
|
453
|
+
{
|
|
454
|
+
type: "image_url",
|
|
455
|
+
image_url: "data:image/jpeg;base64,{myImage}",
|
|
456
|
+
},
|
|
457
|
+
],
|
|
458
|
+
});
|
|
459
|
+
const objectImageUrl = new HumanMessage({
|
|
460
|
+
content: [
|
|
461
|
+
{
|
|
462
|
+
type: "image_url",
|
|
463
|
+
image_url: {
|
|
464
|
+
url: "data:image/jpeg;base64,{myImage}",
|
|
465
|
+
detail: "high",
|
|
466
|
+
},
|
|
467
|
+
},
|
|
468
|
+
],
|
|
469
|
+
});
|
|
470
|
+
const normalMessage = new HumanMessage({
|
|
471
|
+
content: [
|
|
472
|
+
{
|
|
473
|
+
type: "text",
|
|
474
|
+
text: "What is in this object {objectName}",
|
|
475
|
+
},
|
|
476
|
+
],
|
|
477
|
+
});
|
|
478
|
+
const template = ChatPromptTemplate.fromMessages([
|
|
479
|
+
["system", "You are an AI assistant named {name}"],
|
|
480
|
+
inlineImageUrl,
|
|
481
|
+
normalMessage,
|
|
482
|
+
objectImageUrl,
|
|
483
|
+
[
|
|
484
|
+
"human",
|
|
485
|
+
[
|
|
486
|
+
{
|
|
487
|
+
type: "text",
|
|
488
|
+
text: "What is in this object {objectName}",
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
type: "image_url",
|
|
492
|
+
image_url: {
|
|
493
|
+
url: "{myUrl}",
|
|
494
|
+
detail: "high",
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
],
|
|
498
|
+
],
|
|
499
|
+
]);
|
|
500
|
+
const messages = await template.formatMessages({
|
|
501
|
+
name,
|
|
502
|
+
objectName,
|
|
503
|
+
myImage,
|
|
504
|
+
myUrl,
|
|
505
|
+
});
|
|
506
|
+
expect(messages).toMatchSnapshot();
|
|
507
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|