Grok ↗ 是一个通用模型,可用于各种任务,包括生成和理解文本、代码和函数调用。
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok
在向 Grok ↗ 发出请求时,将您当前使用的 URL 中的 https://api.x.ai/v1
替换为 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok
。
在向 Grok 发出请求时,确保您具有以下内容:
- 您的 AI 网关账户 ID。
- 您的 AI 网关网关名称。
- 一个有效的 Grok API 令牌。
- 您要使用的 Grok 模型的名称。
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok/v1/chat/completions \ --header 'content-type: application/json' \ --header 'Authorization: Bearer {grok_api_token}' \ --data '{ "model": "grok-beta", "messages": [ { "role": "user", "content": "What is Cloudflare?" } ]}'
如果您使用 JavaScript 中的 OpenAI SDK,您可以这样设置您的端点:
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: "<api key>", baseURL: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",});
const completion = await openai.chat.completions.create({ model: "grok-beta", messages: [ { role: "system", content: "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", }, { role: "user", content: "What is the meaning of life, the universe, and everything?", }, ],});
console.log(completion.choices[0].message);
如果您使用 Python 中的 OpenAI SDK,您可以这样设置您的端点:
import osfrom openai import OpenAI
XAI_API_KEY = os.getenv("XAI_API_KEY")client = OpenAI( api_key=XAI_API_KEY, base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",)
completion = client.chat.completions.create( model="grok-beta", messages=[ {"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."}, {"role": "user", "content": "What is the meaning of life, the universe, and everything?"}, ],)
print(completion.choices[0].message)
如果您使用 JavaScript 中的 Anthropic SDK,您可以这样设置您的端点:
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({ apiKey: "<api key>", baseURL: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",});
const msg = await anthropic.messages.create({ model: "grok-beta", max_tokens: 128, system: "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", messages: [ { role: "user", content: "What is the meaning of life, the universe, and everything?", }, ],});
console.log(msg);
如果您使用 Python 中的 Anthropic SDK,您可以这样设置您的端点:
import osfrom anthropic import Anthropic
XAI_API_KEY = os.getenv("XAI_API_KEY")client = Anthropic( api_key=XAI_API_KEY, base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",)
message = client.messages.create( model="grok-beta", max_tokens=128, system="You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.", messages=[ { "role": "user", "content": "What is the meaning of life, the universe, and everything?", }, ],)
print(message.content)
You can also use the OpenAI-compatible endpoint (/ai-gateway/chat-completion/
) to access Grok models using the OpenAI API schema. To do so, send your requests to:
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions
Specify:
{"model": "grok/{model}"}
- @2025 Cloudflare Ubitools
- Cf Repo