From ce43c47d15bf85ada5c70417ec83827bee715aff Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Thu, 24 Oct 2024 16:46:22 +0100 Subject: [PATCH 1/7] Grok initial documentation --- .../docs/ai-gateway/providers/grok.mdx | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/content/docs/ai-gateway/providers/grok.mdx 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 00000000000000..6610b22bd84f29 --- /dev/null +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -0,0 +1,140 @@ +--- +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 -X POST 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) + +```js title="JavaScript" +import OpenAI from "openai"; + +const openai = new OpenAI({ + apiKey: "", + baseURL: "https://api.x.ai/v1", +}); + +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) + +```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://api.x.ai/v1", +) + +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) + +```js title="JavaScript" +import Anthropic from "@anthropic-ai/sdk"; + +const anthropic = new Anthropic({ + apiKey: "", + baseURL: "https://api.x.ai/", +}); + +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) + +```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://api.x.ai", +) + +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) +``` From 9b2d1c626440ba076baaf54eb76ebf05fcfa74b1 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Thu, 24 Oct 2024 19:31:43 +0100 Subject: [PATCH 2/7] Grok edit --- src/content/docs/ai-gateway/providers/grok.mdx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index 6610b22bd84f29..1e23f80e5eccca 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -18,7 +18,7 @@ When making requests to [Grok](https://docs.x.ai/docs#getting-started), replace ## Examples -### Curl +### cURL ```bash title="Request" curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok/v1/chat/completions \ @@ -35,7 +35,7 @@ curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok }' ``` -### JavaScript (OpenAI SDK) +If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: ```js title="JavaScript" import OpenAI from "openai"; @@ -63,7 +63,7 @@ const completion = await openai.chat.completions.create({ console.log(completion.choices[0].message); ``` -### Python (OpenAI SDK) +If you are using the OpenAI SDK with Python, you can set your endpoint like this: ```python title="Python" import os @@ -86,7 +86,7 @@ completion = client.chat.completions.create( print(completion.choices[0].message) ``` -### JavaScript (Anthropic SDK) +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"; @@ -112,7 +112,7 @@ const msg = await anthropic.messages.create({ console.log(msg); ``` -### Python (Anthropic SDK) +If you are using the Anthropic SDK with Python, you can set your endpoint like this: ```python title="Python" import os From c8ba35f8fd2f8d30e9fdce67f14524067a44d9d9 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 28 Oct 2024 11:20:36 +0000 Subject: [PATCH 3/7] Update src/content/docs/ai-gateway/providers/grok.mdx Co-authored-by: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> --- src/content/docs/ai-gateway/providers/grok.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index 1e23f80e5eccca..74d1792ab16ff7 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -41,7 +41,7 @@ If you are using the OpenAI SDK with JavaScript, you can set your endpoint like import OpenAI from "openai"; const openai = new OpenAI({ - apiKey: "", + apiKey: "", baseURL: "https://api.x.ai/v1", }); From afe3c37a2273c3195c1bd3ad5cf180b3fc575414 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 28 Oct 2024 11:20:57 +0000 Subject: [PATCH 4/7] Update src/content/docs/ai-gateway/providers/grok.mdx Co-authored-by: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> --- src/content/docs/ai-gateway/providers/grok.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index 74d1792ab16ff7..8032007218090f 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -6,7 +6,7 @@ sidebar: 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. +[Grok](https://docs.x.ai/docs#getting-started) is a general purpose model that can be used for a variety of tasks, including generating and understanding text, code, and function calling. ## Endpoint From b517dffea698c639197327ece5ddf4a90f03980a Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 28 Oct 2024 11:21:54 +0000 Subject: [PATCH 5/7] Update src/content/docs/ai-gateway/providers/grok.mdx Co-authored-by: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> --- src/content/docs/ai-gateway/providers/grok.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index 8032007218090f..d00b4533ae0931 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -92,7 +92,7 @@ If you are using the Anthropic SDK with JavaScript, you can set your endpoint li import Anthropic from "@anthropic-ai/sdk"; const anthropic = new Anthropic({ - apiKey: "", + apiKey: "", baseURL: "https://api.x.ai/", }); From eff6026c3cbb8beb13a04bf81ad57c8205043bfc Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 28 Oct 2024 11:22:49 +0000 Subject: [PATCH 6/7] Update src/content/docs/ai-gateway/providers/grok.mdx Co-authored-by: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> --- .../docs/ai-gateway/providers/grok.mdx | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index d00b4533ae0931..668707844ab5da 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -21,17 +21,17 @@ When making requests to [Grok](https://docs.x.ai/docs#getting-started), replace ### cURL ```bash title="Request" -curl -X POST 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?" - } - ] +curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok/v1/chat/completions \ +--header "content-type: application/json" \ +--header "Authorization: Bearer " \ +--data '{ + "model": "grok-beta", + "messages": [ + { + "role": "user", + "content": "What is Cloudflare?" + } + ] }' ``` From 9ba80d277252b5ba98c004d4b3f66b126cd98e88 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 28 Oct 2024 16:00:06 +0000 Subject: [PATCH 7/7] URL edits --- .../docs/ai-gateway/providers/grok.mdx | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index 668707844ab5da..d8e5b2caa4c995 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -6,7 +6,7 @@ sidebar: text: Beta --- -[Grok](https://docs.x.ai/docs#getting-started) is a general purpose model that can be used for a variety of tasks, including generating and understanding text, code, and function calling. +[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 @@ -22,16 +22,16 @@ When making requests to [Grok](https://docs.x.ai/docs#getting-started), replace ```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 " \ ---data '{ - "model": "grok-beta", - "messages": [ - { - "role": "user", - "content": "What is Cloudflare?" - } - ] + --header 'content-type: application/json' \ + --header 'Authorization: Bearer {grok_api_token}' \ + --data '{ + "model": "grok-beta", + "messages": [ + { + "role": "user", + "content": "What is Cloudflare?" + } + ] }' ``` @@ -41,8 +41,9 @@ If you are using the OpenAI SDK with JavaScript, you can set your endpoint like import OpenAI from "openai"; const openai = new OpenAI({ - apiKey: "", - baseURL: "https://api.x.ai/v1", + apiKey: "", + baseURL: + "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", }); const completion = await openai.chat.completions.create({ @@ -72,7 +73,7 @@ from openai import OpenAI XAI_API_KEY = os.getenv("XAI_API_KEY") client = OpenAI( api_key=XAI_API_KEY, - base_url="https://api.x.ai/v1", + base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", ) completion = client.chat.completions.create( @@ -92,8 +93,9 @@ If you are using the Anthropic SDK with JavaScript, you can set your endpoint li import Anthropic from "@anthropic-ai/sdk"; const anthropic = new Anthropic({ - apiKey: "", - baseURL: "https://api.x.ai/", + apiKey: "", + baseURL: + "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", }); const msg = await anthropic.messages.create({ @@ -121,7 +123,7 @@ from anthropic import Anthropic XAI_API_KEY = os.getenv("XAI_API_KEY") client = Anthropic( api_key=XAI_API_KEY, - base_url="https://api.x.ai", + base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok", ) message = client.messages.create(