Skip to content

tokligence/LocalSQLAgent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

51 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ LocalSQLAgent - 100% Local Text-to-SQL AI System

100% Local Zero API Cost Execution Accuracy Model Size By Tokligence

๐ŸŽฏ 86% execution accuracy on Spider benchmark with zero API costs and 100% data privacy

๐ŸŒ Bilingual support - Works perfectly with English and Chinese queries

English | ไธญๆ–‡ๆ–‡ๆกฃ

๐Ÿ”ฅ Why LocalSQLAgent?

The Problem with Cloud Solutions

  • ๐Ÿ’ธ Ongoing Costs: Continuous API fees that scale with usage
  • ๐Ÿ”“ Privacy Risk: Your sensitive data leaves your infrastructure
  • ๐ŸŒ Network Dependency: Requires internet, adds latency
  • ๐Ÿšซ Compliance Issues: Many industries can't send data to cloud

Our Solution: 100% Local AI

  • โœ… Zero Cost: No API fees, ever
  • ๐Ÿ”’ 100% Private: Data never leaves your machine
  • โšก Fast: 5-6 seconds average response time
  • ๐Ÿ“Š Proven: 86% execution accuracy on Spider benchmark

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     ๐Ÿ  Your Local Environment                      โ”‚
โ”‚                                                                   โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚   User     โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  LocalSQLAgent  โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  Ollama + LLM   โ”‚ โ”‚
โ”‚  โ”‚   Query    โ”‚     โ”‚  (Intelligent   โ”‚     โ”‚ qwen2.5-coder:7bโ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚    Agent)       โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                         โ”‚
โ”‚                              โ–ผ                                   โ”‚
โ”‚                     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”            โ”‚
โ”‚                     โ”‚    Your Databases           โ”‚            โ”‚
โ”‚                     โ”‚ PostgreSQLโ”‚MySQLโ”‚MongoDBโ”‚... โ”‚            โ”‚
โ”‚                     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜            โ”‚
โ”‚                                                                   โ”‚
โ”‚  ๐Ÿ’ฐ $0 Cost    ๐Ÿ”’ 100% Private    โšก 5.4s Avg    ๐Ÿ“Š 86% EA      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Quick Start

1. Install Ollama

# macOS/Linux
curl -fsSL https://ollama.com/install.sh | sh

# Pull the recommended model (4.7GB)
ollama pull qwen2.5-coder:7b

2. Install LocalSQLAgent

git clone https://github.com/tokligence/LocalSQLAgent.git
cd LocalSQLAgent
pip install -e .

3. Run Your First Query

from localsql import IntelligentSQLAgent

# Connect to your database
agent = IntelligentSQLAgent("postgresql://localhost/mydb")

# Ask questions in natural language
result = agent.query("Show me top 10 customers by revenue last month")
print(result)

๐Ÿ“Š Performance & Model Selection

Recommended Model

โœ… qwen2.5-coder:7b - Best balance of accuracy, speed, and resource usage

  • 86% execution accuracy on Spider benchmark*
  • 5.4s average response time
  • 4.7GB disk space
  • ~6GB RAM required

*Tested on MacBook Pro (M-series, 48GB RAM) with Spider dev dataset (50 samples)

Alternative Models Tested

Model EA (%) Speed Verdict
qwen2.5-coder:7b 86% 5.4s โœ… Best Choice
deepseek-coder-v2:16b 68% 4.0s โœ… Good alternative
codestral:22b 82% 30.6s โš ๏ธ Too slow
qwen2.5:14b 82% 10.0s โŒ General model, not optimized

Key Finding: Smaller domain-specific models outperform larger general models for SQL tasks

View detailed model analysis โ†’

๐Ÿ’ก Key Features

๐Ÿง  Intelligent Error Learning

  • Automatically learns from SQL execution errors
  • Self-corrects common mistakes (ambiguous columns, missing GROUP BY, etc.)
  • Improves accuracy from 82% to 86% through error recovery

๐ŸŒ True Bilingual Support

# English
result = agent.query("Show me sales trends")

# ไธญๆ–‡ๅŒๆ ทๅฎŒ็พŽๆ”ฏๆŒ
result = agent.query("ๆ˜พ็คบไธŠไธชๆœˆ้”€ๅ”ฎๅ‰10็š„ไบงๅ“")

๐Ÿ”Œ Multi-Database Support

  • PostgreSQL, MySQL, SQLite
  • MongoDB (via SQL interface)
  • ClickHouse, DuckDB
  • Any SQL-compatible database

๐Ÿš€ Production Ready

  • REST API with FastAPI
  • Docker support
  • Concurrent request handling (10+ QPS)
  • Comprehensive test suite

๐Ÿ“ˆ Benchmarks

Spider Dataset Results (50 samples)

  • Execution Accuracy (EA): 86%
  • Average Latency: 5.41s
  • Average Attempts: 2.5
  • Success Rate: 100% (with retries)

Multi-Attempt Strategy

Attempts EA (%) Latency Finding
1 84% 2.4s Fast but may fail
5 85% 4.0s +1% EA improvement
7 85% 4.8s No further gain

Recommendation: Use 1-3 attempts for best speed/accuracy balance

๐Ÿ› ๏ธ Advanced Usage

API Server

# Start the API server
python api_server.py

# Query via HTTP
curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{"query": "Show me all users who joined this month"}'

Docker Deployment

docker build -t localsqlagent .
docker run -p 8000:8000 localsqlagent

Custom Model Configuration

agent = IntelligentSQLAgent(
    db_url="postgresql://localhost/mydb",
    model_name="deepseek-coder-v2:16b",  # Use alternative model
    max_attempts=3,
    temperature=0.1
)

๐Ÿ’ฐ Solution Comparison

Solution Cost Model Data Privacy Setup Time
LocalSQLAgent Free Forever โœ… 100% Local 5 minutes
Cloud APIs Usage-based billing โš ๏ธ Data leaves premises 30 minutes
Self-hosted GPU Infrastructure costs โœ… Local Days-Weeks

๐Ÿค Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

๐Ÿ“„ License

Apache 2.0 - Free for commercial use

๐Ÿ™ Acknowledgments

  • Powered by Ollama
  • Spider dataset from Yale University
  • Built with love by Tokligence

Ready to eliminate API costs? Star โญ this repo and get started in 5 minutes!

About

๐Ÿš€ Local Text-to-SQL agent system powered by Ollama. 75%+ accuracy with 7B models, zero API cost, 100% privacy

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages