#!/usr/bin/env python3
"""P2Z-G Step 3: Direct OpenRouter test - Qwen non-thinking with Graphiti-style JSON prompt."""
import json, urllib.request
key = ""
with open("/etc/graphiti/graphiti.env") as f:
for line in f:
if line.startswith("OPENROUTER_API_KEY="):
key = line.split("=", 1)[1].strip()
break
# Typical Graphiti structured-output prompt (entity extraction style)
payload = {
"model": "qwen/qwen3.7-flash",
"messages": [
{"role": "system", "content": "You are a helpful assistant that extracts entities from text. Return ONLY valid JSON."},
{"role": "user", "content": 'Extract entities from: "Karlo nutzt Hermes auf einem Windows-PC und einem VPS." Return JSON: {"entities": [{"name": "...", "type": "person|tool|location"}]}'}
],
"temperature": 0,
"max_tokens": 4096,
"response_format": {"type": "json_object"},
"reasoning": {"effort": "none"}
}
req = urllib.request.Request(
"https://openrouter.ai/api/v1/chat/completions",
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"},
)
resp = urllib.request.urlopen(req, timeout=90)
data = json.loads(resp.read())
msg = data["choices"][0]["message"]
c = msg.get("content")
usage = data.get("usage", {})
print(f"HTTP 200 OK")
print(f"model: {data['model']}")
print(f"content NULL: {c is None}")
print(f"content: {repr(c[:200] if c else None)}")
print(f"reasoning present: {bool(msg.get('reasoning'))}")
print(f"prompt_tokens: {usage.get('prompt_tokens')}, completion_tokens: {usage.get('completion_tokens')}, cost: {usage.get('cost')}")
if c:
try:
j = json.loads(c)
print(f"VALID JSON: {j}")
except Exception as e:
print(f"JSON parse failed: {e}")