Skip to main content

SQL Agent Accuracy Evaluation Framework

Project description

SQL Agent Evaluation SDK

A Python SDK for evaluating SQL Agent accuracy with comprehensive test datasets and intelligent LLM-based scoring.

中文文档 | English

Features

  • 🎯 Simple Interface: Easy-to-use SQLAgent interface
  • 📊 38 Test Questions: Covering 5 major SQL optimization scenarios
  • 🤖 LLM Scoring: Intelligent evaluation with detailed explanations
  • 📈 Auto Reports: Automatic result summary and detailed reports
  • 🔧 Flexible Config: Custom database and LLM configuration

Quick Start

1. Install & Configure

pip install -r requirements.txt

Create config/.env:

DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_USER=root
DATABASE_PASSWORD=your_password
DATABASE_NAME=sql_eval_test

EVAL_LLM_API_KEY=your_api_key
EVAL_LLM_BASE_URL=https://api.openai.com/v1
EVAL_LLM_MODEL=gpt-4

2. Create & Evaluate Agent

import asyncio
from sqleval import SQLAgent, SQLEvaluator

class MyAgent(SQLAgent):
    def optimize(self, sql_query: str) -> str:
        # Your optimization logic here
        return "Your optimization suggestions"
    
    def get_name(self) -> str:
        return "My Agent"

async def main():
    evaluator = SQLEvaluator()
    agent = MyAgent()
    
    # Evaluate agent (automatically displays results)
    dataset_results = await evaluator.evaluate(agent, datasets=['example'])
    
    # Extract result and save report
    result = dataset_results['example'][0]
    result.save_report("reports/my_agent_report.md")

asyncio.run(main())

Evaluation Datasets

Dataset Questions Weight Description
index_invalidation 10 25% Index usage issues (functions, type conversion, wildcards)
rule_based_traps 9 25% Scenarios that trap rule-based agents
inefficient_join 10 20% JOIN and subquery optimization
execution_plan_issues 3 15% Execution plan problems (statistics, parameter sniffing)
access_path_issues 6 15% Data access optimization (SELECT *, filtering)

Examples

  • examples/quick_start.py - Minimal example to get started
  • examples/custom_agent_example.py - How to create custom agents
  • examples/batch_evaluation_example.py - Compare multiple agents
  • examples/manual_environment_control.py - Manual environment setup and cleanup
  • examples/reuse_dataset_example.py - Reuse prepared datasets for faster evaluation
  • examples/validate_config.py - Validate your configuration

API Reference

SQLAgent Interface

class MyAgent(SQLAgent):
    def optimize(self, sql_query: str) -> str:
        """Return optimization suggestions for the SQL query"""
        return "Your suggestions"
    
    def get_name(self) -> str:
        """Return agent name"""
        return "My Agent"

SQLEvaluator

Basic Evaluation

evaluator = SQLEvaluator()

# Single agent evaluation
dataset_results = await evaluator.evaluate(agent, datasets=['example'])

# Batch evaluation (compare multiple agents)
dataset_results = await evaluator.evaluate([agent1, agent2, agent3])

Note: The evaluate method automatically displays comparison results and saves combined reports (*_combined_report.md). For individual dataset reports, call result.save_report().

Manual Environment Control

For large datasets or repeated evaluations, you can manually control environment setup and cleanup:

evaluator = SQLEvaluator()

try:
    # 1. Prepare environment once (may be slow for large datasets)
    evaluator.prepare_environment(datasets=['execution_plan_issues'])
    
    # 2. Run multiple evaluations reusing the prepared environment
    await evaluator.evaluate(agent1, auto_cleanup=False)
    await evaluator.evaluate(agent2, auto_cleanup=False)
    await evaluator.evaluate(agent3, auto_cleanup=False)
    
finally:
    # 3. Clean up environment (automatically closes connection)
    evaluator.clean_environment()

Benefits:

  • Significantly faster for repeated evaluations (no re-setup)
  • Full control over when to clean up data
  • Useful for debugging (inspect database state between runs)

Project Structure

sqleval/
├── sqleval/                 # Core SDK
│   ├── core/
│   │   ├── agent.py        # SQLAgent base class
│   │   ├── evaluator.py    # SQLEvaluator main class
│   │   └── result.py       # EvaluationResult class
├── datasets/               # Evaluation datasets
│   ├── index_invalidation/ # Index usage scenarios
│   ├── rule_based_traps/   # Rule-based agent traps
│   ├── inefficient_join/   # JOIN optimization
│   ├── execution_plan_issues/ # Execution plan problems
│   ├── access_path_issues/ # Data access optimization
│   └── meta.txt           # Dataset weights
├── examples/              # Usage examples
└── reports/               # Generated reports

License

Apache License 2.0 - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqleval-0.2.0.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqleval-0.2.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file sqleval-0.2.0.tar.gz.

File metadata

  • Download URL: sqleval-0.2.0.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqleval-0.2.0.tar.gz
Algorithm Hash digest
SHA256 27b6bb25453e12f4598f50c8d2cd59aced0d6c0e1771eae4212ef4e1691d5647
MD5 2ee466512a494875a299d51984d4ba7c
BLAKE2b-256 1768bd4262407eaf476588491a1c02e615c540b6470410621d60f6cd4a1d7046

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqleval-0.2.0.tar.gz:

Publisher: publish.yml on sunzhaoyang/sqleval

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqleval-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sqleval-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqleval-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1deb3e670d94b70feae2415ee3c46fa61d82dd5f444a2b3e07baace2a2261ee2
MD5 4d54341bf28601300bde278c7c84b821
BLAKE2b-256 4eac923648c06fb4b6fa5a42d44719783e79ad1401bcaba8f1e53663ef3cd2f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqleval-0.2.0-py3-none-any.whl:

Publisher: publish.yml on sunzhaoyang/sqleval

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page