CoolFace
Apppublic

MKooi/Coding_Agents

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
llm_clients.py128 linesDownload Raw Back to root
1import requests2import json3 4# ============================================================5# CONFIG6# ============================================================7 8BASE_URL = "http://192.168.1.8:2805/v1/chat/completions"9 10# 🔥 Default model11MODEL = "qwen2.5-14b-instruct"12# MODEL = "DeepSeek-Coder-V2-Lite-Instruct"13 14TIMEOUT = 12015 16 17# ============================================================18# CORE LLM CALL19# ============================================================20 21def call_llm(system_prompt, user_prompt, max_tokens=1200, temperature=0, model=None):22 23    if model is None:24        model = MODEL25 26    # ------------------------------------------------------------27    # Force strict JSON behavior28    # ------------------------------------------------------------29    full_prompt = f"""30{system_prompt}31 32USER REQUEST:33{user_prompt}34 35CRITICAL INSTRUCTIONS:36- Return ONLY valid JSON37- Do NOT explain38- Do NOT add markdown39- Do NOT wrap with ```json40- Output must start with {{41"""42 43    payload = {44        "model": model,45        "temperature": temperature,46        "max_tokens": max_tokens,47        "messages": [48            {49                "role": "user",50                "content": full_prompt51            }52        ],53        "stop": ["```"]54    }55 56    try:57        response = requests.post(BASE_URL, json=payload, timeout=TIMEOUT)58 59        if response.status_code != 200:60            raise RuntimeError(f"HTTP {response.status_code}: {response.text}")61 62        data = response.json()63        content = data["choices"][0]["message"]["content"]64 65        # --------------------------------------------------------66        # Clean accidental markdown67        # --------------------------------------------------------68        content = content.strip()69 70        if content.startswith("```"):71            content = content.replace("```json", "")72            content = content.replace("```", "")73            content = content.strip()74 75        return content76 77    except Exception as e:78        print("🔥 LLM ERROR:", e)79        return None80 81 82# ============================================================83# SAFE JSON PARSER84# ============================================================85 86def call_llm_json(system_prompt, user_prompt, max_tokens=1200, temperature=0, model=None):87 88    raw = call_llm(system_prompt, user_prompt, max_tokens, temperature, model)89 90    if not raw:91        return None92 93    try:94        return json.loads(raw)95    except json.JSONDecodeError:96        print("⚠️ JSON PARSE FAILED")97        print("RAW OUTPUT:\n", raw)98        return None99 100 101# ============================================================102# SIMPLE RAW CALL (for benchmark / quick test)103# ============================================================104 105def call_model(model, messages, temperature=0, max_tokens=512):106    """107    Dùng cho test nhanh không ép JSON108    """109 110    payload = {111        "model": model,112        "messages": messages,113        "temperature": temperature,114        "max_tokens": max_tokens115    }116 117    try:118        response = requests.post(BASE_URL, json=payload, timeout=TIMEOUT)119 120        if response.status_code != 200:121            raise RuntimeError(f"HTTP {response.status_code}: {response.text}")122 123        data = response.json()124        return data["choices"][0]["message"]["content"]125 126    except Exception as e:127        print("🔥 RAW CALL ERROR:", e)128        return None