-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_llm.py
More file actions
116 lines (93 loc) · 3.78 KB
/
test_llm.py
File metadata and controls
116 lines (93 loc) · 3.78 KB
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
"""
Test script for LLM integration in RAG system
"""
import sys
import os
from pathlib import Path
# Add the src directory to Python path
sys.path.append(str(Path(__file__).parent))
from src.main_pipeline import load_config, RAGPipeline
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_llm_integration():
"""Test the LLM integration"""
try:
print("🚀 Testing LLM Integration...")
# Load configuration
config = load_config()
print(f"✅ Configuration loaded: {config.get('generation', {}).get('provider', 'unknown')} provider")
# Initialize pipeline
pipeline = RAGPipeline(config)
print("✅ RAG Pipeline initialized successfully")
# Test with a simple query (without documents for now)
test_query = "What is artificial intelligence?"
# Create a mock retrieved document for testing
from src.retrieval.retriever import RetrievalResult
mock_doc = RetrievalResult(
text="Artificial intelligence (AI) is a field of computer science that focuses on creating systems capable of performing tasks that typically require human intelligence.",
page_number=1,
chunk_id="test_chunk_1",
similarity_score=0.9,
metadata={"source": "test_document"},
source_document="test.pdf"
)
# Test response generation
print(f"\n🤖 Testing query: '{test_query}'")
response = pipeline.response_generator.generate_response(test_query, [mock_doc])
print("\n📝 Generated Response:")
print("-" * 50)
print(f"Answer: {response.answer}")
print(f"Confidence: {response.confidence_score:.2f}")
print(f"Citations: {len(response.citations)}")
print(f"Provider: {response.metadata.get('provider', 'unknown')}")
print(f"Model: {response.metadata.get('model', 'unknown')}")
print("-" * 50)
return True
except Exception as e:
print(f"❌ Error during testing: {e}")
logger.error(f"Test failed: {e}", exc_info=True)
return False
def check_environment():
"""Check if environment variables are set up correctly"""
print("\n🔍 Checking Environment Variables...")
required_vars = {
'openai': 'OPENAI_API_KEY',
'anthropic': 'ANTHROPIC_API_KEY',
'local': 'LOCAL_MODEL_PATH'
}
config = load_config()
provider = config.get('generation', {}).get('provider', 'openai')
print(f"Current provider: {provider}")
if provider in required_vars:
var_name = required_vars[provider]
value = os.getenv(var_name)
if value:
print(f"✅ {var_name} is set")
if provider in ['openai', 'anthropic']:
print(f" Key preview: {value[:8]}...")
else:
print(f"❌ {var_name} is not set")
print(f" Please set {var_name} in your .env file")
return False
return True
if __name__ == "__main__":
print("=" * 60)
print("RAG SYSTEM LLM INTEGRATION TEST")
print("=" * 60)
# Check environment first
if not check_environment():
print("\n❌ Environment setup incomplete. Please configure your .env file.")
sys.exit(1)
# Run the test
success = test_llm_integration()
if success:
print("\n🎉 LLM Integration test completed successfully!")
print("\nNext steps:")
print("1. Add your API key to the .env file")
print("2. Test with actual PDF documents")
print("3. Run the full RAG pipeline")
else:
print("\n❌ LLM Integration test failed. Check the logs above.")
sys.exit(1)