Files
engine/examples/server/anthropic_streaming.py
zeekayandHanzo Dev 42e34c2d41 refactor: name only Hanzo Engine in code, comments and docs
Every mention of another inference engine is rewritten to say what our code
does, or the math it computes. Reference values are "the reference value
from the published config" or "the recorded reference output", line
citations into another engine's source become the rule itself, comparisons
with other engines are dropped, and asides about other engines' bugs go.

Identifiers follow:
- CUDA: hanzo-paged-attn's `namespace vllm` is `pagedattn`, hanzo-engine's
  MoE helpers `moe_dtype` and `moe_detail`, the rotary kernels'
  `hanzo_rotary`; the shuffle/ldg macros are HANZO_*.
- GPTQ: the shuffled-layout path (`reconstruct_exllama`, `use_exllama`) is
  `reconstruct_shuffled` / `is_shuffled`, on the kernel and the FFI side.
- Tests: `*_matches_vllm` and friends name what they compare against (the
  reference fixture, the tile rule).
- MiniCPM-o's vision-LLM embedding is `vlm_embedding`; the rotary custom op
  is `rotary-embedding`.
- hanzo-bench's board scores ours against a "rival" whose name comes from
  the manifest: `rival_<tag>.json`, `--rival*` manifest flags, `rival t/s`
  columns; dossier.sh and spec_decode.sh take `--rival-bench`.
- install.ps1 installs this repository's hanzo-cli, as install.sh does,
  with HANZO_INSTALL_NCCL / HANZO_INSTALL_NO_NCCL.
- bench_http.py reads only the hanzo: metric families.

Kept, because they are what they are: LICENSE, LICENSE-APACHE, NOTICE and
tools/upstream-map.sh; each ported file's copyright/licence/provenance
header; Cargo.toml dependency names; data identifiers (Hugging Face repo
IDs, fixture file names and fixture metadata keys the tests read, on-disk
paths, executable names the GPU and engine guards match on a host); model
and format names.

LLM.md says the rule once, under "One name: Hanzo Engine".


Co-authored-by: Hanzo Dev <[email protected]>
2026-10-16 20:59:50 -07:00

68 lines
1.8 KiB
Python

"""
Streaming Anthropic Messages API example for the Hanzo Engine server.
Run the server:
hanzo-engine serve -p 1234 -m Qwen/Qwen3-4B
Then run:
python3 examples/server/anthropic_streaming.py
"""
import json
import os
import urllib.request
BASE_URL = os.environ.get("HANZO_BASE_URL", "http://localhost:1234")
API_KEY = os.environ.get("ANTHROPIC_API_KEY", "local")
def sse_events(response):
event = None
data = []
for raw_line in response:
line = raw_line.decode("utf-8").strip()
if not line:
if event and data:
yield event, json.loads("".join(data))
event = None
data = []
elif line.startswith("event:"):
event = line.removeprefix("event:").strip()
elif line.startswith("data:"):
data.append(line.removeprefix("data:").strip())
request = urllib.request.Request(
f"{BASE_URL}/v1/messages",
data=json.dumps(
{
"model": "default",
"max_tokens": 512,
"stream": True,
"messages": [
{
"role": "user",
"content": "Write a short haiku about local inference.",
}
],
}
).encode("utf-8"),
headers={
"content-type": "application/json",
"x-api-key": API_KEY,
"anthropic-version": "2023-06-01",
},
method="POST",
)
with urllib.request.urlopen(request) as response:
for event, payload in sse_events(response):
if event == "content_block_delta":
delta = payload["delta"]
if delta["type"] == "text_delta":
print(delta["text"], end="", flush=True)
elif delta["type"] == "thinking_delta":
print(delta["thinking"], end="", flush=True)
elif event == "message_stop":
print()