#!/usr/bin/env python3 import urllib.request, json, os, time def call_ollama_real(model_short, system_prompt, user_prompt): key = os.environ.get("OLLAMA_KEY", "") host = "https://ollama.com/v1" payload = json.dumps({ "model": model_short, "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt} ], "temperature": 0.3, "max_tokens": 2048 }).encode() req = urllib.request.Request( f"{host}/chat/completions", data=payload, headers={ "Content-Type": "application/json", "Authorization": f"Bearer {key}" if key else "Bearer", "User-Agent": "Mozilla/5.0" }, method="POST" ) start = time.time() try: with urllib.request.urlopen(req, timeout=120) as resp: data = json.loads(resp.read().decode()) text = data.get("choices", [{}])[0].get("message", {}).get("content", "") usage = data.get("usage", {}) elapsed = int((time.time() - start) * 1000) print(f"Status: {resp.status}") print(f"Latency: {elapsed}ms") print(f"Tokens: prompt={usage.get('prompt_tokens')}, completion={usage.get('completion_tokens')}") return text except urllib.error.HTTPError as e: body = e.read().decode()[:200] print(f"HTTP Error: {e.code} {e.reason}") print(f"Body: {body}") return "" except Exception as e: print(f"Error: {e}") return "" if __name__ == "__main__": print("=== Test real Ollama API ===") text = call_ollama_real( "kimi-k2.6", "You are a code reviewer. Find bugs.", "Review: def f(x): return x+1" ) print(f"\nResponse (first 300 chars):\n{text[:300]}") print(f"\nTotal length: {len(text)} chars") print(f"Keyword 'naming' in response: {'naming' in text.lower()}") print(f"Keyword 'return' in response: {'return' in text.lower()}")