62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
# =============================================================================
|
|
# RAGFlow 配置加载器
|
|
# =============================================================================
|
|
# 说明:从数据库 system_configs 表加载 RAGFlow 配置,创建客户端实例
|
|
# 配置项:integration_ragflow_api_url + integration_ragflow_api_key
|
|
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.system_config import SystemConfig
|
|
|
|
from .client import RagflowClient
|
|
from .exceptions import RagflowConfigError
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 默认 RAGFlow API 地址(生产环境)
|
|
DEFAULT_RAGFLOW_BASE_URL = "http://10.80.0.85:9380"
|
|
|
|
|
|
async def _get_config(db: AsyncSession, key: str) -> str:
|
|
"""从数据库读取单个配置值。"""
|
|
result = await db.execute(
|
|
select(SystemConfig.config_value).where(SystemConfig.config_key == key)
|
|
)
|
|
row = result.scalar()
|
|
return row if row else ""
|
|
|
|
|
|
async def get_ragflow_client(db: AsyncSession) -> RagflowClient:
|
|
"""从数据库配置创建 RAGFlow 客户端实例。
|
|
|
|
读取 system_configs 表中的:
|
|
- integration_ragflow_api_url: RAGFlow API 地址
|
|
- integration_ragflow_api_key: RAGFlow API Key
|
|
|
|
Args:
|
|
db: 数据库会话
|
|
|
|
Returns:
|
|
RagflowClient: 客户端实例
|
|
|
|
Raises:
|
|
RagflowConfigError: 配置缺失
|
|
"""
|
|
api_url = await _get_config(db, "integration_ragflow_api_url")
|
|
api_key = await _get_config(db, "integration_ragflow_api_key")
|
|
|
|
# 如果数据库没有配置,使用默认地址
|
|
if not api_url:
|
|
api_url = DEFAULT_RAGFLOW_BASE_URL
|
|
|
|
if not api_key:
|
|
raise RagflowConfigError(
|
|
"RAGFlow API Key 未配置,请在管理后台 → 集成管理 → RAGFlow 中设置"
|
|
)
|
|
|
|
return RagflowClient(api_key=api_key, base_url=api_url)
|