MCP_Res / mcp /gemini.py
mgbam's picture
Create gemini.py
b25beac verified
raw
history blame
971 Bytes
# 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