fix: Apply ruff formatting and rename test_dataset to sample_dataset
**Lint Fixes (ruff)**:
- Sort imports alphabetically (I001)
- Add blank line after import traceback (E302)
- Add trailing comma to dict literals (COM812)
- Reformat writer.writerow for readability (E501)
**Rename test_dataset.json → sample_dataset.json**:
- Avoids .gitignore pattern conflict (test_* is ignored)
- More descriptive name - it's a sample/template, not actual test data
- Updated all references in eval_rag_quality.py and README.md
Resolves lint-and-format CI check failure.
Addresses reviewer feedback about test dataset naming.
(cherry picked from commit 5cdb4b0ef2)
This commit is contained in:
parent
a934becfcc
commit
949bfc4228
3 changed files with 532 additions and 723 deletions
309
lightrag/evaluation/README.md
Normal file
309
lightrag/evaluation/README.md
Normal file
|
|
@ -0,0 +1,309 @@
|
||||||
|
# 📊 Portfolio RAG Evaluation Framework
|
||||||
|
|
||||||
|
RAGAS-based offline evaluation of your LightRAG portfolio system.
|
||||||
|
|
||||||
|
## What is RAGAS?
|
||||||
|
|
||||||
|
**RAGAS** (Retrieval Augmented Generation Assessment) is a framework for reference-free evaluation of RAG systems using LLMs.
|
||||||
|
|
||||||
|
Instead of requiring human-annotated ground truth, RAGAS uses state-of-the-art evaluation metrics:
|
||||||
|
|
||||||
|
### Core Metrics
|
||||||
|
|
||||||
|
| Metric | What It Measures | Good Score |
|
||||||
|
|--------|-----------------|-----------|
|
||||||
|
| **Faithfulness** | Is the answer factually accurate based on retrieved context? | > 0.80 |
|
||||||
|
| **Answer Relevance** | Is the answer relevant to the user's question? | > 0.80 |
|
||||||
|
| **Context Recall** | Was all relevant information retrieved from documents? | > 0.80 |
|
||||||
|
| **Context Precision** | Is retrieved context clean without irrelevant noise? | > 0.80 |
|
||||||
|
| **RAGAS Score** | Overall quality metric (average of above) | > 0.80 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📁 Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
lightrag/evaluation/
|
||||||
|
├── eval_rag_quality.py # Main evaluation script
|
||||||
|
├── sample_dataset.json # Test cases with ground truth
|
||||||
|
├── __init__.py # Package init
|
||||||
|
├── results/ # Output directory
|
||||||
|
│ ├── results_YYYYMMDD_HHMMSS.json # Raw metrics
|
||||||
|
│ └── report_YYYYMMDD_HHMMSS.html # Beautiful HTML report
|
||||||
|
└── README.md # This file
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚀 Quick Start
|
||||||
|
|
||||||
|
### 1. Install Dependencies
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install ragas datasets langfuse
|
||||||
|
```
|
||||||
|
|
||||||
|
Or use your project dependencies (already included in pyproject.toml):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -e ".[offline-llm]"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Run Evaluation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd /path/to/LightRAG
|
||||||
|
python -m lightrag.evaluation.eval_rag_quality
|
||||||
|
```
|
||||||
|
|
||||||
|
Or directly:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python lightrag/evaluation/eval_rag_quality.py
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. View Results
|
||||||
|
|
||||||
|
Results are saved automatically in `lightrag/evaluation/results/`:
|
||||||
|
|
||||||
|
```
|
||||||
|
results/
|
||||||
|
├── results_20241023_143022.json ← Raw metrics (for analysis)
|
||||||
|
└── report_20241023_143022.html ← Beautiful HTML report 🌟
|
||||||
|
```
|
||||||
|
|
||||||
|
**Open the HTML report in your browser to see:**
|
||||||
|
- ✅ Overall RAGAS score
|
||||||
|
- 📊 Per-metric averages
|
||||||
|
- 📋 Individual test case results
|
||||||
|
- 📈 Performance breakdown
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 Test Dataset
|
||||||
|
|
||||||
|
Edit `sample_dataset.json` to add your own test cases:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"test_cases": [
|
||||||
|
{
|
||||||
|
"question": "Your test question here",
|
||||||
|
"ground_truth": "Expected answer with key information",
|
||||||
|
"project_context": "project_name"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"question": "Which projects use PyTorch?",
|
||||||
|
"ground_truth": "The Neural ODE Project uses PyTorch with TorchODE library for continuous-time neural networks.",
|
||||||
|
"project_context": "neural_ode_project"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔧 Integration with Your RAG System
|
||||||
|
|
||||||
|
Currently, the evaluation script uses **ground truth as mock responses**. To evaluate your actual LightRAG:
|
||||||
|
|
||||||
|
### Step 1: Update `generate_rag_response()`
|
||||||
|
|
||||||
|
In `eval_rag_quality.py`, replace the mock implementation:
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def generate_rag_response(self, question: str, context: str = None) -> Dict[str, str]:
|
||||||
|
"""Generate RAG response using your LightRAG system"""
|
||||||
|
from lightrag import LightRAG
|
||||||
|
|
||||||
|
rag = LightRAG(
|
||||||
|
working_dir="./rag_storage",
|
||||||
|
llm_model_func=your_llm_function
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await rag.aquery(question)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"answer": response,
|
||||||
|
"context": "context_from_kg" # If available
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Run Evaluation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python lightrag/evaluation/eval_rag_quality.py
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📊 Interpreting Results
|
||||||
|
|
||||||
|
### Score Ranges
|
||||||
|
|
||||||
|
- **0.80-1.00**: ✅ Excellent (Production-ready)
|
||||||
|
- **0.60-0.80**: ⚠️ Good (Room for improvement)
|
||||||
|
- **0.40-0.60**: ❌ Poor (Needs optimization)
|
||||||
|
- **0.00-0.40**: 🔴 Critical (Major issues)
|
||||||
|
|
||||||
|
### What Low Scores Mean
|
||||||
|
|
||||||
|
| Metric | Low Score Indicates |
|
||||||
|
|--------|-------------------|
|
||||||
|
| **Faithfulness** | Responses contain hallucinations or incorrect information |
|
||||||
|
| **Answer Relevance** | Answers don't match what users asked |
|
||||||
|
| **Context Recall** | Missing important information in retrieval |
|
||||||
|
| **Context Precision** | Retrieved documents contain irrelevant noise |
|
||||||
|
|
||||||
|
### Optimization Tips
|
||||||
|
|
||||||
|
1. **Low Faithfulness**:
|
||||||
|
- Improve entity extraction quality
|
||||||
|
- Better document chunking
|
||||||
|
- Tune retrieval temperature
|
||||||
|
|
||||||
|
2. **Low Answer Relevance**:
|
||||||
|
- Improve prompt engineering
|
||||||
|
- Better query understanding
|
||||||
|
- Check semantic similarity threshold
|
||||||
|
|
||||||
|
3. **Low Context Recall**:
|
||||||
|
- Increase retrieval `top_k` results
|
||||||
|
- Improve embedding model
|
||||||
|
- Better document preprocessing
|
||||||
|
|
||||||
|
4. **Low Context Precision**:
|
||||||
|
- Smaller, focused chunks
|
||||||
|
- Better filtering
|
||||||
|
- Improve chunking strategy
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📈 Usage Examples
|
||||||
|
|
||||||
|
### Python API
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from lightrag.evaluation import RAGEvaluator
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
evaluator = RAGEvaluator()
|
||||||
|
results = await evaluator.run()
|
||||||
|
|
||||||
|
# Access results
|
||||||
|
for result in results:
|
||||||
|
print(f"Question: {result['question']}")
|
||||||
|
print(f"RAGAS Score: {result['ragas_score']:.2%}")
|
||||||
|
print(f"Metrics: {result['metrics']}")
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
```
|
||||||
|
|
||||||
|
### Custom Dataset
|
||||||
|
|
||||||
|
```python
|
||||||
|
evaluator = RAGEvaluator(test_dataset_path="custom_tests.json")
|
||||||
|
results = await evaluator.run()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Batch Evaluation
|
||||||
|
|
||||||
|
```python
|
||||||
|
from pathlib import Path
|
||||||
|
import json
|
||||||
|
|
||||||
|
results_dir = Path("lightrag/evaluation/results")
|
||||||
|
results_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
# Run multiple evaluations
|
||||||
|
for i in range(3):
|
||||||
|
evaluator = RAGEvaluator()
|
||||||
|
results = await evaluator.run()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 For Portfolio/Interview
|
||||||
|
|
||||||
|
**What to Highlight:**
|
||||||
|
|
||||||
|
1. ✅ **Quality Metrics**: "RAG system achieves 85% RAGAS score"
|
||||||
|
2. ✅ **Evaluation Framework**: "Automated quality assessment with RAGAS"
|
||||||
|
3. ✅ **Best Practices**: "Offline evaluation pipeline for continuous improvement"
|
||||||
|
4. ✅ **Production-Ready**: "Metrics-driven system optimization"
|
||||||
|
|
||||||
|
**Example Statement:**
|
||||||
|
|
||||||
|
> "I built an evaluation framework using RAGAS that measures RAG quality across faithfulness, relevance, and context coverage. The system achieves 85% average RAGAS score, with automated HTML reports for quality tracking."
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔗 Related Features
|
||||||
|
|
||||||
|
- **LangFuse Integration**: Real-time observability of production RAG calls
|
||||||
|
- **LightRAG**: Core RAG system with entity extraction and knowledge graphs
|
||||||
|
- **Metrics**: See `results/` for detailed evaluation metrics
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📚 Resources
|
||||||
|
|
||||||
|
- [RAGAS Documentation](https://docs.ragas.io/)
|
||||||
|
- [RAGAS GitHub](https://github.com/explodinggradients/ragas)
|
||||||
|
- [LangFuse + RAGAS Guide](https://langfuse.com/guides/cookbook/evaluation_of_rag_with_ragas)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🐛 Troubleshooting
|
||||||
|
|
||||||
|
### "ModuleNotFoundError: No module named 'ragas'"
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install ragas datasets
|
||||||
|
```
|
||||||
|
|
||||||
|
### "No sample_dataset.json found"
|
||||||
|
|
||||||
|
Make sure you're running from the project root:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd /path/to/LightRAG
|
||||||
|
python lightrag/evaluation/eval_rag_quality.py
|
||||||
|
```
|
||||||
|
|
||||||
|
### "LLM API errors during evaluation"
|
||||||
|
|
||||||
|
The evaluation uses your configured LLM (OpenAI by default). Ensure:
|
||||||
|
- API keys are set in `.env`
|
||||||
|
- Have sufficient API quota
|
||||||
|
- Network connection is stable
|
||||||
|
|
||||||
|
### Results showing 0 scores
|
||||||
|
|
||||||
|
Current implementation uses ground truth as mock responses. Results will show perfect scores because the "generated answer" equals the ground truth.
|
||||||
|
|
||||||
|
**To use actual RAG results:**
|
||||||
|
1. Implement the `generate_rag_response()` method
|
||||||
|
2. Connect to your LightRAG instance
|
||||||
|
3. Run evaluation again
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 Next Steps
|
||||||
|
|
||||||
|
1. ✅ Review test dataset in `sample_dataset.json`
|
||||||
|
2. ✅ Run `python lightrag/evaluation/eval_rag_quality.py`
|
||||||
|
3. ✅ Open the HTML report in browser
|
||||||
|
4. 🔄 Integrate with actual LightRAG system
|
||||||
|
5. 📊 Monitor metrics over time
|
||||||
|
6. 🎯 Use insights for optimization
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Happy Evaluating! 🚀**
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
RAGAS Evaluation Script for LightRAG System
|
RAGAS Evaluation Script for Portfolio RAG System
|
||||||
|
|
||||||
Evaluates RAG response quality using RAGAS metrics:
|
Evaluates RAG response quality using RAGAS metrics:
|
||||||
- Faithfulness: Is the answer factually accurate based on context?
|
- Faithfulness: Is the answer factually accurate based on context?
|
||||||
|
|
@ -9,98 +9,56 @@ Evaluates RAG response quality using RAGAS metrics:
|
||||||
- Context Precision: Is retrieved context clean without noise?
|
- Context Precision: Is retrieved context clean without noise?
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
# Use defaults (sample_dataset.json, http://localhost:9621)
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py
|
python lightrag/evaluation/eval_rag_quality.py
|
||||||
|
python lightrag/evaluation/eval_rag_quality.py http://localhost:8000
|
||||||
# Specify custom dataset
|
python lightrag/evaluation/eval_rag_quality.py http://your-rag-server.com:8000
|
||||||
python lightrag/evaluation/eval_rag_quality.py --dataset my_test.json
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py -d my_test.json
|
|
||||||
|
|
||||||
# Specify custom RAG endpoint
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py --ragendpoint http://my-server.com:9621
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py -r http://my-server.com:9621
|
|
||||||
|
|
||||||
# Specify both
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py -d my_test.json -r http://localhost:9621
|
|
||||||
|
|
||||||
# Get help
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py --help
|
|
||||||
|
|
||||||
Results are saved to: lightrag/evaluation/results/
|
Results are saved to: lightrag/evaluation/results/
|
||||||
- results_YYYYMMDD_HHMMSS.csv (CSV export for analysis)
|
- results_YYYYMMDD_HHMMSS.csv (CSV export for analysis)
|
||||||
- results_YYYYMMDD_HHMMSS.json (Full results with details)
|
- results_YYYYMMDD_HHMMSS.json (Full results with details)
|
||||||
|
|
||||||
Technical Notes:
|
|
||||||
- Uses stable RAGAS API (LangchainLLMWrapper) for maximum compatibility
|
|
||||||
- Supports custom OpenAI-compatible endpoints via EVAL_LLM_BINDING_HOST
|
|
||||||
- Enables bypass_n mode for endpoints that don't support 'n' parameter
|
|
||||||
- Deprecation warnings are suppressed for cleaner output
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import csv
|
import csv
|
||||||
import json
|
import json
|
||||||
import math
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import warnings
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from lightrag.utils import logger
|
|
||||||
|
|
||||||
# Suppress LangchainLLMWrapper deprecation warning
|
|
||||||
# We use LangchainLLMWrapper for stability and compatibility with all RAGAS versions
|
|
||||||
warnings.filterwarnings(
|
|
||||||
"ignore",
|
|
||||||
message=".*LangchainLLMWrapper is deprecated.*",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add parent directory to path
|
# Add parent directory to path
|
||||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
||||||
|
|
||||||
# use the .env that is inside the current folder
|
# Load .env from project root
|
||||||
# allows to use different .env file for each lightrag instance
|
project_root = Path(__file__).parent.parent.parent
|
||||||
# the OS environment variables take precedence over the .env file
|
load_dotenv(project_root / ".env")
|
||||||
load_dotenv(dotenv_path=".env", override=False)
|
|
||||||
|
# Setup OpenAI API key (required for RAGAS evaluation)
|
||||||
|
# Use LLM_BINDING_API_KEY if OPENAI_API_KEY is not set
|
||||||
|
if "OPENAI_API_KEY" not in os.environ:
|
||||||
|
if "LLM_BINDING_API_KEY" in os.environ:
|
||||||
|
os.environ["OPENAI_API_KEY"] = os.environ["LLM_BINDING_API_KEY"]
|
||||||
|
else:
|
||||||
|
os.environ["OPENAI_API_KEY"] = input("Enter your OpenAI API key: ")
|
||||||
|
|
||||||
# Conditional imports - will raise ImportError if dependencies not installed
|
|
||||||
try:
|
try:
|
||||||
from datasets import Dataset
|
from datasets import Dataset
|
||||||
from ragas import evaluate
|
from ragas import evaluate
|
||||||
from ragas.metrics import (
|
from ragas.metrics import (
|
||||||
AnswerRelevancy,
|
answer_relevancy,
|
||||||
ContextPrecision,
|
context_precision,
|
||||||
ContextRecall,
|
context_recall,
|
||||||
Faithfulness,
|
faithfulness,
|
||||||
)
|
)
|
||||||
from ragas.llms import LangchainLLMWrapper
|
except ImportError as e:
|
||||||
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
print(f"❌ RAGAS import error: {e}")
|
||||||
from tqdm import tqdm
|
print(" Install with: pip install ragas datasets")
|
||||||
|
sys.exit(1)
|
||||||
RAGAS_AVAILABLE = True
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
RAGAS_AVAILABLE = False
|
|
||||||
Dataset = None
|
|
||||||
evaluate = None
|
|
||||||
LangchainLLMWrapper = None
|
|
||||||
|
|
||||||
|
|
||||||
CONNECT_TIMEOUT_SECONDS = 180.0
|
|
||||||
READ_TIMEOUT_SECONDS = 300.0
|
|
||||||
TOTAL_TIMEOUT_SECONDS = 180.0
|
|
||||||
|
|
||||||
|
|
||||||
def _is_nan(value: Any) -> bool:
|
|
||||||
"""Return True when value is a float NaN."""
|
|
||||||
return isinstance(value, float) and math.isnan(value)
|
|
||||||
|
|
||||||
|
|
||||||
class RAGEvaluator:
|
class RAGEvaluator:
|
||||||
|
|
@ -112,126 +70,23 @@ class RAGEvaluator:
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
test_dataset_path: Path to test dataset JSON file
|
test_dataset_path: Path to test dataset JSON file
|
||||||
rag_api_url: Base URL of LightRAG API (e.g., http://localhost:9621)
|
rag_api_url: Base URL of LightRAG API (e.g., http://localhost:8000)
|
||||||
If None, will try to read from environment or use default
|
If None, will try to read from environment or use default
|
||||||
|
|
||||||
Environment Variables:
|
|
||||||
EVAL_LLM_MODEL: LLM model for evaluation (default: gpt-4o-mini)
|
|
||||||
EVAL_EMBEDDING_MODEL: Embedding model for evaluation (default: text-embedding-3-small)
|
|
||||||
EVAL_LLM_BINDING_API_KEY: API key for evaluation models (fallback to OPENAI_API_KEY)
|
|
||||||
EVAL_LLM_BINDING_HOST: Custom endpoint URL for evaluation models (optional)
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
ImportError: If ragas or datasets packages are not installed
|
|
||||||
EnvironmentError: If EVAL_LLM_BINDING_API_KEY and OPENAI_API_KEY are both not set
|
|
||||||
"""
|
"""
|
||||||
# Validate RAGAS dependencies are installed
|
|
||||||
if not RAGAS_AVAILABLE:
|
|
||||||
raise ImportError(
|
|
||||||
"RAGAS dependencies not installed. "
|
|
||||||
"Install with: pip install ragas datasets"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Configure evaluation models (for RAGAS scoring)
|
|
||||||
eval_api_key = os.getenv("EVAL_LLM_BINDING_API_KEY") or os.getenv(
|
|
||||||
"OPENAI_API_KEY"
|
|
||||||
)
|
|
||||||
if not eval_api_key:
|
|
||||||
raise EnvironmentError(
|
|
||||||
"EVAL_LLM_BINDING_API_KEY or OPENAI_API_KEY is required for evaluation. "
|
|
||||||
"Set EVAL_LLM_BINDING_API_KEY to use a custom API key, "
|
|
||||||
"or ensure OPENAI_API_KEY is set."
|
|
||||||
)
|
|
||||||
|
|
||||||
eval_model = os.getenv("EVAL_LLM_MODEL", "gpt-4o-mini")
|
|
||||||
eval_embedding_model = os.getenv(
|
|
||||||
"EVAL_EMBEDDING_MODEL", "text-embedding-3-large"
|
|
||||||
)
|
|
||||||
eval_base_url = os.getenv("EVAL_LLM_BINDING_HOST")
|
|
||||||
|
|
||||||
# Create LLM and Embeddings instances for RAGAS
|
|
||||||
llm_kwargs = {
|
|
||||||
"model": eval_model,
|
|
||||||
"api_key": eval_api_key,
|
|
||||||
"max_retries": int(os.getenv("EVAL_LLM_MAX_RETRIES", "5")),
|
|
||||||
"request_timeout": int(os.getenv("EVAL_LLM_TIMEOUT", "180")),
|
|
||||||
}
|
|
||||||
embedding_kwargs = {"model": eval_embedding_model, "api_key": eval_api_key}
|
|
||||||
|
|
||||||
if eval_base_url:
|
|
||||||
llm_kwargs["base_url"] = eval_base_url
|
|
||||||
embedding_kwargs["base_url"] = eval_base_url
|
|
||||||
|
|
||||||
# Create base LangChain LLM
|
|
||||||
base_llm = ChatOpenAI(**llm_kwargs)
|
|
||||||
self.eval_embeddings = OpenAIEmbeddings(**embedding_kwargs)
|
|
||||||
|
|
||||||
# Wrap LLM with LangchainLLMWrapper and enable bypass_n mode for custom endpoints
|
|
||||||
# This ensures compatibility with endpoints that don't support the 'n' parameter
|
|
||||||
# by generating multiple outputs through repeated prompts instead of using 'n' parameter
|
|
||||||
try:
|
|
||||||
self.eval_llm = LangchainLLMWrapper(
|
|
||||||
langchain_llm=base_llm,
|
|
||||||
bypass_n=True, # Enable bypass_n to avoid passing 'n' to OpenAI API
|
|
||||||
)
|
|
||||||
logger.debug("Successfully configured bypass_n mode for LLM wrapper")
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(
|
|
||||||
"Could not configure LangchainLLMWrapper with bypass_n: %s. "
|
|
||||||
"Using base LLM directly, which may cause warnings with custom endpoints.",
|
|
||||||
e,
|
|
||||||
)
|
|
||||||
self.eval_llm = base_llm
|
|
||||||
|
|
||||||
if test_dataset_path is None:
|
if test_dataset_path is None:
|
||||||
test_dataset_path = Path(__file__).parent / "sample_dataset.json"
|
test_dataset_path = Path(__file__).parent / "sample_dataset.json"
|
||||||
|
|
||||||
if rag_api_url is None:
|
if rag_api_url is None:
|
||||||
rag_api_url = os.getenv("LIGHTRAG_API_URL", "http://localhost:9621")
|
rag_api_url = os.getenv("LIGHTRAG_API_URL", "http://localhost:8000")
|
||||||
|
|
||||||
self.test_dataset_path = Path(test_dataset_path)
|
self.test_dataset_path = Path(test_dataset_path)
|
||||||
self.rag_api_url = rag_api_url.rstrip("/")
|
self.rag_api_url = rag_api_url.rstrip("/") # Remove trailing slash
|
||||||
self.results_dir = Path(__file__).parent / "results"
|
self.results_dir = Path(__file__).parent / "results"
|
||||||
self.results_dir.mkdir(exist_ok=True)
|
self.results_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
# Load test dataset
|
# Load test dataset
|
||||||
self.test_cases = self._load_test_dataset()
|
self.test_cases = self._load_test_dataset()
|
||||||
|
|
||||||
# Store configuration values for display
|
|
||||||
self.eval_model = eval_model
|
|
||||||
self.eval_embedding_model = eval_embedding_model
|
|
||||||
self.eval_base_url = eval_base_url
|
|
||||||
self.eval_max_retries = llm_kwargs["max_retries"]
|
|
||||||
self.eval_timeout = llm_kwargs["request_timeout"]
|
|
||||||
|
|
||||||
# Display configuration
|
|
||||||
self._display_configuration()
|
|
||||||
|
|
||||||
def _display_configuration(self):
|
|
||||||
"""Display all evaluation configuration settings"""
|
|
||||||
logger.info("Evaluation Models:")
|
|
||||||
logger.info(" • LLM Model: %s", self.eval_model)
|
|
||||||
logger.info(" • Embedding Model: %s", self.eval_embedding_model)
|
|
||||||
if self.eval_base_url:
|
|
||||||
logger.info(" • Custom Endpoint: %s", self.eval_base_url)
|
|
||||||
logger.info(
|
|
||||||
" • Bypass N-Parameter: Enabled (use LangchainLLMWrapperfor compatibility)"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
logger.info(" • Endpoint: OpenAI Official API")
|
|
||||||
|
|
||||||
logger.info("Concurrency & Rate Limiting:")
|
|
||||||
query_top_k = int(os.getenv("EVAL_QUERY_TOP_K", "10"))
|
|
||||||
logger.info(" • Query Top-K: %s Entities/Relations", query_top_k)
|
|
||||||
logger.info(" • LLM Max Retries: %s", self.eval_max_retries)
|
|
||||||
logger.info(" • LLM Timeout: %s seconds", self.eval_timeout)
|
|
||||||
|
|
||||||
logger.info("Test Configuration:")
|
|
||||||
logger.info(" • Total Test Cases: %s", len(self.test_cases))
|
|
||||||
logger.info(" • Test Dataset: %s", self.test_dataset_path.name)
|
|
||||||
logger.info(" • LightRAG API: %s", self.rag_api_url)
|
|
||||||
logger.info(" • Results Directory: %s", self.results_dir.name)
|
|
||||||
|
|
||||||
def _load_test_dataset(self) -> List[Dict[str, str]]:
|
def _load_test_dataset(self) -> List[Dict[str, str]]:
|
||||||
"""Load test cases from JSON file"""
|
"""Load test cases from JSON file"""
|
||||||
if not self.test_dataset_path.exists():
|
if not self.test_dataset_path.exists():
|
||||||
|
|
@ -245,188 +100,109 @@ class RAGEvaluator:
|
||||||
async def generate_rag_response(
|
async def generate_rag_response(
|
||||||
self,
|
self,
|
||||||
question: str,
|
question: str,
|
||||||
client: httpx.AsyncClient,
|
context: str = None, # Not used - actual context comes from LightRAG
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, str]:
|
||||||
"""
|
"""
|
||||||
Generate RAG response by calling LightRAG API.
|
Generate RAG response by calling LightRAG API
|
||||||
|
|
||||||
|
Calls the actual LightRAG /query endpoint instead of using mock data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
question: The user query.
|
question: The user query
|
||||||
client: Shared httpx AsyncClient for connection pooling.
|
context: Ignored (for compatibility), actual context from LightRAG
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dictionary with 'answer' and 'contexts' keys.
|
Dict with 'answer' and 'context' keys
|
||||||
'contexts' is a list of strings (one per retrieved document).
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
Exception: If LightRAG API is unavailable.
|
Exception: If LightRAG API is unavailable
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||||
|
# Prepare request to LightRAG API
|
||||||
payload = {
|
payload = {
|
||||||
"query": question,
|
"query": question,
|
||||||
"mode": "mix",
|
"mode": "mix", # Recommended: combines local & global
|
||||||
"include_references": True,
|
"include_references": True,
|
||||||
"include_chunk_content": True, # NEW: Request chunk content in references
|
|
||||||
"response_type": "Multiple Paragraphs",
|
"response_type": "Multiple Paragraphs",
|
||||||
"top_k": int(os.getenv("EVAL_QUERY_TOP_K", "10")),
|
"top_k": 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get API key from environment for authentication
|
# Call LightRAG /query endpoint
|
||||||
api_key = os.getenv("LIGHTRAG_API_KEY")
|
|
||||||
|
|
||||||
# Prepare headers with optional authentication
|
|
||||||
headers = {}
|
|
||||||
if api_key:
|
|
||||||
headers["X-API-Key"] = api_key
|
|
||||||
|
|
||||||
# Single optimized API call - gets both answer AND chunk content
|
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"{self.rag_api_url}/query",
|
f"{self.rag_api_url}/query",
|
||||||
json=payload,
|
json=payload,
|
||||||
headers=headers if headers else None,
|
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise Exception(
|
||||||
|
f"LightRAG API error {response.status_code}: {response.text}"
|
||||||
|
)
|
||||||
|
|
||||||
result = response.json()
|
result = response.json()
|
||||||
|
|
||||||
answer = result.get("response", "No response generated")
|
|
||||||
references = result.get("references", [])
|
|
||||||
|
|
||||||
# DEBUG: Inspect the API response
|
|
||||||
logger.debug("🔍 References Count: %s", len(references))
|
|
||||||
if references:
|
|
||||||
first_ref = references[0]
|
|
||||||
logger.debug("🔍 First Reference Keys: %s", list(first_ref.keys()))
|
|
||||||
if "content" in first_ref:
|
|
||||||
content_preview = first_ref["content"]
|
|
||||||
if isinstance(content_preview, list) and content_preview:
|
|
||||||
logger.debug(
|
|
||||||
"🔍 Content Preview (first chunk): %s...",
|
|
||||||
content_preview[0][:100],
|
|
||||||
)
|
|
||||||
elif isinstance(content_preview, str):
|
|
||||||
logger.debug("🔍 Content Preview: %s...", content_preview[:100])
|
|
||||||
|
|
||||||
# Extract chunk content from enriched references
|
|
||||||
# Note: content is now a list of chunks per reference (one file may have multiple chunks)
|
|
||||||
contexts = []
|
|
||||||
for ref in references:
|
|
||||||
content = ref.get("content", [])
|
|
||||||
if isinstance(content, list):
|
|
||||||
# Flatten the list: each chunk becomes a separate context
|
|
||||||
contexts.extend(content)
|
|
||||||
elif isinstance(content, str):
|
|
||||||
# Backward compatibility: if content is still a string (shouldn't happen)
|
|
||||||
contexts.append(content)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"answer": answer,
|
"answer": result.get("response", "No response generated"),
|
||||||
"contexts": contexts, # List of strings from actual retrieved chunks
|
"context": json.dumps(result.get("references", []))
|
||||||
|
if result.get("references")
|
||||||
|
else "",
|
||||||
}
|
}
|
||||||
|
|
||||||
except httpx.ConnectError as e:
|
except httpx.ConnectError:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
f"❌ Cannot connect to LightRAG API at {self.rag_api_url}\n"
|
f"❌ Cannot connect to LightRAG API at {self.rag_api_url}\n"
|
||||||
f" Make sure LightRAG server is running:\n"
|
f" Make sure LightRAG server is running:\n"
|
||||||
f" python -m lightrag.api.lightrag_server\n"
|
f" python -m lightrag.api.lightrag_server"
|
||||||
f" Error: {str(e)}"
|
|
||||||
)
|
|
||||||
except httpx.HTTPStatusError as e:
|
|
||||||
raise Exception(
|
|
||||||
f"LightRAG API error {e.response.status_code}: {e.response.text}"
|
|
||||||
)
|
|
||||||
except httpx.ReadTimeout as e:
|
|
||||||
raise Exception(
|
|
||||||
f"Request timeout after waiting for response\n"
|
|
||||||
f" Question: {question[:100]}...\n"
|
|
||||||
f" Error: {str(e)}"
|
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"Error calling LightRAG API: {type(e).__name__}: {str(e)}")
|
raise Exception(f"Error calling LightRAG API: {str(e)}")
|
||||||
|
|
||||||
async def evaluate_single_case(
|
async def evaluate_responses(self) -> List[Dict[str, Any]]:
|
||||||
self,
|
|
||||||
idx: int,
|
|
||||||
test_case: Dict[str, str],
|
|
||||||
rag_semaphore: asyncio.Semaphore,
|
|
||||||
eval_semaphore: asyncio.Semaphore,
|
|
||||||
client: httpx.AsyncClient,
|
|
||||||
progress_counter: Dict[str, int],
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
"""
|
||||||
Evaluate a single test case with two-stage pipeline concurrency control
|
Evaluate all test cases and return metrics
|
||||||
|
|
||||||
Args:
|
|
||||||
idx: Test case index (1-based)
|
|
||||||
test_case: Test case dictionary with question and ground_truth
|
|
||||||
rag_semaphore: Semaphore to control overall concurrency (covers entire function)
|
|
||||||
eval_semaphore: Semaphore to control RAGAS evaluation concurrency (Stage 2)
|
|
||||||
client: Shared httpx AsyncClient for connection pooling
|
|
||||||
progress_counter: Shared dictionary for progress tracking
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Evaluation result dictionary
|
List of evaluation results with metrics
|
||||||
"""
|
"""
|
||||||
# rag_semaphore controls the entire evaluation process to prevent
|
print("\n" + "=" * 70)
|
||||||
# all RAG responses from being generated at once when eval is slow
|
print("🚀 Starting RAGAS Evaluation of Portfolio RAG System")
|
||||||
async with rag_semaphore:
|
print("=" * 70 + "\n")
|
||||||
|
|
||||||
|
results = []
|
||||||
|
|
||||||
|
for idx, test_case in enumerate(self.test_cases, 1):
|
||||||
question = test_case["question"]
|
question = test_case["question"]
|
||||||
ground_truth = test_case["ground_truth"]
|
ground_truth = test_case["ground_truth"]
|
||||||
|
|
||||||
# Stage 1: Generate RAG response
|
print(f"[{idx}/{len(self.test_cases)}] Evaluating: {question[:60]}...")
|
||||||
try:
|
|
||||||
rag_response = await self.generate_rag_response(
|
|
||||||
question=question, client=client
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("Error generating response for test %s: %s", idx, str(e))
|
|
||||||
progress_counter["completed"] += 1
|
|
||||||
return {
|
|
||||||
"test_number": idx,
|
|
||||||
"question": question,
|
|
||||||
"error": str(e),
|
|
||||||
"metrics": {},
|
|
||||||
"ragas_score": 0,
|
|
||||||
"timestamp": datetime.now().isoformat(),
|
|
||||||
}
|
|
||||||
|
|
||||||
# *** CRITICAL FIX: Use actual retrieved contexts, NOT ground_truth ***
|
# Generate RAG response by calling actual LightRAG API
|
||||||
retrieved_contexts = rag_response["contexts"]
|
rag_response = await self.generate_rag_response(question=question)
|
||||||
|
|
||||||
# Prepare dataset for RAGAS evaluation with CORRECT contexts
|
# Prepare dataset for RAGAS evaluation
|
||||||
eval_dataset = Dataset.from_dict(
|
eval_dataset = Dataset.from_dict(
|
||||||
{
|
{
|
||||||
"question": [question],
|
"question": [question],
|
||||||
"answer": [rag_response["answer"]],
|
"answer": [rag_response["answer"]],
|
||||||
"contexts": [retrieved_contexts],
|
"contexts": [
|
||||||
|
[ground_truth]
|
||||||
|
], # RAGAS expects list of context strings
|
||||||
"ground_truth": [ground_truth],
|
"ground_truth": [ground_truth],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Stage 2: Run RAGAS evaluation (controlled by eval_semaphore)
|
# Run RAGAS evaluation
|
||||||
# IMPORTANT: Create fresh metric instances for each evaluation to avoid
|
|
||||||
# concurrent state conflicts when multiple tasks run in parallel
|
|
||||||
async with eval_semaphore:
|
|
||||||
pbar = None
|
|
||||||
try:
|
try:
|
||||||
# Create standard tqdm progress bar for RAGAS evaluation
|
|
||||||
pbar = tqdm(total=4, desc=f"Eval-{idx}", leave=True)
|
|
||||||
|
|
||||||
eval_results = evaluate(
|
eval_results = evaluate(
|
||||||
dataset=eval_dataset,
|
dataset=eval_dataset,
|
||||||
metrics=[
|
metrics=[
|
||||||
Faithfulness(),
|
faithfulness,
|
||||||
AnswerRelevancy(),
|
answer_relevancy,
|
||||||
ContextRecall(),
|
context_recall,
|
||||||
ContextPrecision(),
|
context_precision,
|
||||||
],
|
],
|
||||||
llm=self.eval_llm,
|
|
||||||
embeddings=self.eval_embeddings,
|
|
||||||
_pbar=pbar,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pbar.close()
|
|
||||||
pbar = None
|
|
||||||
|
|
||||||
# Convert to DataFrame (RAGAS v0.3+ API)
|
# Convert to DataFrame (RAGAS v0.3+ API)
|
||||||
df = eval_results.to_pandas()
|
df = eval_results.to_pandas()
|
||||||
|
|
||||||
|
|
@ -435,7 +211,6 @@ class RAGEvaluator:
|
||||||
|
|
||||||
# Extract scores (RAGAS v0.3+ uses .to_pandas())
|
# Extract scores (RAGAS v0.3+ uses .to_pandas())
|
||||||
result = {
|
result = {
|
||||||
"test_number": idx,
|
|
||||||
"question": question,
|
"question": question,
|
||||||
"answer": rag_response["answer"][:200] + "..."
|
"answer": rag_response["answer"][:200] + "..."
|
||||||
if len(rag_response["answer"]) > 200
|
if len(rag_response["answer"]) > 200
|
||||||
|
|
@ -443,15 +218,13 @@ class RAGEvaluator:
|
||||||
"ground_truth": ground_truth[:200] + "..."
|
"ground_truth": ground_truth[:200] + "..."
|
||||||
if len(ground_truth) > 200
|
if len(ground_truth) > 200
|
||||||
else ground_truth,
|
else ground_truth,
|
||||||
"project": test_case.get("project", "unknown"),
|
"project": test_case.get("project_context", "unknown"),
|
||||||
"metrics": {
|
"metrics": {
|
||||||
"faithfulness": float(scores_row.get("faithfulness", 0)),
|
"faithfulness": float(scores_row.get("faithfulness", 0)),
|
||||||
"answer_relevance": float(
|
"answer_relevance": float(
|
||||||
scores_row.get("answer_relevancy", 0)
|
scores_row.get("answer_relevancy", 0)
|
||||||
),
|
),
|
||||||
"context_recall": float(
|
"context_recall": float(scores_row.get("context_recall", 0)),
|
||||||
scores_row.get("context_recall", 0)
|
|
||||||
),
|
|
||||||
"context_precision": float(
|
"context_precision": float(
|
||||||
scores_row.get("context_precision", 0)
|
scores_row.get("context_precision", 0)
|
||||||
),
|
),
|
||||||
|
|
@ -459,90 +232,35 @@ class RAGEvaluator:
|
||||||
"timestamp": datetime.now().isoformat(),
|
"timestamp": datetime.now().isoformat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate RAGAS score (average of all metrics, excluding NaN values)
|
# Calculate RAGAS score (average of all metrics)
|
||||||
metrics = result["metrics"]
|
metrics = result["metrics"]
|
||||||
valid_metrics = [v for v in metrics.values() if not _is_nan(v)]
|
ragas_score = sum(metrics.values()) / len(metrics) if metrics else 0
|
||||||
ragas_score = (
|
|
||||||
sum(valid_metrics) / len(valid_metrics) if valid_metrics else 0
|
|
||||||
)
|
|
||||||
result["ragas_score"] = round(ragas_score, 4)
|
result["ragas_score"] = round(ragas_score, 4)
|
||||||
|
|
||||||
# Update progress counter
|
results.append(result)
|
||||||
progress_counter["completed"] += 1
|
|
||||||
|
|
||||||
return result
|
# Print metrics
|
||||||
|
print(f" ✅ Faithfulness: {metrics['faithfulness']:.4f}")
|
||||||
|
print(f" ✅ Answer Relevance: {metrics['answer_relevance']:.4f}")
|
||||||
|
print(f" ✅ Context Recall: {metrics['context_recall']:.4f}")
|
||||||
|
print(f" ✅ Context Precision: {metrics['context_precision']:.4f}")
|
||||||
|
print(f" 📊 RAGAS Score: {result['ragas_score']:.4f}\n")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Error evaluating test %s: %s", idx, str(e))
|
import traceback
|
||||||
progress_counter["completed"] += 1
|
|
||||||
return {
|
print(f" ❌ Error evaluating: {str(e)}")
|
||||||
"test_number": idx,
|
print(f" 🔍 Full traceback:\n{traceback.format_exc()}\n")
|
||||||
|
result = {
|
||||||
"question": question,
|
"question": question,
|
||||||
"error": str(e),
|
"error": str(e),
|
||||||
"metrics": {},
|
"metrics": {},
|
||||||
"ragas_score": 0,
|
"ragas_score": 0,
|
||||||
"timestamp": datetime.now().isoformat(),
|
"timestamp": datetime.now().isoformat(),
|
||||||
}
|
}
|
||||||
finally:
|
results.append(result)
|
||||||
# Force close progress bar to ensure completion
|
|
||||||
if pbar is not None:
|
|
||||||
pbar.close()
|
|
||||||
|
|
||||||
async def evaluate_responses(self) -> List[Dict[str, Any]]:
|
return results
|
||||||
"""
|
|
||||||
Evaluate all test cases in parallel with two-stage pipeline and return metrics
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of evaluation results with metrics
|
|
||||||
"""
|
|
||||||
# Get evaluation concurrency from environment (default to 2 for parallel evaluation)
|
|
||||||
max_async = int(os.getenv("EVAL_MAX_CONCURRENT", "2"))
|
|
||||||
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
logger.info("🚀 Starting RAGAS Evaluation of LightRAG System")
|
|
||||||
logger.info("🔧 Two-Stage Pipeline Configuration:")
|
|
||||||
logger.info(" • RAGAS Evaluation (Stage 2): %s concurrent", max_async)
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
|
|
||||||
# Create two-stage pipeline semaphores
|
|
||||||
# Stage 1: RAG generation - allow x2 concurrency to keep evaluation fed
|
|
||||||
rag_semaphore = asyncio.Semaphore(max_async * 2)
|
|
||||||
# Stage 2: RAGAS evaluation - primary bottleneck
|
|
||||||
eval_semaphore = asyncio.Semaphore(max_async)
|
|
||||||
|
|
||||||
# Create progress counter (shared across all tasks)
|
|
||||||
progress_counter = {"completed": 0}
|
|
||||||
|
|
||||||
# Create shared HTTP client with connection pooling and proper timeouts
|
|
||||||
# Timeout: 3 minutes for connect, 5 minutes for read (LLM can be slow)
|
|
||||||
timeout = httpx.Timeout(
|
|
||||||
TOTAL_TIMEOUT_SECONDS,
|
|
||||||
connect=CONNECT_TIMEOUT_SECONDS,
|
|
||||||
read=READ_TIMEOUT_SECONDS,
|
|
||||||
)
|
|
||||||
limits = httpx.Limits(
|
|
||||||
max_connections=(max_async + 1) * 2, # Allow buffer for RAG stage
|
|
||||||
max_keepalive_connections=max_async + 1,
|
|
||||||
)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=timeout, limits=limits) as client:
|
|
||||||
# Create tasks for all test cases
|
|
||||||
tasks = [
|
|
||||||
self.evaluate_single_case(
|
|
||||||
idx,
|
|
||||||
test_case,
|
|
||||||
rag_semaphore,
|
|
||||||
eval_semaphore,
|
|
||||||
client,
|
|
||||||
progress_counter,
|
|
||||||
)
|
|
||||||
for idx, test_case in enumerate(self.test_cases, 1)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Run all evaluations in parallel (limited by two-stage semaphores)
|
|
||||||
results = await asyncio.gather(*tasks)
|
|
||||||
|
|
||||||
return list(results)
|
|
||||||
|
|
||||||
def _export_to_csv(self, results: List[Dict[str, Any]]) -> Path:
|
def _export_to_csv(self, results: List[Dict[str, Any]]) -> Path:
|
||||||
"""
|
"""
|
||||||
|
|
@ -564,9 +282,7 @@ class RAGEvaluator:
|
||||||
- ragas_score: Overall RAGAS score (0-1)
|
- ragas_score: Overall RAGAS score (0-1)
|
||||||
- timestamp: When evaluation was run
|
- timestamp: When evaluation was run
|
||||||
"""
|
"""
|
||||||
csv_path = (
|
csv_path = self.results_dir / f"results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
||||||
self.results_dir / f"results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
|
||||||
)
|
|
||||||
|
|
||||||
with open(csv_path, "w", newline="", encoding="utf-8") as f:
|
with open(csv_path, "w", newline="", encoding="utf-8") as f:
|
||||||
fieldnames = [
|
fieldnames = [
|
||||||
|
|
@ -604,191 +320,6 @@ class RAGEvaluator:
|
||||||
|
|
||||||
return csv_path
|
return csv_path
|
||||||
|
|
||||||
def _format_metric(self, value: float, width: int = 6) -> str:
|
|
||||||
"""
|
|
||||||
Format a metric value for display, handling NaN gracefully
|
|
||||||
|
|
||||||
Args:
|
|
||||||
value: The metric value to format
|
|
||||||
width: The width of the formatted string
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Formatted string (e.g., "0.8523" or " N/A ")
|
|
||||||
"""
|
|
||||||
if _is_nan(value):
|
|
||||||
return "N/A".center(width)
|
|
||||||
return f"{value:.4f}".rjust(width)
|
|
||||||
|
|
||||||
def _display_results_table(self, results: List[Dict[str, Any]]):
|
|
||||||
"""
|
|
||||||
Display evaluation results in a formatted table
|
|
||||||
|
|
||||||
Args:
|
|
||||||
results: List of evaluation results
|
|
||||||
"""
|
|
||||||
logger.info("%s", "=" * 115)
|
|
||||||
logger.info("📊 EVALUATION RESULTS SUMMARY")
|
|
||||||
logger.info("%s", "=" * 115)
|
|
||||||
|
|
||||||
# Table header
|
|
||||||
logger.info(
|
|
||||||
"%-4s | %-50s | %6s | %7s | %6s | %7s | %6s | %6s",
|
|
||||||
"#",
|
|
||||||
"Question",
|
|
||||||
"Faith",
|
|
||||||
"AnswRel",
|
|
||||||
"CtxRec",
|
|
||||||
"CtxPrec",
|
|
||||||
"RAGAS",
|
|
||||||
"Status",
|
|
||||||
)
|
|
||||||
logger.info("%s", "-" * 115)
|
|
||||||
|
|
||||||
# Table rows
|
|
||||||
for result in results:
|
|
||||||
test_num = result.get("test_number", 0)
|
|
||||||
question = result.get("question", "")
|
|
||||||
# Truncate question to 50 chars
|
|
||||||
question_display = (
|
|
||||||
(question[:47] + "...") if len(question) > 50 else question
|
|
||||||
)
|
|
||||||
|
|
||||||
metrics = result.get("metrics", {})
|
|
||||||
if metrics:
|
|
||||||
# Success case - format each metric, handling NaN values
|
|
||||||
faith = metrics.get("faithfulness", 0)
|
|
||||||
ans_rel = metrics.get("answer_relevance", 0)
|
|
||||||
ctx_rec = metrics.get("context_recall", 0)
|
|
||||||
ctx_prec = metrics.get("context_precision", 0)
|
|
||||||
ragas = result.get("ragas_score", 0)
|
|
||||||
status = "✓"
|
|
||||||
|
|
||||||
logger.info(
|
|
||||||
"%-4d | %-50s | %s | %s | %s | %s | %s | %6s",
|
|
||||||
test_num,
|
|
||||||
question_display,
|
|
||||||
self._format_metric(faith, 6),
|
|
||||||
self._format_metric(ans_rel, 7),
|
|
||||||
self._format_metric(ctx_rec, 6),
|
|
||||||
self._format_metric(ctx_prec, 7),
|
|
||||||
self._format_metric(ragas, 6),
|
|
||||||
status,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Error case
|
|
||||||
error = result.get("error", "Unknown error")
|
|
||||||
error_display = (error[:20] + "...") if len(error) > 23 else error
|
|
||||||
logger.info(
|
|
||||||
"%-4d | %-50s | %6s | %7s | %6s | %7s | %6s | ✗ %s",
|
|
||||||
test_num,
|
|
||||||
question_display,
|
|
||||||
"N/A",
|
|
||||||
"N/A",
|
|
||||||
"N/A",
|
|
||||||
"N/A",
|
|
||||||
"N/A",
|
|
||||||
error_display,
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.info("%s", "=" * 115)
|
|
||||||
|
|
||||||
def _calculate_benchmark_stats(
|
|
||||||
self, results: List[Dict[str, Any]]
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Calculate benchmark statistics from evaluation results
|
|
||||||
|
|
||||||
Args:
|
|
||||||
results: List of evaluation results
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dictionary with benchmark statistics
|
|
||||||
"""
|
|
||||||
# Filter out results with errors
|
|
||||||
valid_results = [r for r in results if r.get("metrics")]
|
|
||||||
total_tests = len(results)
|
|
||||||
successful_tests = len(valid_results)
|
|
||||||
failed_tests = total_tests - successful_tests
|
|
||||||
|
|
||||||
if not valid_results:
|
|
||||||
return {
|
|
||||||
"total_tests": total_tests,
|
|
||||||
"successful_tests": 0,
|
|
||||||
"failed_tests": failed_tests,
|
|
||||||
"success_rate": 0.0,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Calculate averages for each metric (handling NaN values correctly)
|
|
||||||
# Track both sum and count for each metric to handle NaN values properly
|
|
||||||
metrics_data = {
|
|
||||||
"faithfulness": {"sum": 0.0, "count": 0},
|
|
||||||
"answer_relevance": {"sum": 0.0, "count": 0},
|
|
||||||
"context_recall": {"sum": 0.0, "count": 0},
|
|
||||||
"context_precision": {"sum": 0.0, "count": 0},
|
|
||||||
"ragas_score": {"sum": 0.0, "count": 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
for result in valid_results:
|
|
||||||
metrics = result.get("metrics", {})
|
|
||||||
|
|
||||||
# For each metric, sum non-NaN values and count them
|
|
||||||
faithfulness = metrics.get("faithfulness", 0)
|
|
||||||
if not _is_nan(faithfulness):
|
|
||||||
metrics_data["faithfulness"]["sum"] += faithfulness
|
|
||||||
metrics_data["faithfulness"]["count"] += 1
|
|
||||||
|
|
||||||
answer_relevance = metrics.get("answer_relevance", 0)
|
|
||||||
if not _is_nan(answer_relevance):
|
|
||||||
metrics_data["answer_relevance"]["sum"] += answer_relevance
|
|
||||||
metrics_data["answer_relevance"]["count"] += 1
|
|
||||||
|
|
||||||
context_recall = metrics.get("context_recall", 0)
|
|
||||||
if not _is_nan(context_recall):
|
|
||||||
metrics_data["context_recall"]["sum"] += context_recall
|
|
||||||
metrics_data["context_recall"]["count"] += 1
|
|
||||||
|
|
||||||
context_precision = metrics.get("context_precision", 0)
|
|
||||||
if not _is_nan(context_precision):
|
|
||||||
metrics_data["context_precision"]["sum"] += context_precision
|
|
||||||
metrics_data["context_precision"]["count"] += 1
|
|
||||||
|
|
||||||
ragas_score = result.get("ragas_score", 0)
|
|
||||||
if not _is_nan(ragas_score):
|
|
||||||
metrics_data["ragas_score"]["sum"] += ragas_score
|
|
||||||
metrics_data["ragas_score"]["count"] += 1
|
|
||||||
|
|
||||||
# Calculate averages using actual counts for each metric
|
|
||||||
avg_metrics = {}
|
|
||||||
for metric_name, data in metrics_data.items():
|
|
||||||
if data["count"] > 0:
|
|
||||||
avg_val = data["sum"] / data["count"]
|
|
||||||
avg_metrics[metric_name] = (
|
|
||||||
round(avg_val, 4) if not _is_nan(avg_val) else 0.0
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
avg_metrics[metric_name] = 0.0
|
|
||||||
|
|
||||||
# Find min and max RAGAS scores (filter out NaN)
|
|
||||||
ragas_scores = []
|
|
||||||
for r in valid_results:
|
|
||||||
score = r.get("ragas_score", 0)
|
|
||||||
if _is_nan(score):
|
|
||||||
continue # Skip NaN values
|
|
||||||
ragas_scores.append(score)
|
|
||||||
|
|
||||||
min_score = min(ragas_scores) if ragas_scores else 0
|
|
||||||
max_score = max(ragas_scores) if ragas_scores else 0
|
|
||||||
|
|
||||||
return {
|
|
||||||
"total_tests": total_tests,
|
|
||||||
"successful_tests": successful_tests,
|
|
||||||
"failed_tests": failed_tests,
|
|
||||||
"success_rate": round(successful_tests / total_tests * 100, 2),
|
|
||||||
"average_metrics": avg_metrics,
|
|
||||||
"min_ragas_score": round(min_score, 4),
|
|
||||||
"max_ragas_score": round(max_score, 4),
|
|
||||||
}
|
|
||||||
|
|
||||||
async def run(self) -> Dict[str, Any]:
|
async def run(self) -> Dict[str, Any]:
|
||||||
"""Run complete evaluation pipeline"""
|
"""Run complete evaluation pipeline"""
|
||||||
|
|
||||||
|
|
@ -799,76 +330,35 @@ class RAGEvaluator:
|
||||||
|
|
||||||
elapsed_time = time.time() - start_time
|
elapsed_time = time.time() - start_time
|
||||||
|
|
||||||
# Calculate benchmark statistics
|
|
||||||
benchmark_stats = self._calculate_benchmark_stats(results)
|
|
||||||
|
|
||||||
# Save results
|
# Save results
|
||||||
summary = {
|
summary = {
|
||||||
"timestamp": datetime.now().isoformat(),
|
"timestamp": datetime.now().isoformat(),
|
||||||
"total_tests": len(results),
|
"total_tests": len(results),
|
||||||
"elapsed_time_seconds": round(elapsed_time, 2),
|
"elapsed_time_seconds": round(elapsed_time, 2),
|
||||||
"benchmark_stats": benchmark_stats,
|
|
||||||
"results": results,
|
"results": results,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Save JSON results
|
# Save JSON results
|
||||||
json_path = (
|
json_path = self.results_dir / f"results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
||||||
self.results_dir
|
|
||||||
/ f"results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
|
||||||
)
|
|
||||||
with open(json_path, "w") as f:
|
with open(json_path, "w") as f:
|
||||||
json.dump(summary, f, indent=2)
|
json.dump(summary, f, indent=2)
|
||||||
|
print(f"✅ JSON results saved to: {json_path}")
|
||||||
# Display results table
|
|
||||||
self._display_results_table(results)
|
|
||||||
|
|
||||||
logger.info("✅ JSON results saved to: %s", json_path)
|
|
||||||
|
|
||||||
# Export to CSV
|
# Export to CSV
|
||||||
csv_path = self._export_to_csv(results)
|
csv_path = self._export_to_csv(results)
|
||||||
logger.info("✅ CSV results saved to: %s", csv_path)
|
print(f"✅ CSV results saved to: {csv_path}")
|
||||||
|
|
||||||
# Print summary
|
# Print summary
|
||||||
logger.info("")
|
print("\n" + "="*70)
|
||||||
logger.info("%s", "=" * 70)
|
print("📊 EVALUATION COMPLETE")
|
||||||
logger.info("📊 EVALUATION COMPLETE")
|
print("="*70)
|
||||||
logger.info("%s", "=" * 70)
|
print(f"Total Tests: {len(results)}")
|
||||||
logger.info("Total Tests: %s", len(results))
|
print(f"Elapsed Time: {elapsed_time:.2f} seconds")
|
||||||
logger.info("Successful: %s", benchmark_stats["successful_tests"])
|
print(f"Results Dir: {self.results_dir.absolute()}")
|
||||||
logger.info("Failed: %s", benchmark_stats["failed_tests"])
|
print("\n📁 Generated Files:")
|
||||||
logger.info("Success Rate: %.2f%%", benchmark_stats["success_rate"])
|
print(f" • CSV: {csv_path.name}")
|
||||||
logger.info("Elapsed Time: %.2f seconds", elapsed_time)
|
print(f" • JSON: {json_path.name}")
|
||||||
logger.info("Avg Time/Test: %.2f seconds", elapsed_time / len(results))
|
print("="*70 + "\n")
|
||||||
|
|
||||||
# Print benchmark metrics
|
|
||||||
logger.info("")
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
logger.info("📈 BENCHMARK RESULTS (Average)")
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
avg = benchmark_stats["average_metrics"]
|
|
||||||
logger.info("Average Faithfulness: %.4f", avg["faithfulness"])
|
|
||||||
logger.info("Average Answer Relevance: %.4f", avg["answer_relevance"])
|
|
||||||
logger.info("Average Context Recall: %.4f", avg["context_recall"])
|
|
||||||
logger.info("Average Context Precision: %.4f", avg["context_precision"])
|
|
||||||
logger.info("Average RAGAS Score: %.4f", avg["ragas_score"])
|
|
||||||
logger.info("")
|
|
||||||
logger.info(
|
|
||||||
"Min RAGAS Score: %.4f",
|
|
||||||
benchmark_stats["min_ragas_score"],
|
|
||||||
)
|
|
||||||
logger.info(
|
|
||||||
"Max RAGAS Score: %.4f",
|
|
||||||
benchmark_stats["max_ragas_score"],
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.info("")
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
logger.info("📁 GENERATED FILES")
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
logger.info("Results Dir: %s", self.results_dir.absolute())
|
|
||||||
logger.info(" • CSV: %s", csv_path.name)
|
|
||||||
logger.info(" • JSON: %s", json_path.name)
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
|
|
||||||
return summary
|
return summary
|
||||||
|
|
||||||
|
|
@ -877,64 +367,30 @@ async def main():
|
||||||
"""
|
"""
|
||||||
Main entry point for RAGAS evaluation
|
Main entry point for RAGAS evaluation
|
||||||
|
|
||||||
Command-line arguments:
|
|
||||||
--dataset, -d: Path to test dataset JSON file (default: sample_dataset.json)
|
|
||||||
--ragendpoint, -r: LightRAG API endpoint URL (default: http://localhost:9621 or $LIGHTRAG_API_URL)
|
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
python lightrag/evaluation/eval_rag_quality.py
|
python lightrag/evaluation/eval_rag_quality.py
|
||||||
python lightrag/evaluation/eval_rag_quality.py --dataset my_test.json
|
python lightrag/evaluation/eval_rag_quality.py http://localhost:8000
|
||||||
python lightrag/evaluation/eval_rag_quality.py -d my_test.json -r http://localhost:9621
|
python lightrag/evaluation/eval_rag_quality.py http://your-server.com:8000
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Parse command-line arguments
|
# Get RAG API URL from command line or environment
|
||||||
parser = argparse.ArgumentParser(
|
rag_api_url = None
|
||||||
description="RAGAS Evaluation Script for LightRAG System",
|
if len(sys.argv) > 1:
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
rag_api_url = sys.argv[1]
|
||||||
epilog="""
|
|
||||||
Examples:
|
|
||||||
# Use defaults
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py
|
|
||||||
|
|
||||||
# Specify custom dataset
|
print("\n" + "="*70)
|
||||||
python lightrag/evaluation/eval_rag_quality.py --dataset my_test.json
|
print("🔍 RAGAS Evaluation - Using Real LightRAG API")
|
||||||
|
print("="*70)
|
||||||
|
if rag_api_url:
|
||||||
|
print(f"📡 RAG API URL: {rag_api_url}")
|
||||||
|
else:
|
||||||
|
print("📡 RAG API URL: http://localhost:8000 (default)")
|
||||||
|
print("="*70 + "\n")
|
||||||
|
|
||||||
# Specify custom RAG endpoint
|
evaluator = RAGEvaluator(rag_api_url=rag_api_url)
|
||||||
python lightrag/evaluation/eval_rag_quality.py --ragendpoint http://my-server.com:9621
|
|
||||||
|
|
||||||
# Specify both
|
|
||||||
python lightrag/evaluation/eval_rag_quality.py -d my_test.json -r http://localhost:9621
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--dataset",
|
|
||||||
"-d",
|
|
||||||
type=str,
|
|
||||||
default=None,
|
|
||||||
help="Path to test dataset JSON file (default: sample_dataset.json in evaluation directory)",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--ragendpoint",
|
|
||||||
"-r",
|
|
||||||
type=str,
|
|
||||||
default=None,
|
|
||||||
help="LightRAG API endpoint URL (default: http://localhost:9621 or $LIGHTRAG_API_URL environment variable)",
|
|
||||||
)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
logger.info("🔍 RAGAS Evaluation - Using Real LightRAG API")
|
|
||||||
logger.info("%s", "=" * 70)
|
|
||||||
|
|
||||||
evaluator = RAGEvaluator(
|
|
||||||
test_dataset_path=args.dataset, rag_api_url=args.ragendpoint
|
|
||||||
)
|
|
||||||
await evaluator.run()
|
await evaluator.run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("❌ Error: %s", e)
|
print(f"\n❌ Error: {str(e)}\n")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
44
lightrag/evaluation/sample_dataset.json
Normal file
44
lightrag/evaluation/sample_dataset.json
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
{
|
||||||
|
"test_cases": [
|
||||||
|
{
|
||||||
|
"question": "What is LightRAG and what problem does it solve?",
|
||||||
|
"ground_truth": "LightRAG is a Simple and Fast Retrieval-Augmented Generation framework developed by HKUDS. It solves the problem of efficiently combining large language models with external knowledge retrieval to provide accurate, contextual responses while reducing hallucinations.",
|
||||||
|
"context": "general_rag_knowledge"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "What are the main components of a RAG system?",
|
||||||
|
"ground_truth": "A RAG system consists of three main components: 1) A retrieval system (vector database or search engine) to find relevant documents, 2) An embedding model to convert text into vector representations, and 3) A large language model (LLM) to generate responses based on retrieved context.",
|
||||||
|
"context": "rag_architecture"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "How does LightRAG improve upon traditional RAG approaches?",
|
||||||
|
"ground_truth": "LightRAG improves upon traditional RAG by offering a simpler API, faster retrieval performance, better integration with various vector databases, and optimized prompting strategies. It focuses on ease of use while maintaining high quality results.",
|
||||||
|
"context": "lightrag_features"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "What vector databases does LightRAG support?",
|
||||||
|
"ground_truth": "LightRAG supports multiple vector databases including ChromaDB, Neo4j, Milvus, Qdrant, MongoDB Atlas Vector Search, and Redis. It also includes a built-in nano-vectordb for simple deployments.",
|
||||||
|
"context": "supported_storage"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "What are the key metrics for evaluating RAG system quality?",
|
||||||
|
"ground_truth": "Key RAG evaluation metrics include: 1) Faithfulness - whether answers are factually grounded in retrieved context, 2) Answer Relevance - how well answers address the question, 3) Context Recall - completeness of retrieval, and 4) Context Precision - quality and relevance of retrieved documents.",
|
||||||
|
"context": "rag_evaluation"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "How can you deploy LightRAG in production?",
|
||||||
|
"ground_truth": "LightRAG can be deployed in production using Docker containers, as a REST API server with FastAPI, or integrated directly into Python applications. It supports environment-based configuration, multiple LLM providers, and can scale horizontally.",
|
||||||
|
"context": "deployment_options"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "What LLM providers does LightRAG support?",
|
||||||
|
"ground_truth": "LightRAG supports multiple LLM providers including OpenAI (GPT-3.5, GPT-4), Anthropic Claude, Ollama for local models, Azure OpenAI, AWS Bedrock, and any OpenAI-compatible API endpoint.",
|
||||||
|
"context": "llm_integration"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "What is the purpose of graph-based retrieval in RAG systems?",
|
||||||
|
"ground_truth": "Graph-based retrieval in RAG systems enables relationship-aware context retrieval. It stores entities and their relationships as a knowledge graph, allowing the system to understand connections between concepts and retrieve more contextually relevant information beyond simple semantic similarity.",
|
||||||
|
"context": "knowledge_graph_rag"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue