38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
|
|
# 简化版本的搜索端点,只支持GET方法
|
|||
|
|
@router.get("/{kb_id}/search")
|
|||
|
|
async def search_knowledge_base(
|
|||
|
|
kb_id: int,
|
|||
|
|
query: str,
|
|||
|
|
limit: int = 5,
|
|||
|
|
db: Session = Depends(get_db),
|
|||
|
|
current_user: User = Depends(AuthService.get_current_user)
|
|||
|
|
):
|
|||
|
|
"""Search documents in a knowledge base."""
|
|||
|
|
try:
|
|||
|
|
# Verify knowledge base exists and user has access
|
|||
|
|
kb_service = KnowledgeBaseService(db)
|
|||
|
|
kb = kb_service.get_knowledge_base(kb_id)
|
|||
|
|
if not kb:
|
|||
|
|
raise HTTPException(
|
|||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|||
|
|
detail="Knowledge base not found"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# Perform search
|
|||
|
|
doc_service = DocumentService(db)
|
|||
|
|
results = doc_service.search_documents(kb_id, query, limit)
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
"knowledge_base_id": kb_id,
|
|||
|
|
"query": query,
|
|||
|
|
"results": results,
|
|||
|
|
"total_results": len(results)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
except HTTPException:
|
|||
|
|
raise
|
|||
|
|
except Exception as e:
|
|||
|
|
raise HTTPException(
|
|||
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|||
|
|
detail=f"Failed to search knowledge base: {str(e)}"
|
|||
|
|
)
|