forked from mlc-ai/web-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_round_chat.test.ts
223 lines (192 loc) · 9.08 KB
/
multi_round_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
import { describe, expect, test } from '@jest/globals';
import {
ChatCompletionMessageParam,
ChatCompletionRequest,
ChatCompletionUserMessageParam,
} from "../src/openai_api_protocols/chat_completion";
import { MLCEngine } from '../src/engine';
import { Conversation, compareConversationObject } from '../src/conversation';
import { ChatConfig, Role } from '../src/config';
const configStr = "{" +
" \"conv_template\": {" +
" \"name\": \"llama-2\"," +
" \"system_template\": \"[INST] <<SYS>>\\n{system_message}\\n<</SYS>>\\n\\n\"," +
" \"system_message\": \"You are a helpful, respectful and honest assistant.\"," +
" \"system_prefix_token_ids\": [" +
" 1" +
" ]," +
" \"add_role_after_system_message\": false," +
" \"roles\": {" +
" \"user\": \"[INST]\"," +
" \"assistant\": \"[/INST]\"," +
" \"tool\": \"[INST]\"" +
" }," +
" \"role_templates\": {" +
" \"user\": \"{user_message}\"," +
" \"assistant\": \"{assistant_message}\"," +
" \"tool\": \"{tool_message}\"" +
" }," +
" \"messages\": []," +
" \"seps\": [" +
" \" \"" +
" ]," +
" \"role_content_sep\": \" \"," +
" \"role_empty_sep\": \" \"," +
" \"stop_str\": [" +
" \"[INST]\"" +
" ]," +
" \"stop_token_ids\": [" +
" 2" +
" ]," +
" \"function_string\": \"\"," +
" \"use_function_calling\": false" +
" }" +
"}";
describe('Test multi-round chatting', () => {
test('Test is multi-round', () => {
// Setups
const config_json = JSON.parse(configStr);
const chatConfig = { ...config_json } as ChatConfig;
const engine = new MLCEngine();
// Simulate request0
const messages: ChatCompletionMessageParam[] = [
{
"role": "system",
"content": "[INST] <<SYS>>\n\nYou are a helpful, respectful and honest assistant. " +
"Be as happy as you can when speaking please.\n<</SYS>>\n\n "
},
{ "role": "user", "content": "Provide me three US states." },
];
const request0: ChatCompletionRequest = {
messages: messages,
};
// Simulate processing of request0, appending response to convA (done by LLMChatPipeline)
const conv0: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request0, chatConfig));
conv0.appendMessage(Role.user, "Provide me three US states.");
const reply0 = "California, New York, Nevada.";
conv0.appendMessage(Role.assistant, reply0); // simulated response
// Simulate request1, where user maintain the chat history, appending the resposne
const newMessages = [...messages];
newMessages.push({ "role": "assistant", "content": reply0 });
newMessages.push({ "role": "user", "content": "Two more please" }); // next input
const request1: ChatCompletionRequest = {
messages: newMessages,
}
const conv1: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request1, chatConfig));
expect(compareConversationObject(conv0, conv1)).toBe(true);
});
test('Test is NOT multi-round due to multiple new inputs', () => {
// Setups
const config_json = JSON.parse(configStr);
const chatConfig = { ...config_json } as ChatConfig;
const engine = new MLCEngine();
// Simulate request0
const messages: ChatCompletionMessageParam[] = [
{
"role": "system",
"content": "[INST] <<SYS>>\n\nYou are a helpful, respectful and honest assistant. " +
"Be as happy as you can when speaking please.\n<</SYS>>\n\n "
},
{ "role": "user", "content": "Provide me three US states." },
];
const request0: ChatCompletionRequest = {
messages: messages,
};
// Simulate processing of request0, appending response to convA (done by LLMChatPipeline)
const conv0: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request0, chatConfig));
conv0.appendMessage(Role.user, "Provide me three US states.");
const reply0 = "California, New York, Nevada.";
conv0.appendMessage(Role.assistant, reply0); // simulated response
// Simulate request1, where user maintain the chat history, appending the resposne
const newMessages = [...messages];
newMessages.push({ "role": "assistant", "content": reply0 });
newMessages.push({ "role": "user", "content": "Two more please" }); // next input
// Code above same as previous tests
// Add one more round of chat history
newMessages.push({ "role": "assistant", "content": "Pennsylvania, Florida" }); // next response
newMessages.push({ "role": "user", "content": "Thank you!" }); // next input
const request1: ChatCompletionRequest = {
messages: newMessages,
}
const conv1: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request1, chatConfig));
expect(compareConversationObject(conv0, conv1)).toBe(false);
});
test('Test is NOT multi-round due to change in system prompt', () => {
// Setups
const config_json = JSON.parse(configStr);
const chatConfig = { ...config_json } as ChatConfig;
const engine = new MLCEngine();
// Simulate request0
const messages: ChatCompletionMessageParam[] = [
{
"role": "system",
"content": "[INST] <<SYS>>\n\nYou are a helpful, respectful and honest assistant. " +
"Be as happy as you can when speaking please.\n<</SYS>>\n\n "
},
{ "role": "user", "content": "Provide me three US states." },
];
const request0: ChatCompletionRequest = {
messages: messages,
};
// Simulate processing of request0, appending response to convA (done by LLMChatPipeline)
const conv0: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request0, chatConfig));
conv0.appendMessage(Role.user, "Provide me three US states.");
const reply0 = "California, New York, Nevada.";
conv0.appendMessage(Role.assistant, reply0); // simulated response
// Simulate request1, where user maintain the chat history, appending the resposne
const newMessages = [...messages];
newMessages.push({ "role": "assistant", "content": reply0 });
newMessages.push({ "role": "user", "content": "Two more please" }); // next input
// Code above same as previous tests
// Changed system prompt, should be false
newMessages[0].content = "No system prompt";
const request1: ChatCompletionRequest = {
messages: newMessages,
}
const conv1: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request1, chatConfig));
expect(compareConversationObject(conv0, conv1)).toBe(false);
});
test('Test is NOT multi-round due to change in role name', () => {
// Setups
const config_json = JSON.parse(configStr);
const chatConfig = { ...config_json } as ChatConfig;
const engine = new MLCEngine();
// Simulate request0
const messages: ChatCompletionMessageParam[] = [
{
"role": "system",
"content": "[INST] <<SYS>>\n\nYou are a helpful, respectful and honest assistant. " +
"Be as happy as you can when speaking please.\n<</SYS>>\n\n "
},
{ "role": "user", "content": "Provide me three US states." },
];
const request0: ChatCompletionRequest = {
messages: messages,
};
// Simulate processing of request0, appending response to convA (done by LLMChatPipeline)
const conv0: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request0, chatConfig));
conv0.appendMessage(Role.user, "Provide me three US states.");
const reply0 = "California, New York, Nevada.";
conv0.appendMessage(Role.assistant, reply0); // simulated response
// Simulate request1, where user maintain the chat history, appending the resposne
const newMessages = [...messages];
newMessages.push({ "role": "assistant", "content": reply0 });
newMessages.push({ "role": "user", "content": "Two more please" }); // next input
// Code above same as previous tests
// Changed system prompt, should be false
(newMessages[1] as ChatCompletionUserMessageParam).name = "Bob";
const request1: ChatCompletionRequest = {
messages: newMessages,
}
const conv1: Conversation =
((engine as any).getConversationFromChatCompletionRequest(request1, chatConfig));
expect(compareConversationObject(conv0, conv1)).toBe(false);
});
})