Add files via upload
This commit is contained in:
53
services/geminiService.ts
Normal file
53
services/geminiService.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { GoogleGenAI } from "@google/genai";
|
||||
|
||||
const getClient = () => {
|
||||
const apiKey = process.env.API_KEY;
|
||||
if (!apiKey) return null;
|
||||
return new GoogleGenAI({ apiKey });
|
||||
};
|
||||
|
||||
export const generateEmailContent = async (
|
||||
prompt: string,
|
||||
section: 'header' | 'body' | 'footer'
|
||||
): Promise<string> => {
|
||||
const client = getClient();
|
||||
if (!client) {
|
||||
throw new Error("API Key not found");
|
||||
}
|
||||
|
||||
// Model selection based on text task complexity
|
||||
const modelId = 'gemini-2.5-flash';
|
||||
|
||||
const systemInstruction = `
|
||||
You are an expert email marketing developer.
|
||||
You are generating HTML content for an email template ${section}.
|
||||
|
||||
RULES:
|
||||
1. Return ONLY the HTML code. Do not include markdown ticks, 'html' labels, or explanations.
|
||||
2. Use inline CSS styles for compatibility.
|
||||
3. Use the Handlebars syntax for placeholders: {{variable_name}}.
|
||||
4. If the user asks for a specific variable, format it correctly.
|
||||
5. For Header: Include logo placeholders or standard menu links if asked.
|
||||
6. For Footer: Include unsubscribe links and copyright info if asked.
|
||||
7. For Body: Focus on clean, readable content.
|
||||
`;
|
||||
|
||||
try {
|
||||
const response = await client.models.generateContent({
|
||||
model: modelId,
|
||||
contents: prompt,
|
||||
config: {
|
||||
systemInstruction: systemInstruction,
|
||||
temperature: 0.7,
|
||||
}
|
||||
});
|
||||
|
||||
let text = response.text || "";
|
||||
// Cleanup if model adds markdown despite instructions
|
||||
text = text.replace(/```html/g, '').replace(/```/g, '').trim();
|
||||
return text;
|
||||
} catch (error) {
|
||||
console.error("Gemini API Error:", error);
|
||||
throw new Error("Failed to generate content");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user