59 lines
1.8 KiB
Python
Executable File
59 lines
1.8 KiB
Python
Executable File
"""推理引擎API路由"""
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
router = APIRouter()
|
|
|
|
# 导入推理引擎
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / 'scripts' / 'utils'))
|
|
try:
|
|
from reasoning_engine import ReasoningEngine
|
|
except ImportError:
|
|
# 备用导入方式
|
|
import importlib.util
|
|
reasoning_engine_path = Path(__file__).parent.parent / 'scripts' / 'utils' / 'reasoning_engine.py'
|
|
spec = importlib.util.spec_from_file_location("reasoning_engine", reasoning_engine_path)
|
|
reasoning_engine = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(reasoning_engine)
|
|
ReasoningEngine = reasoning_engine.ReasoningEngine
|
|
|
|
class ReasoningQuery(BaseModel):
|
|
question: str
|
|
|
|
@router.post("/reasoning/query")
|
|
async def reasoning_query(query: ReasoningQuery):
|
|
"""多跳推理查询"""
|
|
try:
|
|
engine = ReasoningEngine()
|
|
result = engine.multi_hop_query(query.question)
|
|
return {"status": "success", **result}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.get("/reasoning/examples")
|
|
async def reasoning_examples():
|
|
"""推理查询示例"""
|
|
return {
|
|
"status": "success",
|
|
"examples": [
|
|
{
|
|
"question": "查找与CYP71D184同组的基因",
|
|
"description": "多跳推理:基因→分组→同组基因",
|
|
"hops": 2
|
|
},
|
|
{
|
|
"question": "P450分组统计比较",
|
|
"description": "聚合统计:所有分组的统计分析",
|
|
"hops": 1
|
|
},
|
|
{
|
|
"question": "最大的分组是哪个",
|
|
"description": "极值查询:查找最大分组",
|
|
"hops": 1
|
|
}
|
|
]
|
|
}
|