Skip to content

feat: OpenAI-compatible API server example (Network#run)#137

Merged
bararchy merged 2 commits into
masterfrom
feat/openai-server
Jun 25, 2026
Merged

feat: OpenAI-compatible API server example (Network#run)#137
bararchy merged 2 commits into
masterfrom
feat/openai-server

Conversation

@bararchy

Copy link
Copy Markdown
Member

What

examples/openai_server.cr — a local HTTP server that exposes a loaded SHAInet model over a subset of the OpenAI REST API, so existing OpenAI clients (the openai Python package, curl, LangChain, …) can point at it as a drop-in endpoint. Built entirely on Network#run.

Endpoints

  • POST /v1/chat/completionsstreaming (SSE) and non-streaming. OpenAI-shaped JSON: choices[].message/delta, finish_reason, and usage (prompt/completion/total tokens).
  • GET /v1/models — lists the loaded model.
  • GET /health — liveness probe.

Reuse / behavior

  • Loads via HFLoader.load + BPETokenizer.from_hf; same quantization/offload env vars as llama_chat (SHAINET_FP32 / SHAINET_Q4 / SHAINET_MOE_OFFLOAD).
  • Chat-template detection: ChatML → LLaMA 3 → plain fallback (multi-message: system/user/assistant).
  • Honors temperature, max_tokens, stop (string or array), seed; maps frequency_penalty/presence_penalty onto SHAInet's repetition penalty (top_p is approximated by a fixed top_k, documented inline).
  • Generation is mutex-serialized — the Network KV cache is shared mutable state, so requests queue rather than corrupt each other.

Security

Binds 127.0.0.1 with no auth by default. Set SHAINET_API_KEY to require Authorization: Bearer <key>; pass an explicit host (e.g. 0.0.0.0) only deliberately.

Verified

Smoke-tested against Qwen3-0.6B:

  • /health and /v1/models return correct JSON.
  • Non-stream chat → valid OpenAI JSON with usage.
  • stream:true → role delta, per-token content deltas, finish chunk, then data: [DONE].
  • Auth: 401 without/with wrong bearer, 200 with the right key.
  • Builds clean (stub + CUDA no-codegen), crystal tool format clean, ameba 0.

Usage:

crystal run examples/openai_server.cr -Denable_cuda -- /path/to/model-dir 8080

bararchy added 2 commits June 25, 2026 16:12
examples/openai_server.cr serves a loaded SHAInet model over a subset of the
OpenAI REST API so existing OpenAI clients (openai Python pkg, curl, LangChain)
work as a drop-in local endpoint:

- POST /v1/chat/completions — streaming (SSE) and non-streaming, OpenAI JSON
  with usage (prompt/completion/total tokens) and finish_reason.
- GET  /v1/models — lists the loaded model.
- GET  /health — liveness probe.

Built entirely on Network#run + BPETokenizer + Sampler, reusing llama_chat's
load path and chat-template detection (ChatML / LLaMA3 / plain). Honors
temperature, max_tokens, stop (string or array), seed, and maps
frequency/presence penalty onto the repetition penalty. Generation is
mutex-serialized because the KV cache is shared mutable state.

Security: binds 127.0.0.1 with no auth by default; set SHAINET_API_KEY to
require an Authorization: Bearer header. Same quantization/offload env vars as
llama_chat (SHAINET_FP32 / SHAINET_Q4 / SHAINET_MOE_OFFLOAD).
Some OpenAI clients are configured with a base URL that already includes /v1
(so they request /v1/models), others without it (requesting /models directly,
which 404'd). Strip an optional leading /v1 before routing so both forms hit
the same handlers.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an example OpenAI-compatible HTTP API server that exposes a locally loaded SHAInet model via a small subset of the OpenAI REST API, making it easy to point existing OpenAI client tooling at a local endpoint.

Changes:

  • Introduces examples/openai_server.cr, implementing /v1/chat/completions (streaming SSE + non-stream), /v1/models, and /health.
  • Documents the new example server and its default local/no-auth security posture in README.md.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
README.md Adds a README entry describing the OpenAI-compatible example server and how to run/secure it.
examples/openai_server.cr New example: OpenAI-shaped chat completions API (streaming + non-streaming) backed by Network#run, with optional bearer auth.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/openai_server.cr
Comment thread examples/openai_server.cr
@bararchy
bararchy merged commit cd2afde into master Jun 25, 2026
6 checks passed
@bararchy
bararchy deleted the feat/openai-server branch June 25, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants