Files
fastapi/app/services/knowledge_service.py
T
2026-06-04 10:55:23 +08:00

34 lines
1.3 KiB
Python

from sqlalchemy.orm import Session
from app.repositories.knowledge_repository import KnowledgeRepository
class KnowledgeService:
"""知识库服务:检索评分参考指南并整理来源引用。"""
def __init__(self, db: Session) -> None:
self.repo = KnowledgeRepository(db)
def search_guidelines(self, department_id: int, training_type: str, keywords: list[str]) -> dict:
"""指南检索:根据科室、任务类型和关键词返回评分参考片段。"""
chunks = self.repo.search_chunks(department_id=department_id, task_type=training_type, keywords=keywords)
matched_chunks = [
{
"chunk_id": chunk.id,
"document_id": chunk.document_id,
"text": chunk.chunk_text,
"keywords": chunk.keywords or [],
"weight": float(chunk.weight),
}
for chunk in chunks
]
source_refs = [
{
"document_id": chunk.document_id,
"title": chunk.document.title if chunk.document else "",
"chunk_id": chunk.id,
}
for chunk in chunks
]
return {"matched_chunks": matched_chunks, "source_refs": source_refs, "no_match": not matched_chunks}