T4: AnySearch 改为全局固定配置 + 修复外部服务弹窗 UI
This commit is contained in:
1 parent
3c9d9518b1
commit
97125d7b65
6 files changed
+634
-441
No files matched your search
@@ -32,12 +32,17 @@ logger = logging.getLogger("aiflow")
|
||||
|
||||
class ExternalServiceConfig(BaseModel):
|
||||
name: str = Field(min_length=1, max_length=200)
|
||||
service_type: str = Field(..., description="dify / anysearch")
|
||||
service_type: str = Field(..., description="dify")
|
||||
api_url: str = Field(min_length=1, max_length=500)
|
||||
api_key: Optional[str] = None
|
||||
config: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
class AnySearchConfig(BaseModel):
|
||||
api_key: str = Field(min_length=1, max_length=500)
|
||||
is_enabled: bool = True
|
||||
|
||||
|
||||
class ExternalServiceUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
api_url: Optional[str] = None
|
||||
@@ -739,14 +744,11 @@ def _get_external_services_table(db: Session):
|
||||
|
||||
|
||||
async def _check_service_connection(service_type: str, api_url: str, api_key: str) -> dict:
|
||||
"""测试外部知识库服务连接状态(支持 Dify / AnySearch)"""
|
||||
"""测试外部知识库服务连接状态(支持 Dify)"""
|
||||
try:
|
||||
if service_type == "dify":
|
||||
from ...dify_kb import dify_test
|
||||
return dify_test(api_url, api_key)
|
||||
if service_type == "anysearch":
|
||||
from ...anysearch_client import anysearch_health
|
||||
return await anysearch_health(api_key or "", api_url or "https://api.anysearch.com/v1/search")
|
||||
else:
|
||||
# 未知类型(如历史遗留的 fastgpt/ragflow),按通用方式探测可达性
|
||||
headers = {}
|
||||
@@ -784,7 +786,6 @@ async def list_external_services(
|
||||
"items": services,
|
||||
"supported_types": [
|
||||
{"type": "dify", "name": "Dify", "description": "LLM 应用开发平台,内置知识库管理(RAG)"},
|
||||
{"type": "anysearch", "name": "AnySearch 联网检索", "description": "实时联网网页/领域搜索,为 AI 聊天注入最新网络信息"},
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -797,8 +798,8 @@ async def create_external_service(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""创建新的外部知识库服务连接配置"""
|
||||
# 验证服务类型(当前仅支持 Dify)
|
||||
valid_types = ["dify", "anysearch"]
|
||||
# 验证服务类型(当前仅支持 Dify;AnySearch 已改为全局固定配置)
|
||||
valid_types = ["dify"]
|
||||
if req.service_type not in valid_types:
|
||||
raise HTTPException(status_code=400, detail=f"不支持的服务类型: {req.service_type}")
|
||||
|
||||
@@ -881,6 +882,54 @@ async def create_external_service(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/anysearch/config", summary="获取 AnySearch 全局配置")
|
||||
async def get_anysearch_config(
|
||||
admin_user=Depends(get_current_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""读取全局 AnySearch 联网检索配置(site_settings 表)。"""
|
||||
from ...anysearch_client import load_anysearch_config, DEFAULT_API_URL
|
||||
cfg = load_anysearch_config(db)
|
||||
return {
|
||||
"code": 0,
|
||||
"data": {
|
||||
"api_key": cfg.get("api_key", ""),
|
||||
"is_enabled": cfg.get("is_enabled", False),
|
||||
"api_url": cfg.get("api_url") or DEFAULT_API_URL,
|
||||
"zone": cfg.get("zone", "cn"),
|
||||
"freshness": cfg.get("freshness"),
|
||||
"top_k": cfg.get("top_k", 5),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@router.post("/anysearch/config", summary="保存 AnySearch 全局配置")
|
||||
async def save_anysearch_config_endpoint(
|
||||
req: AnySearchConfig,
|
||||
admin_user=Depends(get_current_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""保存全局 AnySearch 联网检索配置,并测试连通性。"""
|
||||
from ...anysearch_client import save_anysearch_config, anysearch_health, DEFAULT_API_URL
|
||||
ok = save_anysearch_config(db, api_key=req.api_key, is_enabled=req.is_enabled, api_url=DEFAULT_API_URL)
|
||||
if not ok:
|
||||
raise HTTPException(status_code=500, detail="保存 AnySearch 配置失败")
|
||||
health = await anysearch_health(req.api_key or "", DEFAULT_API_URL)
|
||||
LogService.log_audit(
|
||||
admin_id=admin_user.id,
|
||||
admin_name=admin_user.username,
|
||||
action="update_anysearch_config",
|
||||
target_type="site_setting",
|
||||
target_id="anysearch_config",
|
||||
detail=f"更新 AnySearch 全局配置: enabled={req.is_enabled}, connected={health.get('connected')}",
|
||||
)
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "AnySearch 配置已保存",
|
||||
"data": {"connection": health},
|
||||
}
|
||||
|
||||
|
||||
@router.put("/external/services/{service_id}", summary="更新外部知识库服务配置")
|
||||
async def update_external_service(
|
||||
service_id: str,
|
||||
|
||||
@@ -1496,27 +1496,18 @@ async def web_search_endpoint(
|
||||
):
|
||||
"""联网检索(AnySearch):返回实时网页结果,供前端 / 智能体引用。"""
|
||||
try:
|
||||
from sqlalchemy import text
|
||||
import json as _json
|
||||
from ..anysearch_client import anysearch_search
|
||||
row = db.execute(text(
|
||||
"SELECT api_key, api_url, config FROM external_kb_services "
|
||||
"WHERE service_type='anysearch' AND is_enabled=1 LIMIT 1"
|
||||
)).fetchone()
|
||||
if not row:
|
||||
from ..anysearch_client import anysearch_search, load_anysearch_config
|
||||
cfg = load_anysearch_config(db)
|
||||
if not cfg.get("is_enabled") or not cfg.get("api_key"):
|
||||
return {"code": 0, "data": {"results": [], "enabled": False, "message": "未配置 AnySearch 联网检索服务"}}
|
||||
api_key = row[0] or ""
|
||||
api_url = row[1] or "https://api.anysearch.com/v1/search"
|
||||
cfg = row[2]
|
||||
if isinstance(cfg, str):
|
||||
try:
|
||||
cfg = _json.loads(cfg)
|
||||
except Exception:
|
||||
cfg = {}
|
||||
cfg = cfg or {}
|
||||
zone = cfg.get("zone", "cn")
|
||||
freshness = cfg.get("freshness")
|
||||
hits = await anysearch_search(api_key, api_url, req.query, top_k=req.top_k, zone=zone, freshness=freshness)
|
||||
hits = await anysearch_search(
|
||||
cfg["api_key"],
|
||||
cfg.get("api_url") or "https://api.anysearch.com/v1/search",
|
||||
req.query,
|
||||
top_k=req.top_k,
|
||||
zone=cfg.get("zone", "cn"),
|
||||
freshness=cfg.get("freshness"),
|
||||
)
|
||||
return {"code": 0, "data": {"results": hits, "enabled": True}}
|
||||
except Exception as e:
|
||||
logger.warning(f"web-search endpoint failed: {e}")
|
||||
|
||||
Reference in new issue
Block a user