ChatGLM2-6B API Serving
Use this route for the repo's HTTP services, curl/OpenAI client integration,
SSE streaming, endpoint contracts, or service troubleshooting. Use
chat-and-demos for local UI/CLI generation and
ptuning when the model is a prefix/full fine-tuned
checkpoint.
Choose the endpoint
- Simple API: a FastAPI POST
/ accepting prompt, optional history,
max_length, top_p, and temperature. It returns response, updated
history, status, and a timestamp.
- OpenAI-compatible API:
GET /v1/models plus
POST /v1/chat/completions. Use this for clients that already speak the
Chat Completions protocol. The repository supports normal JSON responses and
Server-Sent Event streaming.
Read api-contracts.md before writing a client.
Read deployment.md before starting Uvicorn; the
service loads model weights once, keeps global model/tokenizer state, and
binds a listener. Run the bundled validators first:
python sub-skills/api-serving/scripts/validate_api_payload.py --json '{"prompt":"你好","history":[]}'
python sub-skills/api-serving/scripts/validate_openai_messages.py --json '{"model":"chatglm2-6b","messages":[{"role":"user","content":"你好"}]}'
Serve safely
- Select a complete model directory or Hub id and a backend; the repo's
launch path calls
.cuda() and assumes model weights fit on one or more
GPUs.
- Install
fastapi, uvicorn, and sse-starlette for the OpenAI streaming
route in addition to the model runtime.
- Bind to localhost while testing. Use one worker unless you deliberately
duplicate model memory and have an external process policy.
- Validate request roles/history before sending traffic. For streaming,
parse JSON chunks until the final
finish_reason chunk and [DONE].
- Treat CORS and
allow_origins=["*"] as development defaults, not a
production security policy. Add authentication, origin restrictions, rate
limits, and timeouts outside this legacy sample.
The source route introspection is safe and confirms /, /v1/models, and
/v1/chat/completions; it does not prove that a model can load. Use
troubleshooting.md for model, port, role,
SSE, and memory failures.
1---2name: api-serving3description: "Guides ChatGLM2-6B FastAPI and OpenAI-compatible serving, request4 validation, conversation-history mapping, streaming SSE, and safe local5 deployment."6disable-model-invocation: true7metadata:8 source: https://skillmd.com/skills/vectorspacelab/api-serving9 disco-role: operating10license: NOASSERTION11---1213# ChatGLM2-6B API Serving1415Use this route for the repo's HTTP services, curl/OpenAI client integration,16SSE streaming, endpoint contracts, or service troubleshooting. Use17[`chat-and-demos`](../chat-and-demos/SKILL.md) for local UI/CLI generation and18[`ptuning`](../ptuning/SKILL.md) when the model is a prefix/full fine-tuned19checkpoint.2021## Choose the endpoint2223- **Simple API:** a FastAPI POST `/` accepting `prompt`, optional `history`,24 `max_length`, `top_p`, and `temperature`. It returns `response`, updated25 `history`, `status`, and a timestamp.26- **OpenAI-compatible API:** `GET /v1/models` plus27 `POST /v1/chat/completions`. Use this for clients that already speak the28 Chat Completions protocol. The repository supports normal JSON responses and29 Server-Sent Event streaming.3031Read [`api-contracts.md`](references/api-contracts.md) before writing a client.32Read [`deployment.md`](references/deployment.md) before starting Uvicorn; the33service loads model weights once, keeps global `model`/`tokenizer` state, and34binds a listener. Run the bundled validators first:3536```text37python sub-skills/api-serving/scripts/validate_api_payload.py --json '{"prompt":"你好","history":[]}'38python sub-skills/api-serving/scripts/validate_openai_messages.py --json '{"model":"chatglm2-6b","messages":[{"role":"user","content":"你好"}]}'39```4041## Serve safely42431. Select a complete model directory or Hub id and a backend; the repo's44 launch path calls `.cuda()` and assumes model weights fit on one or more45 GPUs.462. Install `fastapi`, `uvicorn`, and `sse-starlette` for the OpenAI streaming47 route in addition to the model runtime.483. Bind to localhost while testing. Use one worker unless you deliberately49 duplicate model memory and have an external process policy.504. Validate request roles/history before sending traffic. For streaming,51 parse JSON chunks until the final `finish_reason` chunk and `[DONE]`.525. Treat CORS and `allow_origins=["*"]` as development defaults, not a53 production security policy. Add authentication, origin restrictions, rate54 limits, and timeouts outside this legacy sample.5556The source route introspection is safe and confirms `/`, `/v1/models`, and57`/v1/chat/completions`; it does not prove that a model can load. Use58[`troubleshooting.md`](references/troubleshooting.md) for model, port, role,59SSE, and memory failures.