Skip to content

Commit 8a564bd

Browse files
daisyfaithaumaRebeccaTamachiro
authored andcommitted
[AIG]added responses endpoint and examples (#21745)
* added responses endpoint and examples * tab test * Revert "tab test" This reverts commit d218ea1. * removed tab details * minor fix
1 parent 8c9fe25 commit 8a564bd

File tree

1 file changed

+83
-25
lines changed

1 file changed

+83
-25
lines changed

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

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ pcx_content_type: get-started
1111
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai
1212
```
1313

14+
### Chat completions endpoint
15+
16+
```txt
17+
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
18+
```
19+
20+
### Responses endpoint
21+
22+
```txt
23+
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
24+
```
25+
1426
## URL structure
1527

16-
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`.
28+
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`.
1729

1830
## Prerequisites
1931

@@ -24,34 +36,31 @@ When making requests to OpenAI, ensure you have the following:
2436
- An active OpenAI API token.
2537
- The name of the OpenAI model you want to use.
2638

27-
## Examples
39+
## Chat completions endpoint
2840

29-
### cURL
41+
### cURL example
3042

31-
```bash title="Request"
43+
```bash
3244
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
33-
--header 'Authorization: Bearer {openai_token}' \
34-
--header 'Content-Type: application/json' \
35-
--data ' {
36-
"model": "gpt-4o-mini",
37-
"messages": [
38-
{
39-
"role": "user",
40-
"content": "What is Cloudflare"
41-
}
42-
]
43-
}
44-
'
45+
--header 'Authorization: Bearer {openai_token}' \
46+
--header 'Content-Type: application/json' \
47+
--data '{
48+
"model": "gpt-4o-mini",
49+
"messages": [
50+
{
51+
"role": "user",
52+
"content": "What is Cloudflare?"
53+
}
54+
]
55+
}'
4556
```
4657

47-
### Use OpenAI SDK with JavaScript
58+
### JavaScript SDK example
4859

49-
If you are using a library like openai-node, set the `baseURL` to your OpenAI endpoint like this:
50-
51-
```js title="JavaScript"
60+
```js
5261
import OpenAI from "openai";
5362

54-
const apiKey = "my api key"; // defaults to process.env["OPENAI_API_KEY"]
63+
const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
5564
const accountId = "{account_id}";
5665
const gatewayId = "{gateway_id}";
5766
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
@@ -65,17 +74,66 @@ try {
6574
const model = "gpt-3.5-turbo-0613";
6675
const messages = [{ role: "user", content: "What is a neuron?" }];
6776
const maxTokens = 100;
68-
6977
const chatCompletion = await openai.chat.completions.create({
7078
model,
7179
messages,
7280
max_tokens: maxTokens,
7381
});
74-
7582
const response = chatCompletion.choices[0].message;
83+
console.log(response);
84+
} catch (e) {
85+
console.error(e);
86+
}
87+
```
88+
89+
## OpenAI Responses endpoint
90+
91+
### cURL example
92+
93+
```bash
94+
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/responses \
95+
--header 'Authorization: Bearer {openai_token}' \
96+
--header 'Content-Type: application/json' \
97+
--data '{
98+
"model": "gpt-4.1",
99+
"input": [
100+
{
101+
"role": "user",
102+
"content": "Write a one-sentence bedtime story about a unicorn."
103+
}
104+
]
105+
}'
106+
```
76107

77-
return new Response(JSON.stringify(response));
108+
### JavaScript SDK example
109+
110+
```js
111+
import OpenAI from "openai";
112+
113+
const apiKey = "my api key"; // or process.env["OPENAI_API_KEY"]
114+
const accountId = "{account_id}";
115+
const gatewayId = "{gateway_id}";
116+
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
117+
118+
const openai = new OpenAI({
119+
apiKey,
120+
baseURL,
121+
});
122+
123+
try {
124+
const model = "gpt-4.1";
125+
const input = [
126+
{
127+
role: "user",
128+
content: "Write a one-sentence bedtime story about a unicorn.",
129+
},
130+
];
131+
const response = await openai.responses.create({
132+
model,
133+
input,
134+
});
135+
console.log(response.output_text);
78136
} catch (e) {
79-
return new Response(e);
137+
console.error(e);
80138
}
81139
```

0 commit comments

Comments
 (0)