A natural language to SQL query converter powered by LLMs.
Project description
SQL-Agent
SQL-Agent is a powerful command-line tool that converts natural language questions into SQL queries. It understands your database structure through a simple YAML configuration and securely connects to your database to fetch answers.
Installation
There are several ways to install the SQL-Agent.
Option 1: From PyPI (Recommended)
You can install the package directly from PyPI:
pip install SQL-Agent
Option 2: From a Wheel File
If you have a .whl file, you can install it directly using pip:
pip install /path/to/your/file/sql_agent-0.1.0-py3-none-any.whl
Option 3: From GitHub
Once the project is hosted on GitHub, you can also install it directly from the repository:
pip install git+https://github.com/[your-username]/[your-repo-name].git
Quick Setup Guide for Wheel Installation
If you installed SQL-Agent from a .whl file, follow these steps to get started:
-
Install the wheel file:
pip install /path/to/your/file/sql_agent-0.1.0-py3-none-any.whl
-
Create your working directory:
mkdir my-sql-agent-project cd my-sql-agent-project
-
Create your
config.inifile in this directory:touch config.ini # Edit config.ini and add your database credentials and API keys
-
Create your schema YAML file:
touch my_schema.yaml # Edit my_schema.yaml and define your database structure # You can reference the sample file included with the package
-
Run the agent:
sql-agent "Your question here" "my_schema.yaml"
Configuration
Before using the agent, you need to configure your database connections and API keys.
1. Create config.ini
Create a file named config.ini in the directory where you will run the agent. This file will hold your credentials.
Important for Wheel Installation:
When installing from a .whl file, you have two options for placing your config.ini:
- Option 1 (Recommended): Place
config.iniin your current working directory where you run thesql-agentcommand. - Option 2: Place
config.iniin the package installation directory:- Find the package location:
python -c "import structured_agent; import os; print(os.path.dirname(structured_agent.__file__))" - Copy
config.inito:<package-location>/utils/config.ini
- Find the package location:
The agent first checks for config.ini in your current directory, then falls back to the package's utils folder.
2. Add Your Configurations
Copy the necessary configuration sections into your config.ini and fill in your credentials. Below are some examples.
For Oracle Database:
[database_type]
db_type = oracle
[oracle_dev]
user = <your_oracle_user>
password = <your_oracle_password>
dsn = <your_oracle_dsn>
For Snowflake:
[database_type]
db_type = snowflake
[snowflake_etl_dev]
user = <your_user>
password = <your_password>
account = <your_account>
warehouse = <your_warehouse>
database = <your_database>
schema = <your_schema>
role = <your_role>
Model API Keys: The agent uses a language model to understand questions. Configure your model's API keys like this:
[model_4_o_mini_api_conf]
OAUTH_URL = <your_oauth_url>
OPENAI_API_BASE = <your_api_base>
OPENAI_API_VERSION = <your_api_version>
OPENAI_API_MODEL = <your_model>
OPENAI_API_TYPE = azure
3. Define Your Database Schema
The agent needs a YAML file to understand your database's tables, columns, and relationships.
Create a .yaml file (e.g., my_schema.yaml) and define your database structure as shown in the example below.
Example: school_schema.yaml
db_name: "SchoolDB"
schema_name: "public"
app_name: "Library"
db_type: "snowflake"
name:
description:
tables:
- table_name: "authors"
columns:
- column_name: "author_id"
data_type: "INT"
is_primary_key: true
- column_name: "author_name"
data_type: "VARCHAR(100)"
description: "Name of the author"
- table_name: "books"
columns:
- column_name: "book_id"
data_type: "INT"
is_primary_key: true
- column_name: "title"
data_type: "VARCHAR(255)"
description: "Title of the book"
- column_name: "author_id"
data_type: "INT"
is_foreign_key: true
foreign_key_ref: "authors(author_id)"
foreign_key_relationships:
- "books.author_id can be joined with authors.author_id"
verified_queries:
- "Total number of books by each author"
Note on YAML File Location:
- Your schema YAML file (e.g.,
school_schema.yaml) can be placed anywhere you prefer. - You'll provide the path to this file when running the
sql-agentcommand. - For convenience, you can place it in your current working directory and reference it by name only.
- A sample YAML file (
sample_school_library_inventory.yaml) is included in the package for reference. You can find it at<package-location>/utils/sample_school_library_inventory.yaml.
Usage
Once your config.ini and schema YAML file are ready, you can run the agent from your terminal.
The command format is:
sql-agent "Your natural language question" "path/to/your_schema.yaml"
Example
From the directory containing your config.ini and school_schema.yaml, run:
sql-agent "How many books are there in the library?" "school_schema.yaml"
The agent will process your question, generate the appropriate SQL query, execute it, and return a natural language answer.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file structured_sql_agent-0.1.0.tar.gz.
File metadata
- Download URL: structured_sql_agent-0.1.0.tar.gz
- Upload date:
- Size: 29.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fde07a21790ccfe0eb0942cf93868722bbc0906543bfe3a8065d8e88ae719fc9
|
|
| MD5 |
3a32c6cdf14b6de20e191f93891653d1
|
|
| BLAKE2b-256 |
ad958116581ca308f924b73d4b465bc7202344f1856aeeca55ecd7f44bfd93e0
|
File details
Details for the file structured_sql_agent-0.1.0-py3-none-any.whl.
File metadata
- Download URL: structured_sql_agent-0.1.0-py3-none-any.whl
- Upload date:
- Size: 35.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fce6ad02ce950f59e268a62590731a9768e2f450d4948508e2937e0e7b687956
|
|
| MD5 |
383874c4ba5e36fc26df0bbe0a58efc4
|
|
| BLAKE2b-256 |
156c526fd2d0a49dff2f487f2e6f8a75e82e625b3736920244503e11d3ed152e
|