Enhance environment configuration and error handling in API server and backend. Implement dynamic loading of .env files for various execution contexts, improve schema file path resolution, and refine vector search error handling in the orchestrator. Update ChromaDB integration to support memory mode and add persistence options for few-shot learning. Include additional logging for better traceability.
This commit is contained in:
+46
-3
@@ -44,7 +44,45 @@ logging.basicConfig(
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
load_dotenv(Path(__file__).resolve().parent / ".env")
|
||||
# 加载 .env 文件(支持 PyInstaller 打包后的目录结构)
|
||||
def _find_env_file() -> Path:
|
||||
"""查找 .env 文件,支持多种运行环境"""
|
||||
import sys
|
||||
|
||||
# 尝试1: 当前工作目录
|
||||
cwd_env = Path.cwd() / ".env"
|
||||
if cwd_env.exists():
|
||||
return cwd_env
|
||||
|
||||
# 尝试2: PyInstaller 临时目录(单文件模式)
|
||||
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
|
||||
meipass_env = Path(sys._MEIPASS) / ".env"
|
||||
if meipass_env.exists():
|
||||
return meipass_env
|
||||
|
||||
# 尝试3: 脚本/可执行文件所在目录
|
||||
if getattr(sys, 'frozen', False):
|
||||
# PyInstaller 打包后
|
||||
exe_dir = Path(sys.executable).parent
|
||||
env_path = exe_dir / ".env"
|
||||
if env_path.exists():
|
||||
return env_path
|
||||
else:
|
||||
# 开发环境
|
||||
script_dir = Path(__file__).resolve().parent
|
||||
env_path = script_dir / ".env"
|
||||
if env_path.exists():
|
||||
return env_path
|
||||
|
||||
# 默认返回当前目录
|
||||
return cwd_env
|
||||
|
||||
env_file = _find_env_file()
|
||||
if env_file.exists():
|
||||
load_dotenv(env_file)
|
||||
logger.info(f"[OK] 已加载配置文件: {env_file}")
|
||||
else:
|
||||
logger.warning(f"[WARN] 未找到 .env 文件: {env_file}")
|
||||
|
||||
orchestrator = None
|
||||
schema_manager = None
|
||||
@@ -78,7 +116,7 @@ def get_orchestrator():
|
||||
max_retry = int(os.getenv("MAX_RETRY", "2"))
|
||||
embedding_model = None
|
||||
vector_db = os.getenv("VECTOR_DB_PATH", "./data/embeddings/chroma")
|
||||
no_vector_search = False
|
||||
no_vector_search = False # 启用向量搜索(ChromaDB 已修复)
|
||||
no_fewshot = not os.getenv("FEWSHOT_ENABLED", "true").lower() in ("true", "1", "yes")
|
||||
fewshot_top_k = int(os.getenv("FEWSHOT_TOP_K", "3"))
|
||||
fewshot_min_rating = int(os.getenv("FEWSHOT_MIN_RATING", "7"))
|
||||
@@ -644,6 +682,7 @@ async def admin_visibility():
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
import multiprocessing
|
||||
|
||||
port = int(os.getenv("API_PORT", "8041"))
|
||||
host = os.getenv("API_HOST", "0.0.0.0")
|
||||
@@ -651,8 +690,12 @@ if __name__ == "__main__":
|
||||
logger.info(f"启动服务: http://{host}:{port}")
|
||||
logger.info(f"API文档: http://{host}:{port}/docs")
|
||||
|
||||
# PyInstaller + multiprocessing(spawn)兼容
|
||||
multiprocessing.freeze_support()
|
||||
|
||||
# PyInstaller 打包后必须使用 app 对象,不能使用字符串模块名
|
||||
uvicorn.run(
|
||||
"api_server:app",
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
reload=False,
|
||||
|
||||
Reference in New Issue
Block a user