/{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>
99 lines
8.0 KiB
Bash
Executable File
99 lines
8.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Local smoke for the native-Ollama dialect. Two instances: one at the real
|
|
# forge, one at a dead upstream (the fallback / 503 paths). Uses the exact
|
|
# client Home Assistant pins (ollama==0.6.2) for the calls HA makes, and curl
|
|
# for raw frame shapes. Never touches Ollama on forge.
|
|
set -uo pipefail
|
|
ROOT="$(cd "$(dirname "$0")/.." && pwd)"; PY="$ROOT/.venv/bin/python"
|
|
GOOD=http://127.0.0.1:18080; BAD=http://127.0.0.1:18081
|
|
|
|
start() { FORGEWAY_NAME=forgeway FORGEWAY_SITE="$1" FORGEWAY_UPSTREAMS="$3" FORGEWAY_DISCOVERY_TIMEOUT=3 \
|
|
"$PY" -m uvicorn app.main:app --host 127.0.0.1 --port "$2" --no-access-log >"/tmp/forgeway-$1.log" 2>&1 & echo $!; }
|
|
cd "$ROOT"
|
|
P1=$(start ollama-good 18080 "http://forge.dmz.falkvinge.net:11434"); P2=$(start ollama-bad 18081 "http://127.0.0.1:1")
|
|
trap 'kill $P1 $P2 2>/dev/null' EXIT; sleep 4
|
|
|
|
hdr() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; }
|
|
CHAT='{"model":"gpt-oss:120b","messages":[{"role":"user","content":"Reply with exactly: PONG"}],"stream":false,"think":false,"keep_alive":"5m","options":{"num_ctx":8192}}'
|
|
|
|
hdr "1 HA-pinned client (ollama==0.6.2): list() + chat(stream=True) exactly as HA calls them, forge up"
|
|
"$PY" - <<'PY'
|
|
import asyncio, ollama
|
|
async def main():
|
|
c = ollama.AsyncClient(host="http://127.0.0.1:18080/local-optimistic")
|
|
print(" list():", [m.model for m in (await c.list()).models])
|
|
content = thinking = ""; done = None; n = 0
|
|
async for part in await c.chat(model="gpt-oss:120b", messages=[{"role":"user","content":"Reply with exactly: PONG"}],
|
|
stream=True, keep_alive=-1, options={"num_ctx": 8192}, think=False):
|
|
n += 1; content += part.message.content or ""; thinking += part.message.thinking or ""
|
|
if part.done: done = part.done_reason
|
|
print(f" chat(stream=True): chunks={n} content={content!r} thinking_chars={len(thinking)} done_reason={done}")
|
|
asyncio.run(main())
|
|
PY
|
|
|
|
hdr "2 raw shapes, forge up: /api/chat non-stream, /api/tags, /api/version, /api/ps, /api/show"
|
|
curl -s -m 120 -D /tmp/h -X POST $GOOD/local-strict/api/chat -H 'Content-Type: application/json' -d "$CHAT" | python3 -c "
|
|
import sys,json; d=json.load(sys.stdin); m=d['message']
|
|
print(' chat:', 'content=%r'%m.get('content'), 'thinking=%s' % ('yes' if m.get('thinking') else 'no'), 'done=%s'%d['done'], 'done_reason=%s'%d.get('done_reason'), 'keys_ok=%s' % all(k in d for k in ('model','created_at','message','done','done_reason','total_duration','eval_count')))"
|
|
echo " backend: $(grep -i x-forgeway-backend /tmp/h | tr -d '\r')"
|
|
echo " tags: $(curl -s -m 10 $GOOD/local-strict/api/tags | python3 -c 'import sys,json; d=json.load(sys.stdin); print([ (m["model"], m.get("details",{}).get("family")) for m in d["models"]])')"
|
|
echo " version: $(curl -s -m 10 $GOOD/local-strict/api/version)"
|
|
echo " ps: $(curl -s -m 10 -o /dev/null -w '%{http_code}' $GOOD/local-strict/api/ps) show: $(curl -s -m 10 -o /dev/null -w '%{http_code}' -X POST $GOOD/local-strict/api/show -d '{"model":"x"}') pull(400 expected): $(curl -s -m 10 -o /dev/null -w '%{http_code}' -X POST $GOOD/local-strict/api/pull -d '{"model":"llama3"}')"
|
|
|
|
hdr "3 raw streaming through forge: framing = NDJSON, last frame done=true"
|
|
curl -s -N -m 120 -D /tmp/h -X POST $GOOD/local-optimistic/api/chat -H 'Content-Type: application/json' -d "${CHAT/\"stream\":false/\"stream\":true}" > /tmp/s.ndjson
|
|
echo " content-type: $(grep -i '^content-type' /tmp/h | tr -d '\r' | awk '{print $2}') frames=$(wc -l < /tmp/s.ndjson) last=$(tail -1 /tmp/s.ndjson | python3 -c 'import sys,json; d=json.load(sys.stdin); print("done=%s reason=%s"%(d["done"],d.get("done_reason")))')"
|
|
|
|
hdr "4 generate through forge (non-stream)"
|
|
curl -s -m 120 -X POST $GOOD/local-strict/api/generate -H 'Content-Type: application/json' -d '{"model":"gpt-oss:120b","prompt":"Reply with exactly: PONG","stream":false,"think":false}' | python3 -c "import sys,json; d=json.load(sys.stdin); print(' response=%r done=%s reason=%s' % (d.get('response'), d.get('done'), d.get('done_reason')))"
|
|
|
|
hdr "5 DEAD UPSTREAM, strict: /api/chat must be a clean Ollama-shaped 503"
|
|
echo " HTTP $(curl -s -m 30 -o /tmp/b -w '%{http_code}' -X POST $BAD/local-strict/api/chat -H 'Content-Type: application/json' -d "$CHAT") body=$(cut -c1-150 /tmp/b)"
|
|
echo " tags still 200 (listing, not liveness): $(curl -s -m 10 -o /dev/null -w '%{http_code}' $BAD/local-strict/api/tags)"
|
|
|
|
hdr "6 DEAD UPSTREAM, optimistic: cloud fallback translated to Ollama shape (non-stream)"
|
|
curl -s -m 120 -D /tmp/h -X POST $BAD/local-optimistic/api/chat -H 'Content-Type: application/json' -d "$CHAT" | python3 -c "
|
|
import sys,json; d=json.load(sys.stdin); m=d['message']
|
|
print(' content=%r done=%s done_reason=%s eval_count=%s created_at=%s' % (m.get('content'), d['done'], d.get('done_reason'), d.get('eval_count'), d['created_at'][:19]))"
|
|
echo " backend: $(grep -i x-forgeway-backend /tmp/h | tr -d '\r')"
|
|
|
|
hdr "7 DEAD UPSTREAM, optimistic: cloud fallback STREAMED as NDJSON, read with the HA-pinned client"
|
|
"$PY" - <<'PY'
|
|
import asyncio, ollama
|
|
async def main():
|
|
c = ollama.AsyncClient(host="http://127.0.0.1:18081/local-optimistic")
|
|
content = ""; done = None; n = 0
|
|
async for part in await c.chat(model="gpt-oss:120b", messages=[{"role":"user","content":"Reply with exactly: PONG"}], stream=True, think=False):
|
|
n += 1; content += part.message.content or ""
|
|
if part.done: done = part.done_reason
|
|
print(f" chunks={n} content={content!r} done_reason={done}")
|
|
asyncio.run(main())
|
|
PY
|
|
|
|
hdr "8 DEAD UPSTREAM, optimistic: tool call round-trip through the cloud (HA Assist-API shape)"
|
|
"$PY" - <<'PY'
|
|
import asyncio, ollama
|
|
async def main():
|
|
c = ollama.AsyncClient(host="http://127.0.0.1:18081/local-optimistic")
|
|
tools=[{"type":"function","function":{"name":"HassTurnOn","description":"Turn on a device","parameters":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}}}]
|
|
msgs=[{"role":"user","content":"Turn on the kitchen light. Use the tool."}]
|
|
calls=[]
|
|
async for part in await c.chat(model="gpt-oss:120b", messages=msgs, tools=tools, stream=True, think=False):
|
|
if part.message.tool_calls: calls += [(t.function.name, dict(t.function.arguments)) for t in part.message.tool_calls]
|
|
print(" tool_calls:", calls)
|
|
# second round: feed the tool result back (ids are synthesised by the router)
|
|
msgs += [{"role":"assistant","content":"","tool_calls":[{"function":{"name":"HassTurnOn","arguments":{"name":"kitchen light"}}}]},
|
|
{"role":"tool","content":'{"data":{"success":true},"error":null}'}]
|
|
r = await c.chat(model="gpt-oss:120b", messages=msgs, tools=tools, stream=False, think=False)
|
|
print(" after tool result:", repr((r.message.content or "")[:80]), "done_reason=", r.done_reason)
|
|
asyncio.run(main())
|
|
PY
|
|
|
|
hdr "9 DEAD UPSTREAM, optimistic: generate via cloud (stream), and format=json"
|
|
curl -s -N -m 120 -X POST $BAD/local-optimistic/api/generate -H 'Content-Type: application/json' -d '{"model":"gpt-oss:120b","prompt":"Reply with exactly: PONG","stream":true}' > /tmp/g.ndjson
|
|
echo " generate stream frames=$(wc -l < /tmp/g.ndjson) text=$(python3 -c 'import json; print(repr("".join(json.loads(l).get("response","") for l in open("/tmp/g.ndjson"))))') last=$(tail -1 /tmp/g.ndjson | cut -c1-90)"
|
|
echo " format=json: $(curl -s -m 120 -X POST $BAD/local-optimistic/api/chat -H 'Content-Type: application/json' -d '{"model":"x","messages":[{"role":"user","content":"Return JSON with key a = 1"}],"stream":false,"format":"json"}' | python3 -c 'import sys,json; print(repr(json.load(sys.stdin)["message"]["content"][:60]))')"
|
|
|
|
hdr "10 OpenAI dialect untouched (regression): /v1/models + strict chat"
|
|
echo " models: $(curl -s -m 10 -o /dev/null -w '%{http_code}' $GOOD/local-strict/v1/models) chat: $(curl -s -m 120 -o /tmp/b -w '%{http_code}' -X POST $GOOD/local-strict/v1/chat/completions -H 'Content-Type: application/json' -d '{"messages":[{"role":"user","content":"Reply with exactly: PONG"}],"max_tokens":300}') $(python3 -c 'import json; print(repr(json.load(open("/tmp/b"))["choices"][0]["message"]["content"][:20]))')"
|