File size: 3,419 Bytes
b30f068 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | """
RAG Tool
Searches documentation and knowledge base for answers
"""
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
from typing import Dict
from src.rag.hybrid_retriever import HybridRetriever
def execute_rag_tool(question: str) -> Dict:
"""
Execute RAG tool - search documentation and knowledge base
Args:
question: User's natural language question
Returns:
Dict with tool execution result:
{
"success": True/False,
"tool": "rag",
"data": {
"api_matches": [...],
"document_context": [...]
} or None,
"error": "error message" if failed
}
Examples:
>>> execute_rag_tool("How do I use the weather API?")
{
"success": True,
"tool": "rag",
"data": {
"api_matches": [...],
"document_context": [...]
}
}
"""
try:
# Initialize hybrid retriever
retriever = HybridRetriever()
# Search for relevant content
results = retriever.retrieve(question, top_k=5)
# Check if we found anything
if not results["results"] and not results["document_context"]:
return {
"success": False,
"tool": "rag",
"error": "No relevant documentation found. Make sure PDFs are processed: python src/cdms/document_loader.py",
"data": {
"api_matches": [],
"document_context": []
}
}
# Return successful result
return {
"success": True,
"tool": "rag",
"data": {
"api_matches": results["results"],
"document_context": results["document_context"]
}
}
except Exception as e:
return {
"success": False,
"tool": "rag",
"error": f"Unexpected error: {str(e)}"
}
# Test function
if __name__ == "__main__":
print("Testing RAG Tool...")
print("-" * 70)
test_questions = [
"How do I use the weather API?",
"What's the weather API?",
"Show me documentation about APIs",
"How can I get weather data?"
]
for question in test_questions:
print(f"\n📝 Question: {question}")
print("-" * 70)
result = execute_rag_tool(question)
if result["success"]:
print("✅ Success!")
data = result["data"]
if data["api_matches"]:
print(f"\n🎯 API Matches ({len(data['api_matches'])}):")
for api in data["api_matches"][:3]:
print(f" • {api['api_name']}: {api['score']}% match")
if data["document_context"]:
print(f"\n📚 Document Context ({len(data['document_context'])}):")
for doc in data["document_context"][:2]:
print(f" • {doc['source_file']}: {doc['score']:.2f} similarity")
print(f" Preview: {doc['content'][:100]}...")
else:
print(f"❌ Error: {result['error']}")
|