Skip to content

Open full-duplex voice models that hear your tone and speak with one. One GPU with vLLM-Omni, or fully offline on a Mac with MLX. Zero-shot voice cloning, OpenAI Realtime / GPT-Live compatible, free hosted API trial. Apache-2.0.

7.1 KB
Raw Blame

在 LiveKit / Pipecat 中使用 Sophon-Realtime

把 Sophon-Realtime 跑在你自己的 GPU 上,作为 LiveKit Agents 或 Pipecat 智能体的"嗓子"。两个框架都支持两种模式:

模式 协议 适合 LiveKit Pipecat
Realtime OpenAI Realtime(/v1/realtime) 想要一个像 gpt-realtime 那样能调函数的语音对语音模型 sophon.RealtimeModel(插件) OpenAIRealtimeLLMService(base_url=...)
Live OpenAI GPT-Live(/v1/live/sessions) 已经按 GPT-Live 客户端委派写好了智能体(前台说话,后台办事) sophon.LiveModel OpenAILiveLLMService(base_url=...)

智能体代码不用改,只换地址。话轮判断在服务端完成(模型边说边听,自己判断你说完没有),所以不用再加 VAD 或话轮检测。

LiveKit / Pipecat 智能体 ──Realtime 或 GPT-Live──▶ Sophon Live 网关(:8780)──▶ Sophon 引擎(:8000,一张卡)

1. 启动模型和网关(你的 GPU 服务器)

# 引擎:32B 版本一张 80 GB 卡
pip install "sophon-realtime[trt]" && sophon-realtime serve --port 8000

# 网关:两种协议在同一个端口
git clone https://github.com/adventists-ai/sophon-realtime && cd sophon-realtime/live
BUILD=tag PORT=8000 LIVE_PORT=8780 bash start_live.sh

网关会加载一个小的语音识别模型用于用户转写(约 2 GB 显存;国内请先从魔搭下载,见 live/README_CN.md)。其他版本(Flash、4B)启动方式相同,见模型列表。把 8780 端口开放给运行智能体的机器。需要鉴权时,在网关上设置 SOPHON_LIVE_REALTIME_KEYS_FILE(Realtime)和 SOPHON_LIVE_API_KEYS(Live)。

2a. LiveKit Agents

pip install livekit-plugins-sophon        # 会一起装上 livekit-agents 和 OpenAI 插件

Realtime 模式

from livekit import agents
from livekit.agents import Agent, AgentSession, function_tool
from livekit.plugins import sophon


class Assistant(Agent):
    def __init__(self):
        super().__init__(instructions="你是小诺,一个友好的语音助手,回答简短。")

    @function_tool
    async def get_weather(self, city: str) -> str:
        """查询某个城市现在的天气。"""
        return f"{city}:18 度,小雨"


async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()
    session = AgentSession(llm=sophon.RealtimeModel(base_url="http://你的服务器:8780/v1"))
    await session.start(room=ctx.room, agent=Assistant())


if __name__ == "__main__":
    agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))

Live 模式(客户端委派)

import asyncio


class Receptionist(Agent):
    def __init__(self):
        super().__init__(instructions="你是小诺,一个友好的前台,回答简短。")

    async def on_enter(self):
        self.duplex_session.on("delegation_created", self._on_delegation)

    def _on_delegation(self, delegation):
        async def work():
            # 要办的事就是用户刚说的话;交给你自己的后台(大模型、工具、数据库……)
            answer = await my_backend(delegation.pending_transcript)
            self.duplex_session.append_commentary(answer, delegation_id=delegation.id)
        asyncio.create_task(work())


async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()
    session = AgentSession(llm=sophon.LiveModel(base_url="http://你的服务器:8780/v1"))
    await session.start(room=ctx.room, agent=Receptionist())

模型先说一句短话("我查一下"),把这一轮交给你的代码,再用自己的话把你的结果说出来。

原来用 OpenAI GPT-Live?openai.realtime.GPTLiveModel(base_url="http://你的服务器:8780/v1", delegation="client") 也可以直接用,sophon.LiveModel 只是把默认值填好了。

2b. Pipecat

pip install "pipecat-ai[openai]"

Realtime 模式

from pipecat.services.openai.realtime.llm import OpenAIRealtimeLLMService

llm = OpenAIRealtimeLLMService(
    api_key="any",                                   # 网关设了 key 就填 key
    base_url="ws://你的服务器:8780/v1/realtime",
    settings=OpenAIRealtimeLLMService.Settings(
        model="sophon-realtime",
        system_instruction="你是小诺,一个友好的语音助手,回答简短。",
    ),
)
pipeline = Pipeline([transport.input(), llm, transport.output()])   # 不要 VAD,不要话轮检测

Live 模式

from pipecat.services.openai.live.llm import OpenAILiveLLMService

llm = OpenAILiveLLMService(api_key="any", base_url="ws://你的服务器:8780/v1/live/sessions")
# 和 OpenAI GPT-Live 一样,会话在第一个 LLMContextFrame(你的系统提示)到达时开始

完整可运行的 Pipecat 机器人(浏览器 WebRTC)见 live/examples/pipecat_bot.py,LiveKit 示例见 live/examples/livekit_agent.py。

3. 实测

从用户说完到客户端听到第一声回复的时间。每个模型一张 H100,一次一个对话,网关和引擎在同一台机器;每格是 3 条录音问题(中英文)× 3 次;livekit-agents 1.9.0、pipecat-ai 1.17.0。中位数(P90),单位秒。

模型 直连 WebSocket LiveKit · Realtime Pipecat · Realtime LiveKit · Live Pipecat · Live
32B Tag 0.47(0.56) 0.55(0.63) 0.50(0.52) 0.54(0.61) 0.51(0.52)
4B Flash 0.46(0.59) 0.47(0.74) 0.47(0.67) 0.48(0.64) 0.50(1.52)
  • 框架几乎不增加延迟:LiveKit 约多 0.05 秒,Pipecat 基本持平。
  • 第 4 条录音(结尾带笑声的英文句子)在 32B Tag 上所有客户端都约 2.4 秒(模型在等确认你说完了),4B Flash 约 0.55–0.8 秒,没有计入上表。
  • 另外测了:打断(32B Tag 在你开口约 0.26 秒后停声,你说完约 0.13 秒后接话)、通过 @function_tool 调函数(32B;你说完约 0.9 秒发出调用)、Live 模式下用 append_commentary 回答委派。
  • 测试脚本:live/bench/frameworks/。

4. 建议和已知限制

  • **在指令里给智能体起名字、写人设。**不写的话,模型可能自己编一个名字。
  • Live 模式下回复语言跟着指令走:指令用用户说的语言写,或者写上"用用户的语言回答"。
  • 函数调用:32B 版本(Tag、Flash、E2E)支持;4B 版本不调工具,需要工具请用 Live 模式接你自己的后台,或者用 32B。
  • 转写里可能出现 [laughter] 这类声音标签,它们是被"演"出来的,不会被念出来;要展示文字时可以过滤掉。
  • 结尾带笑声的句子,32B Tag 版可能要约 2.4 秒才回答(它在等确认你说完了),我们在改。
  • 一个网关对应一台引擎;要更大并发,在负载均衡后面放多个网关。