Skip to content

Commit

Permalink
feat: detect reasoning from chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaclen committed Jan 26, 2025
1 parent 0a40b3b commit 042dcc0
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/routes/sessions/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import Messages from './Messages.svelte';
import Prompt from './Prompt.svelte';
const THINK_TAG = '<think>';
const END_THINK_TAG = '</think>';
interface Props {
data: PageData;
}
Expand Down Expand Up @@ -135,8 +138,9 @@
async function handleCompletion(messages: Message[]) {
editor.abortController = new AbortController();
editor.isCompletionInProgress = true;
editor.prompt = ''; // Reset the prompt form field
editor.prompt = '';
editor.completion = '';
editor.reasoning = '';
const server = $serversStore.find((s) => s.id === session.model?.serverId);
if (!server) throw new Error('Server not found');
Expand All @@ -161,19 +165,40 @@
}
if (!strategy) throw new Error('Invalid strategy');
let isInThinkTag = false;
await strategy.chat(chatRequest, editor.abortController.signal, async (chunk) => {
editor.completion += chunk;
if (chunk.includes(THINK_TAG)) {
isInThinkTag = true;
chunk = chunk.replace(THINK_TAG, '');
}
if (chunk.includes(END_THINK_TAG)) {
isInThinkTag = false;
chunk = chunk.replace(END_THINK_TAG, '');
}
if (isInThinkTag) {
editor.reasoning += chunk;
} else {
editor.completion += chunk;
}
await scrollToBottom();
});
// After the completion save the session
const message: Message = { role: 'assistant', content: editor.completion };
const message: Message = {
role: 'assistant',
content: editor.completion,
reasoning: editor.reasoning
};
session.messages = [...session.messages, message];
session.updatedAt = new Date().toISOString();
saveSession(session);
// Final housekeeping
editor.completion = '';
editor.reasoning = '';
editor.shouldFocusTextarea = true;
editor.isCompletionInProgress = false;
await scrollToBottom();
Expand Down

0 comments on commit 042dcc0

Please sign in to comment.