使用 xAI 的 Grok API 进行文本转文本生成(OpenAI 兼容格式)
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "I'm Grok, a helpful and maximally truthful AI built by xAI. I'm designed to answer questions, provide information, and tackle problems with a bit of wit and wisdom—think of me as inspired by the Hitchhiker's Guide to the Galaxy and JARVIS from Iron Man. Not omniscient, but I'll do my best. What's on your mind?",
"role": "assistant"
}
}
],
"created": 1770018847,
"id": "abc",
"model": "grok-4",
"object": "chat.completion",
"system_fingerprint": "fp_dd0aa291c6",
"usage": {
"completion_tokens": 196,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 124,
"rejected_prediction_tokens": 0
},
"prompt_tokens": 688,
"prompt_tokens_details": {
"audio_tokens": 0,
"cached_tokens": 679
},
"total_tokens": 884
}
}| 参数 | 类型 | 必填 | 默认值 | 范围 | 描述 |
|---|---|---|---|---|---|
model | string | 是 | - | - | 用于生成补全的模型 ID。Grok 提供多种具有不同功能、性能特点和价格点的模型。 |
messages | array | 是 | - | - | 包含当前对话的消息列表。根据您使用的模型,支持不同的消息类型(模态),如文本、图像和音频。 |
messages.role | string | 是 | - | developer, user, system | 消息作者的角色。 |
messages.name | string | 否 | - | - | 参与者的可选名称。为模型提供信息以区分相同角色的参与者。 |
messages.content | string / array | 是 | - | - | 开发者消息的内容。 |
messages.content.type | string | 是 | - | text, image_url | 内容部分的类型 |
messages.content.text | string | 否 | - | - | 文本内容 |
messages.content.image_url | object | 是(如果 type 为 image_url) | - | - | 图像 URL 内容 |
messages.content.image_url.url | string | 否 | - | - | 图像的 URL |
| 参数 | 类型 | 必填 | 默认值 | 范围 | 描述 |
|---|---|---|---|---|---|
max_tokens | integer / null | 否 | - | - | 补全可生成的 token 数量的上限,包括可见输出 token 和推理 token。 |
stream | boolean | 否 | false | true, false | 是否逐步流式返回响应。默认为 false。 |
| 错误码 | 错误名称 | 描述 |
|---|---|---|
| 401 | Unauthorized | API key 缺失或无效 |
| 403 | Forbidden | 您的 API key 没有权限访问此资源,或请求操作的余额不足 |
| 429 | Too Many Requests | 您已超出速率限制 |
| 500 | Internal server error | 发生内部服务器错误 |
| 503 | Content policy violation | 由于安全顾虑内容被阻止(实际状态码为 400) |