Spaces:
Running
Running
File size: 1,184 Bytes
478bfd0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
from agent.rag_engine import RAGEngine
_rag_engine = None
def get_rag_engine():
global _rag_engine
if _rag_engine is None:
_rag_engine = RAGEngine()
return _rag_engine
async def search_documents(query: str, k: int = 5) -> dict:
"""
Search documents using RAG
Args:
query: Search query
k: Number of results
Returns:
Dict with search results
"""
try:
rag = get_rag_engine()
results = await rag.search(query, k=k)
return {
'success': True,
'query': query,
'results': results,
'count': len(results)
}
except Exception as e:
return {'error': str(e), 'success': False}
async def add_document_to_rag(text: str, metadata: dict) -> dict:
"""Add document to RAG index"""
try:
rag = get_rag_engine()
doc_id = await rag.add_document(text, metadata)
return {
'success': True,
'document_id': doc_id,
'text_length': len(text)
}
except Exception as e:
return {'error': str(e), 'success': False} |