feat: Add reasoning token support with expandable thinking blocks
- Add reasoning field to AssistantMessage type - Create ReasoningBlock component with truncate/expand functionality - Add reasoning-capable models (Qwen QwQ, DeepSeek R1, Qwen 3 32B) - Implement reasoning event handling in stream processing - Enable reasoning format for Groq and Anthropic models - Style reasoning blocks with grey border and reduced font size - Display reasoning above assistant messages when available The reasoning blocks show truncated thinking tokens by default and expand on click to show full reasoning process from models like Qwen3-32b that support step-by-step reasoning output.
This commit is contained in:
parent
ace85a6c69
commit
62000dcdac
6 changed files with 384 additions and 300 deletions
|
|
@ -5,6 +5,7 @@ import rehypeHighlight from "rehype-highlight";
|
|||
import { type Message } from "../types";
|
||||
import ToolCallMessage from "./ToolCallMessage";
|
||||
import ToolResultMessage from "./ToolResultMessage";
|
||||
import ReasoningBlock from "./ReasoningBlock";
|
||||
|
||||
interface ChatMessageProps {
|
||||
message: Message;
|
||||
|
|
@ -37,6 +38,11 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
|
|||
: " text-zinc-900 w-full"
|
||||
}`}
|
||||
>
|
||||
{/* Show reasoning block for assistant messages with reasoning */}
|
||||
{message.role === "assistant" && "reasoning" in message && message.reasoning && (
|
||||
<ReasoningBlock reasoning={message.reasoning} />
|
||||
)}
|
||||
|
||||
<div className="prose prose-zinc">
|
||||
<ReactMarkdown
|
||||
remarkPlugins={[remarkGfm]}
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ const ConversationThread: React.FC<ConversationThreadProps> = ({
|
|||
}
|
||||
}, [conversations])
|
||||
|
||||
console.log({ currentConversation })
|
||||
// console.log({ currentConversation })
|
||||
|
||||
return (
|
||||
<div className={`flex flex-col min-h-screen w-full ${currentConversation.messages.length === 0 ? 'justify-center' : ''}`}>
|
||||
|
|
@ -233,7 +233,7 @@ const ConversationThread: React.FC<ConversationThreadProps> = ({
|
|||
<span className="text-lg">🧠</span>
|
||||
<span className="text-sm text-zinc-500">
|
||||
{hasApiKey(selectedModel.provider.id) ? (
|
||||
selectedModel.name.toLowerCase()
|
||||
selectedModel.name.toLowerCase() + ' (' + selectedModel.provider.name.toLowerCase() + ')'
|
||||
) : (
|
||||
"none"
|
||||
)}
|
||||
|
|
|
|||
54
examples/chat-ui/src/components/ReasoningBlock.tsx
Normal file
54
examples/chat-ui/src/components/ReasoningBlock.tsx
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import React, { useState } from "react";
|
||||
|
||||
interface ReasoningBlockProps {
|
||||
reasoning: string;
|
||||
}
|
||||
|
||||
const ReasoningBlock: React.FC<ReasoningBlockProps> = ({ reasoning }) => {
|
||||
const [isExpanded, setIsExpanded] = useState(false);
|
||||
|
||||
if (!reasoning || reasoning.trim().length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const toggleExpanded = () => {
|
||||
setIsExpanded(!isExpanded);
|
||||
};
|
||||
|
||||
// Truncate the reasoning text for the collapsed state
|
||||
const maxLength = 120;
|
||||
const shouldTruncate = reasoning.length > maxLength;
|
||||
const displayText = isExpanded || !shouldTruncate
|
||||
? reasoning
|
||||
: reasoning.substring(0, maxLength) + "...";
|
||||
|
||||
return (
|
||||
<div className="mb-3">
|
||||
<div
|
||||
className={`text-sm text-zinc-600 border border-zinc-200 rounded-lg p-3 bg-zinc-50 cursor-pointer hover:bg-zinc-100 transition-colors ${shouldTruncate && !isExpanded ? "line-clamp-1" : ""
|
||||
}`}
|
||||
onClick={toggleExpanded}
|
||||
>
|
||||
<div className="flex items-start gap-2">
|
||||
<span className="text-zinc-400 text-xs mt-0.5 flex-shrink-0">💭</span>
|
||||
<div className={`${shouldTruncate && !isExpanded ? "truncate" : "whitespace-pre-wrap"}`}>
|
||||
{displayText}
|
||||
</div>
|
||||
{shouldTruncate && (
|
||||
<button
|
||||
className="text-zinc-400 hover:text-zinc-600 text-xs ml-auto flex-shrink-0"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
toggleExpanded();
|
||||
}}
|
||||
>
|
||||
{isExpanded ? "↑" : "↓"}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ReasoningBlock;
|
||||
|
|
@ -80,6 +80,19 @@ export const useStreamResponse = ({
|
|||
return aiTools
|
||||
}
|
||||
|
||||
// Check if a model supports reasoning
|
||||
const supportsReasoning = (model: Model) => {
|
||||
const reasoningModels = [
|
||||
'qwen/qwen3-32b',
|
||||
'qwen-qwq-32b',
|
||||
'deepseek-r1-distill-llama-70b',
|
||||
'claude-4-opus-20250514',
|
||||
'claude-4-sonnet-20250514',
|
||||
'claude-3-7-sonnet-20250219'
|
||||
]
|
||||
return reasoningModels.includes(model.modelId)
|
||||
}
|
||||
|
||||
const getModelInstance = (model: Model, apiKey: string) => {
|
||||
switch (model.provider.id) {
|
||||
case 'groq': {
|
||||
|
|
@ -102,6 +115,7 @@ export const useStreamResponse = ({
|
|||
|
||||
const streamResponse = async (messages: Message[]) => {
|
||||
let aiResponse = ''
|
||||
let reasoning = ''
|
||||
let assistantMessageIndex = -1 // Track the index of our assistant message
|
||||
let assistantMessageCreated = false // Track if we've created the assistant message yet
|
||||
|
||||
|
|
@ -138,7 +152,8 @@ export const useStreamResponse = ({
|
|||
content: hasContent(m) ? m.content.substring(0, 100) + '...' : 'no content'
|
||||
})))
|
||||
|
||||
const result = await streamText({
|
||||
// Prepare stream options
|
||||
const streamOptions: any = {
|
||||
model: modelInstance,
|
||||
messages: messages.filter(msg =>
|
||||
['user', 'assistant', 'system'].includes(msg.role) && hasContent(msg)
|
||||
|
|
@ -152,7 +167,30 @@ export const useStreamResponse = ({
|
|||
tools: Object.keys(aiTools).length > 0 ? aiTools : undefined,
|
||||
maxSteps: 5, // Allow up to 5 steps for tool calling
|
||||
abortSignal: controller.signal,
|
||||
})
|
||||
}
|
||||
|
||||
// Add reasoning parameters for supported models
|
||||
if (supportsReasoning(selectedModel)) {
|
||||
debugLog(`[useStreamResponse] Enabling reasoning for model: ${selectedModel.modelId}`)
|
||||
|
||||
if (selectedModel.provider.id === 'groq') {
|
||||
// For Groq models, use reasoning_format parameter
|
||||
streamOptions.experimental_providerOptions = {
|
||||
groq: {
|
||||
reasoning_format: 'parsed'
|
||||
}
|
||||
}
|
||||
} else if (selectedModel.provider.id === 'anthropic') {
|
||||
// For Anthropic models, enable thinking
|
||||
streamOptions.experimental_providerOptions = {
|
||||
anthropic: {
|
||||
thinking: { type: 'enabled', budgetTokens: 12000 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await streamText(streamOptions)
|
||||
|
||||
debugLog(`[useStreamResponse] streamText result obtained, starting to process fullStream`)
|
||||
|
||||
|
|
@ -163,7 +201,10 @@ export const useStreamResponse = ({
|
|||
try {
|
||||
debugLog(`[useStreamResponse] Full stream event:`, event.type, event)
|
||||
|
||||
if (event.type === 'tool-call') {
|
||||
if (event.type === 'reasoning') {
|
||||
debugLog(`[useStreamResponse] Reasoning event:`, event)
|
||||
reasoning += (event as any).textDelta || ''
|
||||
} else if (event.type === 'tool-call') {
|
||||
const eventKey = `tool-call-${event.toolCallId}`
|
||||
if (processedEvents.has(eventKey)) {
|
||||
debugLog(`[useStreamResponse] Skipping duplicate tool call:`, eventKey)
|
||||
|
|
@ -196,40 +237,6 @@ export const useStreamResponse = ({
|
|||
} else {
|
||||
console.warn('[useStreamResponse] Tool call event missing toolName:', event)
|
||||
}
|
||||
} else if (event.type === 'tool-result') {
|
||||
const eventKey = `tool-result-${event.toolCallId}`
|
||||
if (processedEvents.has(eventKey)) {
|
||||
debugLog(`[useStreamResponse] Skipping duplicate tool result:`, eventKey)
|
||||
continue
|
||||
}
|
||||
processedEvents.add(eventKey)
|
||||
|
||||
debugLog(`[useStreamResponse] Tool result event:`, event)
|
||||
if (event.toolName) {
|
||||
setConversations((prev) => {
|
||||
const updated = [...prev]
|
||||
const conv = updated.find((c) => c.id === conversationId)
|
||||
if (conv) {
|
||||
// Check if this tool result already exists
|
||||
const existingToolResult = conv.messages.find(
|
||||
(msg) => msg.role === 'tool-result' && 'callId' in msg && msg.callId === event.toolCallId
|
||||
)
|
||||
if (!existingToolResult) {
|
||||
conv.messages.push({
|
||||
role: 'tool-result',
|
||||
toolName: event.toolName,
|
||||
toolArgs: event.args || {},
|
||||
toolResult: event.result,
|
||||
callId: event.toolCallId || 'unknown',
|
||||
})
|
||||
}
|
||||
}
|
||||
return updated
|
||||
})
|
||||
scrollToBottom()
|
||||
} else {
|
||||
console.warn('[useStreamResponse] Tool result event missing toolName:', event)
|
||||
}
|
||||
} else if (event.type === 'text-delta') {
|
||||
debugLog(`[useStreamResponse] Text delta:`, event.textDelta)
|
||||
|
||||
|
|
@ -240,7 +247,7 @@ export const useStreamResponse = ({
|
|||
const updated = [...prev]
|
||||
const conv = updated.find((c) => c.id === conversationId)
|
||||
if (conv) {
|
||||
conv.messages.push({ role: 'assistant', content: '' })
|
||||
conv.messages.push({ role: 'assistant', content: '', reasoning: reasoning || undefined })
|
||||
assistantMessageIndex = conv.messages.length - 1
|
||||
debugLog(`[useStreamResponse] Created assistant message at index ${assistantMessageIndex}`)
|
||||
}
|
||||
|
|
@ -277,6 +284,10 @@ export const useStreamResponse = ({
|
|||
const assistantMessage = conv.messages[assistantMessageIndex]
|
||||
if (assistantMessage && hasContent(assistantMessage) && assistantMessage.role === 'assistant') {
|
||||
assistantMessage.content = aiResponse
|
||||
// Update reasoning if available
|
||||
if (reasoning) {
|
||||
assistantMessage.reasoning = reasoning
|
||||
}
|
||||
debugLog(`[useStreamResponse] Updated assistant message at index ${assistantMessageIndex}`)
|
||||
} else {
|
||||
debugLog(`[useStreamResponse] Could not find assistant message at index ${assistantMessageIndex}`, assistantMessage)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ export interface SystemMessage extends BaseMessage {
|
|||
|
||||
export interface AssistantMessage extends BaseMessage {
|
||||
role: "assistant";
|
||||
reasoning?: string;
|
||||
}
|
||||
|
||||
export interface ToolCallMessage {
|
||||
|
|
|
|||
|
|
@ -42,10 +42,22 @@ export const availableModels: Model[] = [
|
|||
},
|
||||
{
|
||||
id: 'qwen-3-32b',
|
||||
name: 'Qwen 3 32B',
|
||||
name: 'Qwen 3 32B (Reasoning)',
|
||||
provider: providers.groq,
|
||||
modelId: 'qwen/qwen3-32b'
|
||||
},
|
||||
{
|
||||
id: 'qwen-qwq-32b',
|
||||
name: 'Qwen QwQ 32B (Reasoning)',
|
||||
provider: providers.groq,
|
||||
modelId: 'qwen-qwq-32b'
|
||||
},
|
||||
{
|
||||
id: 'deepseek-r1-distill-llama-70b',
|
||||
name: 'DeepSeek R1 Distil Llama 70B (Reasoning)',
|
||||
provider: providers.groq,
|
||||
modelId: 'deepseek-r1-distill-llama-70b'
|
||||
},
|
||||
{
|
||||
id: 'claude-3-5-sonnet-20241022',
|
||||
name: 'Claude 4 Sonnet',
|
||||
|
|
|
|||
Loading…
Reference in a new issue