File size: 9,294 Bytes
e66f0d9 | 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 | # debug_rag.py - Run this script to test and debug your RAG system
import sys
import os
import json
from datetime import datetime
# Add the app directory to Python path
sys.path.append(os.path.join(os.getcwd(), 'app'))
def test_rag_system():
"""Test the RAG system functionality."""
print("๐ง Testing RAG System...")
print("=" * 50)
try:
# Import the RAG module
from rag_integration import (
vectorstore,
debug_add_test_data,
query_rag_vectorstore,
get_vectorstore_stats,
add_to_rag_vectorstore,
force_reinitialize
)
print("โ
RAG module imported successfully")
# Check vectorstore status
if vectorstore is None:
print("โ Vectorstore is None - attempting force reinitialization...")
if force_reinitialize():
print("โ
Force reinitialization successful")
else:
print("โ Force reinitialization failed")
return False
else:
print(f"โ
Vectorstore loaded with {vectorstore.index.ntotal} documents")
# Get and display stats
print("\n๐ Vectorstore Statistics:")
stats = get_vectorstore_stats()
for key, value in stats.items():
if isinstance(value, dict):
print(f" {key}:")
for sub_key, sub_value in value.items():
print(f" {sub_key}: {sub_value}")
else:
print(f" {key}: {value}")
# Add test data
print("\nโ Adding test data...")
test_count = debug_add_test_data()
print(f"โ
Added {test_count} test entries")
# Test queries
print("\n๐ Testing queries...")
test_queries = [
"cooking tutorial",
"video analysis",
"nature documentary",
"recipe ingredients",
"animal species"
]
for query in test_queries:
results = query_rag_vectorstore(query, k=3)
print(f" Query: '{query}' -> {len(results)} results")
for i, doc in enumerate(results[:2]): # Show first 2 results
preview = doc.page_content[:100] + "..." if len(doc.page_content) > 100 else doc.page_content
print(f" {i+1}: {preview}")
print("\nโ
RAG system test completed successfully!")
return True
except ImportError as e:
print(f"โ Failed to import RAG module: {e}")
print("๐ก Make sure you have installed: pip install langchain-community sentence-transformers faiss-cpu")
return False
except Exception as e:
print(f"โ Error testing RAG system: {e}")
return False
def install_dependencies():
"""Install required dependencies."""
print("๐ฆ Installing RAG dependencies...")
dependencies = [
"langchain-community",
"sentence-transformers",
"faiss-cpu",
"pickle5" # For Python < 3.8 compatibility
]
import subprocess
for dep in dependencies:
try:
print(f"Installing {dep}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", dep])
print(f"โ
{dep} installed successfully")
except subprocess.CalledProcessError as e:
print(f"โ Failed to install {dep}: {e}")
def create_sample_data():
"""Create comprehensive sample data for testing."""
print("\n๐ฏ Creating comprehensive sample data...")
try:
from rag_integration import add_to_rag_vectorstore
sample_data = [
{
"text": "Video Analysis: A cooking tutorial showing how to make pasta. The chef demonstrates boiling water, adding salt, and cooking spaghetti for 8-10 minutes. The video has clear audio and good lighting.",
"content_type": "video_analysis",
"session_id": "cooking_session_1"
},
{
"text": "User Question: What ingredients do I need for the pasta recipe? The user is asking about the specific ingredients shown in the cooking video.",
"content_type": "user_query",
"session_id": "cooking_session_1"
},
{
"text": "AI Response: Based on the video analysis, the pasta recipe requires: spaghetti noodles, water, salt, olive oil, garlic, tomatoes, and fresh basil. The chef also uses parmesan cheese for garnish.",
"content_type": "ai_response",
"session_id": "cooking_session_1"
},
{
"text": "Video Analysis: Nature documentary featuring African wildlife. Shows lions hunting zebras in the savanna. Excellent cinematography with drone footage and close-up shots of animal behavior.",
"content_type": "video_analysis",
"session_id": "nature_session_1"
},
{
"text": "Video Analysis: Educational content about machine learning concepts. The instructor explains neural networks using whiteboard diagrams and code examples in Python.",
"content_type": "video_analysis",
"session_id": "ml_session_1"
},
{
"text": "System Capability: The AI can identify objects, people, animals, text, and activities in videos. It can also analyze video quality, lighting, audio, and provide detailed scene descriptions.",
"content_type": "capability",
"session_id": "global"
},
{
"text": "User Pattern: Users frequently ask about identifying objects in videos, understanding video content, and getting summaries of long videos.",
"content_type": "user_pattern",
"session_id": "global"
}
]
success_count = 0
for entry in sample_data:
if add_to_rag_vectorstore(
text=entry["text"],
session_id=entry["session_id"],
content_type=entry["content_type"],
source="sample"
):
success_count += 1
print(f"โ
Created {success_count}/{len(sample_data)} sample entries")
return True
except Exception as e:
print(f"โ Failed to create sample data: {e}")
return False
def interactive_query_test():
"""Interactive query testing."""
print("\n๐ฎ Interactive Query Test")
print("Type queries to test the RAG system. Type 'quit' to exit.")
print("-" * 50)
try:
from rag_integration import query_rag_vectorstore, get_vectorstore_stats
while True:
query = input("\n๐ Enter query: ").strip()
if query.lower() in ['quit', 'exit', 'q']:
break
if not query:
continue
print(f"Searching for: '{query}'...")
results = query_rag_vectorstore(query, k=5)
if results:
print(f"Found {len(results)} results:")
for i, doc in enumerate(results, 1):
print(f"\n{i}. Content: {doc.page_content[:150]}...")
print(f" Metadata: {doc.metadata}")
else:
print("No results found.")
# Show stats for debugging
stats = get_vectorstore_stats()
print(f"Total documents in store: {stats.get('total_documents', 0)}")
except KeyboardInterrupt:
print("\n๐ Exiting interactive test...")
except Exception as e:
print(f"โ Error in interactive test: {e}")
if __name__ == "__main__":
print("๐ฅ AI Video Chat RAG System Debug Tool")
print("=" * 50)
# Check if dependencies need to be installed
try:
import langchain_community
import sentence_transformers
import faiss
print("โ
All dependencies are available")
except ImportError:
print("โ ๏ธ Missing dependencies detected")
install_deps = input("Install missing dependencies? (y/n): ").lower().startswith('y')
if install_deps:
install_dependencies()
else:
print("โ Cannot proceed without dependencies")
sys.exit(1)
# Main test sequence
success = test_rag_system()
if success:
# Create more comprehensive sample data
create_sample_data()
# Offer interactive testing
interactive_test = input("\n๐ฎ Run interactive query test? (y/n): ").lower().startswith('y')
if interactive_test:
interactive_query_test()
print("\n๐ Debug session completed!")
print("๐ Check the 'rag_data/debug_info.json' file for detailed logs.") |