~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
Add Claude API grading benchmark integration for source-based strategy testing
LearningCircuit90d9b2bc73d7
diff --git a/.gitignore b/.gitignoreindex 03c58d3526..bd0df22a82 100644--- a/.gitignore+++ b/.gitignore@@ -78,6 +78,30 @@ examples/*/optimization_*/examples/benchmarks/results/examples/optimization/results/tests/import_tests/+benchmark_results/+strategy_benchmark_results/+strategy_sim_results_*/+*_results_*/+optimization_results_*/+optimization_demo_*/++# Utility scripts for benchmarks+update_api_key.py+update_max_tokens.py+verify_grader_config.py+/run_*_benchmark.sh+/run_fixed_benchmark.sh+/run_verification.sh+/run_update_*.sh+/run_*.sh+/*_benchmark.py+benchmark_cleanup.sh+benchmark_*summary.md+simulation_*summary.md++# Exceptions for benchmark example scripts+!examples/benchmarks/**/run_*.sh+!examples/benchmarks/**/*.py# Retain directory structure but ignore contents!data/benchmark_results/.gitkeepdiff --git a/examples/benchmarks/claude_grading/README.md b/examples/benchmarks/claude_grading/README.mdnew file mode 100644index 0000000000..1f578680aa--- /dev/null+++ b/examples/benchmarks/claude_grading/README.md@@ -0,0 +1,43 @@+# Claude API Grading Benchmark++This benchmark integrates Claude 3 Sonnet for grading benchmark results with proper API access through the local database.++## Features++- Uses Claude 3 Sonnet for grading benchmark results+- Accesses API keys from the local database+- Supports SimpleQA and BrowseComp benchmarks+- Provides composite scoring with customizable weights+- Comprehensive metrics and accuracy reports++## Usage++From the project root directory:++```bash+# Run with default settings (source_based strategy, 1 iteration, 5 examples)+./examples/benchmarks/claude_grading/run_benchmark.sh++# Run with custom parameters+./examples/benchmarks/claude_grading/run_benchmark.sh --strategy source_based --iterations 2 --examples 200+```++## How It Works++The benchmark integrates with the evaluation system by patching the grading module to use the local `get_llm` function, which properly retrieves API keys from the database and configures the Claude model for grading.++This approach ensures accurate grading of benchmark results and enables comparison between different strategies and configurations.++## Requirements++- Valid Claude API key stored in the local database+- SearXNG search engine running locally+- Python dependencies installed++## Output++Results are saved in the `benchmark_results` directory with comprehensive metrics:+- Accuracy scores+- Processing times+- Grading confidence+- Detailed evaluation reports\ No newline at end of filediff --git a/examples/benchmarks/claude_grading/__init__.py b/examples/benchmarks/claude_grading/__init__.pynew file mode 100644index 0000000000..724f6ab992--- /dev/null+++ b/examples/benchmarks/claude_grading/__init__.py@@ -0,0 +1,6 @@+"""+Claude API Grading Benchmark Module++This module provides tools for benchmarking search strategies+with proper grading through Claude API integration.+"""\ No newline at end of filediff --git a/examples/benchmarks/claude_grading/benchmark.py b/examples/benchmarks/claude_grading/benchmark.pynew file mode 100755index 0000000000..042bb513a6--- /dev/null+++ b/examples/benchmarks/claude_grading/benchmark.py@@ -0,0 +1,290 @@+#!/usr/bin/env python+"""+Benchmark with Claude API Grading Integration++This script runs a comprehensive evaluation of search strategies with+proper Claude API integration for grading benchmark results.++Features:+- Uses the local database for API keys+- Configures Claude 3 Sonnet for grading+- Supports SimpleQA and BrowseComp evaluations+- Provides detailed metrics and accuracy reports+"""++import logging+import os+import sys+import time+from datetime import datetime++# Set up Python path+src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "src"))+if src_dir not in sys.path:+ sys.path.insert(0, src_dir)++# Set the data directory with the database+data_dir = os.path.join(src_dir, "data")+if os.path.exists(os.path.join(data_dir, "ldr.db")):+ print(f"Found database at {os.path.join(data_dir, 'ldr.db')}")+ # Set environment variable to use this database+ os.environ["LDR_DATA_DIR"] = data_dir+else:+ print(f"Warning: Database not found at {os.path.join(data_dir, 'ldr.db')}")++# Configure logging+logging.basicConfig(+ level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"+)+logger = logging.getLogger(__name__)++def setup_grading_config():+ """+ Create a custom evaluation configuration that uses the local database+ for API keys and specifically uses Claude 3 Sonnet for grading.++ Returns:+ Dict containing the evaluation configuration+ """+ # Import necessary function to get database settings+ try:+ from local_deep_research.utilities.db_utils import get_db_setting+ except ImportError as e:+ print(f"Error importing database utilities: {e}")+ print("Current sys.path:", sys.path)+ return None++ # Create config that uses Claude 3 Sonnet via Anthropic directly+ # This will use the API key from the database+ # Only use parameters that get_llm() accepts+ evaluation_config = {+ "model_name": "claude-3-sonnet-20240229", # Correct Anthropic model name+ "provider": "anthropic", # Use Anthropic directly+ "temperature": 0, # Zero temp for consistent evaluation+ }++ # Check if anthropic API key is available in the database+ try:+ anthropic_key = get_db_setting("llm.anthropic.api_key")+ if anthropic_key:+ print("Found Anthropic API key in database, will use Claude 3 Sonnet for grading")+ else:+ print("Warning: No Anthropic API key found in database")+ print("Checking for alternative providers...")++ # Try OpenRouter as a fallback+ openrouter_key = get_db_setting("llm.openai_endpoint.api_key")+ if openrouter_key:+ print("Found OpenRouter API key, will use OpenRouter with Claude 3 Sonnet")+ evaluation_config = {+ "model_name": "anthropic/claude-3-sonnet-20240229", # OpenRouter format+ "provider": "openai_endpoint",+ "openai_endpoint_url": "https://openrouter.ai/api/v1",+ "temperature": 0,+ }+ except Exception as e:+ print(f"Error checking for API keys: {e}")++ return evaluation_config++def run_benchmark(strategy="source_based", iterations=1, examples=5):+ """+ Run a comprehensive benchmark evaluation of a specific strategy configuration.++ Args:+ strategy: Search strategy to evaluate (default: source_based)+ iterations: Number of iterations for the strategy (default: 1)+ examples: Number of examples to evaluate (default: 5)+ """+ # Import the benchmark components+ try:+ from local_deep_research.benchmarks.evaluators.simpleqa import SimpleQAEvaluator+ from local_deep_research.benchmarks.evaluators.browsecomp import BrowseCompEvaluator+ from local_deep_research.benchmarks.evaluators.composite import CompositeBenchmarkEvaluator+ from local_deep_research.config.llm_config import get_llm+ except ImportError as e:+ print(f"Error importing benchmark components: {e}")+ print("Current sys.path:", sys.path)+ return++ # Set up custom grading configuration+ evaluation_config = setup_grading_config()+ if not evaluation_config:+ print("Failed to setup evaluation configuration, proceeding with default config")++ # Patch the graders module to use our local get_llm+ try:+ # This ensures we use the local get_llm function that accesses the database+ import local_deep_research.benchmarks.graders as graders++ # Store the original function for reference+ original_get_evaluation_llm = graders.get_evaluation_llm++ # Define a new function that uses our local get_llm directly+ def custom_get_evaluation_llm(custom_config=None):+ """+ Override that uses the local get_llm with database access.+ """+ if custom_config is None:+ custom_config = evaluation_config++ print(f"Getting evaluation LLM with config: {custom_config}")+ return get_llm(**custom_config)++ # Replace the function with our custom version+ graders.get_evaluation_llm = custom_get_evaluation_llm+ print("Successfully patched graders.get_evaluation_llm to use local get_llm function")++ except Exception as e:+ print(f"Error patching graders module: {e}")+ import traceback+ traceback.print_exc()++ # Create timestamp for output+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")+ output_dir = os.path.join("benchmark_results", f"claude_grading_{timestamp}")+ os.makedirs(output_dir, exist_ok=True)++ config = {+ "search_strategy": strategy,+ "iterations": iterations,+ # Add other fixed parameters to ensure a complete run+ "questions_per_iteration": 1,+ "max_results": 10,+ "search_tool": "searxng", # Specify SearXNG search engine+ "timeout": 10, # Very short timeout to speed up the demo+ }++ # Run SimpleQA benchmark+ print(f"\n=== Running SimpleQA benchmark with {strategy} strategy, {iterations} iterations ===")+ simpleqa_start = time.time()++ try:+ # Create SimpleQA evaluator (without the evaluation_config parameter)+ simpleqa = SimpleQAEvaluator()++ # The evaluation_config will be used automatically through our patched function+ # when grade_results is called inside the evaluator+ simpleqa_results = simpleqa.evaluate(+ config,+ num_examples=examples,+ output_dir=os.path.join(output_dir, "simpleqa")+ )++ simpleqa_duration = time.time() - simpleqa_start+ print(f"SimpleQA evaluation complete in {simpleqa_duration:.1f} seconds")+ print(f"SimpleQA accuracy: {simpleqa_results.get('accuracy', 0):.4f}")+ print(f"SimpleQA metrics: {simpleqa_results.get('metrics', {})}")++ # Save results+ import json+ with open(os.path.join(output_dir, "simpleqa_results.json"), "w") as f:+ json.dump(simpleqa_results, f, indent=2)+ except Exception as e:+ print(f"Error during SimpleQA evaluation: {e}")+ import traceback+ traceback.print_exc()++ # Run BrowseComp benchmark+ print(f"\n=== Running BrowseComp benchmark with {strategy} strategy, {iterations} iterations ===")+ browsecomp_start = time.time()++ try:+ # Create BrowseComp evaluator (without the evaluation_config parameter)+ browsecomp = BrowseCompEvaluator()++ # The evaluation_config will be used automatically through our patched function+ # when grade_results is called inside the evaluator+ browsecomp_results = browsecomp.evaluate(+ config,+ num_examples=examples,+ output_dir=os.path.join(output_dir, "browsecomp")+ )++ browsecomp_duration = time.time() - browsecomp_start+ print(f"BrowseComp evaluation complete in {browsecomp_duration:.1f} seconds")+ print(f"BrowseComp score: {browsecomp_results.get('score', 0):.4f}")+ print(f"BrowseComp metrics: {browsecomp_results.get('metrics', {})}")++ # Save results+ with open(os.path.join(output_dir, "browsecomp_results.json"), "w") as f:+ json.dump(browsecomp_results, f, indent=2)+ except Exception as e:+ print(f"Error during BrowseComp evaluation: {e}")+ import traceback+ traceback.print_exc()++ # Run composite benchmark+ print(f"\n=== Running Composite benchmark with {strategy} strategy, {iterations} iterations ===")+ composite_start = time.time()++ try:+ # Create composite evaluator with benchmark weights (without evaluation_config parameter)+ benchmark_weights = {"simpleqa": 0.5, "browsecomp": 0.5}+ composite = CompositeBenchmarkEvaluator(+ benchmark_weights=benchmark_weights+ )+ composite_results = composite.evaluate(+ config,+ num_examples=examples,+ output_dir=os.path.join(output_dir, "composite")+ )++ composite_duration = time.time() - composite_start+ print(f"Composite evaluation complete in {composite_duration:.1f} seconds")+ print(f"Composite score: {composite_results.get('score', 0):.4f}")++ # Save results+ with open(os.path.join(output_dir, "composite_results.json"), "w") as f:+ json.dump(composite_results, f, indent=2)+ except Exception as e:+ print(f"Error during composite evaluation: {e}")+ import traceback+ traceback.print_exc()++ # Generate summary+ print(f"\n=== Evaluation Summary ===")+ print(f"Strategy: {strategy}")+ print(f"Iterations: {iterations}")+ print(f"Examples: {examples}")+ print(f"Results saved to: {output_dir}")++ # If we patched the graders module, restore the original function+ if 'original_get_evaluation_llm' in locals():+ graders.get_evaluation_llm = original_get_evaluation_llm+ print("Restored original graders.get_evaluation_llm function")++ return {+ "simpleqa": simpleqa_results if 'simpleqa_results' in locals() else None,+ "browsecomp": browsecomp_results if 'browsecomp_results' in locals() else None,+ "composite": composite_results if 'composite_results' in locals() else None,+ }++def main():+ # Parse command line arguments+ import argparse+ parser = argparse.ArgumentParser(description="Run benchmark with Claude API grading")+ parser.add_argument("--strategy", type=str, default="source_based",+ help="Strategy to evaluate (default: source_based)")+ parser.add_argument("--iterations", type=int, default=1,+ help="Number of iterations (default: 1)")+ parser.add_argument("--examples", type=int, default=5,+ help="Number of examples to evaluate (default: 5)")++ args = parser.parse_args()++ print(f"Starting benchmark of {args.strategy} strategy with {args.iterations} iterations")+ print(f"Evaluating with {args.examples} examples")++ # Run the evaluation+ results = run_benchmark(+ strategy=args.strategy,+ iterations=args.iterations,+ examples=args.examples+ )++ # Return success if at least one benchmark completed+ return 0 if any(results.values()) else 1++if __name__ == "__main__":+ sys.exit(main())\ No newline at end of filediff --git a/examples/benchmarks/claude_grading/run_benchmark.sh b/examples/benchmarks/claude_grading/run_benchmark.shnew file mode 100755index 0000000000..9eda9cbb0f--- /dev/null+++ b/examples/benchmarks/claude_grading/run_benchmark.sh@@ -0,0 +1,29 @@+#!/bin/bash+# Run a benchmark with Claude API grading integration++# Navigate to project root which is needed for proper imports+cd "$(dirname "$0")/../../.."+echo "Changed to project root: $(pwd)"++# Activate virtual environment if it exists+VENV_PATH=".venv/bin/activate"+if [ -f "$VENV_PATH" ]; then+ echo "Activating virtual environment..."+ source "$VENV_PATH"+else+ echo "Warning: Virtual environment not found at $VENV_PATH"+fi++# Create a timestamp for output directory+TIMESTAMP=$(date +"%Y%m%d_%H%M%S")+OUTPUT_DIR="benchmark_results/claude_benchmark_${TIMESTAMP}"+mkdir -p "$OUTPUT_DIR"++echo "Running benchmark with Claude API grading..."+echo "Results will be saved to: $OUTPUT_DIR"++# Use a long timeout for comprehensive benchmarks+pdm run timeout 86400 python -m examples.benchmarks.claude_grading.benchmark "$@"++echo "Benchmark complete or timed out."+echo "Check $OUTPUT_DIR for results."\ No newline at end of file