Mirrored from https://github.com/Camo67/OCIU-npo.git
Synced Nov 21, 03:37 UTC
main
| 1 | import os |
| 2 | import httpx |
| 3 | import hashlib |
| 4 | from backend.services.redis_cache import redis_cache |
| 5 | |
| 6 | class GeminiService: |
| 7 | def __init__(self): |
| 8 | self.api_key = os.getenv("GEMINI_API_KEY") |
| 9 | self.base_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent" |
| 10 | |
| 11 | async def generate(self, prompt: str, temperature: float = 0.3) -> str: |
| 12 | prompt_hash = hashlib.sha256(prompt.encode()).hexdigest() |
| 13 | cached = await redis_cache.get(f"gemini:{prompt_hash}") |
| 14 | if cached: |
| 15 | return cached |
| 16 | |
| 17 | if not self.api_key: |
| 18 | return "Gemini API key not configured." |
| 19 | |
| 20 | url = f"{self.base_url}?key={self.api_key}" |
| 21 | payload = { |
| 22 | "contents": [{"parts": [{"text": prompt}]}], |
| 23 | "generationConfig": { |
| 24 | "temperature": temperature, |
| 25 | "maxOutputTokens": 2048, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | async with httpx.AsyncClient() as client: |
| 30 | resp = await client.post(url, json=payload) |
| 31 | if resp.status_code == 200: |
| 32 | result = resp.json()["candidates"][0]["content"]["parts"][0]["text"] |
| 33 | await redis_cache.set(f"gemini:{prompt_hash}", result, ttl=86400) # 24h |
| 34 | return result |
| 35 | return f"Gemini Error: {resp.text}" |
| 36 | |
| 37 | gemini_service = GeminiService() |