- Introduce new fields for voice, speed, and language in the AssistantConfig and ProviderCredential models to support TTS and ASR configurations. - Update the database schema and seeding script to accommodate the new fields, ensuring backward compatibility. - Implement credential testing endpoints and logic to validate OpenAI-compatible credentials, enhancing user experience and reliability. - Modify frontend components to include new fields in the credential forms and improve connection testing feedback. - Refactor related services and API interactions to support the new credential testing feature.
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
"""创建 STT / LLM / TTS 服务。
|
|
|
|
对应 dograh 的 service_factory.py,但只留一套国产栈(OpenAI 兼容),
|
|
按 provider 扩展时在这里加分支即可——这是未来接更多模型的唯一入口。
|
|
"""
|
|
|
|
import config
|
|
from loguru import logger
|
|
from models import AssistantConfig
|
|
|
|
from pipecat.services.openai.llm import OpenAILLMService
|
|
from pipecat.services.openai.stt import OpenAISTTService
|
|
from pipecat.services.openai.tts import OpenAITTSService
|
|
from pipecat.transcriptions.language import Language
|
|
|
|
|
|
def _language(value: str) -> Language | None:
|
|
if not value:
|
|
return None
|
|
try:
|
|
return Language(value)
|
|
except ValueError:
|
|
logger.warning(f"忽略不支持的 ASR language: {value}")
|
|
return None
|
|
|
|
|
|
def create_stt(cfg: AssistantConfig):
|
|
"""SenseVoice / FunASR 等,走 OpenAI 兼容的 /v1/audio/transcriptions。
|
|
|
|
连接信息优先用 cfg(由 config_resolver 从 DB 注入),为空回退 .env 默认。
|
|
"""
|
|
return OpenAISTTService(
|
|
api_key=cfg.stt_api_key or config.STT_API_KEY,
|
|
base_url=cfg.stt_base_url or config.STT_BASE_URL,
|
|
model=cfg.asr or config.STT_MODEL,
|
|
language=_language(cfg.stt_language),
|
|
)
|
|
|
|
|
|
def create_llm(cfg: AssistantConfig):
|
|
"""DeepSeek 等,走 OpenAI 兼容的 /v1/chat/completions。"""
|
|
return OpenAILLMService(
|
|
api_key=cfg.llm_api_key or config.LLM_API_KEY,
|
|
base_url=cfg.llm_base_url or config.LLM_BASE_URL,
|
|
model=cfg.model or config.LLM_MODEL,
|
|
)
|
|
|
|
|
|
def create_tts(cfg: AssistantConfig):
|
|
"""CosyVoice 等,走 OpenAI 兼容的 /v1/audio/speech。"""
|
|
return OpenAITTSService(
|
|
api_key=cfg.tts_api_key or config.TTS_API_KEY,
|
|
base_url=cfg.tts_base_url or config.TTS_BASE_URL,
|
|
model=config.TTS_MODEL,
|
|
voice=cfg.voice or config.TTS_VOICE,
|
|
speed=cfg.tts_speed,
|
|
)
|
|
|
|
|
|
def create_services(cfg: AssistantConfig):
|
|
logger.info(
|
|
f"创建服务: stt={cfg.asr or config.STT_MODEL} "
|
|
f"llm={cfg.model or config.LLM_MODEL} "
|
|
f"tts={cfg.voice or config.TTS_VOICE}"
|
|
)
|
|
return create_stt(cfg), create_llm(cfg), create_tts(cfg)
|