-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathchat.test.ts
767 lines (721 loc) Β· 22.6 KB
/
chat.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect, test } from "@jest/globals";
import {
AIMessagePromptTemplate,
ChatPromptTemplate,
ChatMessagePromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
MessagesPlaceholder,
} from "../chat.js";
import { PromptTemplate } from "../prompt.js";
import {
SystemMessage,
HumanMessage,
AIMessage,
ChatMessage,
FunctionMessage,
} from "../../messages/index.js";
import { Document } from "../../documents/document.js";
function createChatPromptTemplate() {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}. Thanks for the {context}",
inputVariables: ["foo", "bar", "context"],
});
const aiPrompt = new PromptTemplate({
template: "I'm an AI. I'm {foo}. I'm {bar}.",
inputVariables: ["foo", "bar"],
});
const genericPrompt = new PromptTemplate({
template: "I'm a generic message. I'm {foo}. I'm {bar}.",
inputVariables: ["foo", "bar"],
});
// return new ChatPromptTemplate({
// promptMessages: [
// new SystemMessagePromptTemplate(systemPrompt),
// new HumanMessagePromptTemplate(userPrompt),
// new AIMessagePromptTemplate({ prompt: aiPrompt }),
// new ChatMessagePromptTemplate(genericPrompt, "test"),
// ],
// inputVariables: ["context", "foo", "bar"],
// });
return ChatPromptTemplate.fromMessages<{
foo: string;
bar: string;
context: string;
}>([
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
new AIMessagePromptTemplate({ prompt: aiPrompt }),
new ChatMessagePromptTemplate(genericPrompt, "test"),
]);
}
test("Test format", async () => {
const chatPrompt = createChatPromptTemplate();
const messages = await chatPrompt.formatPromptValue({
context: "This is a context",
foo: "Foo",
bar: "Bar",
unused: "extra",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("Here's some context: This is a context"),
new HumanMessage("Hello Foo, I'm Bar. Thanks for the This is a context"),
new AIMessage("I'm an AI. I'm Foo. I'm Bar."),
new ChatMessage("I'm a generic message. I'm Foo. I'm Bar.", "test"),
]);
});
test("Test format with invalid input values", async () => {
const chatPrompt = createChatPromptTemplate();
let error: any | undefined;
try {
// @ts-expect-error TS compiler should flag missing input variables
await chatPrompt.formatPromptValue({
context: "This is a context",
foo: "Foo",
});
} catch (e) {
error = e;
}
expect(error?.message).toContain("Missing value for input variable `bar`");
expect(error?.lc_error_code).toEqual("INVALID_PROMPT_INPUT");
});
test("Test format with invalid input variables", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}",
inputVariables: ["foo", "bar"],
});
expect(
() =>
new ChatPromptTemplate({
promptMessages: [
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
],
inputVariables: ["context", "foo", "bar", "baz"],
})
).toThrow(
"Input variables `baz` are not used in any of the prompt messages."
);
expect(
() =>
new ChatPromptTemplate({
promptMessages: [
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
],
inputVariables: ["context", "foo"],
})
).toThrow(
"Input variables `bar` are used in prompt messages but not in the prompt template."
);
});
test("Test fromTemplate", async () => {
const chatPrompt = ChatPromptTemplate.fromTemplate("Hello {foo}, I'm {bar}");
expect(chatPrompt.inputVariables).toEqual(["foo", "bar"]);
const messages = await chatPrompt.formatPromptValue({
foo: "Foo",
bar: "Bar",
});
expect(messages.toChatMessages()).toEqual([
new HumanMessage("Hello Foo, I'm Bar"),
]);
});
test("Test fromTemplate", async () => {
const chatPrompt = ChatPromptTemplate.fromTemplate("Hello {foo}, I'm {bar}");
expect(chatPrompt.inputVariables).toEqual(["foo", "bar"]);
expect(
(
await chatPrompt.invoke({
foo: ["barbar"],
bar: [new Document({ pageContent: "bar" })],
})
).toChatMessages()
).toEqual([
new HumanMessage(
`Hello ["barbar"], I'm [{"pageContent":"bar","metadata":{}}]`
),
]);
});
test("Test fromMessages", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}",
inputVariables: ["foo", "bar"],
});
// TODO: Fix autocomplete for the fromMessages method
const chatPrompt = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
]);
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
const messages = await chatPrompt.formatPromptValue({
context: "This is a context",
foo: "Foo",
bar: "Bar",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("Here's some context: This is a context"),
new HumanMessage("Hello Foo, I'm Bar"),
]);
});
test("Test fromMessages with non-string inputs", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}",
inputVariables: ["foo", "bar"],
});
// TODO: Fix autocomplete for the fromMessages method
const chatPrompt = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
]);
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
const messages = await chatPrompt.formatPromptValue({
context: [new Document({ pageContent: "bar" })],
foo: "Foo",
bar: "Bar",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage(
`Here's some context: [{"pageContent":"bar","metadata":{}}]`
),
new HumanMessage("Hello Foo, I'm Bar"),
]);
});
test("Test fromMessages with a variety of ways to declare prompt messages", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
// TODO: Fix autocomplete for the fromMessages method
const chatPrompt = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(systemPrompt),
"Hello {foo}, I'm {bar}",
["assistant", "Nice to meet you, {bar}!"],
["human", "Thanks {foo}!!"],
]);
const messages = await chatPrompt.formatPromptValue({
context: "This is a context",
foo: "Foo",
bar: "Bar",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("Here's some context: This is a context"),
new HumanMessage("Hello Foo, I'm Bar"),
new AIMessage("Nice to meet you, Bar!"),
new HumanMessage("Thanks Foo!!"),
]);
});
test("Test fromMessages with an extra input variable", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}",
inputVariables: ["foo", "bar"],
});
// TODO: Fix autocomplete for the fromMessages method
const chatPrompt = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
]);
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
const messages = await chatPrompt.formatPromptValue({
context: "This is a context",
foo: "Foo",
bar: "Bar",
unused: "No problemo!",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("Here's some context: This is a context"),
new HumanMessage("Hello Foo, I'm Bar"),
]);
});
test("Test fromMessages is composable", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}",
inputVariables: ["foo", "bar"],
});
const chatPromptInner = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
]);
const chatPrompt = ChatPromptTemplate.fromMessages([
chatPromptInner,
AIMessagePromptTemplate.fromTemplate("I'm an AI. I'm {foo}. I'm {bar}."),
]);
expect(chatPrompt.inputVariables).toEqual(["context", "foo", "bar"]);
const messages = await chatPrompt.formatPromptValue({
context: "This is a context",
foo: "Foo",
bar: "Bar",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("Here's some context: This is a context"),
new HumanMessage("Hello Foo, I'm Bar"),
new AIMessage("I'm an AI. I'm Foo. I'm Bar."),
]);
});
test("Test fromMessages is composable with partial vars", async () => {
const systemPrompt = new PromptTemplate({
template: "Here's some context: {context}",
inputVariables: ["context"],
});
const userPrompt = new PromptTemplate({
template: "Hello {foo}, I'm {bar}",
inputVariables: ["foo", "bar"],
});
const chatPromptInner = ChatPromptTemplate.fromMessages([
new SystemMessagePromptTemplate(systemPrompt),
new HumanMessagePromptTemplate(userPrompt),
]);
const chatPrompt = ChatPromptTemplate.fromMessages([
await chatPromptInner.partial({
context: "This is a context",
foo: "Foo",
}),
AIMessagePromptTemplate.fromTemplate("I'm an AI. I'm {foo}. I'm {bar}."),
]);
expect(chatPrompt.inputVariables).toEqual(["bar"]);
const messages = await chatPrompt.formatPromptValue({
bar: "Bar",
});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("Here's some context: This is a context"),
new HumanMessage("Hello Foo, I'm Bar"),
new AIMessage("I'm an AI. I'm Foo. I'm Bar."),
]);
});
test("Test SimpleMessagePromptTemplate", async () => {
const prompt = new MessagesPlaceholder("foo");
const values = { foo: [new HumanMessage("Hello Foo, I'm Bar")] };
const messages = await prompt.formatMessages(values);
expect(messages).toEqual([new HumanMessage("Hello Foo, I'm Bar")]);
});
test("Test MessagesPlaceholder optional", async () => {
const prompt = new MessagesPlaceholder({
variableName: "foo",
optional: true,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const messages = await prompt.formatMessages({} as any);
expect(messages).toEqual([]);
});
test("Test MessagesPlaceholder optional in a chat prompt template", async () => {
const prompt = ChatPromptTemplate.fromMessages([
new MessagesPlaceholder({
variableName: "foo",
optional: true,
}),
]);
const messages = await prompt.formatMessages({});
expect(messages).toEqual([]);
});
test("Test MessagesPlaceholder not optional", async () => {
const prompt = new MessagesPlaceholder({
variableName: "foo",
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await expect(prompt.formatMessages({} as any)).rejects.toThrow(
'Field "foo" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages as an input value. Received: undefined'
);
});
test("Test MessagesPlaceholder not optional with invalid input should throw", async () => {
const prompt = new MessagesPlaceholder({
variableName: "foo",
});
const badInput = [new Document({ pageContent: "barbar", metadata: {} })];
await expect(
prompt.formatMessages({
foo: [new Document({ pageContent: "barbar", metadata: {} })],
})
).rejects.toThrow(
`Field "foo" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages or coerceable values as input.\n\nReceived value: ${JSON.stringify(
badInput,
null,
2
)}\n\nAdditional message: Unable to coerce message from array: only human, AI, system, developer, or tool message coercion is currently supported.`
);
});
test("Test MessagesPlaceholder shorthand in a chat prompt template should throw for invalid syntax", async () => {
expect(() =>
ChatPromptTemplate.fromMessages([["placeholder", "foo"]])
).toThrow();
});
test("Test MessagesPlaceholder shorthand in a chat prompt template", async () => {
const prompt = ChatPromptTemplate.fromMessages([["placeholder", "{foo}"]]);
const messages = await prompt.formatMessages({
foo: [new HumanMessage("Hi there!"), new AIMessage("how r u")],
});
expect(messages).toEqual([
new HumanMessage("Hi there!"),
new AIMessage("how r u"),
]);
});
test("Test MessagesPlaceholder shorthand in a chat prompt template with object format", async () => {
const prompt = ChatPromptTemplate.fromMessages([["placeholder", "{foo}"]]);
const messages = await prompt.formatMessages({
foo: [
{
type: "system",
content: "some initial content",
},
{
type: "human",
content: [
{
text: "page: 1\ndescription: One Purchase Flow\ntimestamp: '2024-06-04T14:46:46.062Z'\ntype: navigate\nscreenshot_present: true\n",
type: "text",
},
{
text: "page: 3\ndescription: intent_str=buy,mode_str=redirect,screenName_str=order-completed,\ntimestamp: '2024-06-04T14:46:58.846Z'\ntype: Screen View\nscreenshot_present: false\n",
type: "text",
},
],
},
{
type: "assistant",
content: "some captivating response",
},
],
});
expect(messages).toEqual([
new SystemMessage("some initial content"),
new HumanMessage({
content: [
{
text: "page: 1\ndescription: One Purchase Flow\ntimestamp: '2024-06-04T14:46:46.062Z'\ntype: navigate\nscreenshot_present: true\n",
type: "text",
},
{
text: "page: 3\ndescription: intent_str=buy,mode_str=redirect,screenName_str=order-completed,\ntimestamp: '2024-06-04T14:46:58.846Z'\ntype: Screen View\nscreenshot_present: false\n",
type: "text",
},
],
}),
new AIMessage("some captivating response"),
]);
});
test("Test MessagesPlaceholder with invalid shorthand should throw", async () => {
const prompt = ChatPromptTemplate.fromMessages([["placeholder", "{foo}"]]);
await expect(() =>
prompt.formatMessages({
foo: [{ badFormatting: true }],
})
).rejects.toThrow();
});
test("Test using partial", async () => {
const userPrompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo", "bar"],
});
const prompt = new ChatPromptTemplate({
promptMessages: [new HumanMessagePromptTemplate(userPrompt)],
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({ foo: "foo" });
// original prompt is not modified
expect(prompt.inputVariables).toEqual(["foo", "bar"]);
// partial prompt has only remaining variables
expect(partialPrompt.inputVariables).toEqual(["bar"]);
expect(await partialPrompt.format({ bar: "baz" })).toMatchInlineSnapshot(
`"Human: foobaz"`
);
});
test("Test BaseMessage", async () => {
const prompt = ChatPromptTemplate.fromMessages([
new SystemMessage("You are a chatbot {mock_variable}"),
AIMessagePromptTemplate.fromTemplate("{name} is my name."),
new FunctionMessage({ content: "{}", name: "get_weather" }),
]);
const messages = await prompt.formatPromptValue({ name: "Bob" });
expect(prompt.inputVariables).toEqual(["name"]);
expect(prompt.partialVariables).toEqual({});
expect(messages.toChatMessages()).toEqual([
new SystemMessage("You are a chatbot {mock_variable}"),
new AIMessage("Bob is my name."),
new FunctionMessage({ content: "{}", name: "get_weather" }),
]);
});
test("Throws if trying to pass non BaseMessage inputs to MessagesPlaceholder", async () => {
const prompt = ChatPromptTemplate.fromMessages([
["system", "some string"],
new MessagesPlaceholder("chatHistory"),
["human", "{question}"],
]);
const value = "this is not a valid input type!";
try {
await prompt.formatMessages({
chatHistory: value,
question: "What is the meaning of life?",
});
} catch (e) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof Error) {
expect(e.name).toBe("InputFormatError");
} else {
throw e;
}
}
});
test("Does not throws if null or undefined is passed as input to MessagesPlaceholder", async () => {
const prompt = ChatPromptTemplate.fromMessages([
["system", "some string"],
new MessagesPlaceholder("chatHistory"),
new MessagesPlaceholder("chatHistory2"),
["human", "{question}"],
]);
const value1 = null;
const value2 = undefined;
try {
await prompt.formatMessages({
chatHistory: value1,
chatHistory2: value2,
question: "What is the meaning of life?",
});
} catch (e) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof Error) {
expect(e.name).toBe("InputFormatError");
} else {
throw e;
}
}
});
test("Multi part chat prompt template", async () => {
const name = "Bob";
const objectName = "chair";
const template = ChatPromptTemplate.fromMessages([
["system", "You are an AI assistant named {name}"],
[
"human",
[
{
type: "text",
text: "What is in this object {objectName}",
},
],
],
]);
const messages = await template.formatMessages({
name,
objectName,
});
expect(messages).toEqual([
new SystemMessage("You are an AI assistant named Bob"),
new HumanMessage({
content: [
{
type: "text",
text: "What is in this object chair",
},
],
}),
]);
});
test("Multi part chat prompt template with image", async () => {
const name = "Bob";
const objectName = "chair";
const myImage = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAA";
const myUrl = "https://www.example.com/image.png";
const template = ChatPromptTemplate.fromMessages([
["system", "You are an AI assistant named {name}"],
[
"human",
[
{
type: "image_url",
image_url: "data:image/jpeg;base64,{myImage}",
},
{
type: "text",
text: "What is in this object {objectName}",
},
{
type: "image_url",
image_url: {
url: "{myUrl}",
detail: "high",
},
},
],
],
]);
const messages = await template.formatMessages({
name,
objectName,
myImage,
myUrl,
});
expect(messages).toEqual([
new SystemMessage("You are an AI assistant named Bob"),
new HumanMessage({
content: [
{
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${myImage}`,
},
},
{
type: "text",
text: `What is in this object ${objectName}`,
},
{
type: "image_url",
image_url: {
url: `${myUrl}`,
detail: "high",
},
},
],
}),
]);
});
test("Multi-modal, multi part chat prompt works with instances of BaseMessage", async () => {
const name = "Bob";
const objectName = "chair";
const myImage = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAA";
const myUrl = "https://www.example.com/image.png";
const inlineImageUrl = new HumanMessage({
content: [
{
type: "image_url",
image_url: "data:image/jpeg;base64,{myImage}",
},
],
});
const objectImageUrl = new HumanMessage({
content: [
{
type: "image_url",
image_url: {
url: "data:image/jpeg;base64,{myImage}",
detail: "high",
},
},
],
});
const normalMessage = new HumanMessage({
content: [
{
type: "text",
text: "What is in this object {objectName}",
},
],
});
const template = ChatPromptTemplate.fromMessages([
["system", "You are an AI assistant named {name}"],
inlineImageUrl,
normalMessage,
objectImageUrl,
[
"human",
[
{
type: "text",
text: "What is in this object {objectName}",
},
{
type: "image_url",
image_url: {
url: "{myUrl}",
detail: "high",
},
},
],
],
]);
const messages = await template.formatMessages({
name,
objectName,
myImage,
myUrl,
});
expect(messages).toMatchSnapshot();
});
test("Format complex messages and keep additional fields", async () => {
const examplePrompt = ChatPromptTemplate.fromMessages([
[
"human",
[
{
type: "text",
text: "{input}",
cache_control: { type: "ephemeral" },
},
],
],
[
"ai",
[
{
type: "text",
text: "{output}",
cache_control: { type: "ephemeral" },
},
],
],
]);
const formatted = await examplePrompt.formatMessages({
input: "hello",
output: "ciao",
});
expect(formatted).toHaveLength(2);
expect(formatted[0]._getType()).toBe("human");
expect(formatted[0].content[0]).toHaveProperty("cache_control");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((formatted[0].content[0] as any).cache_control).toEqual({
type: "ephemeral",
});
expect(formatted[1]._getType()).toBe("ai");
expect(formatted[1].content[0]).toHaveProperty("cache_control");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((formatted[1].content[0] as any).cache_control).toEqual({
type: "ephemeral",
});
});
test("Format image content messages and keep additional fields", async () => {
const examplePrompt = ChatPromptTemplate.fromMessages([
[
"human",
[
{
type: "image_url",
image_url: "{image_url}",
cache_control: { type: "ephemeral" },
},
],
],
]);
const formatted = await examplePrompt.formatMessages({
image_url: "image_url",
});
expect(formatted).toHaveLength(1);
expect(formatted[0]._getType()).toBe("human");
expect(formatted[0].content[0]).toHaveProperty("cache_control");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((formatted[0].content[0] as any).cache_control).toEqual({
type: "ephemeral",
});
});