-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqwen_template.js
More file actions
62 lines (59 loc) · 2.04 KB
/
Copy pathqwen_template.js
File metadata and controls
62 lines (59 loc) · 2.04 KB
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
export function reconstructAssistantRaw(prompt, message, enableThinking) {
const content = message?.content ?? "";
const reasoning = message?.reasoning_content ?? "";
if (!enableThinking) return prompt + content;
if (!reasoning) return prompt + content;
const think = `<think>\n${reasoning}`;
return content
? `${prompt}${think}\n</think>\n\n${content}`
: prompt + think;
}
export function parseRawToolCall(content, enableThinking = false) {
const match = content.match(/<tool_call>\s*([\s\S]*?)\s*<\/tool_call>/);
if (!match) return null;
const payload = JSON.parse(match[1]);
const before = content.slice(0, match.index);
let reasoningSuffix = "";
let contentPrefix = before;
if (enableThinking) {
const close = before.indexOf("</think>");
if (close >= 0) {
reasoningSuffix = before.slice(0, close).replace(/^<think>\s*/, "");
contentPrefix = before.slice(close + "</think>".length);
} else {
reasoningSuffix = before.replace(/^<think>\s*/, "");
contentPrefix = "";
}
} else {
contentPrefix = before.replace(/<\/?think>/g, "");
}
return {
reasoningSuffix,
contentPrefix,
name: payload.name,
arguments: typeof payload.arguments === "string"
? payload.arguments
: JSON.stringify(payload.arguments ?? {})
};
}
export function rawStructureState(content) {
const openThink = content.lastIndexOf("<think>") > content.lastIndexOf("</think>");
const openTool = content.lastIndexOf("<tool_call>") > content.lastIndexOf("</tool_call>");
return {
open_think: openThink,
open_tool_call: openTool,
complete_tool_call: /<tool_call>[\s\S]*<\/tool_call>/.test(content)
};
}
export function splitRawThinking(content) {
const normalized = content.replace(/^<think>\s*/, "");
const close = normalized.indexOf("</think>");
if (close < 0) {
return { reasoning: normalized, content: "", closed: false };
}
return {
reasoning: normalized.slice(0, close),
content: normalized.slice(close + "</think>".length).replace(/^\s+/, ""),
closed: true
};
}