File size: 2,674 Bytes
7c012de |
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 |
#!/usr/bin/env python3
"""
Quick test for Knowledge Base Browser component
"""
import os
import sys
import time
from pathlib import Path
# Add kb_browser to path
sys.path.insert(0, str(Path(__file__).parent / "kb_browser"))
try:
from kb_browser.retriever import KnowledgeRetriever
from kb_browser import KnowledgeBrowser
print("β Successfully imported components")
# Test 1: Basic retriever functionality
print("\n1. Testing basic retriever...")
retriever = KnowledgeRetriever()
print(f"β Retriever initialized with {len(retriever.documents)} sample documents")
# Test 2: Text search
print("\n2. Testing text search...")
results = retriever.search("retrieval augmented generation", search_type="keyword", k=2)
print(f"β Text search found {results['total_count']} results in {results['search_time']:.3f}s")
if results['documents']:
doc = results['documents'][0]
print(f" - First result: '{doc['title'][:50]}...'")
print(f" - Relevance: {doc['relevance_score']:.2f}")
# Test 3: Gradio component
print("\n3. Testing Gradio component...")
kb_browser = KnowledgeBrowser()
component_results = kb_browser.search("vector databases", max_results=1)
print(f"β Component search returned {len(component_results.get('results', []))} results")
# Test 4: API info
print("\n4. Testing API structure...")
api_info = kb_browser.api_info()
properties = api_info['info']['properties']
print(f"β API has {len(properties)} properties: {list(properties.keys())}")
# Test 5: Check OpenAI integration
print("\n5. Checking OpenAI integration...")
openai_key = os.getenv('OPENAI_API_KEY')
if openai_key:
print("β OpenAI API key is available")
try:
# Try semantic search
semantic_results = retriever.search("LlamaIndex", search_type="semantic", k=1)
print(f"β Semantic search completed in {semantic_results['search_time']:.3f}s")
except Exception as e:
print(f"β Semantic search fell back to text search: {str(e)[:50]}...")
else:
print("β OpenAI API key not found, using text search fallback")
print("\nπ All tests completed successfully!")
print("\nComponent is ready for:")
print("- Human interactive search")
print("- AI agent integration")
print("- Citation tracking")
print("- Multi-modal search (semantic, keyword, hybrid)")
except ImportError as e:
print(f"β Import error: {e}")
except Exception as e:
print(f"β Test error: {e}")
import traceback
traceback.print_exc() |