diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx new file mode 100644 index 000000000000000..d8e5b2caa4c9953 --- /dev/null +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -0,0 +1,142 @@ +--- +title: Grok +pcx_content_type: get-started +sidebar: + badge: + text: Beta +--- + +[Grok](https://docs.x.ai/docs#getting-started) is s a general purpose model that can be used for a variety of tasks, including generating and understanding text, code, and function calling. + +## Endpoint + +`https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok` + +## URL structure + +When making requests to [Grok](https://docs.x.ai/docs#getting-started), replace `https://api.x.ai/v1` in the URL you are currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok`. + +## Examples + +### cURL + +```bash title="Request" +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?" + } + ] +}' +``` + +If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: + +```js title="JavaScript" +import OpenAI from "openai"; + +const openai = new OpenAI({ + apiKey: "", + 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); +``` + +If you are using the OpenAI SDK with Python, you can set your endpoint like this: + +```python title="Python" +import os +from 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) +``` + +If you are using the Anthropic SDK with JavaScript, you can set your endpoint like this: + +```js title="JavaScript" +import Anthropic from "@anthropic-ai/sdk"; + +const anthropic = new Anthropic({ + apiKey: "", + 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); +``` + +If you are using the Anthropic SDK with Python, you can set your endpoint like this: + +```python title="Python" +import os +from 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) +```