Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: webui support think tag #1140

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: webui support think tag
  • Loading branch information
sigoden committed Feb 3, 2025
commit 3a0b9d37d601827d37b8ce54cf767b71ab42367c
29 changes: 28 additions & 1 deletion assets/arena.html
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,35 @@
</div>`;
}
};
const thinkExtension = {
name: 'think',
level: 'block',
start(src) {
return src.indexOf('<think>');
},
tokenizer(src, tokens) {
const rule = /^\s*<think>([\s\S]*?)(<\/think>|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'think',
raw: match[0],
text: match[1].trim(),
};
}
},
renderer(token) {
const isOpen = !token.raw.endsWith('</think>');
const text = '<p>' + token.text.trim().replace(/\n+/g, '</p><p>') + '</p>';
return `<details ${isOpen ? "open" : ""} class="think">
<summary>Deeply thought</summary>
<blockquote>${text}</blockquote>
</details>`;
},
};
marked.use({ renderer });
marked.use(markedKatex({ throwOnError: false, inlineTolerantNoSpace: true }));
marked.use(markedKatex({ throwOnError: false, inlineTolerantNoSpace: true }));
marked.use({ extensions: [thinkExtension] })
}

function escapeForHTML(input) {
Expand Down
27 changes: 27 additions & 0 deletions assets/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,35 @@
</div>`;
}
};
const thinkExtension = {
name: 'think',
level: 'block',
start(src) {
return src.indexOf('<think>');
},
tokenizer(src, tokens) {
const rule = /^\s*<think>([\s\S]*?)(<\/think>|$)/;
const match = rule.exec(src);
if (match) {
return {
type: 'think',
raw: match[0],
text: match[1].trim(),
};
}
},
renderer(token) {
const isOpen = !token.raw.endsWith('</think>');
const text = '<p>' + token.text.trim().replace(/\n+/g, '</p><p>') + '</p>';
return `<details ${isOpen ? "open" : ""} class="think">
<summary>Deeply thought</summary>
<blockquote>${text}</blockquote>
</details>`;
},
};
marked.use({ renderer });
marked.use(markedKatex({ throwOnError: false, inlineTolerantNoSpace: true }));
marked.use({ extensions: [thinkExtension] })
}

function escapeForHTML(input) {
Expand Down