Skip to content

Commit 4ff8189

Browse files
authored
fix: GeneralChatPromptWrapper output (#70)
* fix: `GeneralChatPromptWrapper` output * test: add tests to chat prompt wrappers
1 parent c41da09 commit 4ff8189

File tree

5 files changed

+595
-5
lines changed

5 files changed

+595
-5
lines changed

src/chatWrappers/GeneralChatPromptWrapper.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export class GeneralChatPromptWrapper extends ChatPromptWrapper {
1717
systemPrompt: string, promptIndex: number, lastStopString: string | null, lastStopStringSuffix: string | null
1818
}) {
1919
if (promptIndex === 0)
20-
return systemPrompt + `\n\n### ${this._instructionName}:\n\n` + prompt + `\n\n### ${this._responseName}:\n\n`;
20+
return systemPrompt + `\n\n### ${this._instructionName}:\n` + prompt + `\n\n### ${this._responseName}:\n`;
2121

22-
return this._getPromptPrefix(lastStopString, lastStopStringSuffix) + prompt + `\n\n### ${this._responseName}:\n\n`;
22+
return this._getPromptPrefix(lastStopString, lastStopStringSuffix) + prompt + `\n\n### ${this._responseName}:\n`;
2323
}
2424

2525
public override getStopStrings(): string[] {
@@ -42,9 +42,9 @@ export class GeneralChatPromptWrapper extends ChatPromptWrapper {
4242
? lastStopStringSuffix
4343
: ((lastStopString ?? "") + (lastStopStringSuffix ?? "")),
4444
[
45-
`\n\n### ${this._instructionName}:\n\n`,
46-
`### ${this._instructionName}:\n\n`
45+
`\n\n### ${this._instructionName}:\n`,
46+
`### ${this._instructionName}:\n`
4747
]
48-
) ?? `\n\n### ${this._instructionName}:\n\n`;
48+
) ?? `\n\n### ${this._instructionName}:\n`;
4949
}
5050
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import {describe, expect, test} from "vitest";
2+
import {generateContextTextFromConversationHistory} from "../../../src/chatWrappers/generateContextTextFromConversationHistory.js";
3+
import {ChatMLChatPromptWrapper, ConversationInteraction} from "../../../src/index.js";
4+
5+
6+
describe("ChatMLChatPromptWrapper", () => {
7+
const conversationHistory: ConversationInteraction[] = [{
8+
prompt: "Hi there!",
9+
response: "Hello!"
10+
}];
11+
const conversationHistory2: ConversationInteraction[] = [{
12+
prompt: "Hi there!",
13+
response: "Hello!"
14+
}, {
15+
prompt: "How are you?",
16+
response: "I'm good, how are you?"
17+
}];
18+
19+
test("should generate valid output for default roles", () => {
20+
const chatWrapper = new ChatMLChatPromptWrapper();
21+
const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory);
22+
23+
expect(response).toMatchInlineSnapshot(`
24+
"<|im_start|>system
25+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
26+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.<|im_end|>
27+
<|im_start|>user
28+
Hi there!<|im_end|>
29+
<|im_start|>assistant
30+
Hello!<|im_end|>"
31+
`);
32+
33+
const chatWrapper2 = new ChatMLChatPromptWrapper();
34+
const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2);
35+
36+
expect(response2).toMatchInlineSnapshot(`
37+
"<|im_start|>system
38+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
39+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.<|im_end|>
40+
<|im_start|>user
41+
Hi there!<|im_end|>
42+
<|im_start|>assistant
43+
Hello!<|im_end|>
44+
<|im_start|>user
45+
How are you?<|im_end|>
46+
<|im_start|>assistant
47+
I'm good, how are you?<|im_end|>"
48+
`);
49+
50+
const chatWrapper3 = new ChatMLChatPromptWrapper();
51+
const {text: response3, stopStringSuffix, stopString} = generateContextTextFromConversationHistory(chatWrapper3, conversationHistory);
52+
53+
const newPrompt = conversationHistory2[1].prompt;
54+
const wrappedNewPrompt = chatWrapper3.wrapPrompt(newPrompt, {
55+
systemPrompt: response3,
56+
promptIndex: 1,
57+
lastStopString: stopString,
58+
lastStopStringSuffix: stopStringSuffix
59+
});
60+
61+
expect(response3).toMatchInlineSnapshot(`
62+
"<|im_start|>system
63+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
64+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.<|im_end|>
65+
<|im_start|>user
66+
Hi there!<|im_end|>
67+
<|im_start|>assistant
68+
Hello!<|im_end|>"
69+
`);
70+
71+
expect(wrappedNewPrompt).toMatchInlineSnapshot(`
72+
"
73+
<|im_start|>user
74+
How are you?<|im_end|>
75+
<|im_start|>assistant
76+
"
77+
`);
78+
79+
expect(response3 + wrappedNewPrompt).toMatchInlineSnapshot(`
80+
"<|im_start|>system
81+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
82+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.<|im_end|>
83+
<|im_start|>user
84+
Hi there!<|im_end|>
85+
<|im_start|>assistant
86+
Hello!<|im_end|>
87+
<|im_start|>user
88+
How are you?<|im_end|>
89+
<|im_start|>assistant
90+
"
91+
`);
92+
});
93+
94+
test("should generate valid output for custom system prompt", () => {
95+
const chatWrapper = new ChatMLChatPromptWrapper();
96+
const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory, {
97+
systemPrompt: "Below is an instruction the describes a task, Write a response the appropriately completes the request."
98+
});
99+
100+
expect(response).toMatchInlineSnapshot(`
101+
"<|im_start|>system
102+
Below is an instruction the describes a task, Write a response the appropriately completes the request.<|im_end|>
103+
<|im_start|>user
104+
Hi there!<|im_end|>
105+
<|im_start|>assistant
106+
Hello!<|im_end|>"
107+
`);
108+
109+
const chatWrapper2 = new ChatMLChatPromptWrapper();
110+
const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2, {
111+
systemPrompt: "Below is an instruction the describes a task, Write a response the appropriately completes the request."
112+
});
113+
114+
expect(response2).toMatchInlineSnapshot(`
115+
"<|im_start|>system
116+
Below is an instruction the describes a task, Write a response the appropriately completes the request.<|im_end|>
117+
<|im_start|>user
118+
Hi there!<|im_end|>
119+
<|im_start|>assistant
120+
Hello!<|im_end|>
121+
<|im_start|>user
122+
How are you?<|im_end|>
123+
<|im_start|>assistant
124+
I'm good, how are you?<|im_end|>"
125+
`);
126+
});
127+
});
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import {describe, expect, test} from "vitest";
2+
import {generateContextTextFromConversationHistory} from "../../../src/chatWrappers/generateContextTextFromConversationHistory.js";
3+
import {ConversationInteraction, FalconChatPromptWrapper} from "../../../src/index.js";
4+
5+
6+
describe("FalconChatPromptWrapper", () => {
7+
const conversationHistory: ConversationInteraction[] = [{
8+
prompt: "Hi there!",
9+
response: "Hello!"
10+
}];
11+
const conversationHistory2: ConversationInteraction[] = [{
12+
prompt: "Hi there!",
13+
response: "Hello!"
14+
}, {
15+
prompt: "How are you?",
16+
response: "I'm good, how are you?"
17+
}];
18+
19+
test("should generate valid output for default roles", () => {
20+
const chatWrapper = new FalconChatPromptWrapper();
21+
const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory);
22+
23+
expect(response).toMatchInlineSnapshot(`
24+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
25+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
26+
User: Hi there!
27+
Assistant: Hello!
28+
User: "
29+
`);
30+
31+
const chatWrapper2 = new FalconChatPromptWrapper();
32+
const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2);
33+
34+
expect(response2).toMatchInlineSnapshot(`
35+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
36+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
37+
User: Hi there!
38+
Assistant: Hello!
39+
User: How are you?
40+
Assistant: I'm good, how are you?
41+
User: "
42+
`);
43+
44+
const chatWrapper3 = new FalconChatPromptWrapper();
45+
const {text: response3, stopStringSuffix, stopString} = generateContextTextFromConversationHistory(chatWrapper3, conversationHistory);
46+
47+
const newPrompt = conversationHistory2[1].prompt;
48+
const wrappedNewPrompt = chatWrapper3.wrapPrompt(newPrompt, {
49+
systemPrompt: response3,
50+
promptIndex: 1,
51+
lastStopString: stopString,
52+
lastStopStringSuffix: stopStringSuffix
53+
});
54+
55+
expect(response3).toMatchInlineSnapshot(`
56+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
57+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
58+
User: Hi there!
59+
Assistant: Hello!
60+
User: "
61+
`);
62+
63+
expect(wrappedNewPrompt).toMatchInlineSnapshot(`
64+
"How are you?
65+
Assistant: "
66+
`);
67+
68+
expect(response3 + wrappedNewPrompt).toMatchInlineSnapshot(`
69+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
70+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
71+
User: Hi there!
72+
Assistant: Hello!
73+
User: How are you?
74+
Assistant: "
75+
`);
76+
});
77+
78+
test("should generate valid output for custom roles", () => {
79+
const chatWrapper = new FalconChatPromptWrapper({
80+
instructionName: "Instruction",
81+
responseName: "Response"
82+
});
83+
const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory);
84+
85+
expect(response).toMatchInlineSnapshot(`
86+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
87+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
88+
Instruction: Hi there!
89+
Response: Hello!
90+
Instruction: "
91+
`);
92+
93+
const chatWrapper2 = new FalconChatPromptWrapper({
94+
instructionName: "Instruction",
95+
responseName: "Response"
96+
});
97+
const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2);
98+
99+
expect(response2).toMatchInlineSnapshot(`
100+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
101+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
102+
Instruction: Hi there!
103+
Response: Hello!
104+
Instruction: How are you?
105+
Response: I'm good, how are you?
106+
Instruction: "
107+
`);
108+
});
109+
110+
test("should generate valid output for custom system prompt", () => {
111+
const chatWrapper = new FalconChatPromptWrapper();
112+
const {text: response} = generateContextTextFromConversationHistory(chatWrapper, conversationHistory, {
113+
systemPrompt: "Below is an instruction the describes a task, Write a response the appropriately completes the request."
114+
});
115+
116+
expect(response).toMatchInlineSnapshot(`
117+
"Below is an instruction the describes a task, Write a response the appropriately completes the request.
118+
User: Hi there!
119+
Assistant: Hello!
120+
User: "
121+
`);
122+
123+
const chatWrapper2 = new FalconChatPromptWrapper({
124+
instructionName: "Instruction",
125+
responseName: "Response"
126+
});
127+
const {text: response2} = generateContextTextFromConversationHistory(chatWrapper2, conversationHistory2, {
128+
systemPrompt: "Below is an instruction the describes a task, Write a response the appropriately completes the request."
129+
});
130+
131+
expect(response2).toMatchInlineSnapshot(`
132+
"Below is an instruction the describes a task, Write a response the appropriately completes the request.
133+
Instruction: Hi there!
134+
Response: Hello!
135+
Instruction: How are you?
136+
Response: I'm good, how are you?
137+
Instruction: "
138+
`);
139+
});
140+
});

0 commit comments

Comments
 (0)