简介
基于 LiveKit 开源框架的语音 AI 开发指南,支持 STT-LLM-TTS 全链路搭建,助力快速构建生产级实时对话 Agent。
原始描述
Build voice AI agents with LiveKit. Use when developing realtime voice applications, voice agent pipelines (STT-LLM-TTS), WebRTC communication, or deploying conversational AI. Covers LiveKit Agents SDK, provider selection (Deepgram, OpenAI, ElevenLabs, Cartesia), cloud vs self-hosted deployment.
中文使用说明 来自收录源
核心用法
LiveKit Voice AI Skill 是一套面向开发者的完整语音代理构建指南,核心围绕 STT-LLM-TTS 三阶段流水线 架构展开。开发者通过 livekit-agents SDK 可快速组装语音识别(STT)、大语言模型(LLM)、语音合成(TTS)组件,支持 Python 与 Node.js 双栈。
关键 API 模式包括:
- AgentSession: 核心编排对象,负责管理音频流、组件协调与生命周期
- Provider 插件化: 通过
livekit-plugins-*包解耦供应商,支持 Deepgram(STT)、OpenAI/Claude(LLM)、Cartesia/ElevenLabs(TTS)灵活切换 - 工具调用:
@function_tool装饰器实现 LLM Function Calling,扩展天气查询、日历管理等外部能力 - 电话集成: SIP 协议支持,可直接对接传统电话网络实现呼入/呼出
- 实时 vs 管道: 明确对比 OpenAI Realtime API(端到端语音,高成本)与分阶段流水线(可控、低成本)的适用场景
部署与成本
- LiveKit Cloud: 一键 CLI 部署,适合快速验证与中小规模
- 自托管: Docker 单机运行,端口 7880-7882,适合高容量或数据合规场景
- 成本预估:100 小时/月约 $150-250,生产级 B2B 约 $300-500
显著优点
- 生产级成熟度: LiveKit 作为开源 WebRTC 基础设施,在音视频传输、网络自适应、断线重连等方面积累深厚,非纯语音 SDK 可比
- 架构灵活性: 流水线模式允许「最佳组合」策略——例如 Deepgram Nova-3(性价比 STT)+ GPT-4.1 mini(低延迟 LLM)+ Cartesia Sonic-3(低延迟 TTS),而非绑定单一厂商
- 中断与轮次管理: 内置
turn_detection与user_speech_started事件,解决语音对话中抢话、停顿检测等复杂交互问题 - 生态完整: 覆盖从原型(Playground)→ 开发(Examples)→ 生产(Cloud/Self-hosted)→ 电话(SIP)的全链路
- 代码即文档: 示例代码可直接运行,最小 Agent 仅 15 行 Python,学习曲线平缓
潜在缺点与局限性
- 多供应商协调成本: 流水线模式虽灵活,但需分别管理 Deepgram、OpenAI、Cartesia 等 API 密钥与配额,监控维度分散
- 延迟堆叠: STT→LLM→TTS 三阶段延迟累加,即使各组件优化,端到端延迟通常高于 OpenAI Realtime API 的语音直连模式
- T3 来源风险: Skill 由社区(openclaw)维护,非 LiveKit 官方直接发布,版本同步与长期维护存在不确定性
- 自托管复杂度: 生产级自部署需考虑信令服务器高可用、TURN 服务部署、媒体服务器集群等,非简单 Docker 单机能承载
- 中文生态相对薄弱: 示例与文档以英文场景为主,中文 ASR、TTS 需额外适配(如替换为阿里云、科大讯飞等)
适合人群
- AI 应用开发者: 需快速将 LLM 能力产品化为语音交互形态
- 企业 IT 架构师: 评估云 vs 私有化部署方案,关注数据主权
- 实时音视频工程师: 已有 WebRTC 经验,需扩展 AI 对话能力
- Startup 技术负责人: 验证语音 Agent MVP,需控制初期成本
常规风险
| 风险类别 | 说明 | 缓解建议 |
|---|---|---|
| 密钥泄露 | 示例代码直接读取环境变量,易误提交至 Git | 使用密钥管理服务,.env 加入 .gitignore |
| 供应商锁定 | 虽支持多供应商切换,但事件抽象层仍绑定 LiveKit 生态 | 核心逻辑与 LiveKit SDK 解耦,便于未来迁移 |
| 成本失控 | 语音按分钟计费,高频场景费用累积快 | 设置用量告警,评估自托管临界点 |
| 合规盲区 | 语音数据可能含 PII,跨境传输需关注 GDPR/个保法 | 选择区域化部署,启用端到端加密 |
技能正文
name: livekit description: Build voice AI agents with LiveKit. Use when developing realtime voice applications, voice agent pipelines (STT-LLM-TTS), WebRTC communication, or deploying conversational AI. Covers LiveKit Agents SDK, provider selection (Deepgram, OpenAI, ElevenLabs, Cartesia), cloud vs self-hosted deployment.
LiveKit Voice AI Skill
Build production voice agents with LiveKit's open-source platform.
Quick Start
# Install SDK
pip install livekit-agents livekit-plugins-openai livekit-plugins-deepgram livekit-plugins-cartesia
# Or Node.js
npm install @livekit/agents @livekit/agents-plugin-openai
Minimal Agent (Python)
from livekit.agents import AgentSession, JobContext, WorkerOptions, cli
from livekit.plugins import deepgram, openai, cartesia
async def entrypoint(ctx: JobContext):
await ctx.connect()
session = AgentSession(
stt=deepgram.STT(),
llm=openai.LLM(model="gpt-4.1-mini"),
tts=cartesia.TTS(),
)
session.start(ctx.room)
await session.say("Hello! How can I help?")
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Provider Selection
| Component | Budget | Quality | Low Latency |
|---|---|---|---|
| STT | Deepgram Nova-3 | AssemblyAI | Deepgram Keychain |
| LLM | GPT-4.1 mini | Claude Sonnet | GPT-4.1 mini |
| TTS | Cartesia Sonic-3 | ElevenLabs | Cartesia Sonic-3 |
Voice Pipeline vs Realtime
STT-LLM-TTS Pipeline:
- More control, mix providers
- Generally cheaper
- Easier to debug
OpenAI Realtime API:
- Speech-to-speech, more expressive
- Higher cost (~$0.10/min)
- Less control
Environment Variables
LIVEKIT_URL=wss://your-app.livekit.cloud
LIVEKIT_API_KEY=your-api-key
LIVEKIT_API_SECRET=your-api-secret
# Provider keys (if not using LiveKit Inference)
OPENAI_API_KEY=
DEEPGRAM_API_KEY=
CARTESIA_API_KEY=
ELEVENLABS_API_KEY=
Tool Use
from livekit.agents import function_tool
@function_tool()
async def get_weather(location: str) -> str:
"""Get current weather for a location."""
# Your implementation
return f"Weather in {location}: 72°F, sunny"
session = AgentSession(
stt=deepgram.STT(),
llm=openai.LLM(),
tts=cartesia.TTS(),
tools=[get_weather],
)
Telephony (SIP)
from livekit import api
# Outbound call
await lk_api.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id="trunk-id",
sip_call_to="+15551234567",
room_name="my-room",
)
)
Deployment
LiveKit Cloud: livekit-server-cli deploy --project my-project
Self-hosted:
docker run -d \
-p 7880:7880 -p 7881:7881 -p 7882:7882/udp \
-e LIVEKIT_KEYS="api-key: api-secret" \
livekit/livekit-server
Cost Estimates
| Scenario | Monthly Cost |
|---|---|
| Dev/testing | Free tier |
| 100 hrs/mo voice | ~$150-250 |
| Production B2B | ~$300-500 |
| High volume | Self-host |
Common Patterns
Turn Detection
session = AgentSession(
turn_detection=openai.TurnDetection(
threshold=0.5,
silence_duration_ms=500,
),
...
)
Interruption Handling
@session.on("user_speech_started")
async def handle_interruption():
session.stop_speaking()
Multi-Agent Handoff
await session.transfer_to(specialist_agent)
References
- Docs: https://docs.livekit.io/agents/
- Examples: https://github.com/livekit/agents/tree/main/examples
- Playground: https://agents-playground.livekit.io
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。