Files
ai-video-fullstack/backend/services/pipecat/transports.py
Xin Wang e25dfd4003 Add support for Xfyun ASR and TTS services in the backend
- Introduce new Xfyun ASR and TTS services, enabling integration with iFlytek's voice recognition and synthesis capabilities.
- Update AssistantConfig model to include interface types for STT and TTS.
- Enhance credential testing to validate Xfyun credentials.
- Modify service factory to create Xfyun services based on configuration.
- Update README with new configuration details for Xfyun integration.
- Add new frontend components for visualizing audio streams and managing user interactions.
2026-06-11 10:51:08 +08:00

53 lines
1.7 KiB
Python

"""Transport 工厂——管线与"输出方式"解耦的关键。
同一条 STT→LLM→TTS 管线,可以挂在不同 transport 上:
- WebRTC:浏览器,低延迟,带 NAT 穿透 -> build_webrtc_transport
- WS: 裸音频流,服务端/话务/自定义客户端,简单 -> build_ws_transport
未来加电话(Twilio/Vonage)只是再加一个 build_xxx_transport + 对应 serializer。
对应 dograh 的 transport_setup.py(WebRTC)+ 各 telephony provider 的 transport.py(WS)。
"""
from fastapi import WebSocket
from pipecat.transports.base_transport import TransportParams
# WebRTC
from pipecat.transports.smallwebrtc.connection import SmallWebRTCConnection
from pipecat.transports.smallwebrtc.transport import SmallWebRTCTransport
# 裸 WS 音频流
from pipecat.transports.websocket.fastapi import (
FastAPIWebsocketTransport,
FastAPIWebsocketParams,
)
from pipecat.serializers.protobuf import ProtobufFrameSerializer
def _base_params() -> dict:
"""两种 transport 共享的音频参数。"""
return dict(
audio_in_enabled=True,
audio_out_enabled=True,
)
def build_webrtc_transport(connection: SmallWebRTCConnection) -> SmallWebRTCTransport:
return SmallWebRTCTransport(
webrtc_connection=connection,
params=TransportParams(**_base_params()),
)
def build_ws_transport(websocket: WebSocket) -> FastAPIWebsocketTransport:
"""裸 WS 输出。序列化用 protobuf(自定义客户端用同款解码);
若对接电话商,把 serializer 换成对应的 TwilioFrameSerializer 等即可。
"""
return FastAPIWebsocketTransport(
websocket=websocket,
params=FastAPIWebsocketParams(
serializer=ProtobufFrameSerializer(),
**_base_params(),
),
)