Patter Test Mode实战:免电话、免STT/TTS,在终端快速调试语音AI Agent
Patter Test Mode实战:免电话、免STT/TTS,在终端快速调试语音AI Agent
Patter Test Mode 是开源语音 AI SDK Patter 内置的终端调试模式:不需要真实电话线路,不需要 STT/TTS 服务,纯文本对话即可模拟完整通话,让语音 AI Agent 的开发调试速度成倍提升。本文带你用最短路径上手 Test Mode。
一、什么是 Patter Test Mode?💡
Patter 是一个 MIT 协议的开源语音 AI SDK(Vapi/Retell 的自托管替代方案),支持 Python 和 TypeScript,可对接 Twilio、Telnyx、Plivo 三大运营商。
调试语音 Agent 时,传统流程很痛苦:拨真电话 → 等 STT 识别 → 等 LLM 响应 → 等 TTS 合成,一轮交互耗时数秒,还要消耗真金白银的通话费用。
Test Mode 的解法是用纯文本 REPL 模拟电话会话:
- 📵 免电话:不经过运营商,模拟 Caller/Callee 号码
- 🎙️ 免 STT/TTS:直接键入文本,直接输出文本
- ⚡ 完整生命周期:
on_call_start/on_call_end等回调与生产环境serve()完全一致
核心实现位于 test_mode.py,入口是 Patter 客户端的 test() 方法,见 client.py。
二、3 步快速上手 Test Mode
第 1 步:安装 SDK
pip install getpatter
第 2 步:定义一个 Agent
import asyncio
from getpatter import Patter, Twilio, OpenAIRealtime
phone = Patter(carrier=Twilio(), phone_number="+15550001234")
agent = phone.agent(
engine=OpenAIRealtime(), # 从环境变量读取 OPENAI_API_KEY
system_prompt="You are a helpful customer service agent for Acme Corp.",
first_message="Hello! Welcome to Acme Corp. How can I help you today?",
)
第 3 步:一行启动
asyncio.run(phone.test(agent))
启动后即进入交互式终端会话:
============================================================
PATTER TEST MODE
============================================================
Agent: gpt-realtime-mini / alloy
Provider: openai_realtime
Call ID: test_a1b2c3d4e5f6
Caller: +15550000001 → Callee: +15550000002
------------------------------------------------------------
Commands: /quit /transfer <number> /hangup /history
============================================================
Agent: Hello! Welcome to Acme Corp. How can I help you today?
You: I need to return an item
Agent: I'd be happy to help with your return...
无需部署隧道、无需公网地址,改一行 prompt 就能立刻重新测试。
三、终端命令清单:随时查看历史与模拟挂断
Test Mode 提供 4 个快捷命令,覆盖测试中最高频的操作:
| 命令 | 作用 |
|---|---|
/quit |
结束测试会话 |
/hangup |
模拟你主动挂断 |
/transfer <number> |
模拟转接到指定号码 |
/history |
打印完整对话历史 |
💡 调试技巧:多轮对话后输入 /history,可以一次性核对 Agent 是否记住了上下文、有没有答非所问,比翻日志快得多。
四、与 on_message 处理器配合:和生产行为一致
如果你用自定义消息处理器而非内置 LLM 循环,Test Mode 会以与 serve() 完全相同的方式调用它,包括自动注入 call_control(转接、挂断控制):
async def on_message(data, call_control):
user_text = data["text"]
history = data["history"]
if "cancel" in user_text.lower():
await call_control.transfer("+15550001111")
return "Let me transfer you to our cancellation team."
return f"You said: {user_text}"
asyncio.run(phone.test(agent, on_message=on_message))
只要处理器签名接受两个参数,call_control 就会被自动注入——这意味着你无需为"测试环境"编写任何分支代码。
五、零配置调试:内置 LLM Loop 与 Tool Calling
不提供 on_message 且环境中存在 OPENAI_API_KEY 时,Test Mode 自动启用内置 LLM 循环,并支持 Tool Calling——工具通过配置的 webhook 执行,与生产完全一致。餐厅订位场景示例:
agent = phone.agent(
system_prompt="You are a restaurant booking assistant.",
tools=[
{
"name": "check_availability",
"description": "Check table availability",
"parameters": {
"type": "object",
"properties": {
"date": {"type": "string"},
"party_size": {"type": "integer"},
},
},
"webhook_url": "https://api.example.com/availability",
}
],
)
asyncio.run(phone.test(agent))
在终端输入"帮我查一下周六晚上 4 人的位置",即可观察 Agent 是否正确触发 check_availability 工具。
六、事件回调:复现生产通话的完整生命周期
Test Mode 会触发与 serve() 相同的 on_call_start / on_call_end 回调,结束时的 transcript 字段包含完整对话记录,方便做自动化断言:
async def on_call_end(event):
print(f"Test call ended: {event['call_id']}")
print(f"Transcript: {len(event['transcript'])} messages")
asyncio.run(phone.test(
agent,
on_call_start=on_call_start,
on_call_end=on_call_end,
))
七、TypeScript 版本:同等的调试体验 🚀
TypeScript SDK 提供完全对等的 Test Mode,API 一一对应(onMessage / onCallStart / onCallEnd):
npm install getpatter
import { Patter, Twilio, OpenAIRealtime } from "getpatter";
const phone = new Patter({ carrier: new Twilio(), phoneNumber: "+15550001234" });
const agent = phone.agent({
engine: new OpenAIRealtime(),
systemPrompt: "You are a friendly receptionist for Acme Corp.",
firstMessage: "Hello! How can I help?",
});
await phone.test({ agent });
官方文档分别位于 python-sdk/test-mode.mdx 与 typescript-sdk/test-mode.mdx,行为一致性由测试用例 test-mode.test.ts 保障。
八、适用边界:什么时候该用 Test Mode?
✅ 适合
- 迭代 system prompt、开场白(
first_message) - 验证多轮上下文、工具调用链路
- 调试转接/挂断等
call_control逻辑 - CI 中快速回归(配合
on_call_end拿到 transcript)
⚠️ 不适合
- 验证 VAD、打断(barge-in)、TTS 音色与延迟——这些只有真实语音链路才能暴露
- 文档明确标注 Test Mode 仅面向开发:除内置 LLM 循环所需的 OpenAI 外,不依赖任何外部 API
最佳实践:先用 Test Mode 把对话逻辑调到满意,再切换到 serve() 真机验证语音链路,两者代码零改动。
总结
Patter Test Mode 把"改一行 prompt → 拨真电话验证"的低效循环,压缩成"改一行 prompt → 终端回车验证",免电话、免 STT/TTS、免部署,是语音 AI Agent 开发中最值得优先使用的调试工具。配合 /history、事件回调与 Tool Calling 支持,绝大多数 Agent 逻辑问题都能在终端内闭环解决。
