Chat Completions API
Contract request/response OpenAI-compatible của POST /v1/chat/completions.
Body bắt buộc có model và ít nhất một message. Role hợp lệ gồm system, user, assistant và tool; content phải là chuỗi không rỗng hoặc mảng content parts không rỗng.
temperature nếu có phải từ 0 đến 2 và max_tokens phải lớn hơn 0. stream=false trả JSON hoàn chỉnh; stream=true trả các data event SSE và kết thúc bằng [DONE].
Điểm cần nhớ
- Header: Authorization Bearer và Content-Type application/json.
- model: public model ID lấy từ GET /v1/models.
- messages: mảng message theo thứ tự hội thoại.
- Response thành công giữ cấu trúc choices, model và usage tương thích OpenAI.
- Với SSE, nối choices[].delta.content theo thứ tự và chỉ xem là hoàn tất sau data: [DONE].
- Nếu có terminal error sau khi đã nhận delta, giữ output một phần và không tự động retry để tránh trùng nội dung.
- Response header x-routing-request-id định danh request nội bộ.
Request mẫu
curl --no-buffer --request POST 'https://api.xpncore.com/v1/chat/completions' \
--max-time 35 \
--header "Accept: text/event-stream" \
--header "Authorization: Bearer $API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "MODEL_ID_FROM_GET_V1_MODELS",
"messages": [
{
"role": "user",
"content": "Hello from the gateway"
}
],
"temperature": 0.7,
"max_tokens": 512,
"stream": true
}'