From b0fd4ff1aeee5fa117a4b32790cf75150dfe2ecb Mon Sep 17 00:00:00 2001 From: Zacharias-Brohn Date: Wed, 14 Jan 2026 23:13:12 +0100 Subject: [PATCH] changes --- app/api/chat/title/route.ts | 54 +++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/app/api/chat/title/route.ts b/app/api/chat/title/route.ts index 990ea61..2b38948 100644 --- a/app/api/chat/title/route.ts +++ b/app/api/chat/title/route.ts @@ -20,13 +20,28 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: 'Model and messages array are required' }, { status: 400 }); } - // Format the conversation for context + // Format the conversation for context (truncate long messages) const conversationContext = messages - .map((msg: Message) => `${msg.role === 'user' ? 'User' : 'Assistant'}: ${msg.content}`) + .map((msg: Message) => { + const role = msg.role === 'user' ? 'User' : 'Assistant'; + const content = msg.content.length > 300 ? `${msg.content.slice(0, 300)}...` : msg.content; + return `${role}: ${content}`; + }) .join('\n\n'); // Create a prompt asking for a short title with full context - const systemPrompt = `You are a helpful assistant that generates very short, descriptive titles for conversations. Based on the conversation below, generate a concise title (3-6 words maximum) that captures the main topic or intent. Reply with ONLY the title text, nothing else - no quotes, no prefixes, no explanation.`; + const systemPrompt = `Your task is to generate a SHORT title (3-6 words) for a conversation. + +Rules: +- Output ONLY the title, nothing else +- No quotes, no "Title:" prefix, no explanation +- Maximum 6 words +- Be descriptive but concise +- Examples of good titles: + - "Weather Comparison Three Cities" + - "Python Debugging Help" + - "Recipe for Chocolate Cake" + - "React Component Tutorial"`; const response = await ollama.chat({ model, @@ -34,31 +49,48 @@ export async function POST(request: NextRequest) { { role: 'system', content: systemPrompt }, { role: 'user', - content: `Generate a short title for this conversation:\n\n${conversationContext}`, + content: `Conversation:\n\n${conversationContext}\n\nGenerate a 3-6 word title:`, }, ], options: { - temperature: 0.7, - num_predict: 25, // Limit output length + temperature: 0.3, // Lower temperature for more focused output + num_predict: 20, // Limit output length }, }); - // Clean up the response - remove quotes, extra whitespace, etc. + // eslint-disable-next-line no-console + console.log('[Title API] Raw model response:', response.message.content); + + // Clean up the response - remove quotes, extra whitespace, thinking, etc. let title = response.message.content .trim() + .replace(/[\s\S]*?<\/think>/g, '') // Remove thinking tags .replace(/^["']|["']$/g, '') // Remove surrounding quotes .replace(/^Title:\s*/i, '') // Remove "Title:" prefix if present + .replace(/^["']|["']$/g, '') // Remove quotes again after prefix removal .replace(/\n.*/g, '') // Only keep first line .trim(); - // Fallback if title is empty or too long - if (!title || title.length > 50) { + // If still too long, take first 6 words + const words = title.split(/\s+/); + if (words.length > 6) { + title = words.slice(0, 6).join(' '); + } + + // Fallback if title is empty or suspiciously long (model didn't follow instructions) + if (!title || title.length > 60) { const firstUserMessage = messages.find((m: Message) => m.role === 'user')?.content || ''; - title = firstUserMessage.slice(0, 30) + (firstUserMessage.length > 30 ? '...' : ''); + // Extract key words instead of just truncating + const keyWords = firstUserMessage + .replace(/[?!.,]/g, '') + .split(/\s+/) + .slice(0, 5) + .join(' '); + title = keyWords || 'New Chat'; } // eslint-disable-next-line no-console - console.log('[Title API] Generated title:', title); + console.log('[Title API] Final title:', title); return NextResponse.json({ title }); } catch (error) {