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: 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ó lỗi sau khi đã nhận một phần, hãy giữ phần đã nhận và đừng tự gửi lại để tránh trùng nội dung.
- Header x-routing-request-id trong response là mã request của bạn; gửi kèm mã này khi cần hỗ trợ.
Request mẫu
POST /v1/chat/completions
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
}'