Tài liệu tham khảo đầy đủ về API cho mô hình ngôn ngữ lớn Grok 4.7 của xAI.
Các biến thể của mô hình
API này hỗ trợ một biến thể của mô hình:
Bảng so sánh nhanh
Grok 4.7
Điểm cuối
POST /api/v1/chat/completions
Tham số yêu cầu
Bắt buộc
Các loại nội dung thông điệp được hỗ trợ
Không bắt buộc
Ví dụ về yêu cầu
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'text/event-stream',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'grok-4.7',
messages: [
{
role: 'user',
content: 'Explain how a browser caches an image in two sentences.'
}
],
stream: true,
max_tokens: 2048,
reasoning_effort: 'medium'
})
});
Định dạng phản hồi
Grok 4.7 trả về phản hồi hoàn thành tương thích với OpenAI. Khi đặt stream: true, phản hồi được gửi dưới dạng sự kiện từ máy chủ (SSE). Khi đặt stream: false hoặc bỏ qua stream, phản hồi là một đối tượng JSON duy nhất.
Phản hồi thành công
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{"reasoning_content":"Explain cache reuse."},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{"content":"The browser saves a copy of the image. It reuses the copy while it is fresh."},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.7","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
Phản hồi lỗi
data: {"error":{"message":"API requests too frequent, exceeding rate limit","type":"rate_limit_error","code":"1302","param":null}}
Khuyến nghị sử dụng
- Dùng thông điệp có cấu trúc để gửi lịch sử hội thoại qua
messages[].
- Đính kèm ảnh trong nội dung thông điệp bằng cách thêm khối
image_url vào messages[].content.
- Xử lý sự kiện SSE bằng cách nối lần lượt nội dung của
choices[0].delta.content và coi data: [DONE] là dấu hiệu kết thúc luồng.