Spaces:
Sleeping
Sleeping
Adarsh Kumar commited on
Delete README.md
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261---title: ZCode OpenAI Provideremoji: 🧰colorFrom: bluecolorTo: indigosdk: dockerapp_port: 7860pinned: false---
# ZCode OpenAI Provider
A small, production-shaped **OpenAI-compatible relay** for a Hugging Face Docker Space. It is designed for **ZCode** and other coding agents that use Chat Completions.
## What it supports
- `GET /v1/models` — live upstream catalog with a short cache- `POST /v1/chat/completions` — JSON and SSE streaming- OpenAI function tools and `tool_choice`- usage, reasoning and provider-specific response fields- OpenAI multimodal `content` arrays with `image_url`- clean OpenAI-shaped errors, body-size checks, concurrency control and rate limiting- optional server API-key protection- `/healthz`, `/readyz`, `/docs`, and a status page at `/`
The request JSON is forwarded without rebuilding it. This is important: ZCode's full tool definitions, stream options, tool results and image parts reach the upstream unchanged.
## Deploy to Hugging Face Spaces
### 1. Create the Space
1. Open <https://huggingface.co/new-space>.2. Choose a name such as `zcode-model-relay`.3. Select **Docker** as the SDK.4. Select **Public**, as requested.5. Create the Space.
### 2. Upload this folder as the Space repository root
The Space root must contain `README.md`, `Dockerfile`, `app.py`, and `requirements.txt`.
Using Git:
```bashgit clone https://huggingface.co/spaces/YOUR_USERNAME/zcode-model-relaycp -a /path/to/public-repo/hf-space/. zcode-model-relay/cd zcode-model-relaygit add .git commit -m "Deploy OpenAI-compatible ZCode relay"git push```
Hugging Face builds the image and exposes port `7860`. No GPU is needed because this container proxies a remote model API.
### 3. Optional Space variables
The defaults already target the tested upstream. In **Space → Settings → Variables and secrets**, you may configure:
| Name | Type | Default | Purpose ||---|---|---|---|| `UPSTREAM_BASE_URL` | Variable | `https://osaii.wyvernhub.net/api/v1` | OpenAI-compatible upstream ending in `/v1` || `UPSTREAM_API_KEY` | Secret | empty | Upstream Bearer key, if required later || `PUBLIC_ACCESS` | Variable | `true` | `true` accepts requests without server authentication || `PROVIDER_API_KEY` | Secret | empty | Used only when `PUBLIC_ACCESS=false` || `RATE_LIMIT_PER_MINUTE` | Variable | `12` | Per-client in-memory limit; `0` disables it || `MAX_CONCURRENT_REQUESTS` | Variable | `6` | Concurrent upstream requests || `MAX_BODY_MB` | Variable | `25` | Allows base64 image input while bounding request size || `ALLOWED_MODELS` | Variable | empty | Optional comma-separated model allowlist |
A restart applies variable changes.
## Connect it to ZCode
ZCode supports custom OpenAI-compatible providers. It appends `/chat/completions`, so the Base URL must end at `/v1`.
1. Open **ZCode → Settings → Model settings**.2. Click **Add provider → Create custom provider**.3. Configure:
```textName: HF Model RelayAPI format: Chat completions (/chat/completions)Base URL: https://YOUR_USERNAME-zcode-model-relay.hf.space/v1API key: public```
`public` is only a placeholder because this requested deployment has `PUBLIC_ACCESS=true`; the server ignores the Authorization value. If ZCode offers an **API key required** switch, turn it off.
4. Click **Add model** and enter this recommended coding model exactly:
```textosaii/faster-experimental```
5. Enable the provider and run **Test model**.
### Recommended models
Always check `https://YOUR_USERNAME-zcode-model-relay.hf.space/v1/models` because the upstream catalog can change.
| Model | Best use | Verified behavior ||---|---|---|| `osaii/faster-experimental` | **ZCode coding agent** | streaming, usage and structured function tool calls || `osaii/ultrafast-experimental` | fast chat | low-latency alias; catalog availability is dynamic || `poe/Gemma-4-31B` | **image understanding** | correctly classified independently generated red and blue test images || `poe/DeepSeek-V4.1-Flash-T` | reasoning chat | streaming chat; its current Poe route rejects ZCode-style tool payloads || `poe/GLM-5.3-Flash-T` | reasoning chat | chat works; its current Poe route rejects forced tool payloads |
For ZCode's agent loop, start with `osaii/faster-experimental`. The Gemma route understands images but is not the recommended tool-calling coding model.
### Optional manual ZCode configuration
ZCode can also store a custom provider in `~/.zcode/v2/config.json`. Back up the file first and merge rather than replacing existing providers:
```json{ "provider": { "hf-model-relay": { "name": "HF Model Relay", "kind": "openai-compatible", "source": "custom", "options": { "baseURL": "https://YOUR_USERNAME-zcode-model-relay.hf.space/v1", "apiKey": "public", "apiKeyRequired": false }, "models": { "osaii/faster-experimental": { "name": "OSAI Faster — Tool Calling", "limit": { "context": 131072, "output": 32768 } }, "poe/Gemma-4-31B": { "name": "Gemma 4 31B — Vision", "limit": { "context": 32768, "output": 8192 } } } } }}```
The ZCode UI is safer than hand-editing configuration because ZCode's schema can change between releases.
## API checks
Replace the base URL first:
```bashBASE=https://YOUR_USERNAME-zcode-model-relay.hf.space```
Health and models:
```bashcurl -sS "$BASE/healthz"curl -sS "$BASE/v1/models" | python -m json.tool```
Non-streaming chat:
```bashcurl -sS "$BASE/v1/chat/completions" \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer public' \ -d '{ "model": "osaii/faster-experimental", "stream": false, "messages": [{"role": "user", "content": "Reply with exactly OK"}] }' | python -m json.tool```
Streaming:
```bashcurl -N "$BASE/v1/chat/completions" \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer public' \ -d '{ "model": "osaii/faster-experimental", "stream": true, "stream_options": {"include_usage": true}, "messages": [{"role": "user", "content": "Write a two-line Python function"}] }'```
## Vision request shape
Image understanding uses standard OpenAI multimodal message parts. The relay preserves this array unchanged:
```json{ "model": "poe/Gemma-4-31B", "stream": false, "messages": [ { "role": "user", "content": [ {"type": "text", "text": "Describe this image."}, { "type": "image_url", "image_url": {"url": "data:image/png;base64,BASE64_DATA"} } ] } ]}```
## Run locally with Docker
```bashcp .env.example .envdocker compose up --build```
Then use:
```texthttp://localhost:7860/v1```
Without Compose:
```bashdocker build -t zcode-model-relay .docker run --rm -p 7860:7860 --env-file .env zcode-model-relay```
## Run locally without Docker
```bashpython -m venv .venv. .venv/bin/activate # Windows: .venv\Scripts\activatepip install -r requirements-dev.txtuvicorn app:app --host 0.0.0.0 --port 7860```
Tests:
```bashpytest -qpython smoke_test.py http://127.0.0.1:7860```
## Public-deployment warning
With `PUBLIC_ACCESS=true`, anyone who discovers the Space URL can use the upstream through your Space. The built-in per-client limit reduces accidental abuse but is not durable authentication, and Hugging Face replicas do not share its in-memory counters.
For a safer deployment later:
1. Set `PUBLIC_ACCESS=false` as a Space Variable.2. Set a long random `PROVIDER_API_KEY` as a Space Secret.3. Put that key into ZCode instead of `public`.
Also verify that the upstream's terms allow redistribution through a public relay. Free Hugging Face Spaces sleep when idle, so ZCode's first request after inactivity may need to wait for the Space to wake up.README.mdDeleted-261