58 lines
2.3 KiB
TypeScript
58 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { GoogleGenAI } from '@google/genai'
|
|
|
|
// POST /api/gemini — AI content generation
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const apiKey = process.env.GEMINI_API_KEY
|
|
if (!apiKey || apiKey === 'PLACEHOLDER_API_KEY') {
|
|
return NextResponse.json(
|
|
{ error: 'Clé API Gemini non configurée' },
|
|
{ status: 503 }
|
|
)
|
|
}
|
|
|
|
const ai = new GoogleGenAI({ apiKey })
|
|
const body = await request.json()
|
|
const { action, name, category, keywords } = body
|
|
|
|
if (action === 'description') {
|
|
const prompt = `
|
|
Tu es un expert en copywriting marketing pour Afropreunariat.
|
|
Rédige une description professionnelle, attrayante et optimisée SEO pour une entreprise.
|
|
|
|
Nom de l'entreprise : ${name}
|
|
Secteur : ${category}
|
|
Mots-clés/Services : ${keywords}
|
|
|
|
La description doit faire environ 80-100 mots, être en français, inspirer confiance et professionnalisme.
|
|
Ne mets pas de guillemets au début ou à la fin.
|
|
`
|
|
const response = await ai.models.generateContent({
|
|
model: 'gemini-2.5-flash',
|
|
contents: prompt,
|
|
})
|
|
return NextResponse.json({ text: response.text || '' })
|
|
}
|
|
|
|
if (action === 'slogans') {
|
|
const prompt = `Donne-moi 3 idées de slogans courts et percutants pour une entreprise dans le secteur : ${category}. Retourne uniquement une liste JSON de chaînes de caractères.`
|
|
const response = await ai.models.generateContent({
|
|
model: 'gemini-2.5-flash',
|
|
contents: prompt,
|
|
config: { responseMimeType: 'application/json' },
|
|
})
|
|
const json = JSON.parse(response.text || '[]')
|
|
return NextResponse.json({ slogans: Array.isArray(json) ? json : [] })
|
|
}
|
|
|
|
return NextResponse.json({ error: 'Action inconnue' }, { status: 400 })
|
|
} catch (error) {
|
|
console.error('POST /api/gemini error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Erreur lors de la génération IA' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|