Skip to content

Handling delta.reasoning_content in llama-server's webUI #14997

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

Open
wants to merge 3 commits 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 tools/server/public/index.html.gz
Binary file not shown.
18 changes: 17 additions & 1 deletion tools/server/webui/src/components/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,25 @@ export default function ChatMessage({
// for reasoning model, we split the message into content and thought
// TODO: implement this as remark/rehype plugin in the future
const { content, thought, isThinking }: SplitMessage = useMemo(() => {
if (msg.content === null || msg.role !== 'assistant') {
if (msg.role !== 'assistant') {
return { content: msg.content };
}

// Handle case where we have reasoningContent (content can be null or empty)
if (msg.reasoningContent !== null) {
return {
content: msg.content || '',
thought: msg.reasoningContent,
isThinking: msg.content === null || msg.content === '',
};
}

// If content is null or empty, return as-is
if (msg.content === null || msg.content === '') {
return { content: msg.content };
}

// perhaps there are <think>...</think> tags in the regular content
let actualContent = '';
let thought = '';
let isThinking = false;
Expand Down
19 changes: 15 additions & 4 deletions tools/server/webui/src/utils/app.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const AppContextProvider = ({
content: null,
parent: leafNodeId,
children: [],
reasoningContent: '',
};
setPending(convId, pendingMsg);

Expand Down Expand Up @@ -254,13 +255,23 @@ export const AppContextProvider = ({
if (chunk.error) {
throw new Error(chunk.error?.message || 'Unknown error');
}
const addedContent = chunk.choices[0].delta.content;
const lastContent = pendingMsg.content || '';
if (addedContent) {

const reasoningContent = chunk.choices[0].delta.reasoning_content;
if (reasoningContent) {
const lastContent = pendingMsg.reasoningContent || '';
pendingMsg = {
...pendingMsg,
content: lastContent + addedContent,
reasoningContent: lastContent + reasoningContent,
};
} else {
const addedContent = chunk.choices[0].delta.content;
const lastContent = pendingMsg.content || '';
if (addedContent) {
pendingMsg = {
...pendingMsg,
content: lastContent + addedContent,
};
}
}
const timings = chunk.timings;
if (timings && config.showTokensPerSecond) {
Expand Down
1 change: 1 addition & 0 deletions tools/server/webui/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface Message {
type: 'text' | 'root';
timestamp: number; // timestamp from Date.now()
role: 'user' | 'assistant' | 'system';
reasoningContent?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We cannot store reasoning content inside the message object - it must be contained inside content. Otherwise, it's impossible to modify assistant message (a planned feature)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is OK because messages is not supposed to include the reasoning content from assistants. Editing reasoningContent wouldn't have any effect on the conversation.

content: string;
timings?: TimingReport;
extra?: MessageExtra[];
Expand Down