interface DescriptionInput { title: string rawDescription: string comment: string vendor: string famille: string } interface Descriptions { shortDesc: string longDesc: string } async function callOpenAI(prompt: string): Promise { const apiKey = process.env.OPENAI_API_KEY if (!apiKey) throw new Error('OPENAI_API_KEY requis') const res = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}` }, body: JSON.stringify({ model: 'gpt-4o', messages: [{ role: 'user', content: prompt }], temperature: 0.7 }), }) if (!res.ok) { const err = await res.text() throw new Error(`OpenAI API error: ${res.status} — ${err}`) } const data = await res.json() return (data.choices?.[0]?.message?.content ?? '').trim() } function buildPrompt(input: DescriptionInput, length: number): string { return `A partir des caractéristiques suivantes, crée un texte descriptif de ${length} caractères en faisant attention au SEO pour qu'il soit en rapport avec la catégorie ${input.famille} et le produit ${input.title} : Nom = ${input.title} Description : ${input.rawDescription} Commentaire : ${input.comment} Marque : ${input.vendor} Formate ce texte en HTML. Ta réponse doit commencer directement par une balise HTML (comme

ou

), sans bloc \`\`\`html, sans aucun mot ou encadré autour.` } export async function generateDescriptions(input: DescriptionInput): Promise { const [shortDesc, longDesc] = await Promise.all([ callOpenAI(buildPrompt(input, 400)), callOpenAI(buildPrompt(input, 1000)), ]) return { shortDesc, longDesc } }