|
1 | 1 | import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks"; |
2 | 2 |
|
3 | | -export interface StringifyMessagesOptions { |
4 | | - sep: string; |
5 | | - start: string; |
6 | | - end: string; |
7 | | - attributeKeyQuotes?: boolean; |
8 | | - customContentEscaper?: (str: string) => string; |
9 | | -} |
10 | | - |
11 | | -export function stringifyMessages(messages: ChatCompletionInputMessage[], opts: StringifyMessagesOptions): string { |
12 | | - const keyRole = opts.attributeKeyQuotes ? `"role"` : "role"; |
13 | | - const keyContent = opts.attributeKeyQuotes ? `"content"` : "content"; |
14 | | - |
15 | | - const messagesStringified = messages.map(({ role, content }) => { |
16 | | - if (typeof content === "string") { |
17 | | - content = JSON.stringify(content).slice(1, -1); |
18 | | - if (opts.customContentEscaper) { |
19 | | - content = opts.customContentEscaper(content); |
20 | | - } |
21 | | - return `{ ${keyRole}: "${role}", ${keyContent}: "${content}" }`; |
22 | | - } else { |
23 | | - 2; |
24 | | - content = content.map(({ image_url, text, type }) => ({ |
25 | | - type, |
26 | | - image_url, |
27 | | - ...(text ? { text: JSON.stringify(text).slice(1, -1) } : undefined), |
28 | | - })); |
29 | | - content = JSON.stringify(content).slice(1, -1); |
30 | | - if (opts.customContentEscaper) { |
31 | | - content = opts.customContentEscaper(content); |
32 | | - } |
33 | | - return `{ ${keyRole}: "${role}", ${keyContent}: [${content}] }`; |
34 | | - } |
35 | | - }); |
36 | | - |
37 | | - return opts.start + messagesStringified.join(opts.sep) + opts.end; |
| 3 | +export function stringifyMessages( |
| 4 | + messages: ChatCompletionInputMessage[], |
| 5 | + opts?: { |
| 6 | + indent?: string; |
| 7 | + attributeKeyQuotes?: boolean; |
| 8 | + customContentEscaper?: (str: string) => string; |
| 9 | + } |
| 10 | +): string { |
| 11 | + let messagesStr = JSON.stringify(messages, null, "\t"); |
| 12 | + if (opts?.indent) { |
| 13 | + messagesStr = messagesStr.replaceAll("\n", `\n${opts.indent}`); |
| 14 | + } |
| 15 | + if (!opts?.attributeKeyQuotes) { |
| 16 | + messagesStr = messagesStr.replace(/"([^"]+)":/g, "$1:"); |
| 17 | + } |
| 18 | + if (opts?.customContentEscaper) { |
| 19 | + messagesStr = opts.customContentEscaper(messagesStr); |
| 20 | + } |
| 21 | + return messagesStr; |
38 | 22 | } |
39 | 23 |
|
40 | 24 | type PartialGenerationParameters = Partial<Pick<GenerationParameters, "temperature" | "max_tokens" | "top_p">>; |
41 | 25 |
|
42 | | -export interface StringifyGenerationConfigOptions { |
43 | | - sep: string; |
44 | | - start: string; |
45 | | - end: string; |
46 | | - attributeValueConnector: string; |
47 | | - attributeKeyQuotes?: boolean; |
48 | | -} |
49 | | - |
50 | 26 | export function stringifyGenerationConfig( |
51 | 27 | config: PartialGenerationParameters, |
52 | | - opts: StringifyGenerationConfigOptions |
| 28 | + opts: { |
| 29 | + indent: string; |
| 30 | + attributeValueConnector: string; |
| 31 | + attributeKeyQuotes?: boolean; |
| 32 | + } |
53 | 33 | ): string { |
54 | 34 | const quote = opts.attributeKeyQuotes ? `"` : ""; |
55 | 35 |
|
56 | | - return ( |
57 | | - opts.start + |
58 | | - Object.entries(config) |
59 | | - .map(([key, val]) => `${quote}${key}${quote}${opts.attributeValueConnector}${val}`) |
60 | | - .join(opts.sep) + |
61 | | - opts.end |
62 | | - ); |
| 36 | + return Object.entries(config) |
| 37 | + .map(([key, val]) => `${quote}${key}${quote}${opts.attributeValueConnector}${val}`) |
| 38 | + .join(`,${opts.indent}`); |
63 | 39 | } |
0 commit comments