import json
import urllib.request

LOCAL_URL = "http://127.0.0.1:8080/v1/chat/completions"

def query_local(system, user):
    payload = {
        "model": "local",
        "temperature": 0.0,
        "messages": [
            {"role": "system", "content": system},
            {"role": "user", "content": user}
        ]
    }
    data = json.dumps(payload).encode()
    req = urllib.request.Request(LOCAL_URL, data=data, headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=180) as r:
        res = json.loads(r.read())
        return res['choices'][0]['message']['content']

