Bolna Python API快速上手:50行代码写出第一个语音AI助手(文本/语音双模式)
Bolna Python API快速上手:50行代码写出第一个语音AI助手(文本/语音双模式)
Bolna 是一个端到端的开源语音 AI 编排框架,让你用 Python API 在几十行代码内搭建出能"听、想、说"的语音 AI 助手。它把 ASR(语音识别)、LLM(大模型)、TTS(语音合成)三大环节串成一条流式管道,支持 Deepgram、OpenAI、ElevenLabs 等主流服务商,同时提供纯文本模式与语音模式两种玩法,是构建电话语音 Agent、客服机器人、AI 外呼系统的理想起点。
🧩 30 秒认识 Bolna 的架构
Bolna 的核心思想是"管道"(pipeline):
用户语音 → Transcriber(ASR) → LLM Agent → Synthesizer(TTS) → 语音回复
- 入口类是 Assistant,通过
add_task()组装任务,再调用execute()以异步生成器流式输出结果 - 所有配置模型(
Transcriber、Synthesizer、LlmAgent等)都定义在 bolna/models.py - 服务商映射表集中在 bolna/providers.py,方便快速了解支持范围
官方示例代码就在仓库里,跟着下面三步走即可跑通 👇
⚙️ 一键安装与环境准备
1. 克隆并安装 Bolna
git clone https://gitcode.com/gh_mirrors/bol/bolna
cd bolna
pip install -r requirements.txt
pip install .
依赖清单见 requirements.txt,要求 Python ≥ 3.10(见 pyproject.toml)。
2. 配置 API Key(按所用服务商选填)
export OPENAI_API_KEY=... # LLM:必需
export DEEPGRAM_AUTH_TOKEN=... # ASR:语音模式需要
export ELEVENLABS_API_KEY=... # TTS:语音模式需要
纯文本模式只需要 OPENAI_API_KEY 一个变量即可运行。
💬 模式一:文本模式(最简入门,零音频依赖)
不想碰音频?开启 enable_textual_input=True,跳过 Transcriber 和 Synthesizer,得到一个流式文本对话 Agent。完整源码见 examples/text_only_assistant.py:
import asyncio
from bolna.assistant import Assistant
from bolna.models import LlmAgent, SimpleLlmAgent
async def main():
assistant = Assistant(name="text_only_agent")
llm_agent = LlmAgent(
agent_type="simple_llm_agent",
agent_flow_type="streaming",
llm_config=SimpleLlmAgent(
provider="openai",
model="gpt-4o-mini",
temperature=0.2,
),
)
# 纯文本管道:不配置 transcriber/synthesizer
assistant.add_task(
task_type="conversation",
llm_agent=llm_agent,
enable_textual_input=True,
)
async for chunk in assistant.execute():
print(chunk)
if __name__ == "__main__":
asyncio.run(main())
运行:
export OPENAI_API_KEY=...
python examples/text_only_assistant.py
assistant.execute() 是一个异步生成器,每一步都会 yield 一个流式字典,按块处理即可。
🎙️ 模式二:完整语音模式(ASR + LLM + TTS 全链路)
加上 Transcriber(听)和 Synthesizer(说),就得到了真正的语音 AI 助手。完整源码见 examples/simple_assistant.py:
import asyncio
from bolna.assistant import Assistant
from bolna.models import (
Transcriber, Synthesizer,
ElevenLabsConfig, LlmAgent, SimpleLlmAgent,
)
async def main():
assistant = Assistant(name="demo_agent")
# 音频输入(ASR)
transcriber = Transcriber(provider="deepgram", model="nova-2", stream=True, language="en")
# 大脑(LLM)
llm_agent = LlmAgent(
agent_type="simple_llm_agent",
agent_flow_type="streaming",
llm_config=SimpleLlmAgent(provider="openai", model="gpt-4o-mini", temperature=0.3),
)
# 音频输出(TTS)
synthesizer = Synthesizer(
provider="elevenlabs",
provider_config=ElevenLabsConfig(
voice="George", voice_id="JBFqnCBsd6RMkjVDRZzb", model="eleven_turbo_v2_5",
),
stream=True,
audio_format="wav",
)
# 组装管道:transcriber -> llm -> synthesizer
assistant.add_task(
task_type="conversation",
llm_agent=llm_agent,
transcriber=transcriber,
synthesizer=synthesizer,
enable_textual_input=False,
)
async for chunk in assistant.execute():
print(chunk)
if __name__ == "__main__":
asyncio.run(main())
export OPENAI_API_KEY=...
export DEEPGRAM_AUTH_TOKEN=...
export ELEVENLABS_API_KEY=...
python examples/simple_assistant.py
💡 小贴士:Synthesizer 的 stream=True 开启流式合成,LLM 刚说完一句话声音就开始播放,端到端延迟显著降低。
📋 核心参数速查表
| 模型 | 常用参数 | 说明 |
|---|---|---|
Transcriber |
provider、model、language、stream、endpointing |
语音识别;endpointing 控制静音断句阈值(默认 500ms) |
LlmAgent |
agent_type、agent_flow_type、llm_config |
支持 simple_llm_agent,也支持图式 Agent |
SimpleLlmAgent |
provider、model、temperature、max_tokens |
继承自 Llm,支持 LiteLLM 全家桶 |
Synthesizer |
provider、provider_config、stream、audio_format |
语音合成;ElevenLabs 必须同时提供 voice 和 voice_id |
更多字段定义可查阅 bolna/models.py 中对应的 Pydantic 模型。
🧰 支持哪些服务商?
Bolna 通过 bolna/providers.py 做了统一抽象,主流厂商开箱即用:
- ASR(Transcriber):Deepgram、Azure、Google、OpenAI、ElevenLabs、AssemblyAI、Gladia、Sarvam、Soniox 等
- LLM:OpenAI、Azure、Gemini、DeepSeek、Cohere、Anthropic、Groq、Ollama、vLLM 等(基于 LiteLLM 路由)
- TTS(Synthesizer):ElevenLabs、OpenAI、Deepgram、AWS Polly、Azure、Cartesia、Smallest、Gemini 等
- 电话网关(Telephony):Twilio、Plivo、Exotel、VoBiz、SIP Trunk、FreeSWITCH
📞 进阶:接入真实电话系统
想在真机上接到打电话的 AI?使用 local_setup/ 下的 Docker 一键部署(Telephony 服务器 + Bolna 服务器 + ngrok + Redis 四容器),详见 local_setup/README.md:
cd local_setup
chmod +x start.sh
./start.sh
- Twilio 接入示例:local_setup/telephony_server/twilio_api_server.py
- Plivo 接入示例:local_setup/telephony_server/plivo_api_server.py
如果不走 Python SDK,Bolna 还提供 REST 接口管理 Agent(创建/查询/编辑/删除),接口定义见 API.md。
🚀 下一步探索清单
- 多分支对话流:
LlmAgentGraph支持节点/边组成的流程图式对话,实现多轮路由与提取,源码见 bolna/agent_types/graph_agent.py - 知识库 Agent:RAG 检索增强,见 bolna/agent_types/knowledgebase_agent.py
- S2S 实时模式:跳过 ASR+TTS,直连 OpenAI Realtime / Gemini Live,见 bolna/s2s/
- 单元测试参考:tests/ 目录包含 200+ 个覆盖各服务商与边界场景的测试用例,是理解内部机制的宝藏
❓ 常见问题(FAQ)
Q:纯文本模式必须配置 language 吗?
A:不需要。language 是 Transcriber 的音频参数,文本模式根本不走 ASR,只配 LLM 即可。
Q:execute() 的输出长什么样?
A:它是异步生成器,逐块 yield 各任务步骤的结果字典;具体字段随服务商和工具配置变化,建议按流式增量处理,不要等待完整结果。
Q:如何降低语音回复延迟?
A:给 Transcriber 和 Synthesizer 都设置 stream=True,并将 LLM 的 agent_flow_type 设为 streaming,让三个环节并行流水作业。
从上面的教程出发,你可以只改几行参数(换一个 TTS 音色、换一家 LLM)就能快速验证新方案;配合 local_setup 还能一步走向真实电话场景——这正是 Bolna 作为语音 AI 编排框架的魅力所在。