This commit is contained in:
Zacharias-Brohn
2026-01-14 23:27:54 +01:00
parent a0f9ec4381
commit fae63deda4
+15 -17
View File
@@ -6,8 +6,9 @@ interface Message {
content: string; content: string;
} }
// Use a fast, consistent model for title generation // Use a fast, non-thinking model for title generation
const TITLE_MODEL = 'deepseek-r1:8b'; // DeepSeek R1 outputs <think> tags which complicates parsing
const TITLE_MODEL = 'ministral-3:14b';
/** /**
* Generate a short, descriptive title for a chat based on the conversation * Generate a short, descriptive title for a chat based on the conversation
@@ -36,18 +37,15 @@ export async function POST(request: NextRequest) {
.join('\n\n'); .join('\n\n');
// Create a prompt asking for a short title with full context // Create a prompt asking for a short title with full context
const systemPrompt = `Your task is to generate a SHORT title (3-6 words) for a conversation. const systemPrompt = `Generate a SHORT title (3-6 words) for the conversation below.
Rules: IMPORTANT:
- Output ONLY the title, nothing else - Output ONLY the title text, nothing else
- No quotes, no "Title:" prefix, no explanation - No quotes, no punctuation, no explanation
- Maximum 6 words - Maximum 6 words
- Be descriptive but concise - Be specific and descriptive
- Examples of good titles:
- "Weather Comparison Three Cities" Good examples: "Weather in Three Cities", "Python Debugging Help", "Chocolate Cake Recipe", "React Component Tutorial"`;
- "Python Debugging Help"
- "Recipe for Chocolate Cake"
- "React Component Tutorial"`;
const response = await ollama.chat({ const response = await ollama.chat({
model: TITLE_MODEL, model: TITLE_MODEL,
@@ -55,26 +53,26 @@ Rules:
{ role: 'system', content: systemPrompt }, { role: 'system', content: systemPrompt },
{ {
role: 'user', role: 'user',
content: `Conversation:\n\n${conversationContext}\n\nGenerate a 3-6 word title:`, content: `Conversation:\n${conversationContext}\n\nTitle:`,
}, },
], ],
options: { options: {
temperature: 0.3, // Lower temperature for more focused output temperature: 0.3, // Lower temperature for more focused output
num_predict: 50, // Allow more tokens for thinking models num_predict: 20, // Short response - just the title
}, },
}); });
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('[Title API] Raw model response:', response.message.content); console.log('[Title API] Raw model response:', response.message.content);
// Clean up the response - remove quotes, extra whitespace, thinking, etc. // Clean up the response - remove quotes, extra whitespace, etc.
let title = response.message.content let title = response.message.content
.trim() .trim()
.replace(/<think>[\s\S]*?<\/think>/g, '') // Remove thinking tags
.replace(/^["']|["']$/g, '') // Remove surrounding quotes .replace(/^["']|["']$/g, '') // Remove surrounding quotes
.replace(/^Title:\s*/i, '') // Remove "Title:" prefix if present .replace(/^Title:\s*/i, '') // Remove "Title:" prefix if present
.replace(/^["']|["']$/g, '') // Remove quotes again after prefix removal .replace(/['"]/g, '') // Remove any remaining quotes
.replace(/\n.*/g, '') // Only keep first line .replace(/\n.*/g, '') // Only keep first line
.replace(/[.!?]$/, '') // Remove trailing punctuation
.trim(); .trim();
// If still too long, take first 6 words // If still too long, take first 6 words