File size: 1,894 Bytes
f3689e4
a636ecb
f3689e4
a636ecb
 
f3689e4
 
a636ecb
 
f3689e4
 
a636ecb
8f20aeb
 
 
 
 
 
a636ecb
f3689e4
a636ecb
8f20aeb
 
 
 
a636ecb
 
 
 
 
8f20aeb
 
 
 
a636ecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Gemini REST API wrapper — works with X-goog-api-key format."""
import logging
import os
import time

import requests

log = logging.getLogger(__name__)

GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent"


def generate(
    prompt: str,
    retries: int = 2,
    temperature: float = 0.7,
    json_mode: bool = False,
) -> str:
    """Call Gemini and return the text response, retrying on empty replies."""
    key = os.getenv("GEMINI_API_KEY", "")
    last_err = None
    generation_config: dict = {"temperature": temperature}
    if json_mode:
        generation_config["responseMimeType"] = "application/json"

    for attempt in range(retries + 1):
        try:
            resp = requests.post(
                GEMINI_URL,
                headers={"Content-Type": "application/json", "X-goog-api-key": key},
                json={
                    "contents": [{"parts": [{"text": prompt}]}],
                    "generationConfig": generation_config,
                },
                timeout=90,
            )
            resp.raise_for_status()
            data = resp.json()
            candidates = data.get("candidates") or []
            if not candidates:
                last_err = RuntimeError("Gemini returned no candidates")
                time.sleep(2 * (attempt + 1))
                continue
            parts = candidates[0].get("content", {}).get("parts") or []
            text = parts[0].get("text", "") if parts else ""
            if text.strip():
                return text
            last_err = RuntimeError("Gemini returned empty text")
            time.sleep(2 * (attempt + 1))
        except Exception as e:
            last_err = e
            if attempt < retries:
                time.sleep(2 * (attempt + 1))
    raise RuntimeError(f"Gemini failed after {retries + 1} attempts: {last_err}")