forked from mlc-ai/web-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_chat_completion.test.ts
444 lines (425 loc) · 14.3 KB
/
openai_chat_completion.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
/* eslint-disable no-useless-escape */
import {
postInitAndCheckFields,
ChatCompletionRequest,
ChatCompletionTool,
} from "../src/openai_api_protocols/chat_completion";
import {
hermes2FunctionCallingSystemPrompt,
officialHermes2FunctionCallSchemaArray,
} from "../src/support";
import { MessagePlaceholders, ModelType } from "../src/config";
import { describe, expect, test } from "@jest/globals";
describe("Check chat completion unsupported requests", () => {
test("stream_options without stream specified", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [{ role: "user", content: "Hello! " }],
stream_options: { include_usage: true },
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow("Only specify stream_options when stream=True.");
});
test("stream_options with stream=false", () => {
expect(() => {
const request: ChatCompletionRequest = {
stream: false,
messages: [{ role: "user", content: "Hello! " }],
stream_options: { include_usage: true },
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow("Only specify stream_options when stream=True.");
});
test("Last message should be from user or tool", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello! " },
{ role: "assistant", content: "Hello! How may I help you today?" },
],
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow("Last message should be from either `user` or `tool`.");
});
test("System prompt should always be the first one in `messages`", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [
{ role: "user", content: "Hello! " },
{ role: "assistant", content: "Hello! How may I help you today?" },
{ role: "user", content: "Tell me about Pittsburgh" },
{ role: "system", content: "You are a helpful assistant." },
],
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow(
"System prompt should always be the first message in `messages`.",
);
});
test("When streaming `n` needs to be 1", () => {
expect(() => {
const request: ChatCompletionRequest = {
stream: true,
n: 2,
messages: [{ role: "user", content: "Hello! " }],
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow("When streaming, `n` cannot be > 1.");
});
test("Non-integer seed", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [{ role: "user", content: "Hello! " }],
max_tokens: 10,
seed: 42.2, // Note that Number.isInteger(42.0) is true
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow("`seed` should be an integer, but got");
});
test("Schema without type json object", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [{ role: "user", content: "Hello! " }],
response_format: { schema: "some json schema" },
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow(
"JSON schema is only supported with `json_object` response format.",
);
});
test("image_url.detail is unsupported", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this image?" },
{
type: "image_url",
image_url: {
url: "https://url_here.jpg",
detail: "high",
},
},
],
},
],
};
postInitAndCheckFields(
request,
"Phi-3.5-vision-instruct-q4f16_1-MLC",
ModelType.VLM,
);
}).toThrow(
"Currently do not support field image_url.detail, but received: high",
);
});
test("User content cannot have multiple text content parts", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this image?" },
{
type: "image_url",
image_url: {
url: "https://url_here.jpg",
},
},
{ type: "text", text: "Thank you." },
],
},
],
};
postInitAndCheckFields(
request,
"Phi-3.5-vision-instruct-q4f16_1-MLC",
ModelType.VLM,
);
}).toThrow(
"Each message can have at most one text contentPart, but received more than 1.",
);
});
test("Non-VLM cannot support non-string content", () => {
expect(() => {
const request: ChatCompletionRequest = {
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this image?" },
{
type: "image_url",
image_url: {
url: "https://url_here.jpg",
},
},
],
},
],
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow(
"The model loaded is not of type ModelType.VLM (vision-language model).",
);
});
});
describe("Supported requests", () => {
test("Supported chat completion request", () => {
const request: ChatCompletionRequest = {
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello! " },
{ role: "assistant", content: "How can I help you? " },
{ role: "user", content: "Give me 5 US states. " },
],
n: 3,
temperature: 1.5,
max_tokens: 25,
frequency_penalty: 0.2,
seed: 42,
logprobs: true,
top_logprobs: 2,
logit_bias: {
"13813": -100,
"10319": 5,
"7660": 5,
},
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
});
test("Support image input, single or multiple images", () => {
const request: ChatCompletionRequest = {
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this image?" },
{
type: "image_url",
image_url: { url: "https://url_here1.jpg" },
},
{
type: "image_url",
image_url: { url: "https://url_here2.jpg" },
},
],
},
],
};
postInitAndCheckFields(
request,
"Phi-3.5-vision-instruct-q4f16_1-MLC",
ModelType.VLM,
);
});
});
describe("Manual function calling", () => {
test("Hermes2 style function calling", () => {
const system_prompt = `You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools> {"type": "function", "function": {"name": "get_stock_fundamentals", "description": "get_stock_fundamentals(symbol: str) -> dict - Get fundamental data for a given stock symbol using yfinance API.\\n\\n Args:\\n symbol (str): The stock symbol.\\n\\n Returns:\\n dict: A dictionary containing fundamental data.\\n Keys:\\n - \'symbol\': The stock symbol.\\n - \'company_name\': The long name of the company.\\n - \'sector\': The sector to which the company belongs.\\n - \'industry\': The industry to which the company belongs.\\n - \'market_cap\': The market capitalization of the company.\\n - \'pe_ratio\': The forward price-to-earnings ratio.\\n - \'pb_ratio\': The price-to-book ratio.\\n - \'dividend_yield\': The dividend yield.\\n - \'eps\': The trailing earnings per share.\\n - \'beta\': The beta value of the stock.\\n - \'52_week_high\': The 52-week high price of the stock.\\n - \'52_week_low\': The 52-week low price of the stock.", "parameters": {"type": "object", "properties": {"symbol": {"type": "string"}}, "required": ["symbol"]}}} </tools> Use the following pydantic model json schema for each tool call you will make: {"properties": {"arguments": {"title": "Arguments", "type": "object"}, "name": {"title": "Name", "type": "string"}}, "required": ["arguments", "name"], "title": "FunctionCall", "type": "object"} For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:\n<tool_call>\n{"arguments": <args-dict>, "name": <function-name>}\n</tool_call>`;
const request: ChatCompletionRequest = {
messages: [
{ role: "system", content: system_prompt },
{
role: "user",
content: "Fetch the stock fundamentals data for Tesla (TSLA)",
},
{
role: "assistant",
content: `<tool_call>\n{"arguments": {"symbol": "TSLA"}, "name": "get_stock_fundamentals"}\n</tool_call>`,
},
{
role: "tool",
tool_call_id: "0",
content: `<tool_response>\n{"name": "get_stock_fundamentals", "content": {'symbol': 'TSLA', 'company_name': 'Tesla, Inc.', 'sector': 'Consumer Cyclical', 'industry': 'Auto Manufacturers', 'market_cap': 611384164352, 'pe_ratio': 49.604652, 'pb_ratio': 9.762013, 'dividend_yield': None, 'eps': 4.3, 'beta': 2.427, '52_week_high': 299.29, '52_week_low': 152.37}}\n</tool_response>`,
},
],
};
postInitAndCheckFields(
request,
"Hermes-2-Theta-Llama-3-8B-q4f16_1-MLC",
ModelType.LLM,
);
});
});
describe("OpenAI API function calling", () => {
const tools: Array<ChatCompletionTool> = [
{
type: "function",
function: {
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["location"],
},
},
},
];
test("Unsupported model", () => {
expect(() => {
const request: ChatCompletionRequest = {
tools: tools,
messages: [
{
role: "user",
content: "Get weather of Tokyo",
},
],
};
postInitAndCheckFields(
request,
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
ModelType.LLM,
);
}).toThrow(
"Llama-3.1-8B-Instruct-q4f32_1-MLC is not supported for ChatCompletionRequest.tools.",
);
});
test("Should not specify response format", () => {
expect(() => {
const request: ChatCompletionRequest = {
tools: tools,
messages: [
{
role: "user",
content: "Get weather of Tokyo",
},
],
response_format: { type: "json_object" },
};
postInitAndCheckFields(
request,
"Hermes-2-Pro-Llama-3-8B-q4f16_1-MLC",
ModelType.LLM,
);
}).toThrow(
"When using Hermes-2-Pro function calling via ChatCompletionRequest.tools, " +
"cannot specify customized response_format. We will set it for you internally.",
);
});
test("Should not specify system prompt", () => {
expect(() => {
const request: ChatCompletionRequest = {
tools: tools,
messages: [
{
role: "system",
content: "Write a function.",
},
{
role: "user",
content: "Get weather of Tokyo",
},
],
};
postInitAndCheckFields(
request,
"Hermes-2-Pro-Llama-3-8B-q4f16_1-MLC",
ModelType.LLM,
);
}).toThrow(
"When using Hermes-2-Pro function calling via ChatCompletionRequest.tools, cannot " +
"specify customized system prompt.",
);
});
test("Should not specify system prompt", () => {
expect(() => {
const request: ChatCompletionRequest = {
tools: tools,
messages: [
{
role: "system",
content: "Write a function.",
},
{
role: "user",
content: "Get weather of Tokyo",
},
],
};
postInitAndCheckFields(
request,
"Hermes-2-Pro-Llama-3-8B-q4f16_1-MLC",
ModelType.LLM,
);
}).toThrow(
"When using Hermes-2-Pro function calling via ChatCompletionRequest.tools, cannot " +
"specify customized system prompt.",
);
});
test("Check system prompt and response format post init", () => {
const request: ChatCompletionRequest = {
tools: tools,
messages: [
{
role: "user",
content: "Get weather of Tokyo",
},
],
};
postInitAndCheckFields(
request,
"Hermes-2-Pro-Llama-3-8B-q4f16_1-MLC",
ModelType.LLM,
);
expect(request.messages[0].role).toEqual("system");
expect(request.messages[0].content).toEqual(
hermes2FunctionCallingSystemPrompt.replace(
MessagePlaceholders.hermes_tools,
JSON.stringify(request.tools),
),
);
expect(request.response_format!.type).toEqual("json_object");
expect(request.response_format!.schema).toEqual(
officialHermes2FunctionCallSchemaArray,
);
});
});