hxf/backend/th_agenter/api/endpoints/knowledge_base_simple.py

38 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 简化版本的搜索端点只支持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)}"
)