-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgemini.ts
More file actions
41 lines (37 loc) · 1.29 KB
/
gemini.ts
File metadata and controls
41 lines (37 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { GoogleGenAI } from "@google/genai";
import { getConfigValue } from "@/lib/config";
let _ai: GoogleGenAI | null = null;
/** Lazy-initialize the GoogleGenAI client (avoids crash at import time if GEMINI_API_KEY is missing). */
function getAI(): GoogleGenAI {
if (!_ai) {
const apiKey = process.env.GEMINI_API_KEY || "";
_ai = new GoogleGenAI({ apiKey });
}
return _ai;
}
/**
* Generate text content using Gemini Flash.
* Used across the content engine for script generation, metadata, and intent extraction.
* @param prompt - User prompt
* @param systemInstruction - Optional system instruction (persona/behavior)
*/
export async function generateWithGemini(
prompt: string,
systemInstruction?: string,
): Promise<string> {
const geminiModel = await getConfigValue("pipeline_config", "geminiModel", "gemini-2.0-flash");
const ai = getAI();
const response = await ai.models.generateContent({
model: geminiModel,
contents: prompt,
...(systemInstruction && { config: { systemInstruction } }),
});
return response.text ?? "";
}
/**
* Strip markdown code fences from a string.
* Gemini often wraps JSON responses in ```json ... ``` blocks.
*/
export function stripCodeFences(text: string): string {
return text.replace(/^```(?:\w+)?\s*\n?/i, '').replace(/\n?```\s*$/i, '').trim();
}