Skip to content

Commit 7bf93a9

Browse files
committed
fix: improve thought segmentation syntax extraction
1 parent 3967e8c commit 7bf93a9

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

src/chatWrappers/QwenChatWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ export class QwenChatWrapper extends ChatWrapper {
8484
segments: {
8585
reiterateStackAfterFunctionCalls: true,
8686
thought: {
87-
prefix: LlamaText(new SpecialTokensText("<think>")),
88-
suffix: LlamaText(new SpecialTokensText("</think>"))
87+
prefix: LlamaText(new SpecialTokensText("<think>\n")),
88+
suffix: LlamaText(new SpecialTokensText("\n</think>"))
8989
}
9090
}
9191
};

src/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@ export function extractSegmentSettingsFromTokenizerAndChatTemplate(
88
function tryMatchPrefixSuffixPair(tryMatchGroups: [prefix: string, suffix: string][]) {
99
if (chatTemplate != null) {
1010
for (const [prefix, suffix] of tryMatchGroups) {
11+
if (
12+
(
13+
hasAll(chatTemplate.replaceAll(prefix + "\\n\\n" + suffix, ""), [
14+
prefix + "\\n\\n",
15+
"\\n\\n" + suffix
16+
])
17+
) || (
18+
hasAll(chatTemplate.replaceAll(prefix + "\n\n" + suffix, ""), [
19+
prefix + "\n\n",
20+
"\n\n" + suffix
21+
])
22+
)
23+
)
24+
return {
25+
prefix: LlamaText(new SpecialTokensText(prefix + "\n\n")),
26+
suffix: LlamaText(new SpecialTokensText("\n\n" + suffix))
27+
};
28+
29+
if (
30+
(
31+
hasAll(chatTemplate.replaceAll(prefix + "\\n" + suffix, ""), [
32+
prefix + "\\n",
33+
"\\n" + suffix
34+
])
35+
) || (
36+
hasAll(chatTemplate.replaceAll(prefix + "\n" + suffix, ""), [
37+
prefix + "\n",
38+
"\n" + suffix
39+
])
40+
)
41+
)
42+
return {
43+
prefix: LlamaText(new SpecialTokensText(prefix + "\n")),
44+
suffix: LlamaText(new SpecialTokensText("\n" + suffix))
45+
};
46+
1147
if (chatTemplate.includes(prefix) && chatTemplate.includes(suffix))
1248
return {
1349
prefix: LlamaText(new SpecialTokensText(prefix)),
@@ -46,3 +82,7 @@ export function extractSegmentSettingsFromTokenizerAndChatTemplate(
4682
])
4783
});
4884
}
85+
86+
function hasAll(text: string, matches: string[]) {
87+
return matches.every((match) => text.includes(match));
88+
}

test/modelDependent/qwen3-0.6b/functions.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ describe("qwen3 0.6b", () => {
4141
}
4242
} as const;
4343

44-
const res = await chatSession.prompt("What is the second word?", {
44+
const res = await chatSession.prompt("What is the second word? No yapping, no formatting", {
4545
...promptOptions,
46-
maxTokens: 200
46+
maxTokens: 250,
47+
budgets: {
48+
thoughtTokens: 100
49+
}
4750
});
4851

4952
expect(res.trim()).to.be.eq('The second word is "secret".');
@@ -96,12 +99,15 @@ describe("qwen3 0.6b", () => {
9699
}
97100
} as const;
98101

99-
const res = await chatSession.prompt("What is the second word?", {
102+
const res = await chatSession.prompt("What is the second word? No yapping, no formatting", {
100103
...promptOptions,
101-
maxTokens: 205
104+
maxTokens: 250,
105+
budgets: {
106+
thoughtTokens: 100
107+
}
102108
});
103109

104-
expect(res.trim()).to.be.eq('The second word is **"secret"**.');
110+
expect(res.trim()).to.be.eq('The second word is "secret".');
105111

106112
const res2 = await chatSession.prompt("Explain what this word means", {
107113
...promptOptions,

0 commit comments

Comments
 (0)