-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_diagnostics.py
More file actions
357 lines (283 loc) · 10.4 KB
/
rag_diagnostics.py
File metadata and controls
357 lines (283 loc) · 10.4 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
"""
RAG System Testing and Management Utility
Test the RAG system, manage knowledge base, and verify configurations
"""
import os
import sys
import logging
from typing import List
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
def print_header(title: str):
"""Print a formatted header."""
print("\n" + "="*60)
print(f" {title}")
print("="*60 + "\n")
def check_environment():
"""Check if all required environment variables are set."""
print_header("🔧 Checking Environment Variables")
required_vars = {
'PORCUPINE_API_KEY': 'Wake word detection API key',
'GROQ_API_KEY': 'Groq LLM API key',
'OPEN_API': 'OpenAI API key'
}
all_present = True
for var, description in required_vars.items():
value = os.getenv(var)
status = "✅" if value else "❌"
print(f"{status} {var:20} - {description}")
if not value:
all_present = False
if all_present:
print("\n✅ All environment variables are configured!")
else:
print("\n❌ Some environment variables are missing. Create .env file.")
return all_present
def check_dependencies():
"""Check if all required packages are installed."""
print_header("📦 Checking Dependencies")
required_packages = {
'groq': 'Groq API client',
'speech_recognition': 'Speech recognition',
'faiss': 'Vector search (faiss-cpu)',
'pyttsx3': 'Text-to-speech',
'pvporcupine': 'Wake word detection',
'sounddevice': 'Audio input/output',
'openai': 'OpenAI API',
'dotenv': 'Environment variable management',
'sklearn': 'Scikit-learn',
'numpy': 'NumPy',
'scipy': 'SciPy',
'langchain_text_splitters': 'LangChain text splitting'
}
missing = []
installed = []
for package, description in required_packages.items():
try:
__import__(package.replace('-', '_'))
print(f"✅ {package:30} - {description}")
installed.append(package)
except ImportError:
print(f"❌ {package:30} - {description} (NOT INSTALLED)")
missing.append(package)
if missing:
print(f"\n⚠️ Missing packages: {', '.join(missing)}")
print(f"\nInstall with: pip install {' '.join(missing)}")
return False
else:
print("\n✅ All dependencies are installed!")
return True
def check_files():
"""Check if all required files exist."""
print_header("📁 Checking Files")
required_files = {
'info.txt': 'Knowledge base (required for first run)',
'faiss_index.idx': 'FAISS index (created on first run)',
'embeddings.npy': 'Document embeddings (created on first run)',
'doc_chunks.pkl': 'Text chunks (created on first run)',
'Hey-Bruce_en_windows_v3_0_0.ppn': 'Porcupine wake word model',
'.env': 'Environment variables'
}
critical_files = {'info.txt'} # Must exist
for filename, description in required_files.items():
exists = os.path.exists(filename)
status = "✅" if exists else "⚠️"
required = " (REQUIRED)" if filename in critical_files else " (auto-generated)"
print(f"{status} {filename:35} - {description}{required}")
info_exists = os.path.exists('info.txt')
if info_exists:
size = os.path.getsize('info.txt') / 1024
lines = len(open('info.txt').readlines())
print(f"\n 📊 info.txt: {size:.1f} KB, {lines} lines")
return info_exists
def test_rag_system():
"""Test the RAG system."""
print_header("🧪 Testing RAG System")
try:
from rag_system import RAGSystem
print("Initializing RAG system...")
rag = RAGSystem()
rag.initialize()
print("✅ RAG system initialized\n")
# Test retrieval
test_queries = [
"What is your name?",
"Tell me about the system",
"How does this work?"
]
for query in test_queries:
print(f"\n📝 Query: {query}")
results = rag.retrieve_context(query, k=2)
if results:
for i, (chunk, score) in enumerate(results, 1):
confidence = rag.get_confidence_level(score)
chunk_preview = chunk[:80].replace('\n', ' ') + "..."
print(f" [{i}] Score: {score:.3f} ({confidence}) | {chunk_preview}")
else:
print(" No results found")
print("\n✅ RAG system test completed")
return True
except Exception as e:
print(f"❌ RAG system test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_prompts():
"""Test prompt generation."""
print_header("🎯 Testing Prompt Generation")
try:
from prompt_manager import PromptGenerator, SystemPrompt
print("System Prompt Preview:")
print("-" * 60)
print(SystemPrompt.get_content()[:200] + "...")
print("-" * 60 + "\n")
# Test different confidence levels
test_question = "What is the weather today?"
test_context = "The weather is sunny and warm, with temperatures around 75°F."
for confidence in ["HIGH", "MEDIUM", "LOW"]:
print(f"\n{confidence} Confidence Prompt:")
print("-" * 60)
if confidence in ["HIGH", "MEDIUM"]:
prompt = PromptGenerator.generate(
confidence,
test_question,
test_context
)
else:
prompt = PromptGenerator.generate(
confidence,
test_question
)
print(prompt[:200] + "...")
print("-" * 60)
print("\n✅ Prompt generation test completed")
return True
except Exception as e:
print(f"❌ Prompt test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_speech_recognition():
"""Test speech recognition (brief test)."""
print_header("🎤 Testing Speech Recognition")
try:
import speech_recognition as sr
recognizer = sr.Recognizer()
print("Available microphones:")
for i, name in enumerate(sr.Microphone.list_microphone_names()):
print(f" [{i}] {name}")
print("\n⚠️ Speech recognition test requires microphone input.")
print("Skipping live test. System will test on first use.")
return True
except Exception as e:
print(f"❌ Speech recognition test failed: {e}")
return False
def test_tts():
"""Test text-to-speech."""
print_header("🔊 Testing Text-to-Speech")
try:
import pyttsx3
engine = pyttsx3.init()
print(f"TTS Engine: {engine.driverName}")
print(f"Available voices: {len(engine.getProperty('voices'))}")
print("\n⚠️ Skipping audio playback test.")
print("System will test on first use.")
return True
except Exception as e:
print(f"❌ TTS test failed: {e}")
return False
def run_diagnostics():
"""Run all diagnostic tests."""
print("\n" + "="*60)
print(" 🔍 RAG System Diagnostics")
print("="*60)
results = {
"Environment": check_environment(),
"Dependencies": check_dependencies(),
"Files": check_files(),
"Prompts": test_prompts(),
"Speech Recognition": test_speech_recognition(),
"Text-to-Speech": test_tts(),
}
# Optional: Test RAG if files exist
if results["Files"]:
results["RAG System"] = test_rag_system()
# Summary
print_header("📋 Diagnostic Summary")
for test_name, passed in results.items():
status = "✅" if passed else "❌"
print(f"{status} {test_name}")
all_passed = all(results.values())
print("\n" + "-"*60)
if all_passed:
print("✅ All diagnostics passed! Ready to run S2S_v10.py")
else:
print("❌ Some diagnostics failed. Please fix issues above.")
print("-"*60 + "\n")
return all_passed
def show_menu():
"""Show interactive menu."""
while True:
print("\n" + "="*60)
print(" Bruce RAG System - Diagnostic & Management Tool")
print("="*60)
print("\n1. Run Full Diagnostics")
print("2. Check Environment Variables")
print("3. Check Dependencies")
print("4. Check Files")
print("5. Test RAG System")
print("6. Test Prompts")
print("7. View Knowledge Base Stats")
print("8. Exit")
choice = input("\nSelect option (1-8): ").strip()
if choice == "1":
run_diagnostics()
elif choice == "2":
check_environment()
elif choice == "3":
check_dependencies()
elif choice == "4":
check_files()
elif choice == "5":
test_rag_system()
elif choice == "6":
test_prompts()
elif choice == "7":
try:
from kb_manager import KnowledgeBaseManager
manager = KnowledgeBaseManager()
manager.display_stats()
except Exception as e:
print(f"Error: {e}")
elif choice == "8":
print("\nGoodbye! 👋\n")
break
else:
print("❌ Invalid option. Please try again.")
input("\nPress Enter to continue...")
def main():
"""Main entry point."""
if len(sys.argv) > 1:
if sys.argv[1] == "--full":
run_diagnostics()
elif sys.argv[1] == "--rag":
test_rag_system()
elif sys.argv[1] == "--prompts":
test_prompts()
elif sys.argv[1] == "--interactive":
show_menu()
else:
print(f"Unknown option: {sys.argv[1]}")
print("Usage: python rag_diagnostics.py [--full|--rag|--prompts|--interactive]")
else:
show_menu()
if __name__ == "__main__":
main()