-
Notifications
You must be signed in to change notification settings - Fork 10k
[AIG]Chat-completions-endpoint #22261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a091c4
initial rest API guide
daisyfaithauma 25710d5
Initial documentation for chat completions
daisyfaithauma 8bc7d17
Revert "initial rest API guide"
daisyfaithauma dd2427e
renamed
daisyfaithauma 328367b
partial-test
daisyfaithauma 28f415e
Merge branch 'production' into chat-completions
daisyfaithauma ebff75a
added partial
daisyfaithauma d4fc46d
minor edit
daisyfaithauma c543908
merge conflict
kodster28 3d6a32b
Partial fixes
kodster28 3fe85b7
Add Universal endpoint
kodster28 89fdc6e
updated content type
kodster28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| --- | ||
| title: OpenAI Compatibility | ||
| pcx_content_type: configuration | ||
| sidebar: | ||
| order: 4 | ||
| --- | ||
|
|
||
| Cloudflare's AI Gateway offers an OpenAI-compatible `/chat/completions` endpoint, enabling integration with multiple AI providers using a single URL. This feature simplifies the integration process, allowing for seamless switching between different models without significant code modifications. | ||
|
|
||
| ## Endpoint URL | ||
|
|
||
| ```txt | ||
| https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions | ||
| ``` | ||
|
|
||
| Replace `{account_id}` and `{gateway_id}` with your Cloudflare account and gateway IDs. | ||
|
|
||
| ## Parameters | ||
|
|
||
| Switch providers by changing the `model` and `apiKey` parameters. | ||
|
|
||
| Specify the model using `{provider}/{model}` format. For example: | ||
|
|
||
| - `openai/gpt-4o-mini` | ||
| - `google-ai-studio/gemini-2.0-flash` | ||
| - `anthropic/claude-3-haiku` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### OpenAI SDK | ||
|
|
||
| ```js | ||
| import OpenAI from "openai"; | ||
| const client = new OpenAI({ | ||
| apiKey: "YOUR_PROVIDER_API_KEY", // Provider API key | ||
| baseURL: | ||
| "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions", | ||
| }); | ||
|
|
||
| const response = await client.chat.completions.create({ | ||
| model: "google-ai-studio/gemini-2.0-flash", | ||
| messages: [{ role: "user", content: "What is Cloudflare?" }], | ||
| }); | ||
|
|
||
| console.log(response.choices[0].message.content); | ||
| ``` | ||
|
|
||
| ### cURL | ||
|
|
||
| ```bash | ||
| curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \ | ||
| --header 'Authorization: Bearer {openai_token}' \ | ||
| --header 'Content-Type: application/json' \ | ||
| --data '{ | ||
| "model": "google-ai-studio/gemini-2.0-flash", | ||
| "messages": [ | ||
| { | ||
| "role": "user", | ||
| "content": "What is Cloudflare?" | ||
| } | ||
| ] | ||
| }' | ||
| ``` | ||
|
|
||
| ### Universal provider | ||
|
|
||
| You can also use this pattern with a [Universal Endpoint](/ai-gateway/universal/). | ||
|
|
||
| ```ts title="index.ts" | ||
| export interface Env { | ||
| AI: Ai; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request: Request, env: Env) { | ||
| return env.AI.gateway("default").run({ | ||
| provider: "compat", | ||
| endpoint: "chat/completions", | ||
| headers: { | ||
| authorization: "Bearer ", | ||
| }, | ||
| query: { | ||
| model: "google-ai-studio/gemini-2.0-flash", | ||
| messages: [ | ||
| { | ||
| role: "user", | ||
| content: "What is Cloudflare?", | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Supported Providers | ||
|
|
||
| The OpenAI-compatible endpoint supports models from the following providers: | ||
|
|
||
| - [Anthropic](/ai-gateway/providers/anthropic/) | ||
| - [OpenAI](/ai-gateway/providers/openai/) | ||
| - [Groq](/ai-gateway/providers/groq/) | ||
| - [Mistral](/ai-gateway/providers/mistral/) | ||
| - [Cohere](/ai-gateway/providers/cohere/) | ||
| - [Perplexity](/ai-gateway/providers/perplexity/) | ||
| - [Workers AI](/ai-gateway/providers/workersai/) | ||
| - [Google-AI-Studio](/ai-gateway/providers/google-ai-studio/) | ||
| - [Grok](/ai-gateway/providers/grok/) | ||
| - [DeepSeek](/ai-gateway/providers/deepseek/) | ||
| - [Cerebras](/ai-gateway/providers/cerebras/) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.