Skip to content

Commit fbd7157

Browse files
committed
feat: add query endpoint for knowledge graph and enhance RAG service with async processing
1 parent 64ffcc6 commit fbd7157

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

runtime/datamate-python/app/module/rag/interface/rag_interface.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from app.db.session import get_db
55
from app.module.rag.service.rag_service import RAGService
66
from app.module.shared.schema import StandardResponse
7+
from ..schema.rag_schema import QueryRequest
78

89
router = APIRouter(prefix="/rag", tags=["rag"])
910

@@ -22,3 +23,16 @@ async def process_knowledge_base(knowledge_base_id: str, db: AsyncSession = Depe
2223
except Exception as e:
2324
raise HTTPException(status_code=500, detail=str(e))
2425

26+
@router.post("/query")
27+
async def query_knowledge_graph(payload: QueryRequest, db: AsyncSession = Depends(get_db)):
28+
"""
29+
Query the knowledge graph with the given query text and knowledge base ID.
30+
"""
31+
try:
32+
rag_service = RAGService(db)
33+
result = await rag_service.query_rag(payload.query, payload.knowledge_base_id)
34+
return StandardResponse(code=200, message="success", data=result)
35+
except HTTPException:
36+
raise
37+
except Exception as e:
38+
raise HTTPException(status_code=500, detail=str(e))

runtime/datamate-python/app/module/rag/schema/rag_schema.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
class ProcessRequest(BaseModel):
44
knowledge_base_id: str
55

6+
class QueryRequest(BaseModel):
7+
knowledge_base_id: str
8+
query: str

runtime/datamate-python/app/module/rag/service/rag_service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import asyncio
23
from typing import Optional, Sequence
34

45
from fastapi import BackgroundTasks, Depends
@@ -61,7 +62,7 @@ async def init_graph_rag(self, knowledge_base_id: str):
6162
if self.background_tasks is not None:
6263
self.background_tasks.add_task(self._process_pending_files, knowledge_base_id)
6364
else:
64-
await self._process_pending_files(knowledge_base_id)
65+
asyncio.create_task(self._process_pending_files(knowledge_base_id))
6566

6667
return {"status": "initialized", "knowledge_base_id": knowledge_base_id}
6768

@@ -125,3 +126,9 @@ async def _get_model_config(self, model_id: Optional[str]):
125126
if not model:
126127
raise ValueError(f"Model config with ID {model_id} not found.")
127128
return model
129+
130+
131+
async def query_rag(self, query: str, knowledge_base_id: str) -> str:
132+
if not self.rag:
133+
await self.init_graph_rag(knowledge_base_id)
134+
return await self.rag.get_knowledge_graph(query)

0 commit comments

Comments
 (0)