Spaces:
Sleeping

File size: 1,990 Bytes
f1eaad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
# gemini_flash_beta_llm.py

import os
import requests
from dotenv import load_dotenv
from langchain.llms.base import LLM
from typing import Optional, List

load_dotenv()

# Default settings for the Flash endpoint
API_KEY = os.getenv("GEMINI_API_KEY")
BASE_URL = "https://generativelanguage.googleapis.com/v1beta"
DEFAULT_MODEL_NAME = "gemini-2.0-flash"

class GeminiFlashBetaLLM(LLM):
    """LangChain LLM wrapper around the v1beta Flash endpoint."""
    # Pydantic fields for BaseModel compatibility
    model_name: str = DEFAULT_MODEL_NAME
    api_key: str = API_KEY

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        # Ensure API key
        key = self.api_key or API_KEY
        if not key:
            raise ValueError("Set your GEMINI_API_KEY in .env")

        url = f"{BASE_URL}/models/{self.model_name}:generateContent"
        params = {"key": key}
        body = {"contents": [{"parts": [{"text": prompt}]}]}

        r = requests.post(url, params=params, json=body)
        r.raise_for_status()
        data = r.json()

        # Extract the first candidate
        candidates = data.get("candidates") or []
        if not candidates:
            return ""
        first = candidates[0]

        # Handle content vs output fields
        content = first.get("content") if "content" in first else first.get("output")
        text = ""

        if isinstance(content, dict):
            # Some models return nested parts
            parts = content.get("parts") or []
            for part in parts:
                # support dict or plain text
                fragment = part.get("text") if isinstance(part, dict) else part
                if fragment:
                    text += fragment
        elif isinstance(content, str):
            text = content

        return text.strip()

    @property
    def _llm_type(self) -> str:
        return f"gemini-flash-v1beta-{self.model_name}"