Create a python virtual environment.
Install python package
pip install virtualenvCreate a virtual environment
python -m venv <env_name>Activating Virtual enviornment
Windows:
.<env_name>/Scipts/activateInstall dependencies
pip install -r requirment.txtCreate .env file with following configurations
OPENAI_API_KEY=YOUR_OPEN_API_KEY
DEPLOYMENT_NAME=YOUR_DEPLOYMENT
ENDPOINT=YOUR_ENDPOINT
LOCATION=YOUR_LOCATION
HOST=CONNECTION_URL
USER=USERNAME
PASSWORD=PASSWORD
DATABASE=SCHEMA_NAMEpython app.pyGenerates a SQL query based on a text input provided by the user. This input is processed and transformed into an SQL query using predefined schema and OpenAI's API.
- text (required): A string of text describing the desired query.
{
"text": "Show me the total sales of each customer"
}/text-sql?text=Show%20me%20the%20total%20sales%20for%20each%20customer
Status: 200 OK
{
"query": "SELECT customer_id, SUM(sales) FROM orders GROUP BY customer_id"
}If no text is provided in the request, the API will respond with an error message.
{
"error": "Text is required"
}Generates a text description for the sql query provided by the user
- sql (required): The sql query of which description is required
{
"sql": "SELECT c.first_name, c.last_name, SUM(o.total_amount) AS total_spent FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id HAVING total_spent > 500;"
}/sql-text?query=SELECT c.first_name, c.last_name, SUM(o.total_amount) AS total_spent FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id HAVING total_spent > 500
Status: 200 OK
{
"description": "This query retrieves the first name, last name, and total amount spent for each customer who has spent more than $500. It joins the 'customers' table with the 'orders' table based on the customer ID and groups the results by customer ID."
}If no query is provided in the request, the API will respond with an error message.
{
"error": "Query is required"
}Executes a SQL query against the database. The query is provided as a parameter in the URL.
- query (required): The SQL query to be executed.
{
"query": "SELECT customer_id, SUM(sales) FROM orders GROUP BY customer_id"
}/execute-query?query=SELECT%20customer_id,%20SUM(sales)%20FROM%20orders%20GROUP%20BY%20customer_id
Status: 200 OK
{
"columns": ["first_name", "last_name", "total_spent"],
"data": [
{"first_name": "John", "last_name": "Doe", "total_spent": "1499.98"},
{"first_name": "Jane", "last_name": "Smith", "total_spent": "799.98"},
{"first_name": "Emily", "last_name": "Johnson", "total_spent": "599.97"},
],
"message": "Sucess"
}If no text is provided in the request, the API will respond with an error message.
{
"error": "Query is required"
}