Improve AI description generation to prevent repetitive content and support German

Enhance the AI service to include memory for previously generated descriptions, implement German language support for prompts, and refine prompt instructions to avoid mentioning unknown information.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 170e18f0-0f13-4eca-8643-546bba1dd8cc
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/170e18f0-0f13-4eca-8643-546bba1dd8cc/LY6xmBI
This commit is contained in:
sebastjanartic 2025-09-02 13:16:05 +00:00
parent b8eb1642e9
commit 6b80ac00c6

View File

@ -3,6 +3,26 @@ import OpenAI from "openai";
// the newest OpenAI model is "gpt-5" which was released August 7, 2025. do not change this unless explicitly requested by the user // the newest OpenAI model is "gpt-5" which was released August 7, 2025. do not change this unless explicitly requested by the user
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// Memory for generated descriptions to avoid repetition
const generatedDescriptions = new Map<string, Set<string>>();
function getArtistFromTitle(title: string): string {
// Extract artist name from title (everything before the first " - ")
const match = title.match(/^([^-]+)/);
return match ? match[1].trim() : "";
}
function addToMemory(artist: string, description: string) {
if (!generatedDescriptions.has(artist)) {
generatedDescriptions.set(artist, new Set());
}
generatedDescriptions.get(artist)!.add(description);
}
function getPreviousDescriptions(artist: string): string[] {
return Array.from(generatedDescriptions.get(artist) || []);
}
export interface DescriptionGenerationOptions { export interface DescriptionGenerationOptions {
maxCharacters?: number; maxCharacters?: number;
language?: string; language?: string;
@ -22,27 +42,38 @@ export async function generateVideoDescription(
} = options; } = options;
try { try {
const prompt = `Analiziraj naslov videoisranja: "${title}" // Extract artist name for memory checking
const artist = getArtistFromTitle(title);
const previousDescriptions = getPreviousDescriptions(artist);
const avoidRepetition = previousDescriptions.length > 0
? `\n\nWICHTIG: Für diesen Künstler wurden bereits folgende Beschreibungen erstellt:\n${previousDescriptions.map((desc, i) => `${i+1}. ${desc.substring(0, 100)}...`).join('\n')}\n\nErstelle eine VÖLLIG ANDERE Beschreibung mit anderen Worten, Fokus und Stil.`
: '';
Nalogo izvršit v slovenskem jeziku. const prompt = `Analysiere diesen Musikvideo-Titel: "${title}"
Iz naslova izvleci: Schreibe auf DEUTSCH eine informative Beschreibung.
- Ime izvajalca/umetnika
- Naslov skladbe/komada
- Tip vsebine (pesem, instrumental, live nastop, itd.)
Ustvari informativen opis, ki vključuje: Aus dem Titel extrahieren:
${includeArtistInfo ? '- Informacije o izvajalcu (stil glasbe, kratka zgodovina, znani komadi)' : ''} - Name des Interpreten/Künstlers
${includeLabelInfo ? '- Informacije o založbi ali labelu, če je znan' : ''} - Titel des Liedes/Stücks
- Opis stila glasbe in žanra - Art des Inhalts (Lied, Instrumental, Live-Auftritt, etc.)
- Kratko ozadje o komadu, če je znan
- Čemu je namenjen (ples, poslušanje, koncert, itd.)
Opis naj bo dolg maksimalno ${maxCharacters} znakov. Erstelle eine informative Beschreibung, die Folgendes beinhaltet:
Opis naj bo napisan v prijaznem, informativnem tonu. ${includeArtistInfo ? '- Informationen über den Interpreten (Musikstil, kurze Geschichte, bekannte Stücke)' : ''}
Ne uporabljaj besed "video" ali "posnetek" - piši o glasbi sami. - Beschreibung des Musikstils und Genres
- Kurzer Hintergrund zum Stück, falls bekannt
- Wofür es gedacht ist (Tanz, Zuhören, Konzert, etc.)
Odgovori samo z opisom, brez dodatnih pojasnil.`; NUR wenn du SICHERE Informationen hast, erwähne:
${includeLabelInfo ? '- Label/Plattenfirma (nur wenn du es sicher weißt)' : ''}
Wenn du keine sicheren Informationen zu einem Punkt hast, lasse ihn WEG. Schreibe NIEMALS "Label unbekannt" oder ähnliches.
Die Beschreibung soll maximal ${maxCharacters} Zeichen lang sein.
Schreibe in einem freundlichen, informativen Ton.
Verwende nicht die Wörter "Video" oder "Aufnahme" - schreibe über die Musik selbst.
Schreibe nur die Beschreibung, keine zusätzlichen Erklärungen.${avoidRepetition}";`
console.log("Sending request to OpenAI with title:", title); // Debug log console.log("Sending request to OpenAI with title:", title); // Debug log
@ -51,7 +82,7 @@ Odgovori samo z opisom, brez dodatnih pojasnil.`;
messages: [ messages: [
{ {
role: "system", role: "system",
content: "Si strokovnjak za glasbo in pomagaš ustvarjati kakovostne opise za glasbene vsebine. Odgovarjaš vedno v slovenskem jeziku." content: "Du bist ein Musikexperte und hilfst bei der Erstellung hochwertiger Beschreibungen für Musikinhalte. Du antwortest immer auf Deutsch und vermeidest Wiederholungen."
}, },
{ {
role: "user", role: "user",
@ -66,6 +97,13 @@ Odgovori samo z opisom, brez dodatnih pojasnil.`;
const description = response.choices[0].message.content?.trim() || ""; const description = response.choices[0].message.content?.trim() || "";
// Add to memory for this artist
if (artist && description) {
addToMemory(artist, description);
}
console.log("Generated description:", description); // Debug log
// Ensure we don't exceed character limit // Ensure we don't exceed character limit
if (description.length > maxCharacters) { if (description.length > maxCharacters) {
return description.substring(0, maxCharacters - 3) + "..."; return description.substring(0, maxCharacters - 3) + "...";