import os, json, urllib.request

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

def query_llm(prompt):
    key = os.environ.get("GEMINI_API_KEY", "")
    url = GEMINI_URL + "?key=" + key
    payload = {"contents": [{"parts": [{"text": prompt}]}]}
    data = json.dumps(payload).encode()
    req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=30) as r:
        res = json.loads(r.read())
    return res["candidates"][0]["content"]["parts"][0]["text"]
