sync: description generation fix - simplified prompt

This commit is contained in:
“BeeRad”
2026-01-13 11:38:25 +11:00
parent 4f030c7d29
commit f7b8b2058c
3 changed files with 34 additions and 48 deletions
@@ -32,8 +32,10 @@ export async function POST(
const newDescription = await generateDescription({ const newDescription = await generateDescription({
title: node.title, title: node.title,
content: node.content || undefined, content: node.content || undefined,
link: node.link || undefined,
metadata: node.metadata as { source?: string; channel_name?: string; author?: string; site_name?: string } | undefined, metadata: node.metadata as { source?: string; channel_name?: string; author?: string; site_name?: string } | undefined,
type: (node.metadata as { type?: string } | null)?.type type: (node.metadata as { type?: string } | null)?.type,
dimensions: node.dimensions || []
}); });
// Update the node with the new description // Update the node with the new description
+17 -14
View File
@@ -61,26 +61,29 @@ export async function POST(request: NextRequest) {
const rawContent = typeof body.content === 'string' ? body.content : null; const rawContent = typeof body.content === 'string' ? body.content : null;
// Generate description BEFORE dimension assignment (used as primary context for matching) // Process provided dimensions first (needed for description generation)
let nodeDescription: string | undefined;
try {
nodeDescription = await generateDescription({
title: body.title,
content: rawContent || undefined,
metadata: body.metadata,
type: body.type
});
} catch (error) {
console.error('Error generating description:', error);
// Continue without description - dimension assignment will use content as fallback
}
const providedDimensions = Array.isArray(body.dimensions) ? body.dimensions : []; const providedDimensions = Array.isArray(body.dimensions) ? body.dimensions : [];
const trimmedProvidedDimensions = providedDimensions const trimmedProvidedDimensions = providedDimensions
.map((dim: unknown) => typeof dim === 'string' ? dim.trim() : '') .map((dim: unknown) => typeof dim === 'string' ? dim.trim() : '')
.filter(Boolean) .filter(Boolean)
.slice(0, 8); .slice(0, 8);
// Generate description with all available context
let nodeDescription: string | undefined;
try {
nodeDescription = await generateDescription({
title: body.title,
content: rawContent || undefined,
link: body.link || undefined,
metadata: body.metadata,
type: body.type,
dimensions: trimmedProvidedDimensions
});
} catch (error) {
console.error('Error generating description:', error);
// Continue without description - dimension assignment will use content as fallback
}
// Auto-assign locked dimensions + keyword dimensions for all new nodes // Auto-assign locked dimensions + keyword dimensions for all new nodes
const { locked, keywords } = await DimensionService.assignDimensions({ const { locked, keywords } = await DimensionService.assignDimensions({
title: body.title, title: body.title,
+14 -33
View File
@@ -4,6 +4,7 @@ import { generateText } from 'ai';
export interface DescriptionInput { export interface DescriptionInput {
title: string; title: string;
content?: string; content?: string;
link?: string;
metadata?: { metadata?: {
source?: string; source?: string;
channel_name?: string; channel_name?: string;
@@ -11,11 +12,12 @@ export interface DescriptionInput {
site_name?: string; site_name?: string;
}; };
type?: string; type?: string;
dimensions?: string[];
} }
/** /**
* Generate a 280-character description for a knowledge node. * Generate a 280-character description for a knowledge node.
* The description starts with "This is a..." and identifies the content type. * Contextually grounded - adapts to node type (person, concept, article, etc.)
*/ */
export async function generateDescription(input: DescriptionInput): Promise<string> { export async function generateDescription(input: DescriptionInput): Promise<string> {
try { try {
@@ -32,7 +34,7 @@ export async function generateDescription(input: DescriptionInput): Promise<stri
const description = response.text.trim(); const description = response.text.trim();
// Ensure it starts with "This is a" and is within limit // Ensure within character limit
const finalDescription = description.slice(0, 280); const finalDescription = description.slice(0, 280);
console.log(`[DescriptionService] Generated: "${finalDescription}"`); console.log(`[DescriptionService] Generated: "${finalDescription}"`);
@@ -46,41 +48,20 @@ export async function generateDescription(input: DescriptionInput): Promise<stri
} }
function buildDescriptionPrompt(input: DescriptionInput): string { function buildDescriptionPrompt(input: DescriptionInput): string {
const metadataLines: string[] = []; const lines: string[] = [`Title: ${input.title}`];
if (input.metadata?.source) { if (input.link) lines.push(`URL: ${input.link}`);
metadataLines.push(`Source: ${input.metadata.source}`); if (input.dimensions?.length) lines.push(`Dimensions: ${input.dimensions.join(', ')}`);
} if (input.metadata?.channel_name) lines.push(`Channel: ${input.metadata.channel_name}`);
if (input.metadata?.channel_name) { if (input.metadata?.author) lines.push(`Author: ${input.metadata.author}`);
metadataLines.push(`Channel: ${input.metadata.channel_name}`); if (input.metadata?.site_name) lines.push(`Site: ${input.metadata.site_name}`);
}
if (input.metadata?.author) {
metadataLines.push(`Author: ${input.metadata.author}`);
}
if (input.metadata?.site_name) {
metadataLines.push(`Site: ${input.metadata.site_name}`);
}
if (input.type) {
metadataLines.push(`Type: ${input.type}`);
}
const contentPreview = input.content?.slice(0, 500) || ''; const contentPreview = input.content?.slice(0, 800) || '';
if (contentPreview) lines.push(`Content: ${contentPreview}${input.content && input.content.length > 800 ? '...' : ''}`);
return `Generate a concise description (max 280 characters) for this knowledge item. return `Your job is to do your best to answer 'what is this' - the most simple, high level contextual information of what this thing is. Users will be adding a variety of different nodes (ideas, books, podcasts, people, papers etc). Do your best to take the available information and infer what it is - high level. If unsure, that's fine just give your best guess and say you're unsure. Max 280 characters.
CRITICAL REQUIREMENTS: ${lines.join('\n')}`;
- Start with "This is a..."
- Identify the content type (article, video, paper, podcast episode, tweet, book, tutorial, etc.)
- Be specific about what the content covers
- Maximum 280 characters total
=== KNOWLEDGE ITEM ===
Title: ${input.title}
${metadataLines.length > 0 ? metadataLines.join('\n') : ''}
${contentPreview ? `\nContent preview: ${contentPreview}${input.content && input.content.length > 500 ? '...' : ''}` : ''}
=== YOUR RESPONSE ===
Write ONLY the description, nothing else. Start with "This is a..."`;
} }
export const descriptionService = { export const descriptionService = {