AI 语音助手:使用 Whisper 和 Claude 打造你自己的语音助手

语音控制为 AI 助手带来了全新的维度。无需打字,你只需自然地说话,就能听到回应——无需动手、无需看屏幕,非常适合在做饭、开车,或者只是想要更自然的交互方式时使用。
使用 Whisper(语音转文字)和 Claude(智能处理)构建自定义语音助手,可以让你兼得两者之长:业界领先的语音识别,加上先进的 AI 理解能力。
本指南将向你展示如何构建它,无论是在本地运行还是在云端运行。
为什么要打造自己的语音 AI?
自定义语音助手提供了商业产品无法比拟的能力
与 Alexa/Siri/Google 相比:
- 回应更智能(Claude 更聪明)
- 无需唤醒词(如果你愿意)
- 可选择完全隐私(完全在本地运行)
- 自定义个性和功能
- 可与你的特定系统集成
权衡取舍:
- 比商业产品需要更多的设置
- 延迟可能更高
- 没有内置的硬件生态系统
- 需要维护
对于更看重智能而非便利性的用户来说,自定义语音助手表现出色。
架构概览
语音 AI 各组件如何协同工作
核心组件:
- 麦克风输入 - 捕获你的声音
- Whisper (STT) - 将语音转换为文字
- Claude - 处理请求并生成回应
- Piper (TTS) - 将文字转换为语音
- 扬声器输出 - 播放回应
流程:
你说话 → 麦克风 → Whisper → 文本
文本 → Claude → 回应文本
回应 → Piper → 音频 → 扬声器
每个组件都可以根据你的偏好在本地或云端运行。
第一步:使用 Whisper 设置语音转文字
OpenAI's Whisper 提供最先进的语音识别
方案 A:本地 Whisper(注重隐私)
pip install openai-whisper
pip install faster-whisper
创建转录脚本:
from faster_whisper import WhisperModel
model = WhisperModel("base", device="cpu") # or "cuda" for GPU
def transcribe(audio_file):
segments, info = model.transcribe(audio_file)
return " ".join([segment.text for segment in segments])
方案 B:云端 Whisper(设置更简单)
from openai import OpenAI
client = OpenAI()
def transcribe(audio_file):
with open(audio_file, "rb") as f:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=f
)
return transcript.text
模型大小的权衡:
| 模型 | 大小 | 速度 | 准确度 |
|---|---|---|---|
| tiny | 75MB | 快 | 良好 |
| base | 140MB | 中等 | 较好 |
| small | 460MB | 较慢 | 很好 |
| medium | 1.5GB | 慢 | 优秀 |
| large | 2.9GB | 最慢 | 最佳 |
对于实时使用, base 或 small 能提供良好的平衡。
第二步:添加 Claude 以实现智能功能
Claude's API 提供智能层
将 Whisper 的输出连接到 Claude:
from anthropic import Anthropic
client = Anthropic()
conversation = []
def process_voice_input(transcript):
conversation.append({
"role": "user",
"content": transcript
})
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=500, # Keep responses concise for voice
system="""You are a voice assistant. Keep responses brief
and conversational. Avoid markdown formatting.""",
messages=conversation
)
assistant_message = response.content[0].text
conversation.append({
"role": "assistant",
"content": assistant_message
})
return assistant_message
语音优化提示词:
- 请求简短、适合口语表达的回复
- 避免使用项目符号和格式
- 使用自然的对话式语言
- 在较长的回复之前先做出确认
第 3 步:使用 Piper 设置文字转语音
Piper 提供高质量的本地文字转语音功能
安装 Piper:
pip install piper-tts
下载语音模型:
wget https://huggingface.co/rhasspy/piper-voices/resolve/v1.0.0/en/en_US/lessac/medium/en_US-lessac-medium.onnx
wget https://huggingface.co/rhasspy/piper-voices/resolve/v1.0.0/en/en_US/lessac/medium/en_US-lessac-medium.onnx.json
生成语音:
import subprocess
def speak(text, output_file="response.wav"):
subprocess.run([
"piper",
"--model", "en_US-lessac-medium.onnx",
"--output_file", output_file
], input=text.encode())
# Play the audio
subprocess.run(["aplay", output_file])
语音选项: Piper 拥有多种语言和风格的语音。您可以在以下页面试听: Piper 示例页面.
第 4 步:构建完整的流程
将所有组件整合为一个可运行的助手
完整的语音助手:
import sounddevice as sd
import numpy as np
from faster_whisper import WhisperModel
from anthropic import Anthropic
import subprocess
import tempfile
import wave
whisper = WhisperModel("base", device="cpu")
claude = Anthropic()
conversation = []
def record_audio(duration=5, sample_rate=16000):
"""Record audio from microphone."""
print("Listening...")
audio = sd.rec(
int(duration * sample_rate),
samplerate=sample_rate,
channels=1,
dtype=np.int16
)
sd.wait()
return audio, sample_rate
def save_audio(audio, sample_rate, filename):
"""Save audio to WAV file."""
with wave.open(filename, 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(sample_rate)
f.writeframes(audio.tobytes())
def voice_assistant_loop():
"""Main voice assistant loop."""
print("Voice assistant ready. Press Ctrl+C to exit.")
while True:
# Record
audio, sr = record_audio()
# Transcribe
with tempfile.NamedTemporaryFile(suffix=".wav") as f:
save_audio(audio, sr, f.name)
segments, _ = whisper.transcribe(f.name)
text = " ".join([s.text for s in segments])
if not text.strip():
continue
print(f"You: {text}")
# Process with Claude
response = process_voice_input(text)
print(f"Assistant: {response}")
# Speak response
speak(response)
if __name__ == "__main__":
voice_assistant_loop()
唤醒词检测(可选)
通过唤醒词检测实现免手动激活
若需要持续监听,可以使用以下库添加唤醒词检测: Porcupine 或 OpenWakeWord:
使用 Porcupine(商业产品,高质量):
import pvporcupine
porcupine = pvporcupine.create(
access_key="YOUR_ACCESS_KEY",
keywords=["jarvis", "hey siri", "alexa"] # Or custom
)
keyword_index = porcupine.process(audio_frame)
if keyword_index >= 0:
# Wake word detected, start recording
record_and_process()
使用 OpenWakeWord(开源):
from openwakeword import Model
model = Model()
prediction = model.predict(audio_frame)
if prediction["hey_jarvis"] > 0.5:
record_and_process()
结论
您的定制语音助手所提供的智能是商业产品无法比拟的
结合 Whisper 和 Claude 的定制语音助手,能够提供智能、私密的语音控制,这是商业语音助手无法比拟的。
这套方案的搭建比直接购买 Echo 需要投入更多精力,但您可以完全掌控隐私、个性和功能。
从简单开始:
- 先让基本的 语音转文字 → Claude → 文字转语音 流程运行起来
- 熟悉之后再添加唤醒词
- 与您的其他系统集成
- 优化延迟
继续探索:
语音只是通往 AI 的另一种界面——按你的方式来构建。
常见问题
关于语音 AI 助手的常见问题
延迟如何?
本地 Whisper(base):转录约 1-2 秒。Claude:响应约 1-2 秒。TTS:约 0.5 秒。端到端总计:3-5 秒,对大多数用途来说是可以接受的。
可以完全在本地运行吗?
可以。使用本地 Whisper、搭配 Llama 3 的 Ollama 以及 Piper。质量会低于 Claude,但完全私密。
我需要什么硬件?
本地处理:8GB 以上内存、现代 CPU。GPU 对 Whisper 有显著帮助。云端方案:任何带麦克风的电脑即可。
可以与智能家居设备配合使用吗?
可以,与 Home Assistant 集成。语音命令通过 HA API 触发操作。
为什么不直接用 Alexa/Siri?
如果它们能满足你的需求,就使用它们。自定义助手的优势在于:更好的 AI(Claude)、完全的隐私以及自定义集成。代价是设置较为复杂。
更多文章
OpenClaw AWS 终极设置指南

在 AWS 上设置 OpenClaw 的权威指南。包含 Spot 实例配置、成本优化和分步说明。
在 OpenClaw 中使用工具链构建 AI 工作流
掌握工具和函数调用的链式组合技巧,构建强大的多步骤 AI 自动化工作流——从数据提取到内容生成与部署。
自托管 AI 助手成本优化指南:低预算运行 Claude
降低自托管 AI 助手 API 成本的实用策略——智能模型路由、缓存、批处理以及 OpenClaw 专属优化,让你以更低成本运行 Claude。