Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
122 lines
3.9 KiB
Python
122 lines
3.9 KiB
Python
"""
|
|
Example usage of RAG Evaluation Framework
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path for imports
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from rag_evaluation.core.config import Config
|
|
from rag_evaluation.testset.generator import TestsetGenerator
|
|
from rag_evaluation.storage.database import DatabaseManager
|
|
|
|
|
|
def main():
|
|
"""Main example function"""
|
|
|
|
# 1. Load configuration
|
|
print("Loading configuration...")
|
|
|
|
# Try to load config.yaml first, fallback to config.example.yaml
|
|
config_file = "config.yaml"
|
|
if not Path(config_file).exists():
|
|
if Path("config.example.yaml").exists():
|
|
print("Note: config.yaml not found, using config.example.yaml")
|
|
config_file = "config.example.yaml"
|
|
else:
|
|
raise FileNotFoundError(
|
|
"Configuration file not found. "
|
|
"Please create config.yaml from config.example.yaml"
|
|
)
|
|
|
|
config = Config.from_file(config_file)
|
|
|
|
# 2. Initialize database
|
|
print("Initializing database...")
|
|
db_manager = DatabaseManager(config.database)
|
|
|
|
# Initialize schema (only needed once)
|
|
schema_file = Path(__file__).parent / "storage" / "schema.sql"
|
|
try:
|
|
db_manager.init_schema(str(schema_file))
|
|
print("Database schema initialized successfully")
|
|
except Exception as e:
|
|
print(f"Database initialization error (may already exist): {e}")
|
|
|
|
# 3. Generate testset
|
|
print("\nGenerating testset...")
|
|
generator = TestsetGenerator(config)
|
|
|
|
# Example: Generate testset from sample files
|
|
knowledge_base_id = "kb-example-001"
|
|
file_paths = [
|
|
# Add your knowledge base file paths here
|
|
# Example: "./data/docs/manual.txt"
|
|
]
|
|
|
|
if not file_paths:
|
|
print("No files specified. Creating sample file...")
|
|
# Create a sample file for demonstration
|
|
sample_dir = Path("./data/sample_docs")
|
|
sample_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
sample_file = sample_dir / "sample.txt"
|
|
with open(sample_file, 'w', encoding='utf-8') as f:
|
|
f.write("""
|
|
# Sample Knowledge Base Document
|
|
|
|
This is a sample document for RAG evaluation.
|
|
|
|
## Section 1: Introduction
|
|
RAG (Retrieval-Augmented Generation) systems combine retrieval and generation
|
|
to provide accurate and contextual answers.
|
|
|
|
## Section 2: Evaluation
|
|
Evaluating RAG systems requires measuring both retrieval quality and generation quality.
|
|
|
|
Key metrics include:
|
|
- Context Recall: How well the system retrieves relevant context
|
|
- Context Precision: How precise the retrieved context is
|
|
- Faithfulness: Whether the generated answer is faithful to the context
|
|
- Answer Relevancy: How relevant the answer is to the question
|
|
""")
|
|
|
|
file_paths = [str(sample_file)]
|
|
print(f"Created sample file: {sample_file}")
|
|
|
|
# Generate testset
|
|
testset = generator.generate(
|
|
knowledge_base_id=knowledge_base_id,
|
|
file_paths=file_paths,
|
|
test_size=15,
|
|
name="Example Testset"
|
|
)
|
|
|
|
print(f"\nTestset generated:")
|
|
print(f" ID: {testset['id']}")
|
|
print(f" Name: {testset['name']}")
|
|
print(f" Questions: {len(testset['questions'])}")
|
|
|
|
# 4. Save testset
|
|
print("\nSaving testset...")
|
|
file_path = generator.save_testset(testset)
|
|
print(f"Testset saved to: {file_path}")
|
|
|
|
# 5. Load testset (demonstration)
|
|
print("\nLoading testset...")
|
|
loaded_testset = generator.load_testset(testset['id'])
|
|
print(f"Testset loaded: {loaded_testset['name']}")
|
|
|
|
print("\n✅ Example completed successfully!")
|
|
print("\nNext steps:")
|
|
print("1. Configure your LLM API keys in config.yaml")
|
|
print("2. Add your knowledge base files")
|
|
print("3. Run evaluation (Phase 2 - coming soon)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|