File size: 971 Bytes
b25beac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# mcp/gemini.py
"""
Lightweight Gemini-Pro helper (text in → text out).
Requires env var GEMINI_KEY.
"""

import os, asyncio, google.generativeai as genai  # SDK  :contentReference[oaicite:1]{index=1}

GEN_KEY = os.getenv("GEMINI_KEY")
if GEN_KEY:
    genai.configure(api_key=GEN_KEY)

_MODEL = None
def _model():
    global _MODEL
    if _MODEL is None:
        _MODEL = genai.GenerativeModel("gemini-pro")  # 32 k ctx ­ :contentReference[oaicite:2]{index=2}
    return _MODEL

# ---------- public helpers ----------
async def gemini_summarize(text: str, words: int = 150) -> str:
    prompt = f"Summarize in ≤{words} words:\n{text[:12000]}"
    rsp = await asyncio.to_thread(_model().generate_content, prompt)
    return rsp.text

async def gemini_qa(question: str, context: str = "") -> str:
    prompt = f"Answer briefly.\nContext:\n{context[:10000]}\n\nQ: {question}\nA:"
    rsp = await asyncio.to_thread(_model().generate_content, prompt)
    return rsp.text