/{mode}/api/{tags,chat,generate,version,show,ps} beside the OpenAI surface.
forge path is a raw proxy (forge is Ollama); the cloud fallback is translated
both ways — NDJSON frames byte-compatible with Ollama's, tool calls round-trip
with synthesised ids, format mapped, think→reasoning_effort (forced to none
with tools: OpenAI rejects the combination on gpt-5.4-mini). Tags never wait
on a model load (HA's list() has a 5 s timeout). local-strict still 503s in
Ollama's error shape; the OpenAI dialect is untouched.
Home Assistant (Lounge, 2026.9.2) configured through its REST config flow and
proven with a real conversation. HA's VLAN cannot reach the k8s ingress, so
it uses forgeway.dmz.lounge.falkvinge.net through the entrance HAProxy
(RFC1918-fenced vhost); the .services. alias needs a pfSense rule from Rick.
22/22 in-cluster checks on both sites. scripts/ha_setup_ollama.py is
idempotent and ends with a real answer; local-smoke-ollama.sh uses the exact
client HA pins.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
59 lines
2.9 KiB
Python
59 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Probe gpt-5.4-mini's reasoning_effort / stream_options / tool-call contract.
|
|
|
|
Needed to translate Ollama's `think` flag and streaming shape onto the cloud
|
|
fallback. Reads the key from the environment and never prints it.
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
KEY = os.environ.get("OPENAI_API_KEY", "")
|
|
MODEL = os.environ.get("FORGEWAY_CLOUD_MODEL", "gpt-5.4-mini")
|
|
if not KEY:
|
|
sys.exit("OPENAI_API_KEY not present in the environment")
|
|
|
|
BASE = {"model": MODEL, "messages": [{"role": "user", "content": "say PONG"}], "max_completion_tokens": 64}
|
|
CASES = {
|
|
"reasoning_effort=none": {"reasoning_effort": "none"},
|
|
"reasoning_effort=low": {"reasoning_effort": "low"},
|
|
"reasoning_effort=medium": {"reasoning_effort": "medium"},
|
|
"reasoning_effort=high": {"reasoning_effort": "high"},
|
|
"stream+include_usage": {"stream": True, "stream_options": {"include_usage": True}},
|
|
"response_format=json_object": {"response_format": {"type": "json_object"},
|
|
"messages": [{"role": "user", "content": "Return JSON {\"a\":1}"}]},
|
|
"tools": {"tools": [{"type": "function", "function": {"name": "get_weather",
|
|
"description": "Weather for a city", "parameters": {"type": "object",
|
|
"properties": {"city": {"type": "string"}}, "required": ["city"]}}}],
|
|
"messages": [{"role": "user", "content": "What is the weather in Berlin? Use the tool."}]},
|
|
}
|
|
|
|
for label, extra in CASES.items():
|
|
body = {**BASE, **extra}
|
|
req = urllib.request.Request(
|
|
"https://api.openai.com/v1/chat/completions",
|
|
data=json.dumps(body).encode(),
|
|
headers={"Content-Type": "application/json", "Authorization": f"Bearer {KEY}"},
|
|
method="POST",
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=90) as resp:
|
|
raw = resp.read().decode()
|
|
if body.get("stream"):
|
|
lines = [l for l in raw.splitlines() if l.startswith("data: ")]
|
|
last = json.loads(lines[-2][6:]) if len(lines) >= 2 and lines[-1].strip() == "data: [DONE]" else json.loads(lines[-1][6:])
|
|
print(f"{label:30s} -> {resp.status} sse_lines={len(lines)} last_has_usage={'usage' in last and last['usage'] is not None} last_choices={len(last.get('choices', []))}")
|
|
else:
|
|
d = json.loads(raw)
|
|
msg = d["choices"][0]["message"]
|
|
print(f"{label:30s} -> {resp.status} finish={d['choices'][0]['finish_reason']} content={msg.get('content')!r:.40} tool_calls={json.dumps(msg.get('tool_calls'))[:120] if msg.get('tool_calls') else None}")
|
|
except urllib.error.HTTPError as exc:
|
|
detail = exc.read().decode("utf-8", "replace")
|
|
try:
|
|
detail = json.loads(detail)["error"]["message"]
|
|
except Exception:
|
|
detail = detail[:160]
|
|
print(f"{label:30s} -> {exc.code} {detail}")
|