Skip to content

Commit d218ea1

Browse files
tab test
1 parent 82cbe1e commit d218ea1

File tree

1 file changed

+81
-89
lines changed

1 file changed

+81
-89
lines changed

src/content/docs/ai-gateway/providers/openai.mdx

Lines changed: 81 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -38,104 +38,96 @@ When making requests to OpenAI, ensure you have the following:
3838
- An active OpenAI API token.
3939
- The name of the OpenAI model you want to use.
4040

41-
## Chat completions endpoint
42-
43-
### cURL example
44-
45-
```bash
46-
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
47-
--header 'Authorization: Bearer {openai_token}' \
48-
--header 'Content-Type: application/json' \
49-
--data '{
50-
"model": "gpt-4o-mini",
51-
"messages": [
52-
{
53-
"role": "user",
54-
"content": "What is Cloudflare?"
55-
}
56-
]
57-
}'
58-
```
59-
60-
### JavaScript SDK example
61-
62-
```js
63-
import OpenAI from "openai";
64-
65-
const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
41+
## Examples
42+
43+
<Tabs syncKey="apiExamples">
44+
<TabItem label="cURL">
45+
<div>
46+
<h3>Chat completions endpoint</h3>
47+
<pre>
48+
{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \\
49+
--header 'Authorization: Bearer {openai_token}' \\
50+
--header 'Content-Type: application/json' \\
51+
--data '{
52+
"model": "gpt-4o-mini",
53+
"messages": [
54+
{
55+
"role": "user",
56+
"content": "What is Cloudflare?"
57+
}
58+
]
59+
}'`}
60+
</pre>
61+
<h3>Responses endpoint</h3>
62+
<pre>
63+
{`curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \\
64+
--header 'Authorization: Bearer {openai_token}' \\
65+
--header 'Content-Type: application/json' \\
66+
--data '{
67+
"model": "gpt-4.1",
68+
"input": [
69+
{
70+
"role": "user",
71+
"content": "Write a one-sentence bedtime story about a unicorn."
72+
}
73+
]
74+
}'`}
75+
</pre>
76+
</div>
77+
</TabItem>
78+
<TabItem label="JavaScript">
79+
<div>
80+
<h3>Chat completions endpoint</h3>
81+
<pre>
82+
{`import OpenAI from "openai";
83+
const apiKey = "my api key";
6684
const accountId = "{account_id}";
6785
const gatewayId = "{gateway_id}";
68-
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
69-
86+
const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
7087
const openai = new OpenAI({
71-
apiKey,
72-
baseURL,
88+
apiKey,
89+
baseURL,
7390
});
74-
7591
try {
76-
const model = "gpt-3.5-turbo-0613";
77-
const messages = [{ role: "user", content: "What is a neuron?" }];
78-
const maxTokens = 100;
79-
const chatCompletion = await openai.chat.completions.create({
80-
model,
81-
messages,
82-
max_tokens: maxTokens,
83-
});
84-
const response = chatCompletion.choices[0].message;
85-
console.log(response);
92+
const model = "gpt-3.5-turbo-0613";
93+
const messages = [{ role: "user", content: "What is a neuron?" }];
94+
const maxTokens = 100;
95+
const chatCompletion = await openai.chat.completions.create({
96+
model,
97+
messages,
98+
max_tokens: maxTokens,
99+
});
100+
const response = chatCompletion.choices[0].message;
101+
console.log(response);
86102
} catch (e) {
87-
console.error(e);
88-
}
89-
```
90-
91-
## OpenAI Responses endpoint
92-
93-
### cURL example
94-
95-
```bash
96-
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
97-
--header 'Authorization: Bearer {openai_token}' \
98-
--header 'Content-Type: application/json' \
99-
--data '{
100-
"model": "gpt-4.1",
101-
"input": [
102-
{
103-
"role": "user",
104-
"content": "Write a one-sentence bedtime story about a unicorn."
105-
}
106-
]
107-
}'
108-
```
109-
110-
### JavaScript SDK example
111-
112-
```js
113-
import OpenAI from "openai";
114-
115-
const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
103+
console.error(e);
104+
}`}
105+
</pre>
106+
<h3>Responses endpoint</h3>
107+
<pre>
108+
{`import OpenAI from "openai";
109+
const apiKey = "my api key";
116110
const accountId = "{account_id}";
117111
const gatewayId = "{gateway_id}";
118-
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
119-
112+
const baseURL = \`https://gateway.ai.cloudflare.com/v1/\${accountId}/\${gatewayId}/openai\`;
120113
const openai = new OpenAI({
121-
apiKey,
122-
baseURL,
114+
apiKey,
115+
baseURL,
123116
});
124-
125117
try {
126-
const model = "gpt-4.1";
127-
const input = [
128-
{
129-
role: "user",
130-
content: "Write a one-sentence bedtime story about a unicorn.",
131-
},
132-
];
133-
const response = await openai.responses.create({
134-
model,
135-
input,
136-
});
137-
console.log(response.output_text);
118+
const model = "gpt-4.1";
119+
const input = [
120+
{ role: "user", content: "Write a one-sentence bedtime story about a unicorn." }
121+
];
122+
const response = await openai.responses.create({
123+
model,
124+
input,
125+
});
126+
console.log(response.output_text);
138127
} catch (e) {
139-
console.error(e);
140-
}
141-
```
128+
console.error(e);
129+
}`}
130+
</pre>
131+
</div>
132+
</TabItem>
133+
</Tabs>

0 commit comments

Comments
 (0)