From 82cbe1ef33be429bf6b074205997a0c92cd7d788 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Wed, 16 Apr 2025 17:04:57 +0100 Subject: [PATCH 1/5] added responses endpoint and examples --- .../docs/ai-gateway/providers/openai.mdx | 108 ++++++++++++++---- 1 file changed, 84 insertions(+), 24 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index d009813f78eba3e..fadeafc624ed2a5 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -3,6 +3,8 @@ title: OpenAI pcx_content_type: get-started --- +import { Tabs, TabItem } from "~/components"; + [OpenAI](https://openai.com/about/) helps you build with ChatGPT. ## Endpoint @@ -11,6 +13,18 @@ pcx_content_type: get-started https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai ``` +### Chat completions endpoint + +```txt +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ +``` + +### Responses endpoint + +```txt +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \ +``` + ## URL structure When making requests to OpenAI, replace `https://api.openai.com/v1` in the URL you’re currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai`. @@ -24,34 +38,31 @@ When making requests to OpenAI, ensure you have the following: - An active OpenAI API token. - The name of the OpenAI model you want to use. -## Examples +## Chat completions endpoint -### cURL +### cURL example -```bash title="Request" +```bash curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ - --header 'Authorization: Bearer {openai_token}' \ - --header 'Content-Type: application/json' \ - --data ' { - "model": "gpt-4o-mini", - "messages": [ - { - "role": "user", - "content": "What is Cloudflare" - } - ] - } -' +--header 'Authorization: Bearer {openai_token}' \ +--header 'Content-Type: application/json' \ +--data '{ + "model": "gpt-4o-mini", + "messages": [ + { + "role": "user", + "content": "What is Cloudflare?" + } + ] +}' ``` -### Use OpenAI SDK with JavaScript - -If you are using a library like openai-node, set the `baseURL` to your OpenAI endpoint like this: +### JavaScript SDK example -```js title="JavaScript" +```js import OpenAI from "openai"; -const apiKey = "my api key"; // defaults to process.env["OPENAI_API_KEY"] +const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"] const accountId = "{account_id}"; const gatewayId = "{gateway_id}"; const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`; @@ -65,17 +76,66 @@ try { const model = "gpt-3.5-turbo-0613"; const messages = [{ role: "user", content: "What is a neuron?" }]; const maxTokens = 100; - const chatCompletion = await openai.chat.completions.create({ model, messages, max_tokens: maxTokens, }); - const response = chatCompletion.choices[0].message; + console.log(response); +} catch (e) { + console.error(e); +} +``` + +## OpenAI Responses endpoint + +### cURL example + +```bash +curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \ +--header 'Authorization: Bearer {openai_token}' \ +--header 'Content-Type: application/json' \ +--data '{ + "model": "gpt-4.1", + "input": [ + { + "role": "user", + "content": "Write a one-sentence bedtime story about a unicorn." + } + ] +}' +``` + +### JavaScript SDK example + +```js +import OpenAI from "openai"; + +const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"] +const accountId = "{account_id}"; +const gatewayId = "{gateway_id}"; +const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`; - return new Response(JSON.stringify(response)); +const openai = new OpenAI({ + apiKey, + baseURL, +}); + +try { + const model = "gpt-4.1"; + const input = [ + { + role: "user", + content: "Write a one-sentence bedtime story about a unicorn.", + }, + ]; + const response = await openai.responses.create({ + model, + input, + }); + console.log(response.output_text); } catch (e) { - return new Response(e); + console.error(e); } ``` From d218ea128e5c222b197e0ff3dc838d58820b3c87 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Wed, 16 Apr 2025 17:11:21 +0100 Subject: [PATCH 2/5] tab test --- .../docs/ai-gateway/providers/openai.mdx | 170 +++++++++--------- 1 file changed, 81 insertions(+), 89 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index fadeafc624ed2a5..9216725688a2397 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -38,104 +38,96 @@ When making requests to OpenAI, ensure you have the following: - An active OpenAI API token. - The name of the OpenAI model you want to use. -## Chat completions endpoint - -### cURL example - -```bash -curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ ---header 'Authorization: Bearer {openai_token}' \ ---header 'Content-Type: application/json' \ ---data '{ - "model": "gpt-4o-mini", - "messages": [ - { - "role": "user", - "content": "What is Cloudflare?" - } - ] -}' -``` - -### JavaScript SDK example - -```js -import OpenAI from "openai"; - -const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"] +## Examples + + + +
+

Chat completions endpoint

+
+				{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \\
+  --header 'Authorization: Bearer {openai_token}' \\
+  --header 'Content-Type: application/json' \\
+  --data '{
+        "model": "gpt-4o-mini",
+        "messages": [
+          {
+            "role": "user",
+            "content": "What is Cloudflare?"
+          }
+        ]
+      }'`}
+			
+

Responses endpoint

+
+				{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \\
+  --header 'Authorization: Bearer {openai_token}' \\
+  --header 'Content-Type: application/json' \\
+  --data '{
+        "model": "gpt-4.1",
+        "input": [
+          {
+            "role": "user",
+            "content": "Write a one-sentence bedtime story about a unicorn."
+          }
+        ]
+      }'`}
+			
+
+
+ +
+

Chat completions endpoint

+
+				{`import OpenAI from "openai";
+const apiKey = "my api key";
 const accountId = "{account_id}";
 const gatewayId = "{gateway_id}";
-const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
-
+const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
 const openai = new OpenAI({
-	apiKey,
-	baseURL,
+  apiKey,
+  baseURL,
 });
-
 try {
-	const model = "gpt-3.5-turbo-0613";
-	const messages = [{ role: "user", content: "What is a neuron?" }];
-	const maxTokens = 100;
-	const chatCompletion = await openai.chat.completions.create({
-		model,
-		messages,
-		max_tokens: maxTokens,
-	});
-	const response = chatCompletion.choices[0].message;
-	console.log(response);
+  const model = "gpt-3.5-turbo-0613";
+  const messages = [{ role: "user", content: "What is a neuron?" }];
+  const maxTokens = 100;
+  const chatCompletion = await openai.chat.completions.create({
+    model,
+    messages,
+    max_tokens: maxTokens,
+  });
+  const response = chatCompletion.choices[0].message;
+  console.log(response);
 } catch (e) {
-	console.error(e);
-}
-```
-
-## OpenAI Responses endpoint
-
-### cURL example
-
-```bash
-curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
---header 'Authorization: Bearer {openai_token}' \
---header 'Content-Type: application/json' \
---data '{
-  "model": "gpt-4.1",
-  "input": [
-    {
-      "role": "user",
-      "content": "Write a one-sentence bedtime story about a unicorn."
-    }
-  ]
-}'
-```
-
-### JavaScript SDK example
-
-```js
-import OpenAI from "openai";
-
-const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
+  console.error(e);
+}`}
+			
+

Responses endpoint

+
+				{`import OpenAI from "openai";
+const apiKey = "my api key";
 const accountId = "{account_id}";
 const gatewayId = "{gateway_id}";
-const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
-
+const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
 const openai = new OpenAI({
-	apiKey,
-	baseURL,
+  apiKey,
+  baseURL,
 });
-
 try {
-	const model = "gpt-4.1";
-	const input = [
-		{
-			role: "user",
-			content: "Write a one-sentence bedtime story about a unicorn.",
-		},
-	];
-	const response = await openai.responses.create({
-		model,
-		input,
-	});
-	console.log(response.output_text);
+  const model = "gpt-4.1";
+  const input = [
+    { role: "user", content: "Write a one-sentence bedtime story about a unicorn." }
+  ];
+  const response = await openai.responses.create({
+    model,
+    input,
+  });
+  console.log(response.output_text);
 } catch (e) {
-	console.error(e);
-}
-```
+  console.error(e);
+}`}
+			
+
+
+
From 561b0fe1c08f10d1db42a020cb9d976747828bb8 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Wed, 16 Apr 2025 22:31:09 +0100 Subject: [PATCH 3/5] Revert "tab test" This reverts commit d218ea128e5c222b197e0ff3dc838d58820b3c87. --- .../docs/ai-gateway/providers/openai.mdx | 170 +++++++++--------- 1 file changed, 89 insertions(+), 81 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index 9216725688a2397..fadeafc624ed2a5 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -38,96 +38,104 @@ When making requests to OpenAI, ensure you have the following: - An active OpenAI API token. - The name of the OpenAI model you want to use. -## Examples - - - -
-

Chat completions endpoint

-
-				{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \\
-  --header 'Authorization: Bearer {openai_token}' \\
-  --header 'Content-Type: application/json' \\
-  --data '{
-        "model": "gpt-4o-mini",
-        "messages": [
-          {
-            "role": "user",
-            "content": "What is Cloudflare?"
-          }
-        ]
-      }'`}
-			
-

Responses endpoint

-
-				{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \\
-  --header 'Authorization: Bearer {openai_token}' \\
-  --header 'Content-Type: application/json' \\
-  --data '{
-        "model": "gpt-4.1",
-        "input": [
-          {
-            "role": "user",
-            "content": "Write a one-sentence bedtime story about a unicorn."
-          }
-        ]
-      }'`}
-			
-
-
- -
-

Chat completions endpoint

-
-				{`import OpenAI from "openai";
-const apiKey = "my api key";
+## Chat completions endpoint
+
+### cURL example
+
+```bash
+curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
+--header 'Authorization: Bearer {openai_token}' \
+--header 'Content-Type: application/json' \
+--data '{
+  "model": "gpt-4o-mini",
+  "messages": [
+    {
+      "role": "user",
+      "content": "What is Cloudflare?"
+    }
+  ]
+}'
+```
+
+### JavaScript SDK example
+
+```js
+import OpenAI from "openai";
+
+const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
 const accountId = "{account_id}";
 const gatewayId = "{gateway_id}";
-const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
+const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
+
 const openai = new OpenAI({
-  apiKey,
-  baseURL,
+	apiKey,
+	baseURL,
 });
+
 try {
-  const model = "gpt-3.5-turbo-0613";
-  const messages = [{ role: "user", content: "What is a neuron?" }];
-  const maxTokens = 100;
-  const chatCompletion = await openai.chat.completions.create({
-    model,
-    messages,
-    max_tokens: maxTokens,
-  });
-  const response = chatCompletion.choices[0].message;
-  console.log(response);
+	const model = "gpt-3.5-turbo-0613";
+	const messages = [{ role: "user", content: "What is a neuron?" }];
+	const maxTokens = 100;
+	const chatCompletion = await openai.chat.completions.create({
+		model,
+		messages,
+		max_tokens: maxTokens,
+	});
+	const response = chatCompletion.choices[0].message;
+	console.log(response);
 } catch (e) {
-  console.error(e);
-}`}
-			
-

Responses endpoint

-
-				{`import OpenAI from "openai";
-const apiKey = "my api key";
+	console.error(e);
+}
+```
+
+## OpenAI Responses endpoint
+
+### cURL example
+
+```bash
+curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
+--header 'Authorization: Bearer {openai_token}' \
+--header 'Content-Type: application/json' \
+--data '{
+  "model": "gpt-4.1",
+  "input": [
+    {
+      "role": "user",
+      "content": "Write a one-sentence bedtime story about a unicorn."
+    }
+  ]
+}'
+```
+
+### JavaScript SDK example
+
+```js
+import OpenAI from "openai";
+
+const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
 const accountId = "{account_id}";
 const gatewayId = "{gateway_id}";
-const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
+const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
+
 const openai = new OpenAI({
-  apiKey,
-  baseURL,
+	apiKey,
+	baseURL,
 });
+
 try {
-  const model = "gpt-4.1";
-  const input = [
-    { role: "user", content: "Write a one-sentence bedtime story about a unicorn." }
-  ];
-  const response = await openai.responses.create({
-    model,
-    input,
-  });
-  console.log(response.output_text);
+	const model = "gpt-4.1";
+	const input = [
+		{
+			role: "user",
+			content: "Write a one-sentence bedtime story about a unicorn.",
+		},
+	];
+	const response = await openai.responses.create({
+		model,
+		input,
+	});
+	console.log(response.output_text);
 } catch (e) {
-  console.error(e);
-}`}
-			
-
-
-
+ console.error(e); +} +``` From 5ff3d63215c930c03617f63c7286759b6690229a Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Wed, 16 Apr 2025 22:33:05 +0100 Subject: [PATCH 4/5] removed tab details --- src/content/docs/ai-gateway/providers/openai.mdx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index fadeafc624ed2a5..e0f1a3cee9df1d5 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -3,8 +3,6 @@ title: OpenAI pcx_content_type: get-started --- -import { Tabs, TabItem } from "~/components"; - [OpenAI](https://openai.com/about/) helps you build with ChatGPT. ## Endpoint From 5bf9b772f19bca8d693a25e21bdf36950addc4e3 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Thu, 17 Apr 2025 11:33:25 +0100 Subject: [PATCH 5/5] minor fix --- src/content/docs/ai-gateway/providers/openai.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index e0f1a3cee9df1d5..a4bd9c995ae7e3e 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -25,7 +25,7 @@ https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses ## URL structure -When making requests to OpenAI, replace `https://api.openai.com/v1` in the URL you’re currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai`. +When making requests to OpenAI, replace `https://api.openai.com/v1` in the URL you are currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai`. ## Prerequisites