File size: 2,146 Bytes
cb26688
fb3980b
 
 
 
 
 
172e178
cb26688
1a4aef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---

title: Build AI Agent Text-to-SQL
emoji: πŸ—£οΈ
colorFrom: green
colorTo: blue
sdk: static
pinned: false
app_file: index.html
---


## Build AI Agent Text-to-SQL

πŸ—£οΈ **Text-to-SQL AI Agent**  
A demonstration project for building an intelligent agent that translates natural language queries into SQL, executes them, and returns results using [smolagents](https://github.com/smol-ai/smol-agent), [SQLAlchemy](https://www.sqlalchemy.org/), and [python-dotenv](https://pypi.org/project/python-dotenv/).

---

## Features

- Converts plain English questions into SQL queries.
- Executes queries on an in-memory SQLite database.
- Supports multiple tables and table joins.
- Uses LLMs for advanced query understanding.
- Easily extensible for new tables and schemas.

## Installation

````bash

git clone https://github.com/your-username/txt-to-sql-agent.git

cd txt-to-sql-agent

pip install -r requirements.txt

```plaintext



## Usage



Edit `text_to_sql.py` to define your tables and sample data.

Run the agent:



```bash

python text_to_sql.py

````

## Example

Ask the agent:

> "Which waiter received the highest total amount in tips?"

The agent will generate and execute the appropriate SQL query, returning the answer.

## Project Structure

```text

.

β”œβ”€β”€ README.md

β”œβ”€β”€ text_to_sql.py

β”œβ”€β”€ index.html

```

## How It Works

- Defines tables (`receipts`, `waiters`) using SQLAlchemy.
- Loads sample data into an in-memory SQLite database.
- Uses smolagents to create a CodeAgent with SQL querying tools.
- The agent interprets natural language and generates SQL queries.
- Results are printed to the console.

## Contributing

Pull requests and suggestions are welcome!  
Please open an issue for bugs or feature requests.

## License

[![License](https://img.shields.io/badge/Apache--2.0-green?style=for-the-badge)](https://opensource.org/licenses/Apache-2.0)

---

[![HF Space](https://img.shields.io/badge/-HuggingFace-FDEE21?style=for-the-badge&logo=HuggingFace&logoColor=black)](https://huggingface.co/spaces/dev-jas/text-to-sql-smolagent)