停止为 Vapi/Retell 付费:在 Python 中运行自己的 AI 语音代理
Source: Dev.to
在没有商业许可证的情况下构建 AI 呼叫代理
如果你是 Python 开发者,可以在自己的机器上启动一个延迟低于 500 ms 的语音代理。本指南介绍 Siphon,一个开源(Apache 2.0)Python 框架,用于将 SIP 中继桥接到大语言模型(LLM)。
前置条件
- Python 3.10 及以上
- Twilio 或 Telnyx SIP 中继
- LiveKit 凭证
- OpenAI API 密钥
第一步:安装与设置
克隆 Siphon 仓库并安装包:
pip install siphon-ai
在项目根目录创建 .env 文件,并填入你的提供商密钥。由于 Siphon 是自行托管的,你需要直接向 OpenAI、LiveKit 等提供商付费——没有中间商费用。
LIVEKIT_URL=wss://your-project.livekit.cloud
LIVEKIT_API_KEY=your_livekit_key
LIVEKIT_API_SECRET=your_livekit_secret
OPENAI_API_KEY=sk-yourkey
DEEPGRAM_API_KEY=yourkey
FROM_NUMBER=+15551234567
SIP_USERNAME=your_sip_user
SIP_PASSWORD=your_sip_pass
第二步:定义代理
Siphon 抽象了复杂的 WebRTC 流程和语音活动检测(VAD)。使用 Siphon 的插件架构来定义你的代理:
from siphon.agent import Agent
from siphon.plugins import openai, cartesia, deepgram
# Define the Agent
agent = Agent(
agent_name="Receptionist",
llm=openai.LLM(),
tts=cartesia.TTS(),
stt=deepgram.STT(),
system_instructions="You are a helpful dental receptionist. Help the user book an appointment."
)
第三步:触发外呼
外呼 SIP 信令非常简洁。如果没有预先配置的中继 ID,可以使用 SIP 凭证触发呼叫;Siphon 会根据需要复用或创建外部中继。
import os
from dotenv import load_dotenv
from siphon.telephony.outbound import Call
load_dotenv()
call = Call(
agent_name="Receptionist",
sip_trunk_setup={
"name": "telnyx-primary",
"sip_address": "sip.telnyx.com",
"sip_number": os.getenv("FROM_NUMBER"),
"sip_username": os.getenv("SIP_USERNAME"),
"sip_password": os.getenv("SIP_PASSWORD"),
},
number_to_call="+15550200",
)
# Execute the asynchronous dial and bridge to the LiveKit WebRTC room
call.start()
第四步:处理状态与中断
处理中断(抢话)是语音 AI 中最难的环节之一。Siphon 利用 LiveKit 的 WebRTC 引擎,在检测到人声时即时停止 TTS 输出,从而实现自然、低延迟的对话,全部托管在你的基础设施上。
更多资源
- GitHub 仓库:
- 文档:
如果 Siphon 为你省下了费用,请考虑给仓库点星!