-
Notifications
You must be signed in to change notification settings - Fork 1k
feature: realtime agents #368
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
Open
bonk1t
wants to merge
1
commit into
main
Choose a base branch
from
docs-realtime-agents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,91 @@ | ||
| --- | ||
| title: "Deployment" | ||
| description: "Run the realtime FastAPI bridge, serve the bundled web client, and connect Twilio phone calls." | ||
| icon: "server" | ||
| --- | ||
|
|
||
| Use this guide once your agent is ready and you want to host a realtime bridge or connect phone infrastructure. It builds on the [Overview](./overview) and assumes your agents are ready for deployment. | ||
|
|
||
| ## Host the FastAPI bridge | ||
|
|
||
| `run_realtime` starts a FastAPI app that proxies between your Agency Swarm agents and the OpenAI Realtime API. The helper already converts your agency to the realtime runtime, exposes a `/realtime` websocket, and streams events back to callers. | ||
|
|
||
| ```python | ||
| from agency_swarm import Agency | ||
| from agency_swarm.integrations import run_realtime | ||
| from voice_agent import voice_agent | ||
|
|
||
| agency = Agency(voice_agent) | ||
|
|
||
| run_realtime( | ||
| agency=agency, | ||
| model="gpt-realtime", | ||
| host="0.0.0.0", | ||
| port=8000, | ||
| turn_detection={"type": "server_vad"}, | ||
| ) | ||
| ``` | ||
|
|
||
| ```bash | ||
| python app.py | ||
| ``` | ||
|
|
||
| The server prints every incoming websocket connection. When your agents declare `voice=`, the bridge carries that choice automatically; omit the parameter for the entry agent to inherit its own voice. Supply `cors_origins` when you deploy behind a browser client that runs on a different domain. | ||
|
|
||
| <Tip> | ||
| `run_realtime(..., return_app=True)` returns the FastAPI `app` object if you want to mount it inside an existing application rather than start a dedicated Uvicorn process. | ||
| </Tip> | ||
|
|
||
| ## Serve voice endpoints from `run_fastapi` | ||
|
|
||
| Keep your existing REST endpoints and add realtime voice routes with one flag: | ||
|
|
||
| ```python | ||
| run_fastapi( | ||
| agencies={"support": create_agency}, | ||
| enable_realtime=True, | ||
| realtime_options={ | ||
| "model": "gpt-realtime", | ||
| "turn_detection": {"type": "server_vad", "interrupt_response": True}, | ||
| }, | ||
| enable_logging=True, | ||
| ) | ||
| ``` | ||
|
|
||
| `enable_realtime=True` mounts `/support/realtime` alongside the normal JSON endpoints. Pass `realtime_options` when you want to override model settings (they map directly to `run_realtime` keyword arguments). Authentication and logging apply to the new websocket route automatically. | ||
|
|
||
| ## Serve the packaged browser client | ||
|
|
||
| The static site in `src/agency_swarm/ui/demos/realtime/app` is bundled with the library. Point it at your server by editing `examples/interactive/realtime/demo.py` or by hosting the static files yourself: | ||
|
|
||
| ```bash | ||
| python -m agency_swarm.ui.demos.realtime.app.server | ||
| ``` | ||
|
|
||
| This mounts the frontend and websocket bridge under the same process—ideal for internal demos or QA. | ||
|
|
||
| ## Twilio phone calls | ||
|
|
||
| Pass a Twilio number to `run_realtime` to expose a media-stream bridge. The helper exposes `/incoming-call` (returns TwiML) and `/twilio/media-stream` for bidirectional audio. | ||
|
|
||
| ```python | ||
| run_realtime( | ||
| agency=agency, | ||
| model="gpt-realtime", | ||
| twilio_number="+15551234567", | ||
| twilio_audio_format="g711_ulaw", | ||
| twilio_greeting="Connecting you to the assistant.", | ||
| ) | ||
| ``` | ||
|
|
||
| Deployment checklist: | ||
|
|
||
| 1. Start the server (with extras installed) and expose it publicly, e.g. `ngrok http 8000`. | ||
| 2. In the Twilio Console, set your phone number’s voice webhook to `https://<public-host>/incoming-call`. | ||
| 3. Call the number—the helper streams audio in both directions and reuses your existing tools and handoffs. | ||
|
|
||
| For a lower-level implementation (custom playback tracking, fine-grained buffering), see `src/agency_swarm/ui/demos/realtime/twilio/README.md`. | ||
|
|
||
| <Note> | ||
| Store your Twilio account SID and auth token in a local `.env`, export them before launching the demo, and keep `OPENAI_API_KEY` alongside them. The packaged server reads standard environment variables; no credentials live in source control. | ||
| </Note> |
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,84 @@ | ||
| --- | ||
| title: "Overview" | ||
| description: "Design voice-first assistants with the same Agency Swarm agents you already use." | ||
| icon: "microphone" | ||
| --- | ||
|
|
||
| Agency Swarm reuses your existing `Agent` definitions for voice. This page shows how to adapt agents for spoken conversations; deployment lives on the dedicated [Deployment](./deployment) guide. | ||
|
|
||
| ## What you can build | ||
|
|
||
| - **Phone receptionist** — answers calls, routes to specialists, captures caller details. | ||
| - **Live support triage** — gathers context, lets callers interrupt, and escalates to a human or another agent. | ||
| - **Language coach** — listens, corrects pronunciation, and keeps the dialogue short and encouraging. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Access to OpenAI Realtime models (`gpt-realtime` or `gpt-realtime-mini` are the recommended latest options). | ||
| - `agency-swarm` with FastAPI extras: | ||
|
|
||
| ```bash | ||
| pip install "agency-swarm[fastapi]" | ||
| ``` | ||
|
|
||
| ## Define your agent (same API) | ||
|
|
||
| You keep using the standard `Agent` class—voice agents are regular agents with the same tools, handoffs, and instructions. | ||
|
|
||
| ```python | ||
| from agency_swarm import Agent, function_tool | ||
|
|
||
| @function_tool | ||
| def lookup_order(order_id: str) -> str: | ||
| """Return a short order status by ID.""" | ||
| return f"Order {order_id} has shipped and will arrive soon." | ||
|
|
||
| voice_agent = Agent( | ||
| name="Voice Concierge", | ||
| instructions=( | ||
| "You are a friendly concierge. Answer in one or two sentences and offer to look up order " | ||
| "details when the caller mentions a number." | ||
| ), | ||
| tools=[lookup_order], | ||
| voice="nova", | ||
| ) | ||
| ``` | ||
|
|
||
| <Tip> | ||
| Keep using the same agent definitions—add `voice=` only when you care about the spoken persona. | ||
| </Tip> | ||
|
|
||
| Set `voice` to any of the OpenAI realtime voices: `alloy`, `ash`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, or `shimmer`. Each agent can declare its own voice, and the realtime bridge keeps it consistent across handoffs. If you prefer variety without manual assignments, construct your agency with `randomize_agent_voices=True`; any agent missing an explicit voice receives a deterministic random pick at initialization. | ||
|
|
||
| ## Add handoffs (optional) | ||
|
|
||
| Handoffs work exactly as they do in text mode. Register your flows once and they will carry over to voice sessions. | ||
|
|
||
| ```python | ||
| from agency_swarm import Agency, Agent | ||
| from agency_swarm.tools import SendMessageHandoff | ||
|
|
||
| billing = Agent(name="Billing", instructions="Handle billing questions briefly.") | ||
| faq = Agent(name="FAQ", instructions="Answer frequently asked questions.") | ||
|
|
||
| concierge = Agent( | ||
| name="Concierge", | ||
| instructions="Greet the caller, collect intent, then hand off when a specialist is needed.", | ||
| ) | ||
|
|
||
| agency = Agency( | ||
| concierge, | ||
| communication_flows=[ | ||
| (concierge > billing, SendMessageHandoff), | ||
| (concierge > faq, SendMessageHandoff), | ||
| ], | ||
| ) | ||
| ``` | ||
|
|
||
| When the concierge invokes `SendMessageHandoff`, the realtime session routes audio and tool access to the designated specialist agent. | ||
|
|
||
| ## Next steps | ||
|
|
||
| - Try the [realtime browser demo](https://github.com/VRSEN/agency-swarm/tree/main/examples/interactive/realtime) | ||
| - [Deploy your agents](./deployment) for phone calls using Twilio. | ||
| - Review OpenAI’s realtime [Quickstart](https://openai.github.io/openai-agents-python/realtime/quickstart/) and [Guide](https://openai.github.io/openai-agents-python/realtime/guide/) for protocol details—Agency Swarm builds on those primitives. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Package marker for interactive realtime demo examples.""" |
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,51 @@ | ||
| """ | ||
| Interactive realtime voice demo. | ||
|
|
||
| Launches the packaged browser frontend + FastAPI backend. | ||
| Edit this file to customize the agent behavior. | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Ensure local src/ is importable when running directly from the repo checkout. | ||
| sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "src")) | ||
|
|
||
| from agency_swarm import Agency, Agent, function_tool | ||
| from agency_swarm.ui.demos.realtime import RealtimeDemoLauncher | ||
|
|
||
|
|
||
| @function_tool | ||
| def lookup_order(order_id: str) -> str: | ||
| """Return a short order status by ID.""" | ||
| return f"Order {order_id} has shipped and will arrive soon." | ||
|
|
||
|
|
||
| VOICE_AGENT = Agent( | ||
| name="Voice Concierge", | ||
| instructions=( | ||
| "You are a helpful voice concierge. Answer succinctly and offer to look up order details " | ||
| "with the provided tool when asked about an order number." | ||
| ), | ||
| tools=[lookup_order], | ||
| ) | ||
|
|
||
| VOICE_AGENCY = Agency(VOICE_AGENT) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| print("Agency Swarm Realtime Browser Demo") | ||
| print("=" * 50) | ||
| print("Open http://localhost:8000 after launch.") | ||
| print("Press Ctrl+C to stop.\n") | ||
|
|
||
| RealtimeDemoLauncher.start( | ||
| VOICE_AGENCY, | ||
| model="gpt-realtime", | ||
| voice="alloy", | ||
| turn_detection={"type": "server_vad"}, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.