Skip to content
Merged
84 changes: 84 additions & 0 deletions src/content/docs/ai-gateway/chat-completion.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: OpenAI Compatibility
pcx_content_type: configuration
sidebar:
order: 5
---

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.

## Key benefits

- Standardization: Provides a unified format compatible with OpenAI's schema, reducing the need for code refactoring.
- Ease of Development: Switch between different models and providers quickly using a consistent API structure.

## 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.

## Using the Unified Interface

Switch providers by changing the `model` and `apiKey` parameters.

## Model Parameter Format

Specify the model using `{provider}/{model}` format. For example:

- `openai/gpt-4o-mini`
- `google-ai-studio/gemini-2.0-flash`
- `anthropic/claude-3-haiku`

## Example with OpenAI JavaScript 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` Example

```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?"
}
]
}'
```

## 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/)
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Anthropic
pcx_content_type: get-started
---

import { Render } from "~/components";

[Anthropic](https://www.anthropic.com/) helps build reliable, interpretable, and steerable AI systems.

## Endpoint
Expand Down Expand Up @@ -65,3 +67,17 @@ const message = await anthropic.messages.create({
max_tokens: maxTokens,
});
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Anthropic",
jsonexample: `
{
"model": "anthropic/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/cerebras.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ sidebar:
text: Beta
---

import { Render } from "~/components";

[Cerebras](https://inference-docs.cerebras.ai/) offers developers a low-latency solution for AI model inference.

## Endpoint
Expand Down Expand Up @@ -41,3 +43,17 @@ curl https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/cerebras/chat/comp
]
}'
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Cerebras",
jsonexample: `
{
"model": "cerebras/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/cohere.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Cohere
pcx_content_type: get-started
---

import { Render } from "~/components";

[Cohere](https://cohere.com/) build AI models designed to solve real-world business challenges.

## Endpoint
Expand Down Expand Up @@ -72,3 +74,17 @@ chat = co.chat(
print(chat)

```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Cohere",
jsonexample: `
{
"model": "cohere/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/deepseek.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ sidebar:
text: Beta
---

import { Render } from "~/components";

[DeepSeek](https://www.deepseek.com/) helps you build quickly with DeepSeek's advanced AI models.

## Endpoint
Expand Down Expand Up @@ -80,3 +82,17 @@ try {
return new Response(e);
}
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "DeepSeek",
jsonexample: `
{
"model": "deepseek/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/google-ai-studio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Google AI Studio
pcx_content_type: get-started
---

import { Render } from "~/components";

[Google AI Studio](https://ai.google.dev/aistudio) helps you build quickly with Google Gemini models.

## Endpoint
Expand Down Expand Up @@ -69,3 +71,17 @@ const model = genAI.getGenerativeModel(

await model.generateContent(["What is Cloudflare?"]);
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Google AI Studio",
jsonexample: `
{
"model": "google-ai-studio/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/grok.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Grok
pcx_content_type: get-started
---

import { Render } from "~/components";

[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
Expand Down Expand Up @@ -156,3 +158,17 @@ message = client.messages.create(

print(message.content)
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Grok",
jsonexample: `
{
"model": "grok/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/groq.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Groq
pcx_content_type: get-started
---

import { Render } from "~/components";

[Groq](https://groq.com/) delivers high-speed processing and low-latency performance.

## Endpoint
Expand Down Expand Up @@ -68,3 +70,17 @@ const chatCompletion = await groq.chat.completions.create({
model,
});
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Groq",
jsonexample: `
{
"model": "groq/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/mistral.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Mistral AI
pcx_content_type: get-started
---

import { Render } from "~/components";

[Mistral AI](https://mistral.ai) helps you build quickly with Mistral's advanced AI models.

## Endpoint
Expand Down Expand Up @@ -69,3 +71,17 @@ await client.chat.create({
],
});
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Mistral",
jsonexample: `
{
"model": "mistral/{model}"
}`

}}

/>
16 changes: 16 additions & 0 deletions src/content/docs/ai-gateway/providers/perplexity.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Perplexity
pcx_content_type: get-started
---

import { Render } from "~/components";

[Perplexity](https://www.perplexity.ai/) is an AI powered answer engine.

## Endpoint
Expand Down Expand Up @@ -67,3 +69,17 @@ const chatCompletion = await perplexity.chat.completions.create({
max_tokens: maxTokens,
});
```

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Perplexity",
jsonexample: `
{
"model": "perplexity/{model}"
}`

}}

/>
14 changes: 14 additions & 0 deletions src/content/docs/ai-gateway/providers/workersai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@ Workers AI supports the following parameters for AI gateways:
- Controls whether the request should [skip the cache](/ai-gateway/configuration/caching/#skip-cache-cf-aig-skip-cache).
- `cacheTtl` number
- Controls the [Cache TTL](/ai-gateway/configuration/caching/#cache-ttl-cf-aig-cache-ttl).

<Render
file="chat-completions-providers"
product="ai-gateway"
params={{
name: "Workers AI",
jsonexample: `
{
"model": "workers-ai/{model}"
}`

}}

/>
19 changes: 19 additions & 0 deletions src/content/partials/ai-gateway/chat-completions-providers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
params:
- name
- jsonexample
---

import { Code } from "~/components";

## OpenAI-Compatible Endpoint

You can also use the OpenAI-compatible endpoint (`/ai-gateway/chat-completion/`) to access {props.name} models using the OpenAI API schema. To do so, send your requests to:

```txt
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions
```

Specify:

<Code code={props.jsonexample} lang="json" />
Loading