refactor: 精简环境配置,移除废弃配置项
.env: - 移除 HOST/PORT/DEBUG(代码未读取,由 uvicorn 命令行控制) - 移除 WEWORK_BOT_SEARCH_LIMIT(已被 SEARCH_LIMIT 取代) - 补充硅基流动配置注释 .env.example: - 同步移除 HOST/PORT/DEBUG/WEWORK_BOT_SEARCH_LIMIT - 补充 OpenAI 兼容硅基流动的说明 - 更新机器人注释(集成模式自动启动) config.py: - 移除 HOST/PORT/DEBUG 字段定义 - 移除 WEWORK_BOT_SEARCH_LIMIT 字段定义 wework_bot.py: - 改为从 settings 统一读取配置(不再 os.getenv) - _get_search_limit fallback 改为 settings.SEARCH_LIMIT - 更新模块文档注释
This commit is contained in:
@@ -153,11 +153,6 @@ class Settings(BaseSettings):
|
||||
SEARCH_LIMIT: int = Field(default=3, description="图片搜索默认返回数量(1-10)")
|
||||
JUDGE_BATCH_SIZE: int = Field(default=10, description="每次LLM判断的文章数量(1-50)")
|
||||
|
||||
# ──────────────────────────── 服务端口 ────────────────────────────
|
||||
HOST: str = Field(default="0.0.0.0", description="服务监听地址")
|
||||
PORT: int = Field(default=8000, description="服务监听端口")
|
||||
DEBUG: bool = Field(default=False, description="调试模式")
|
||||
|
||||
# ──────────────────────────── CORS ────────────────────────────
|
||||
CORS_ORIGINS: list[str] = Field(
|
||||
default=["*"],
|
||||
@@ -177,12 +172,6 @@ class Settings(BaseSettings):
|
||||
default=None,
|
||||
description="企业微信智能机器人 Secret",
|
||||
)
|
||||
WEWORK_BOT_SEARCH_LIMIT: int = Field(
|
||||
default=3,
|
||||
ge=1,
|
||||
le=10,
|
||||
description="企业微信机器人搜索返回图片数量上限",
|
||||
)
|
||||
EDUBRAIN_BASE_URL: str = Field(
|
||||
default="http://localhost:8765",
|
||||
description="EduBrain 后端服务地址,企业微信机器人通过此地址调用后端 API(搜索/OCR识别等)。当机器人与后端部署在同一台机器时使用默认值 http://localhost:8765;若机器人独立部署到其他服务器,需改为后端的实际访问地址(如 http://192.168.1.100:8765)",
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
使用官方 SDK (wecom-aibot-python-sdk) 的 WSClient 类,
|
||||
封装了连接管理、心跳、断线重连等能力。
|
||||
|
||||
作为独立进程运行:python -m app.wework_bot
|
||||
支持两种运行方式:
|
||||
1. 集成模式:后端启动时自动拉起(BotManager)
|
||||
2. 独立进程:python -m app.wework_bot
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -28,6 +30,8 @@ from dotenv import load_dotenv
|
||||
# 加载 .env 文件(独立运行时需要)
|
||||
load_dotenv()
|
||||
|
||||
from app.config import settings
|
||||
|
||||
logger = logging.getLogger("wework_bot")
|
||||
|
||||
# 分片上传参数(参考官方文档)
|
||||
@@ -38,13 +42,10 @@ class WeComBotService:
|
||||
"""企业微信智能机器人服务(WebSocket 长连接模式)"""
|
||||
|
||||
def __init__(self):
|
||||
self.bot_id = os.getenv("WEWORK_BOT_ID", "")
|
||||
self.bot_secret = os.getenv("WEWORK_BOT_SECRET", "")
|
||||
self.edubrain_base_url = os.getenv(
|
||||
"EDUBRAIN_BASE_URL", "http://localhost:8765"
|
||||
)
|
||||
self.search_limit = int(os.getenv("WEWORK_BOT_SEARCH_LIMIT", "3"))
|
||||
self.enabled = os.getenv("WEWORK_BOT_ENABLED", "false").lower() == "true"
|
||||
self.bot_id = settings.WEWORK_BOT_ID or ""
|
||||
self.bot_secret = settings.WEWORK_BOT_SECRET or ""
|
||||
self.edubrain_base_url = settings.EDUBRAIN_BASE_URL
|
||||
self.enabled = settings.WEWORK_BOT_ENABLED
|
||||
self.ws_client: WSClient | None = None
|
||||
|
||||
def start(self):
|
||||
@@ -430,10 +431,12 @@ class WeComBotService:
|
||||
resp = await client.get(f"{self.edubrain_base_url}/api/v1/settings")
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
return data.get("search_limit", self.search_limit)
|
||||
limit = data.get("search_limit")
|
||||
if limit:
|
||||
return limit
|
||||
except Exception as e:
|
||||
logger.warning("获取 search_limit 失败,使用默认值: %s", e)
|
||||
return self.search_limit
|
||||
return settings.SEARCH_LIMIT
|
||||
|
||||
async def _search_images(self, keyword: str) -> list:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user