Files
ai-crm/apps/agent/config/settings.py
T
2026-10-21 23:23:48 +08:00

91 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""应用配置:按 AGENT_ENV 加载 .env.{env},环境变量优先。"""
from __future__ import annotations
import os
from functools import lru_cache
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
# 与后端 Spring profile 对齐:dev | test | pre | prod
_AGENT_ENV = (os.getenv("AGENT_ENV") or "dev").strip().lower() or "dev"
def _resolve_env_files() -> tuple[str, ...]:
"""加载 .env.{AGENT_ENV};仅 dev 额外允许 .env 覆盖(本机密钥)。"""
files: list[str] = []
profile = Path(f".env.{_AGENT_ENV}")
if profile.is_file():
files.append(str(profile))
if _AGENT_ENV == "dev":
local = Path(".env")
if local.is_file():
files.append(str(local))
if not files:
# 兼容:尚无分环境文件时回退 .env
if Path(".env").is_file():
files.append(".env")
else:
files.append(f".env.{_AGENT_ENV}")
return tuple(files)
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=_resolve_env_files(),
env_file_encoding="utf-8",
extra="ignore",
)
app_name: str = "ecom-dmb-agent"
# 当前环境名(只读展示;实际由 AGENT_ENV 决定加载哪个文件)
agent_env: str = _AGENT_ENV
host: str = "0.0.0.0"
port: int = 8200
cors_origins: str = "http://localhost:5173,http://localhost:5666"
# 与 Java server jwt.secret 一致(HS512)
jwt_secret: str = "your-secret-key-here-must-be-at-least-64-bytes-long-for-hs512-algorithm-security"
jwt_algorithms: str = "HS512"
mongo_uri: str = "mongodb://ecom_dmb_agent_dev:ecom_dmb_agent_dev@192.168.3.100:27017/?authSource=ecom_dmb_agent_dev"
mongo_db: str = "ecom_dmb_agent_dev"
# 须与同环境 Server Redis 同库,才能命中 agent:llm:rt:* 缓存
redis_url: str = "redis://:redis_qinghua@192.168.3.100:6379/2"
llm_cache_ttl_seconds: int = 600
# Java Server 根地址(内部拉 LLM 运行时)
server_base_url: str = "http://localhost:8080"
# 仅本地开发:公司未配置时的 fallback(test/pre/prod 应关闭)
llm_fallback_enabled: bool = True
llm_fallback_base_url: str = "https://api.openai.com/v1"
llm_fallback_api_key: str = ""
llm_fallback_model: str = "gpt-4o-mini"
mcp_base_url: str = "http://localhost:8080/mcp"
mcp_bearer_token: str = "dev-agent-mcp-token"
mcp_tools_enabled: bool = True
chat_history_limit: int = 40
# 从 Mongo 最多拉多少条再按 token 裁剪(应 ≥ 条数窗口时代的 limit)
chat_history_fetch_limit: int = 200
# 未配置 context_window 且模型名无法识别时的默认
llm_default_context_window: int = 128000
# 为模型输出与本轮 tool 预留的 token
context_reserve_output_tokens: int = 4096
context_reserve_tool_tokens: int = 8192
# Responses/store 能力缓存 TTL(秒)
llm_capability_ttl_seconds: int = 604800
@property
def cors_origin_list(self) -> list[str]:
return [o.strip() for o in self.cors_origins.split(",") if o.strip()]
@lru_cache
def get_settings() -> Settings:
return Settings()