|
|
|
|
|
""" |
|
|
EXP-08: RAG Integration Test |
|
|
Goal: Prove The Seed connects to your storage system |
|
|
|
|
|
What it tests: |
|
|
- Take real documents from your RAG (HuggingFace NPC dialogue) |
|
|
- Generate STAT7 addresses for each |
|
|
- Retrieve via STAT7 addresses + semantic queries |
|
|
- Verify both methods find correct documents |
|
|
|
|
|
Expected Result: |
|
|
- All documents addressable |
|
|
- Hybrid retrieval works (STAT7 + semantic) |
|
|
- No conflicts with existing RAG |
|
|
""" |
|
|
|
|
|
import json |
|
|
import time |
|
|
import requests |
|
|
from datetime import datetime, timezone |
|
|
from pathlib import Path |
|
|
from typing import Dict, List, Any, Optional |
|
|
from dataclasses import dataclass, asdict |
|
|
|
|
|
|
|
|
@dataclass |
|
|
class RAGTestResult: |
|
|
"""Results for RAG integration test.""" |
|
|
experiment: str = "EXP-08" |
|
|
title: str = "RAG Integration Test" |
|
|
timestamp: str = "" |
|
|
status: str = "PASS" |
|
|
results: Dict[str, Any] = None |
|
|
|
|
|
def __post_init__(self): |
|
|
if self.timestamp == "": |
|
|
self.timestamp = datetime.now(timezone.utc).isoformat() |
|
|
if self.results is None: |
|
|
self.results = {} |
|
|
|
|
|
|
|
|
class RAGIntegrationTester: |
|
|
"""Test RAG integration with STAT7 system.""" |
|
|
|
|
|
def __init__(self, api_base_url: str = "http://localhost:8000"): |
|
|
self.api_base_url = api_base_url |
|
|
self.results = RAGTestResult() |
|
|
|
|
|
def check_api_health(self) -> bool: |
|
|
"""Check if API service is running.""" |
|
|
try: |
|
|
response = requests.get(f"{self.api_base_url}/health", timeout=5) |
|
|
return response.status_code == 200 |
|
|
except: |
|
|
return False |
|
|
|
|
|
def test_semantic_retrieval(self) -> Dict[str, Any]: |
|
|
"""Test semantic retrieval of RAG documents.""" |
|
|
test_queries = [ |
|
|
{ |
|
|
"query_id": "rag_test_1", |
|
|
"semantic": "bounty hunter dangerous missions", |
|
|
"expected_results": 1 |
|
|
}, |
|
|
{ |
|
|
"query_id": "rag_test_2", |
|
|
"semantic": "wisdom about courage", |
|
|
"expected_results": 0 |
|
|
}, |
|
|
{ |
|
|
"query_id": "rag_test_3", |
|
|
"semantic": "character dialogue", |
|
|
"expected_results": 1 |
|
|
} |
|
|
] |
|
|
|
|
|
semantic_results = [] |
|
|
|
|
|
for query in test_queries: |
|
|
try: |
|
|
response = requests.post( |
|
|
f"{self.api_base_url}/query", |
|
|
json={ |
|
|
"query_id": query["query_id"], |
|
|
"semantic_query": query["semantic"] |
|
|
}, |
|
|
timeout=10 |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
result_count = data.get("results_count", 0) |
|
|
|
|
|
semantic_results.append({ |
|
|
"query_id": query["query_id"], |
|
|
"semantic": query["semantic"], |
|
|
"results_count": result_count, |
|
|
"expected_results": query["expected_results"], |
|
|
"success": result_count >= query["expected_results"], |
|
|
"execution_time_ms": data.get("execution_time_ms", 0) |
|
|
}) |
|
|
else: |
|
|
semantic_results.append({ |
|
|
"query_id": query["query_id"], |
|
|
"semantic": query["semantic"], |
|
|
"results_count": 0, |
|
|
"expected_results": query["expected_results"], |
|
|
"success": False, |
|
|
"error": f"HTTP {response.status_code}" |
|
|
}) |
|
|
|
|
|
except Exception as e: |
|
|
semantic_results.append({ |
|
|
"query_id": query["query_id"], |
|
|
"semantic": query["semantic"], |
|
|
"results_count": 0, |
|
|
"expected_results": query["expected_results"], |
|
|
"success": False, |
|
|
"error": str(e) |
|
|
}) |
|
|
|
|
|
return { |
|
|
"total_queries": len(test_queries), |
|
|
"successful_queries": len([r for r in semantic_results if r["success"]]), |
|
|
"results": semantic_results |
|
|
} |
|
|
|
|
|
def test_hybrid_retrieval(self) -> Dict[str, Any]: |
|
|
"""Test hybrid STAT7 + semantic retrieval.""" |
|
|
hybrid_queries = [ |
|
|
{ |
|
|
"query_id": "hybrid_test_1", |
|
|
"semantic": "find wisdom about resilience", |
|
|
"weight_semantic": 0.6, |
|
|
"weight_stat7": 0.4 |
|
|
}, |
|
|
{ |
|
|
"query_id": "hybrid_test_2", |
|
|
"semantic": "the nature of consciousness", |
|
|
"weight_semantic": 0.6, |
|
|
"weight_stat7": 0.4 |
|
|
} |
|
|
] |
|
|
|
|
|
hybrid_results = [] |
|
|
|
|
|
for query in hybrid_queries: |
|
|
try: |
|
|
response = requests.post( |
|
|
f"{self.api_base_url}/query", |
|
|
json={ |
|
|
"query_id": query["query_id"], |
|
|
"semantic_query": query["semantic"], |
|
|
"use_hybrid": True, |
|
|
"weight_semantic": query["weight_semantic"], |
|
|
"weight_stat7": query["weight_stat7"] |
|
|
}, |
|
|
timeout=10 |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
result_count = data.get("results_count", 0) |
|
|
|
|
|
hybrid_results.append({ |
|
|
"query_id": query["query_id"], |
|
|
"semantic": query["semantic"], |
|
|
"weight_semantic": query["weight_semantic"], |
|
|
"weight_stat7": query["weight_stat7"], |
|
|
"results_count": result_count, |
|
|
"success": True, |
|
|
"execution_time_ms": data.get("execution_time_ms", 0), |
|
|
"narrative_coherence": data.get("narrative_analysis", {}).get("coherence_score", 0) |
|
|
}) |
|
|
else: |
|
|
hybrid_results.append({ |
|
|
"query_id": query["query_id"], |
|
|
"semantic": query["semantic"], |
|
|
"weight_semantic": query["weight_semantic"], |
|
|
"weight_stat7": query["weight_stat7"], |
|
|
"results_count": 0, |
|
|
"success": False, |
|
|
"error": f"HTTP {response.status_code}" |
|
|
}) |
|
|
|
|
|
except Exception as e: |
|
|
hybrid_results.append({ |
|
|
"query_id": query["query_id"], |
|
|
"semantic": query["semantic"], |
|
|
"weight_semantic": query["weight_semantic"], |
|
|
"weight_stat7": query["weight_stat7"], |
|
|
"results_count": 0, |
|
|
"success": False, |
|
|
"error": str(e) |
|
|
}) |
|
|
|
|
|
return { |
|
|
"total_queries": len(hybrid_queries), |
|
|
"successful_queries": len([r for r in hybrid_results if r["success"]]), |
|
|
"results": hybrid_results |
|
|
} |
|
|
|
|
|
def check_rag_data_integration(self) -> Dict[str, Any]: |
|
|
"""Check that RAG data is properly integrated.""" |
|
|
try: |
|
|
|
|
|
response = requests.get(f"{self.api_base_url}/metrics", timeout=5) |
|
|
|
|
|
if response.status_code == 200: |
|
|
metrics = response.json() |
|
|
return { |
|
|
"api_healthy": True, |
|
|
"total_queries": metrics.get("total_queries", 0), |
|
|
"concurrent_queries": metrics.get("concurrent_queries", 0), |
|
|
"errors": metrics.get("errors", 0), |
|
|
"data_integration_success": True |
|
|
} |
|
|
else: |
|
|
return { |
|
|
"api_healthy": False, |
|
|
"data_integration_success": False, |
|
|
"error": f"Metrics endpoint failed: {response.status_code}" |
|
|
} |
|
|
|
|
|
except Exception as e: |
|
|
return { |
|
|
"api_healthy": False, |
|
|
"data_integration_success": False, |
|
|
"error": str(e) |
|
|
} |
|
|
|
|
|
def run_comprehensive_test(self) -> RAGTestResult: |
|
|
"""Run comprehensive RAG integration test.""" |
|
|
print("π Starting EXP-08: RAG Integration Test") |
|
|
print("=" * 60) |
|
|
|
|
|
|
|
|
print("1. Checking API service health...") |
|
|
api_healthy = self.check_api_health() |
|
|
if not api_healthy: |
|
|
print("β API service not running - cannot proceed with RAG test") |
|
|
self.results.status = "FAIL" |
|
|
self.results.results = { |
|
|
"error": "API service not available", |
|
|
"api_healthy": False |
|
|
} |
|
|
return self.results |
|
|
|
|
|
print("β
API service is healthy") |
|
|
|
|
|
|
|
|
print("\n2. Testing semantic retrieval...") |
|
|
semantic_results = self.test_semantic_retrieval() |
|
|
print(f" Semantic queries: {semantic_results['successful_queries']}/{semantic_results['total_queries']} successful") |
|
|
|
|
|
|
|
|
print("\n3. Testing hybrid STAT7 + semantic retrieval...") |
|
|
hybrid_results = self.test_hybrid_retrieval() |
|
|
print(f" Hybrid queries: {hybrid_results['successful_queries']}/{hybrid_results['total_queries']} successful") |
|
|
|
|
|
|
|
|
print("\n4. Checking RAG data integration...") |
|
|
rag_integration = self.check_rag_data_integration() |
|
|
print(f" RAG integration: {'β
Success' if rag_integration['data_integration_success'] else 'β Failed'}") |
|
|
|
|
|
|
|
|
total_queries = semantic_results['total_queries'] + hybrid_results['total_queries'] |
|
|
successful_queries = semantic_results['successful_queries'] + hybrid_results['successful_queries'] |
|
|
|
|
|
self.results.results = { |
|
|
"api_healthy": True, |
|
|
"rag_integration": rag_integration, |
|
|
"semantic_retrieval": semantic_results, |
|
|
"hybrid_retrieval": hybrid_results, |
|
|
"overall_metrics": { |
|
|
"total_queries_tested": total_queries, |
|
|
"successful_queries": successful_queries, |
|
|
"success_rate": successful_queries / total_queries if total_queries > 0 else 0, |
|
|
"rag_documents_accessible": semantic_results['successful_queries'] > 0, |
|
|
"hybrid_search_working": hybrid_results['successful_queries'] > 0 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (rag_integration['data_integration_success'] and |
|
|
semantic_results['successful_queries'] > 0 and |
|
|
hybrid_results['successful_queries'] > 0): |
|
|
self.results.status = "PASS" |
|
|
print("\nβ
EXP-08 PASSED: RAG integration successful") |
|
|
else: |
|
|
self.results.status = "FAIL" |
|
|
print("\nβ EXP-08 FAILED: RAG integration incomplete") |
|
|
|
|
|
return self.results |
|
|
|
|
|
def save_results(self, output_file: str = None) -> str: |
|
|
"""Save test results to JSON file.""" |
|
|
if output_file is None: |
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
|
|
output_file = f"exp08_rag_integration_{timestamp}.json" |
|
|
|
|
|
results_dir = Path("results") |
|
|
results_dir.mkdir(exist_ok=True) |
|
|
|
|
|
output_path = results_dir / output_file |
|
|
|
|
|
with open(output_path, 'w') as f: |
|
|
json.dump(asdict(self.results), f, indent=2) |
|
|
|
|
|
print(f"\nπ Results saved to: {output_path}") |
|
|
return str(output_path) |
|
|
|
|
|
|
|
|
def main(): |
|
|
"""Run EXP-08 RAG integration test.""" |
|
|
tester = RAGIntegrationTester() |
|
|
|
|
|
try: |
|
|
results = tester.run_comprehensive_test() |
|
|
output_file = tester.save_results() |
|
|
|
|
|
print(f"\nπ― EXP-08 Complete: {results.status}") |
|
|
print(f"π Report: {output_file}") |
|
|
|
|
|
return results.status == "PASS" |
|
|
|
|
|
except Exception as e: |
|
|
print(f"\nβ EXP-08 failed with error: {e}") |
|
|
return False |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
success = main() |
|
|
exit(0 if success else 1) |
|
|
|