fix: KGEmptyError 自定义异常 + CORS 环境变量配置
- qa_service 定义 KGEmptyError 异常,run_query 抛出;routers/query 精确捕获 → HTTP 400 + code 3002,移除字符串匹配 - main.py CORS 改为读 CORS_ORIGINS 环境变量:未设置时 wildcard + credentials=False(合规默认),设置后显式列表 + credentials=True - .env.example 新增 CORS_ORIGINS 默认值(后端 + 前端 4 个域名)
This commit is contained in:
@@ -19,3 +19,5 @@ KEYCLOAK_REALM=plfai
|
|||||||
KEYCLOAK_CLIENT_ID=graphrag-backend
|
KEYCLOAK_CLIENT_ID=graphrag-backend
|
||||||
KEYCLOAK_CLIENT_SECRET=your_keycloak_client_secret_here
|
KEYCLOAK_CLIENT_SECRET=your_keycloak_client_secret_here
|
||||||
KEYCLOAK_AUDIENCE=account
|
KEYCLOAK_AUDIENCE=account
|
||||||
|
|
||||||
|
CORS_ORIGINS=https://graphrag-backend.plfai.cn,https://test-graphrag-backend.plfai.cn,https://graphrag.plfai.cn,https://test-graphrag.plfai.cn
|
||||||
|
|||||||
+15
-2
@@ -2,6 +2,7 @@
|
|||||||
GraphRAG Studio — FastAPI Backend
|
GraphRAG Studio — FastAPI Backend
|
||||||
Entry point: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
Entry point: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -25,10 +26,22 @@ app = FastAPI(
|
|||||||
redoc_url="/redoc",
|
redoc_url="/redoc",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# CORS — explicit origins when set, wildcard otherwise.
|
||||||
|
# Auth uses Bearer headers (not cookies), so credentials are only needed when
|
||||||
|
# the browser is also sending cookies; keep credentials=False with wildcard to
|
||||||
|
# avoid the CORS spec rejecting the combo at runtime.
|
||||||
|
_cors_origins_raw = os.getenv("CORS_ORIGINS", "").strip()
|
||||||
|
if _cors_origins_raw:
|
||||||
|
_cors_origins = [o.strip() for o in _cors_origins_raw.split(",") if o.strip()]
|
||||||
|
_cors_credentials = True
|
||||||
|
else:
|
||||||
|
_cors_origins = ["*"]
|
||||||
|
_cors_credentials = False
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=_cors_origins,
|
||||||
allow_credentials=True,
|
allow_credentials=_cors_credentials,
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from fastapi.responses import JSONResponse
|
|||||||
|
|
||||||
from models.schemas import APIResponse, BatchQueryRequest, QueryRequest
|
from models.schemas import APIResponse, BatchQueryRequest, QueryRequest
|
||||||
from services import qa_service as svc
|
from services import qa_service as svc
|
||||||
|
from services.qa_service import KGEmptyError
|
||||||
|
|
||||||
router = APIRouter(prefix="/query", tags=["QA"])
|
router = APIRouter(prefix="/query", tags=["QA"])
|
||||||
|
|
||||||
@@ -20,12 +21,12 @@ async def run_query(body: QueryRequest):
|
|||||||
partial(svc.run_query, body.question, [m.model_dump() for m in body.history]),
|
partial(svc.run_query, body.question, [m.model_dump() for m in body.history]),
|
||||||
)
|
)
|
||||||
return APIResponse.ok(result)
|
return APIResponse.ok(result)
|
||||||
|
except KGEmptyError as e:
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=400,
|
||||||
|
content=APIResponse.err(3002, str(e)).model_dump(),
|
||||||
|
)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
if "KG_EMPTY" in str(e):
|
|
||||||
return JSONResponse(
|
|
||||||
status_code=400,
|
|
||||||
content=APIResponse.err(3002, "Knowledge graph is empty. Index documents first.").model_dump(),
|
|
||||||
)
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
content=APIResponse.err(4001, str(e)).model_dump(),
|
content=APIResponse.err(4001, str(e)).model_dump(),
|
||||||
|
|||||||
@@ -8,6 +8,13 @@ from datetime import datetime, timezone
|
|||||||
from storage import file_store as fs
|
from storage import file_store as fs
|
||||||
|
|
||||||
|
|
||||||
|
class KGEmptyError(Exception):
|
||||||
|
"""Raised when a QA request is made but the knowledge graph has no nodes."""
|
||||||
|
|
||||||
|
def __init__(self, msg: str = "Knowledge graph is empty. Index documents first."):
|
||||||
|
super().__init__(msg)
|
||||||
|
|
||||||
|
|
||||||
def run_query(question: str, history: list[dict]) -> dict:
|
def run_query(question: str, history: list[dict]) -> dict:
|
||||||
from pipeline.qa_agent import run_qa
|
from pipeline.qa_agent import run_qa
|
||||||
|
|
||||||
@@ -15,7 +22,7 @@ def run_query(question: str, history: list[dict]) -> dict:
|
|||||||
edges = fs.load_kg_edges()
|
edges = fs.load_kg_edges()
|
||||||
|
|
||||||
if not nodes:
|
if not nodes:
|
||||||
raise ValueError("KG_EMPTY")
|
raise KGEmptyError()
|
||||||
|
|
||||||
start = time.time()
|
start = time.time()
|
||||||
result = run_qa(question, history, nodes, edges)
|
result = run_qa(question, history, nodes, edges)
|
||||||
|
|||||||
Reference in New Issue
Block a user