Metadata-Version: 2.1
Name: nlsql
Version: 0.0.1
Summary: generates SQL query from natural language for your PostrgreSQL database 
License: MIT
Keywords: postgres,sql,text-to-sq,llm
Author: Prashant Bhatkal
Author-email: prashantbhatkal2000@gmail.com
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: Jinja2 (>=3.1.2)
Requires-Dist: fastapi (>=0.110.2)
Requires-Dist: groq (>=0.9.0)
Requires-Dist: llama-cpp-python (>=0.2.61)
Requires-Dist: psycopg2 (>=2.9.9)
Requires-Dist: pydantic (>=2.7.1)
Requires-Dist: uvicorn (>=0.29.0)
Description-Content-Type: text/markdown

# 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

## nlsql as http server
1. start up the api server
	```bash
	python main.py -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"
	}'
	```

## nlsql as cli command

```bash
python main.py -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 |           |          | 

 ```

