videofolxtv/server/aiService.ts
sebastjanartic 9202e323bc Improve AI service to better estimate token completion for responses
Update `server/aiService.ts` to use `max_completion_tokens` instead of `max_tokens` for more accurate AI response length estimation.

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/td5Y4HG
2025-09-02 13:00:29 +00:00

101 lines
3.1 KiB
TypeScript

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
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
export interface DescriptionGenerationOptions {
maxCharacters?: number;
language?: string;
includeArtistInfo?: boolean;
includeLabelInfo?: boolean;
}
export async function generateVideoDescription(
title: string,
options: DescriptionGenerationOptions = {}
): Promise<string> {
const {
maxCharacters = 500,
language = "slovenian",
includeArtistInfo = true,
includeLabelInfo = true
} = options;
try {
const prompt = `Analiziraj naslov videoisranja: "${title}"
Nalogo izvršit v slovenskem jeziku.
Iz naslova izvleci:
- Ime izvajalca/umetnika
- Naslov skladbe/komada
- Tip vsebine (pesem, instrumental, live nastop, itd.)
Ustvari informativen opis, ki vključuje:
${includeArtistInfo ? '- Informacije o izvajalcu (stil glasbe, kratka zgodovina, znani komadi)' : ''}
${includeLabelInfo ? '- Informacije o založbi ali labelu, če je znan' : ''}
- Opis stila glasbe in žanra
- Kratko ozadje o komadu, če je znan
- Čemu je namenjen (ples, poslušanje, koncert, itd.)
Opis naj bo dolg maksimalno ${maxCharacters} znakov.
Opis naj bo napisan v prijaznem, informativnem tonu.
Ne uporabljaj besed "video" ali "posnetek" - piši o glasbi sami.
Odgovori samo z opisom, brez dodatnih pojasnil.`;
const response = await openai.chat.completions.create({
model: "gpt-5", // the newest OpenAI model is "gpt-5" which was released August 7, 2025
messages: [
{
role: "system",
content: "Si strokovnjak za glasbo in pomagaš ustvarjati kakovostne opise za glasbene vsebine. Odgovarjaš vedno v slovenskem jeziku."
},
{
role: "user",
content: prompt
}
],
max_completion_tokens: Math.ceil(maxCharacters / 2), // Rough estimate: 2 characters per token
temperature: 0.7,
});
const description = response.choices[0].message.content?.trim() || "";
// Ensure we don't exceed character limit
if (description.length > maxCharacters) {
return description.substring(0, maxCharacters - 3) + "...";
}
return description;
} catch (error) {
console.error("Error generating video description:", error);
throw new Error("Napaka pri generiranju opisa s strani AI");
}
}
export async function generateBulkDescriptions(
videos: Array<{ id: string; title: string }>,
options: DescriptionGenerationOptions = {}
): Promise<Array<{ id: string; description: string; error?: string }>> {
const results = [];
for (const video of videos) {
try {
const description = await generateVideoDescription(video.title, options);
results.push({ id: video.id, description });
// Add small delay to respect API rate limits
await new Promise(resolve => setTimeout(resolve, 100));
} catch (error) {
results.push({
id: video.id,
description: "",
error: error instanceof Error ? error.message : "Unknown error"
});
}
}
return results;
}