Refactor backend configuration management and update environment settings

- Replace the `config.py` module with a new `settings.py` to streamline environment variable management, focusing on database, CORS, and TURN settings.
- Update references throughout the backend codebase to use the new `settings` module instead of the deprecated `config`.
- Modify the `.env.example` file to reflect the new configuration approach, indicating that model provider credentials should be maintained separately.
- Enhance the `AssistantConfig` model to clarify the source of runtime connection information, ensuring it is injected from model resources rather than relying on defaults from the environment.
- Introduce new user scripts for audio and video management in the Tampermonkey environment, enhancing WebRTC capabilities.
This commit is contained in:
Xin Wang
2026-07-09 21:40:29 +08:00
parent 195579c5b7
commit 35d24acf40
19 changed files with 106 additions and 139 deletions

View File

@@ -11,7 +11,6 @@ import base64
from io import BytesIO
from uuid import uuid4
import config
from loguru import logger
from models import AssistantConfig
from openai import AsyncOpenAI
@@ -77,6 +76,12 @@ VISION_ANALYSIS_SYSTEM_PROMPT = (
)
def _require(value: str, label: str) -> str:
if value:
return value
raise ValueError(f"缺少模型资源配置: {label}")
def _vision_uses_main_llm(cfg: AssistantConfig) -> bool:
"""模型自己支持图片时,沿用 Pipecat 的同上下文视觉工具路径。"""
return not cfg.vision_model_resource_id and cfg.llm_support_image_input
@@ -107,12 +112,12 @@ async def _analyze_image_with_vision_model(
extra_body = cfg.vision_llm_values.get("extraBody")
extra = {"extra_body": extra_body} if isinstance(extra_body, dict) else {}
client = AsyncOpenAI(
api_key=cfg.vision_llm_api_key or config.LLM_API_KEY,
base_url=cfg.vision_llm_base_url or config.LLM_BASE_URL,
api_key=_require(cfg.vision_llm_api_key, "Vision LLM apiKey"),
base_url=_require(cfg.vision_llm_base_url, "Vision LLM apiUrl"),
)
try:
response = await client.chat.completions.create(
model=cfg.vision_model or config.LLM_MODEL,
model=_require(cfg.vision_model, "Vision LLM modelId"),
messages=[
{"role": "system", "content": VISION_ANALYSIS_SYSTEM_PROMPT},
{
@@ -665,9 +670,9 @@ async def run_pipeline(
target = await engine.route(
wf_state["current"],
history,
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,
api_key=_require(cfg.llm_api_key, "LLM apiKey"),
base_url=_require(cfg.llm_base_url, "LLM apiUrl"),
model=_require(cfg.model, "LLM modelId"),
)
if target and target != wf_state["current"]:
logger.info(f"文本兜底触发转移 → {engine.name(target)}")

View File

@@ -4,7 +4,6 @@
按 interface_type 扩展时在这里加分支即可——这是未来接更多模型的唯一入口。
"""
import config
from loguru import logger
from models import AssistantConfig
@@ -27,6 +26,12 @@ from services.pipecat.xfyun_tts import DEFAULT_XFYUN_TTS_URL, XfyunTTSService
TTS_STOP_FRAME_TIMEOUT_S = 1.0
def _require(value: str, label: str) -> str:
if value:
return value
raise ValueError(f"缺少模型资源配置: {label}")
def _language(value: str) -> Language | None:
if not value:
return None
@@ -40,7 +45,7 @@ def _language(value: str) -> Language | None:
def create_stt(cfg: AssistantConfig):
"""SenseVoice / FunASR 等,走 OpenAI 兼容的 /v1/audio/transcriptions。
连接信息优先用 cfg(由 config_resolver 从 DB 注入),为空回退 .env 默认
连接信息来自 cfg(由 config_resolver 从 DB 注入,或调试时 inline_config 传入)
"""
if cfg.stt_interface_type == "xfyun-asr":
return XfyunASRService(
@@ -59,10 +64,10 @@ def create_stt(cfg: AssistantConfig):
raise ValueError(f"不支持的 ASR 接口类型: {cfg.stt_interface_type}")
return OpenAISTTService(
api_key=cfg.stt_api_key or config.STT_API_KEY,
base_url=cfg.stt_base_url or config.STT_BASE_URL,
api_key=_require(cfg.stt_api_key, "ASR apiKey"),
base_url=_require(cfg.stt_base_url, "ASR apiUrl"),
settings=OpenAISTTService.Settings(
model=cfg.asr or config.STT_MODEL,
model=_require(cfg.asr, "ASR modelId"),
language=_language(cfg.stt_language),
),
)
@@ -75,10 +80,10 @@ def create_llm(cfg: AssistantConfig):
extra_body = cfg.llm_values.get("extraBody")
extra = {"extra_body": extra_body} if isinstance(extra_body, dict) else {}
return OpenAILLMService(
api_key=cfg.llm_api_key or config.LLM_API_KEY,
base_url=cfg.llm_base_url or config.LLM_BASE_URL,
api_key=_require(cfg.llm_api_key, "LLM apiKey"),
base_url=_require(cfg.llm_base_url, "LLM apiUrl"),
settings=OpenAILLMService.Settings(
model=cfg.model or config.LLM_MODEL,
model=_require(cfg.model, "LLM modelId"),
extra=extra,
),
)
@@ -86,7 +91,7 @@ def create_llm(cfg: AssistantConfig):
def create_tts(cfg: AssistantConfig):
"""CosyVoice 等,走 OpenAI 兼容的 /v1/audio/speech。"""
voice = cfg.voice or config.TTS_VOICE
voice = _require(cfg.voice, "TTS voice")
if cfg.tts_interface_type == "xfyun-super-tts":
return XfyunSuperTTSService(
app_id=str(cfg.tts_secrets.get("appId") or ""),
@@ -126,11 +131,11 @@ def create_tts(cfg: AssistantConfig):
# 注册为原样映射后仍由 OpenAI SDK 按字符串透传给供应商。
VALID_VOICES.setdefault(voice, voice)
return OpenAITTSService(
api_key=cfg.tts_api_key or config.TTS_API_KEY,
base_url=cfg.tts_base_url or config.TTS_BASE_URL,
api_key=_require(cfg.tts_api_key, "TTS apiKey"),
base_url=_require(cfg.tts_base_url, "TTS apiUrl"),
stop_frame_timeout_s=TTS_STOP_FRAME_TIMEOUT_S,
settings=OpenAITTSService.Settings(
model=cfg.tts_model or config.TTS_MODEL,
model=_require(cfg.tts_model, "TTS modelId"),
voice=voice,
speed=cfg.tts_speed,
),
@@ -143,9 +148,9 @@ def create_realtime_service(cfg: AssistantConfig):
from services.pipecat.stepfun_realtime import StepFunRealtimeService
return StepFunRealtimeService(
api_key=cfg.realtime_api_key,
model=cfg.realtimeModel,
base_url=cfg.realtime_base_url,
api_key=_require(cfg.realtime_api_key, "Realtime apiKey"),
model=_require(cfg.realtimeModel, "Realtime modelId"),
base_url=_require(cfg.realtime_base_url, "Realtime apiUrl"),
instructions=cfg.prompt,
voice=str(cfg.realtime_values.get("voice") or "linjiajiejie"),
input_sample_rate=int(