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

server (webui): Fix issue with muliple <think> tags in response #11779

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Binary file modified examples/server/public/index.html.gz
Binary file not shown.
16 changes: 8 additions & 8 deletions examples/server/webui/src/components/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ export default function ChatMessage({
let actualContent = '';
let thought = '';
let isThinking = false;
let thinkSplit = msg.content.split('<think>', 2);
actualContent += thinkSplit[0];
while (thinkSplit[1] !== undefined) {
let [match, ...rest] = msg.content.split('<think>');
actualContent += match;
while (rest.length !== 0) {
// <think> tag found
thinkSplit = thinkSplit[1].split('</think>', 2);
thought += thinkSplit[0];
[match, ...rest] = rest.join('<think>').split('</think>');
thought += thought !== '' ? '\n***\n' + match : match;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why we need this '\n***\n' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really needed. It's just a way to visually separate the different "thoughts" with a horizontal rule/separator using markdown. I think it was a nice thing to have, but if you disagree, I'm ok with removing it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example of the raw content? I still don't get it.

Copy link
Contributor Author

@stduhpf stduhpf Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example, when using a non-reasoning model and incetivizing it to use the <think> tag multiple times in the responses: conversation_conv-1739151618584.json
(system prompt was You are a helpful assstant. You can use <think> and </think> to surround your inner chain of thought or just secret scathing remarks you want to say about the user but would be innapropriate to say out loud. You can use it as much as you'd like.)

Copy link
Collaborator

@ngxson ngxson Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok I think we should remove this '\n***\n' and tweaking the display format at HTML/CSS level instead. It's always better to keep the transformation function straight-forward (i.e. do not add things to the content).

Copy link
Collaborator

@ngxson ngxson Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I misunderstood a bit, thinking again about it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO a better way would be to refactor this to return multiple message parts, so it will become:

interface MessagePart {
  type: 'text' | 'reasoning';
  content: string;
}

// useMemo is used to transform a single content ==> list of MessagePart[]

So for example if your message is <think>test</think>hi<think>abc</think>

Then it will contains 3 parts: test - hi - abc

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also kinda make more sense to display the thought in its correct place, instead of regrouping it to the top of message

isThinking = true;
if (thinkSplit[1] !== undefined) {
if (rest.length !== 0) {
// </think> closing tag found
isThinking = false;
thinkSplit = thinkSplit[1].split('<think>', 2);
actualContent += thinkSplit[0];
[match, ...rest] = rest.join('</think>').split('<think>');
actualContent += match;
}
}
return { content: actualContent, thought, isThinking };
Expand Down