Grok 对话补全(全模型,完整参数)
curl --request POST \
--url https://direct.evolink.ai/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "grok-4.5",
"messages": [
{
"role": "system",
"content": "你是一个简洁的助手。"
},
{
"role": "user",
"content": "用一句话解释什么是 Prompt 缓存。"
}
],
"stream": false,
"max_tokens": 1024,
"temperature": 0.7,
"top_p": 0.95,
"tools": [
{
"type": "function",
"function": {}
}
]
}
'import requests
url = "https://direct.evolink.ai/v1/chat/completions"
payload = {
"model": "grok-4.5",
"messages": [
{
"role": "system",
"content": "你是一个简洁的助手。"
},
{
"role": "user",
"content": "用一句话解释什么是 Prompt 缓存。"
}
],
"stream": False,
"max_tokens": 1024,
"temperature": 0.7,
"top_p": 0.95,
"tools": [
{
"type": "function",
"function": {}
}
]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'grok-4.5',
messages: [
{role: 'system', content: '你是一个简洁的助手。'},
{role: 'user', content: '用一句话解释什么是 Prompt 缓存。'}
],
stream: false,
max_tokens: 1024,
temperature: 0.7,
top_p: 0.95,
tools: [{type: 'function', function: {}}]
})
};
fetch('https://direct.evolink.ai/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://direct.evolink.ai/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'grok-4.5',
'messages' => [
[
'role' => 'system',
'content' => '你是一个简洁的助手。'
],
[
'role' => 'user',
'content' => '用一句话解释什么是 Prompt 缓存。'
]
],
'stream' => false,
'max_tokens' => 1024,
'temperature' => 0.7,
'top_p' => 0.95,
'tools' => [
[
'type' => 'function',
'function' => [
]
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://direct.evolink.ai/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"grok-4.5\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是一个简洁的助手。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用一句话解释什么是 Prompt 缓存。\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 1024,\n \"temperature\": 0.7,\n \"top_p\": 0.95,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {}\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://direct.evolink.ai/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"grok-4.5\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是一个简洁的助手。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用一句话解释什么是 Prompt 缓存。\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 1024,\n \"temperature\": 0.7,\n \"top_p\": 0.95,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {}\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://direct.evolink.ai/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"grok-4.5\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是一个简洁的助手。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用一句话解释什么是 Prompt 缓存。\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 1024,\n \"temperature\": 0.7,\n \"top_p\": 0.95,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {}\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"id": "chatcmpl-20260812164515123456789AbCdEfGh",
"model": "grok-4.5",
"object": "chat.completion",
"created": 1786538000,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Prompt 缓存会复用已处理过的输入前缀,重复上下文按更低价格计费。",
"tool_calls": [
{}
]
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 504,
"completion_tokens": 2,
"total_tokens": 526,
"prompt_tokens_details": {
"cached_tokens": 0
}
}
}{
"error": {
"code": 400,
"message": "Invalid request parameters",
"type": "invalid_request_error"
}
}{
"error": {
"code": 401,
"message": "Invalid or expired token",
"type": "authentication_error"
}
}{
"error": {
"code": 402,
"message": "Insufficient quota",
"type": "insufficient_quota_error",
"fallback_suggestion": "https://evolink.ai/dashboard/billing"
}
}{
"error": {
"code": 429,
"message": "Rate limit exceeded",
"type": "rate_limit_error",
"fallback_suggestion": "retry after 60 seconds"
}
}{
"error": {
"code": 500,
"message": "Internal server error",
"type": "internal_server_error",
"fallback_suggestion": "try again later"
}
}{
"error": {
"code": 503,
"message": "Service temporarily unavailable",
"type": "service_unavailable_error",
"fallback_suggestion": "retry after 30 seconds"
}
}Chat Completions 接口
Grok 全模型接口 - Chat Completions 完整参数
- xAI Grok 系列文本模型的 OpenAI 兼容 Chat Completions 接口,通过
model选择具体模型(全部可选值见model参数的对照表) grok-4.5:50 万 token 上下文窗口;Prompt 达到 20 万 token 起,全部 token 按 2 倍价格计费- Prompt 缓存自动生效:命中缓存的输入 token 按更低的缓存价计费
- 支持同步与流式(SSE)两种模式
- 支持普通
function工具调用;xAI 服务端工具仅在 Responses 接口提供
POST
/
v1
/
chat
/
completions
Grok 对话补全(全模型,完整参数)
curl --request POST \
--url https://direct.evolink.ai/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "grok-4.5",
"messages": [
{
"role": "system",
"content": "你是一个简洁的助手。"
},
{
"role": "user",
"content": "用一句话解释什么是 Prompt 缓存。"
}
],
"stream": false,
"max_tokens": 1024,
"temperature": 0.7,
"top_p": 0.95,
"tools": [
{
"type": "function",
"function": {}
}
]
}
'import requests
url = "https://direct.evolink.ai/v1/chat/completions"
payload = {
"model": "grok-4.5",
"messages": [
{
"role": "system",
"content": "你是一个简洁的助手。"
},
{
"role": "user",
"content": "用一句话解释什么是 Prompt 缓存。"
}
],
"stream": False,
"max_tokens": 1024,
"temperature": 0.7,
"top_p": 0.95,
"tools": [
{
"type": "function",
"function": {}
}
]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'grok-4.5',
messages: [
{role: 'system', content: '你是一个简洁的助手。'},
{role: 'user', content: '用一句话解释什么是 Prompt 缓存。'}
],
stream: false,
max_tokens: 1024,
temperature: 0.7,
top_p: 0.95,
tools: [{type: 'function', function: {}}]
})
};
fetch('https://direct.evolink.ai/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://direct.evolink.ai/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'grok-4.5',
'messages' => [
[
'role' => 'system',
'content' => '你是一个简洁的助手。'
],
[
'role' => 'user',
'content' => '用一句话解释什么是 Prompt 缓存。'
]
],
'stream' => false,
'max_tokens' => 1024,
'temperature' => 0.7,
'top_p' => 0.95,
'tools' => [
[
'type' => 'function',
'function' => [
]
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://direct.evolink.ai/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"grok-4.5\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是一个简洁的助手。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用一句话解释什么是 Prompt 缓存。\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 1024,\n \"temperature\": 0.7,\n \"top_p\": 0.95,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {}\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://direct.evolink.ai/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"grok-4.5\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是一个简洁的助手。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用一句话解释什么是 Prompt 缓存。\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 1024,\n \"temperature\": 0.7,\n \"top_p\": 0.95,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {}\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://direct.evolink.ai/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"grok-4.5\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是一个简洁的助手。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用一句话解释什么是 Prompt 缓存。\"\n }\n ],\n \"stream\": false,\n \"max_tokens\": 1024,\n \"temperature\": 0.7,\n \"top_p\": 0.95,\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {}\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"id": "chatcmpl-20260812164515123456789AbCdEfGh",
"model": "grok-4.5",
"object": "chat.completion",
"created": 1786538000,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Prompt 缓存会复用已处理过的输入前缀,重复上下文按更低价格计费。",
"tool_calls": [
{}
]
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 504,
"completion_tokens": 2,
"total_tokens": 526,
"prompt_tokens_details": {
"cached_tokens": 0
}
}
}{
"error": {
"code": 400,
"message": "Invalid request parameters",
"type": "invalid_request_error"
}
}{
"error": {
"code": 401,
"message": "Invalid or expired token",
"type": "authentication_error"
}
}{
"error": {
"code": 402,
"message": "Insufficient quota",
"type": "insufficient_quota_error",
"fallback_suggestion": "https://evolink.ai/dashboard/billing"
}
}{
"error": {
"code": 429,
"message": "Rate limit exceeded",
"type": "rate_limit_error",
"fallback_suggestion": "retry after 60 seconds"
}
}{
"error": {
"code": 500,
"message": "Internal server error",
"type": "internal_server_error",
"fallback_suggestion": "try again later"
}
}{
"error": {
"code": 503,
"message": "Service temporarily unavailable",
"type": "service_unavailable_error",
"fallback_suggestion": "retry after 30 seconds"
}
}BaseURL 说明:默认 BaseURL 为
https://direct.evolink.ai,对文本模型支持更好,支持长连接;https://api.evolink.ai 是多模态主力地址,对文本模型作为备用地址使用。服务端工具(联网搜索、X 搜索、代码执行、附件搜索、文档集搜索)仅在 Responses 接口提供;Chat Completions 接口只支持普通
function 工具调用。授权
##所有接口均需 Bearer Token 认证##
获取 API Key:
访问 API Key 管理页面 获取你的 API Key
添加到请求头:
Authorization: Bearer YOUR_API_KEY
请求体
application/json
要调用的模型:
| 模型 ID | 定位 |
|---|---|
grok-4.5 | xAI 推理 + 工具调用模型,50 万 token 上下文窗口 |
可用选项:
grok-4.5 示例:
"grok-4.5"
对话消息列表,支持 system、user、assistant 角色。
Minimum array length:
1Show child attributes
Show child attributes
示例:
[
{ "role": "system", "content": "你是一个简洁的助手。" },
{
"role": "user",
"content": "用一句话解释什么是 Prompt 缓存。"
}
]
是否流式返回(SSE,chat.completion.chunk 事件)。默认 false。
示例:
false
生成的最大 token 数,原样透传给模型。
示例:
1024
采样温度(0-2),越大输出越随机。
示例:
0.7
核采样参数(0-1)。
示例:
0.95
普通 OpenAI function 工具定义(客户端函数调用,无按次费用)。xAI 服务端工具仅在 Responses 接口提供。
Show child attributes
Show child attributes
函数选择控制:"auto" / "none" / "required",或用对象指定某个函数。
可用选项:
auto, none, required 响应
对话生成成功(JSON 对象;stream=true 时为 chat.completion.chunk 的 SSE 事件流)
对话补全的唯一标识
示例:
"chatcmpl-20260812164515123456789AbCdEfGh"
实际使用的模型名称
示例:
"grok-4.5"
响应类型
可用选项:
chat.completion 示例:
"chat.completion"
创建时间戳
示例:
1786538000
对话补全选项列表
Show child attributes
Show child attributes
Token 用量统计。Prompt 达到 20 万 token 起,全部 token(输入、缓存输入、输出)按 2 倍价格计费。
Show child attributes
Show child attributes
⌘I