add knowledge-base-search api
This commit is contained in:
parent
76655eb4b1
commit
da86e7d670
@ -2016,4 +2016,124 @@ class AIChatView(BaseView):
|
||||
message_type=message_type,
|
||||
text_content=content
|
||||
)
|
||||
|
||||
|
||||
class KnowledgeBaseSearchView(BaseView):
|
||||
"""知识库搜索API - 直接检索知识库内容"""
|
||||
name = 'knowledge-base-search'
|
||||
auth_check = None
|
||||
|
||||
def post(self, request):
|
||||
"""搜索知识库"""
|
||||
try:
|
||||
# 获取请求参数
|
||||
query = request.data.get('query')
|
||||
max_results = request.data.get('max_results', 5)
|
||||
tenant_id = request.data.get('tenant_id')
|
||||
|
||||
# 验证必需参数
|
||||
if not query:
|
||||
return JsonResponse({
|
||||
'error': 'query is required'
|
||||
}, status=400)
|
||||
|
||||
# 验证 max_results
|
||||
if not isinstance(max_results, int) or max_results < 1 or max_results > 20:
|
||||
return JsonResponse({
|
||||
'error': 'max_results must be an integer between 1 and 20'
|
||||
}, status=400)
|
||||
|
||||
# 导入 RAG 服务
|
||||
try:
|
||||
from .rag_service import CachedLangChainRAG
|
||||
except ImportError as e:
|
||||
return JsonResponse({
|
||||
'error': f'RAG service not available: {str(e)}'
|
||||
}, status=500)
|
||||
|
||||
# 创建 RAG 服务实例
|
||||
try:
|
||||
rag_service = CachedLangChainRAG()
|
||||
except Exception as e:
|
||||
return JsonResponse({
|
||||
'error': f'Failed to initialize RAG service: {str(e)}'
|
||||
}, status=500)
|
||||
|
||||
# 执行搜索
|
||||
try:
|
||||
results = rag_service.search(
|
||||
query=query,
|
||||
max_results=max_results,
|
||||
tenant_id=tenant_id
|
||||
)
|
||||
|
||||
return JsonResponse({
|
||||
'query': query,
|
||||
'max_results': max_results,
|
||||
'tenant_id': tenant_id,
|
||||
'results': results,
|
||||
'count': len(results)
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return JsonResponse({
|
||||
'error': f'Search failed: {str(e)}'
|
||||
}, status=500)
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return JsonResponse({
|
||||
'error': f'Request processing failed: {str(e)}'
|
||||
}, status=500)
|
||||
|
||||
def get(self, request):
|
||||
"""获取知识库搜索使用说明"""
|
||||
return JsonResponse({
|
||||
'name': 'Knowledge Base Search API',
|
||||
'description': 'Search the knowledge base for relevant information',
|
||||
'method': 'POST',
|
||||
'parameters': {
|
||||
'query': {
|
||||
'type': 'string',
|
||||
'required': True,
|
||||
'description': 'The search query'
|
||||
},
|
||||
'max_results': {
|
||||
'type': 'integer',
|
||||
'required': False,
|
||||
'default': 5,
|
||||
'range': '1-20',
|
||||
'description': 'Maximum number of results to return'
|
||||
},
|
||||
'tenant_id': {
|
||||
'type': 'integer',
|
||||
'required': False,
|
||||
'description': 'Tenant ID for tenant-specific search'
|
||||
}
|
||||
},
|
||||
'response': {
|
||||
'query': 'The search query',
|
||||
'max_results': 'Maximum results requested',
|
||||
'tenant_id': 'Tenant ID (if provided)',
|
||||
'results': [
|
||||
{
|
||||
'article_id': 'Article ID',
|
||||
'title': 'Article title',
|
||||
'content': 'Relevant content snippet',
|
||||
'score': 'Relevance score',
|
||||
'metadata': 'Additional metadata'
|
||||
}
|
||||
],
|
||||
'count': 'Number of results returned'
|
||||
},
|
||||
'example': {
|
||||
'request': {
|
||||
'query': '支持多语言吗',
|
||||
'max_results': 3
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user