diff --git a/ruoyi-fastapi-backend/.env.dev b/ruoyi-fastapi-backend/.env.dev index ee1d3b6..fbbe8c7 100644 --- a/ruoyi-fastapi-backend/.env.dev +++ b/ruoyi-fastapi-backend/.env.dev @@ -154,7 +154,7 @@ SEARCH_CACHE_TTL = 1800 #-------------------KB ES/BM25(测试环境)------------------- KB_PROVIDER=es_bm25 KB_ES_URL=http://127.0.0.1:1200 -KB_ES_INDEX=kb_chunks_v1 +KB_ES_INDEX=kb_qa_v1 KB_ES_USERNAME=elastic KB_ES_PASSWORD=infini_rag_flow KB_ES_VERIFY_SSL=true diff --git a/ruoyi-fastapi-backend/config/env.py b/ruoyi-fastapi-backend/config/env.py index 6060652..285e721 100644 --- a/ruoyi-fastapi-backend/config/env.py +++ b/ruoyi-fastapi-backend/config/env.py @@ -239,26 +239,41 @@ class DeepSeekSettings: DEEPSEEK_MODEL = os.getenv("DEEPSEEK_MODEL", "deepseek-chat") -class KBSettings: - """内部知识库 Provider 配置(ragflow / es_bm25)""" +class KBSettings(BaseSettings): + """内部知识库 Provider 配置(ragflow / es_bm25) - KB_PROVIDER: Literal['ragflow', 'es_bm25'] = os.getenv('KB_PROVIDER', 'ragflow') # type: ignore[assignment] + 注意:必须在 `.env.*` 加载完成后再实例化读取, + 所以使用 BaseSettings 而不是在 import 时读取环境变量。 + """ + + KB_PROVIDER: Literal['ragflow', 'es_bm25'] = 'ragflow' # type: ignore[assignment] # ES/OpenSearch - KB_ES_URL: str = os.getenv('KB_ES_URL', os.getenv('KB_OS_URL', '')).rstrip('/') - KB_ES_INDEX: str = os.getenv('KB_ES_INDEX', 'kb_chunks_v1') - KB_ES_USERNAME: str = os.getenv('KB_ES_USERNAME', '') - KB_ES_PASSWORD: str = os.getenv('KB_ES_PASSWORD', '') - KB_ES_VERIFY_SSL: bool = os.getenv('KB_ES_VERIFY_SSL', 'true').lower() == 'true' + KB_ES_URL: str = '' + KB_ES_INDEX: str = 'kb_chunks_v1' + KB_ES_USERNAME: str = '' + KB_ES_PASSWORD: str = '' + KB_ES_VERIFY_SSL: bool = True # retrieval - KB_TOP_K: int = int(os.getenv('KB_TOP_K', '8')) - KB_MIN_SCORE: float = float(os.getenv('KB_MIN_SCORE', '0')) - KB_MIN_COVERAGE: float = float(os.getenv('KB_MIN_COVERAGE', '0.2')) - KB_CACHE_TTL: int = int(os.getenv('KB_CACHE_TTL', '1800')) # seconds + KB_TOP_K: int = 8 + KB_MIN_SCORE: float = 0.0 + KB_MIN_COVERAGE: float = 0.2 + KB_CACHE_TTL: int = 1800 # seconds # fallback - KB_FALLBACK: Literal['ragflow', 'none'] = os.getenv('KB_FALLBACK', 'ragflow') # type: ignore[assignment] + KB_FALLBACK: Literal['ragflow', 'none'] = 'ragflow' # type: ignore[assignment] + + def model_post_init(self, __context) -> None: # type: ignore[override] + # 规范化:dotenv/环境变量可能带空格/大小写 + provider = (str(self.KB_PROVIDER) if self.KB_PROVIDER is not None else 'ragflow').strip().lower() + self.KB_PROVIDER = provider if provider in ('ragflow', 'es_bm25') else 'ragflow' # type: ignore[assignment] + + es_url = (self.KB_ES_URL or '').strip().rstrip('/') + self.KB_ES_URL = es_url + + fallback = (str(self.KB_FALLBACK) if self.KB_FALLBACK is not None else 'ragflow').strip().lower() + self.KB_FALLBACK = fallback if fallback in ('ragflow', 'none') else 'ragflow' # type: ignore[assignment] class GetConfig: