https://github.com/prashantbtkl/nlsql
generates SQL query from natural language for your PostrgreSQL database
https://github.com/prashantbtkl/nlsql
llm llm-inference postgres postgresql sql text-to-sql
Last synced: 11 months ago
JSON representation
generates SQL query from natural language for your PostrgreSQL database
- Host: GitHub
- URL: https://github.com/prashantbtkl/nlsql
- Owner: PrashantBtkl
- License: mit
- Created: 2024-04-18T14:28:42.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-27T06:29:13.000Z (over 1 year ago)
- Last Synced: 2025-03-31T05:31:52.220Z (about 1 year ago)
- Topics: llm, llm-inference, postgres, postgresql, sql, text-to-sql
- Language: Python
- Homepage:
- Size: 40 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
NLSQL
[](https://pypi.org/project/nlsql/)
[](https://pypi.org/project/nlsql/)
[](https://pypi.org/project/nlsql/)
[](https://pypi.org/project/nlsql/)
Generates SQL query from natural language for your PostrgreSQL database.
Simply connects to your database, gets the schema and generates query appropriately.
## User Guide
you can use nlsql as httpserver and as a cli-command
## Installation
```bash
pip install nlsql
```
## run as http server
1. start up the api server
```bash
nlsql -s
```
2. get results via curl
```curl
curl --location 'http://localhost:8000/query' \
--header 'Content-Type: application/json' \
--data-raw '{
"question": "get users with post that has top views and top reactions in the last 24 hours",
"db_url": "postgresql://postgres:postgres@localhost:5432/postgres"
}'
```
## run as cli command
```bash
nlsql -m "./Nous-Hermes-2-Mistral-7B-DPO.Q4_0.gguf" -d "postgresql://postgres:postgres@localhost:5432/postgres" -q "get users with post that has top views and top reactions in the last 24 hours"
```
## Response
```sql
SELECT DISTINCT ON (users.id) users.id, users.name
FROM users
JOIN posts ON users.id = posts.post_creator_user_id
WHERE posts.created_at > now() - interval '1 day'
GROUP BY users.id, users.name
HAVING SUM(posts.views) >= ALL (SELECT SUM(views) FROM posts WHERE created_at > now() - interval '1 day') AND SUM(posts.reactions) >= ALL (SELECT SUM(reactions) FROM posts WHERE created_at > now() - interval '1 day');
```
response was generated over the following `users` and `posts` tables:
```
postgres=# \d users
Column | Type | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
name | text | | |
created_at | timestamp without time zone | | |
postgres=# \d posts
Column | Type | Collation | Nullable | Default
----------------------+-----------------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('posts_id_seq'::regclass)
title | text | | |
url | text | | |
post_creator_user_id | integer | | |
views | integer | | |
reactions | integer | | |
created_at | timestamp without time zone | | |
updated_at | timestamp without time zone | | |
```