新增功能: - execution_log 模块:自动记录引擎调用耗时、成功/失败、错误详情 - 所有搜索引擎自动跟踪每次调用的性能指标 - CLI 搜索后自动显示执行摘要 - health 命令使用 ParallelSearch 检测所有引擎连通性 - 新增 log 命令查看历史执行日志
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
"""
|
|
全局配置管理。
|
|
|
|
从 bot-search-mcp 移植,使用 pydantic-settings 管理环境变量。
|
|
支持 .env 文件自动加载。
|
|
|
|
如果未安装 pydantic-settings,使用简单 os.environ 回退。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Optional
|
|
|
|
try:
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
class Settings(BaseSettings):
|
|
"""应用全局配置,支持环境变量 + .env 文件覆盖。"""
|
|
|
|
model_config = SettingsConfigDict(
|
|
env_file=".env",
|
|
env_file_encoding="utf-8",
|
|
extra="ignore",
|
|
)
|
|
|
|
# --- 搜索引擎配置 ---
|
|
bing_api_key: str = ""
|
|
bing_endpoint: str = ""
|
|
baidu_api_key: str = ""
|
|
baidu_qianfan_api_key: str = ""
|
|
wolfram_app_id: str = ""
|
|
twitter_bearer_token: str = ""
|
|
|
|
# --- 微信公众号搜索 ---
|
|
wechat_search_enabled: bool = True
|
|
wechat_request_delay: float = 1.0
|
|
wechat_max_pages: int = 3
|
|
|
|
# --- 超时控制 ---
|
|
engine_timeout: float = 15.0
|
|
max_concurrent: int = 5
|
|
|
|
# --- 日志 ---
|
|
log_level: str = "INFO"
|
|
|
|
# --- API 引擎每日限额(有 key 的尽量做替补)---
|
|
baidu_daily_limit: int = 50
|
|
minimax_daily_limit: int = 100
|
|
|
|
except ImportError:
|
|
|
|
class Settings: # type: ignore
|
|
"""回退配置类:无 pydantic-settings 时使用 os.environ。"""
|
|
|
|
def __init__(self):
|
|
self.bing_api_key = os.environ.get("BING_API_KEY", "")
|
|
self.bing_endpoint = os.environ.get("BING_ENDPOINT", "")
|
|
self.baidu_api_key = os.environ.get("BAIDU_API_KEY", "")
|
|
self.baidu_qianfan_api_key = os.environ.get(
|
|
"BAIDU_QIANFAN_API_KEY", ""
|
|
)
|
|
self.wolfram_app_id = os.environ.get("WOLFRAM_APP_ID", "")
|
|
self.twitter_bearer_token = os.environ.get(
|
|
"TWITTER_BEARER_TOKEN", ""
|
|
)
|
|
self.wechat_search_enabled = (
|
|
os.environ.get("WECHAT_SEARCH_ENABLED", "true").lower()
|
|
== "true"
|
|
)
|
|
self.wechat_request_delay = float(
|
|
os.environ.get("WECHAT_REQUEST_DELAY", "1.0")
|
|
)
|
|
self.wechat_max_pages = int(
|
|
os.environ.get("WECHAT_MAX_PAGES", "3")
|
|
)
|
|
self.engine_timeout = float(
|
|
os.environ.get("ENGINE_TIMEOUT", "15.0")
|
|
)
|
|
self.max_concurrent = int(
|
|
os.environ.get("MAX_CONCURRENT", "5")
|
|
)
|
|
self.log_level = os.environ.get("LOG_LEVEL", "INFO")
|
|
# API 引擎每日限额
|
|
self.baidu_daily_limit = int(os.environ.get("BAIDU_DAILY_LIMIT", "50"))
|
|
self.minimax_daily_limit = int(os.environ.get("MINIMAX_DAILY_LIMIT", "100"))
|
|
|
|
|
|
# 全局单例
|
|
settings = Settings()
|