实时示例

examples/ 目录中有四个可运行的示例,大致按照它们所教授内容的多少排序。所有示例都使用 server-side bridge;所有示例都可以在 OpenAI 或 Gemini 上运行。请先设置相关密钥:

export OPENAI_API_KEY=sk-…        # for OpenAI sessions
export GEMINI_API_KEY=…           # for Gemini sessions (or GOOGLE_API_KEY)

realtime_tools

无头模式。最清晰的入门读物。 一个控制台探测器(无浏览器),用于在 IntegratedRealtimeRunner 上执行 tool dispatch:包括单工具、并行工具和计算器回合,并集成了会话/内存/转录聚合。展示工具回合如何流动的最小端到端视图。

cargo run --manifest-path examples/realtime_tools/Cargo.toml

从这里开始了解 runner;然后转到 Web 示例以了解音频。


realtime_voice与米娅的正念练习

旗舰示例。 一个完整的网络语音应用:浏览器捕获麦克风 PCM16 并无缝播放 agent,支持打断;Rust 服务器拥有会话。其突出特点是 memory:一个文件支持的 GraphMemoryService 是米娅的长期记忆——她的个人资料卡在会话开始时注入,每个回合都记录到图中,她通过桥接的 remember/relate 工具在对话中整理持久性事实。一个实时“用户记忆洞察”面板通过 /api/memory 读取和写入相同的图。还展示了一个服务器端 get_weather 工具。

浅色深色
与米娅的正念 — 浅色主题与米娅的正念 — 深色主题
cargo run --manifest-path examples/realtime_voice/Cargo.toml
# → http://localhost:3033

阅读它以了解:Web Audio 捕获/播放、知识图谱记忆、agent 自我管理。


customer_serviceAria

多模态展示。 Google '客户服务 agent' Live API 演示的重新设计版本 — 后端无关,用 Rust 编写,具有主题化(系统/浅色/深色)三列 UI。它结合了所有功能:

  • 多模态 — 流式传输麦克风 PCM 摄像头 JPEG 帧;Aria 看到你展示给她的内容 (send_video_frame)。
  • 工具process_refundconnect_to_human 在服务器端运行;结果会以语音形式返回。
  • 情感对话 — 在两个后端上都具有同理心;CS_AFFECTIVE=1 将 Gemini 切换到原生音频模型以实现真实的音调匹配。
浅色深色
客户服务代理 — 浅色主题客户服务代理 — 深色主题
cargo run --manifest-path examples/customer_service/Cargo.toml
# → http://localhost:3066

# headless smoke test (verifies the refund tool runs):
cargo run --manifest-path examples/customer_service/Cargo.toml -- probe openai
cargo run --manifest-path examples/customer_service/Cargo.toml -- probe gemini

# enable Gemini native affective dialogue:
CS_AFFECTIVE=1 cargo run --manifest-path examples/customer_service/Cargo.toml

阅读它以了解:视频输入,在一个 Agent 中混合多模态 + 工具 + 情感。


live_translation

专用模型示例。 一个实时语音到语音翻译器:说英语,几秒钟后听到西班牙语。相同的桥接,但指向提供商的翻译模型 — OpenAI gpt-realtime-translate 和 Gemini gemini-3.5-live-translate-preview — 它们与对话模型使用不同的协议。在 UI 中选择目标语言;服务器在音频流传输前协商每个提供商的音频速率。

浅色深色
实时翻译 — 浅色主题实时翻译 — 深色主题
cargo run --manifest-path examples/live_translation/Cargo.toml
# → http://localhost:3055
cargo run --manifest-path examples/live_translation/Cargo.toml -- probe openai

用途:使用专用的翻译模型和专注、最小化的桥接。


建议的路径

  1. realtime_tools — 查看 runner 和 tool 的轮次,无音频噪音。
  2. realtime_voice — 添加真实音频和知识图谱记忆。
  3. customer_service — 添加视觉和情感;完整的 multimodal agent。
  4. live_translation — 专用模型和协议。

示例目录中的每个 README.md 都包含完整的环境变量表、模型 覆盖 (OPENAI_REALTIME_MODEL / GEMINI_REALTIME_MODEL) 和架构 说明。

← 返回 实时概览