-
Notifications
You must be signed in to change notification settings - Fork 10.2k
[AIG]Websockets Docs #20898
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
[AIG]Websockets Docs #20898
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b72240d
Websockets Docs
daisyfaithauma 02a7181
fixed hyperlink
daisyfaithauma 143aeea
minor edits
daisyfaithauma 8ca437a
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma c44aa24
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma e5dae50
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma 0f81715
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma 023fe2d
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma ab57ee1
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma d538d99
Update src/content/docs/ai-gateway/configuration/websockets-api/index…
daisyfaithauma bbc92ce
fixed table
daisyfaithauma 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
35 changes: 35 additions & 0 deletions
35
src/content/docs/ai-gateway/configuration/websockets-api/index.mdx
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,35 @@ | ||
| --- | ||
| title: WebSockets API | ||
| pcx_content_type: configuration | ||
| sidebar: | ||
| group: | ||
| badge: Beta | ||
| --- | ||
|
|
||
| The AI Gateway WebSockets API provides a persistent connection for AI interactions, eliminating repeated handshakes and reducing latency. This API is divided into two categories: | ||
|
|
||
| 1. **Non-Realtime APIs** - Supports standard WebSocket communication for AI providers, including those that do not natively support WebSockets. | ||
| 2. **Realtime APIs** - Designed for AI providers that offer low-latency, multimodal interactions over WebSockets. | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## When to use WebSockets? | ||
|
|
||
| WebSockets are long-lived TCP connections that enable bi-directional, real-time and non realtime communication between client and server. Unlike HTTP connections, which require repeated handshakes for each request, WebSockets maintain the connection, supporting continuous data exchange with reduced overhead. WebSockets are ideal for applications needing low-latency, real-time data, such as voice assistants. | ||
|
|
||
| ## Key benefits | ||
|
|
||
| - **Reduced Overhead**: Avoid overhead of repeated handshakes and TLS negotiations by maintaining a single, persistent connection. | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **Provider Compatibility**: Works with all AI providers in AI Gateway. Even if your chosen provider does not support WebSockets, we handle it for you, managing the requests to your preferred AI provider. | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## **Key differences** | ||
|
|
||
| | Feature | Non-Realtime APIs | Realtime APIs | | ||
| | :---------------------- | :----------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------ | | ||
| | **Purpose** | Supports WebSocket-based AI interactions with providers that do not natively support WebSockets. | Enables real-time, multimodal AI interactions for providers that offer dedicated WebSocket endpoints. | | ||
| | **Use Case** | Text-based queries and responses, such as LLM requests. | Streaming responses for voice, video, and live interactions. | | ||
| | **AI Provider Support** | [All AI providers in AI Gateway.](/ai-gateway/providers) | [Limited to providers offering real-time WebSocket APIs.](/ai-gateway/configuration/websockets-api/realtime-api/#supported-providers) | | ||
| | **Streaming Support** | AI Gateway handles streaming via WebSockets. | Providers natively support real-time data streaming. | | ||
|
|
||
| For details on implementation, see the next section: | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - [Realtime WebSockets API](/ai-gateway/configuration/websockets-api/realtime-api/) | ||
| - [Non-Realtime WebSockets API](/ai-gateway/configuration/websockets-api/non-realtime-api/) | ||
16 changes: 3 additions & 13 deletions
16
...-gateway/configuration/websockets-api.mdx → ...ation/websockets-api/non-realtime-api.mdx
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
133 changes: 133 additions & 0 deletions
133
src/content/docs/ai-gateway/configuration/websockets-api/realtime-api.mdx
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,133 @@ | ||
| --- | ||
| pcx_content_type: configuration | ||
| title: Realtime WebSockets API | ||
| sidebar: | ||
| order: 2 | ||
| --- | ||
|
|
||
| Some AI providers support real-time, low-latency interactions over WebSockets. AI Gateway allows seamless integration with these APIs, supporting multimodal interactions such as text, audio, and video. | ||
|
|
||
| ## Supported Providers | ||
|
|
||
| - [OpenAI](https://platform.openai.com/docs/guides/realtime-websocket) | ||
| - [Google AI Studio](https://ai.google.dev/gemini-api/docs/multimodal-live) | ||
| - [Cartesia](https://docs.cartesia.ai/api-reference/tts/tts) | ||
| - [ElevenLabs](https://elevenlabs.io/docs/conversational-ai/api-reference/conversational-ai/websocket) | ||
|
|
||
| ## Authentication | ||
|
|
||
| For real-time WebSockets, authentication can be done using: | ||
|
|
||
| - Headers (for non-browser environments) | ||
| - `sec-websocket-protocol` (for browsers) | ||
|
|
||
| ## Examples | ||
|
|
||
| ### OpenAI | ||
|
|
||
| ```javascript | ||
| import WebSocket from "ws"; | ||
|
|
||
| const url = | ||
| "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/openai?model=gpt-4o-realtime-preview-2024-12-17"; | ||
| const ws = new WebSocket(url, { | ||
| headers: { | ||
| "cf-aig-authorization": process.env.CLOUDFLARE_API_KEY, | ||
| Authorization: "Bearer " + process.env.OPENAI_API_KEY, | ||
| "OpenAI-Beta": "realtime=v1", | ||
| }, | ||
| }); | ||
|
|
||
| ws.on("open", () => console.log("Connected to server.")); | ||
| ws.on("message", (message) => console.log(JSON.parse(message.toString()))); | ||
|
|
||
| ws.send( | ||
| JSON.stringify({ | ||
| type: "response.create", | ||
| response: { modalities: ["text"], instructions: "Tell me a joke" }, | ||
| }), | ||
| ); | ||
| ``` | ||
|
|
||
| ### Google AI Studio | ||
|
|
||
| ```javascript | ||
| const ws = new WebSocket( | ||
| "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/google?api_key=<google_api_key>", | ||
| ["cf-aig-authorization.<cloudflare_token>"], | ||
| ); | ||
|
|
||
| ws.on("open", () => console.log("Connected to server.")); | ||
| ws.on("message", (message) => console.log(message.data)); | ||
|
|
||
| ws.send( | ||
| JSON.stringify({ | ||
| setup: { | ||
| model: "models/gemini-2.0-flash-exp", | ||
| generationConfig: { responseModalities: ["TEXT"] }, | ||
| }, | ||
| }), | ||
| ); | ||
| ``` | ||
|
|
||
| ### Cartesia | ||
|
|
||
| ```javascript | ||
| const ws = new WebSocket( | ||
| "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/cartesia?cartesia_version=2024-06-10&api_key=<cartesia_api_key>", | ||
| ["cf-aig-authorization.<cloudflare_token>"], | ||
| ); | ||
|
|
||
| ws.on("open", function open() { | ||
| console.log("Connected to server."); | ||
| }); | ||
|
|
||
| ws.on("message", function incoming(message) { | ||
| console.log(message.data); | ||
| }); | ||
|
|
||
| ws.send( | ||
| JSON.stringify({ | ||
| model_id: "sonic", | ||
| transcript: "Hello, world! I'm generating audio on ", | ||
| voice: { mode: "id", id: "a0e99841-438c-4a64-b679-ae501e7d6091" }, | ||
| language: "en", | ||
| context_id: "happy-monkeys-fly", | ||
| output_format: { | ||
| container: "raw", | ||
| encoding: "pcm_s16le", | ||
| sample_rate: 8000, | ||
| }, | ||
| add_timestamps: true, | ||
| continue: true, | ||
| }), | ||
| ); | ||
| ``` | ||
|
|
||
| ### ElevenLabs | ||
|
|
||
| ```javascript | ||
| const ws = new WebSocket( | ||
| "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/elevenlabs?agent_id=<elevenlabs_agent_id>", | ||
| [ | ||
| "xi-api-key.<elevenlabs_api_key>", | ||
| "cf-aig-authorization.<cloudflare_token>", | ||
| ], | ||
| ); | ||
|
|
||
| ws.on("open", function open() { | ||
| console.log("Connected to server."); | ||
| }); | ||
|
|
||
| ws.on("message", function incoming(message) { | ||
| console.log(message.data); | ||
| }); | ||
|
|
||
| ws.send( | ||
| JSON.stringify({ | ||
| text: "This is a sample text ", | ||
| voice_settings: { stability: 0.8, similarity_boost: 0.8 }, | ||
| generation_config: { chunk_length_schedule: [120, 160, 250, 290] }, | ||
| }), | ||
| ); | ||
| ``` |
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.