sql-agent
allows you to query your SQL Server database using plain English.
It leverages two LLMs for natural language → SQL translation:
- 🌐 Google Gemini (
gemini-2.0-flash
) – Cloud-hosted LLM. - 💻 Ollama (
Alwaleed98/walphi-sql
) – Locally hosted SQL-tuned model.
⚡ Ideal for analytics, debugging, and quick insights without writing raw SQL.
The SQL Agent is built as a conversational pipeline that combines two LLMs and three specialized tools to transform natural language into safe, executable SQL queries.
- User Query → A plain English question is sent to the API.
- Gemini LLM (Core Orchestrator) → Interprets the question and coordinates tools.
- DB Inspector Tool → Inspects the connected SQL Server database to fetch:
- Table names
- Column names
- Data types
- SQL Generator Tool (via SQL-tuned LLM, e.g. Ollama
walphi-sql
) → Uses the schema + user query context to generate a valid SQL statement. - SQL Executor Tool → Executes the generated SQL on the database (read-only mode recommended).
- Gemini LLM → Wraps and refines the response into a conversational answer.
- Response Returned → JSON output with final result.
-
DB Inspector Tool
Helps in getting the list of table names, column names, and data types of the SQL Server database.
Used by Gemini to understand schema and context. -
SQL Generator Tool
Generates SQL using schema metadata + user question.
Ensures correct tables/columns are referenced. -
SQL Executor Tool
Runs the generated SQL query on the target database and returns results.
sequenceDiagram
participant U as User
participant G as Gemini LLM (Orchestrator)
participant I as DBInspector Tool
participant S as SQL Generator Tool (Ollama LLM)
participant E as SQL Executor Tool
participant DB as SQL Server Database
U->>G: Send natural language query
G->>I: Fetch schema (tables, columns, data types)
I-->>G: Return schema metadata
G->>S: Generate SQL using query + schema
S-->>G: Return SQL query
G->>E: Send SQL query for execution
E->>DB: Run SQL query
DB-->>E: Return query results
E-->>G: Results returned
G-->>U: Conversational answer with results
- 🔍 Natural Language to SQL – Ask questions in English, get valid SQL queries.
- 🗄️ Works with SQL Server – Supports connection via DSN string.
- ✅ Read-Only Mode – Recommended for safe
SELECT
queries. - 📦 REST API – Simple endpoint for query execution.
git clone https://github.com/ksdkamesh99/sql-agent.git
cd sql-agent
# SQL Server Connection (Read-Only)
export DATABASE_DSN_STRING=sqlserver://<user>:<password>@<host>:<port>?database=<DB_NAME>&sendStringParametersAsUnicode=false&ApplicationIntent=ReadOnly
# Gemini API Key
export GEMINI_API_KEY=your_gemini_api_key
go run main.go
The API will be available at:
http://localhost:8080
curl --location 'http://localhost:8080/query' \
--header 'Content-Type: application/json' \
--data '{
"question": "what is the top 10 client ids who placed more number of orders today"
}'
{
"result": {
"output": "The top 10 client ids who placed more number of orders today are: S60192979, SB0603, S59054059, S834210, C91239, S57566454, AAAI927921, S833402, CHIEF, and T59505150."
}
}