Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
ai.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_groq.chat_models import ChatGroq
|
| 2 |
+
# from langchain_ollama.chat_models import ChatOllama
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv()
|
| 5 |
+
import sqlite3
|
| 6 |
+
import os
|
| 7 |
+
os.environ["GROQ_API_KEY"]=os.getenv("GROQ_API_KEY")
|
| 8 |
+
|
| 9 |
+
llm=ChatGroq(model_name="llama-3.3-70b-versatile")
|
| 10 |
+
|
| 11 |
+
def generate_sql_response(request):
|
| 12 |
+
prompt=f"""You are a SQL expert with decades of experience.using your experience and skills generate
|
| 13 |
+
a sql query that generates the correct output for the below request.\n
|
| 14 |
+
<INST>You have access to two tables:[Employees (ID, Name, Department, Salary, Hire_Date), Departments (ID, Name, Manager)].
|
| 15 |
+
Return just the sql query, NO DESCRIPTION, NO DELIMITER</INST>\n\n
|
| 16 |
+
|
| 17 |
+
<request>{request}</request>"""
|
| 18 |
+
|
| 19 |
+
response=llm.invoke(prompt)
|
| 20 |
+
return response.content
|
| 21 |
+
def execute_query(query):
|
| 22 |
+
cur=sqlite3.connect("company.db").cursor()
|
| 23 |
+
res=cur.execute(query)
|
| 24 |
+
data=[]
|
| 25 |
+
for e in res:
|
| 26 |
+
data.append(e)
|
| 27 |
+
|
| 28 |
+
return data
|
| 29 |
+
|
| 30 |
+
def print_table(data):
|
| 31 |
+
prompt=f"""You are given a python list in which each element represents the output of an executed sql query.
|
| 32 |
+
your task is to format this data and return in tabular manner titled "Output".
|
| 33 |
+
Don't give the code for the same, only the output.
|
| 34 |
+
In case the list is empty simply return "No Matching Entities found.
|
| 35 |
+
<list>{data}</list>"""
|
| 36 |
+
|
| 37 |
+
response=llm.invoke(prompt)
|
| 38 |
+
|
| 39 |
+
return response.content
|
| 40 |
+
|
app.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from db import main
|
| 3 |
+
from ai import generate_sql_response,execute_query,print_table
|
| 4 |
+
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title="Ajackus Assignment",
|
| 7 |
+
)
|
| 8 |
+
# if "db" not in st.session_state:
|
| 9 |
+
# main()
|
| 10 |
+
user_input=st.text_area(label="Enter:")
|
| 11 |
+
if st.button("Generate"):
|
| 12 |
+
query=generate_sql_response(user_input)
|
| 13 |
+
query_op=execute_query(query)
|
| 14 |
+
response=print_table(query_op)
|
| 15 |
+
|
| 16 |
+
st.write(response)
|
db.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
def main():
|
| 3 |
+
# Connect to SQLite database (or create it if it doesn't exist)
|
| 4 |
+
conn = sqlite3.connect("company.db")
|
| 5 |
+
cursor = conn.cursor()
|
| 6 |
+
|
| 7 |
+
# Create Employees table
|
| 8 |
+
cursor.execute("""
|
| 9 |
+
CREATE TABLE IF NOT EXISTS Employees (
|
| 10 |
+
ID INTEGER PRIMARY KEY,
|
| 11 |
+
Name TEXT NOT NULL,
|
| 12 |
+
Department TEXT NOT NULL,
|
| 13 |
+
Salary INTEGER NOT NULL,
|
| 14 |
+
Hire_Date TEXT NOT NULL
|
| 15 |
+
)
|
| 16 |
+
""")
|
| 17 |
+
|
| 18 |
+
# Create Departments table
|
| 19 |
+
cursor.execute("""
|
| 20 |
+
CREATE TABLE IF NOT EXISTS Departments (
|
| 21 |
+
ID INTEGER PRIMARY KEY,
|
| 22 |
+
Name TEXT NOT NULL,
|
| 23 |
+
Manager TEXT NOT NULL
|
| 24 |
+
)
|
| 25 |
+
""")
|
| 26 |
+
|
| 27 |
+
# Insert data into Employees table
|
| 28 |
+
cursor.executemany("""
|
| 29 |
+
INSERT INTO Employees (ID, Name, Department, Salary, Hire_Date)
|
| 30 |
+
VALUES (?, ?, ?, ?, ?)
|
| 31 |
+
""", [
|
| 32 |
+
(1, "Alice", "Sales", 50000, "2021-01-15"),
|
| 33 |
+
(2, "Bob", "Engineering", 70000, "2020-06-10"),
|
| 34 |
+
(3, "Charlie", "Marketing", 60000, "2022-03-20")
|
| 35 |
+
])
|
| 36 |
+
|
| 37 |
+
# Insert data into Departments table
|
| 38 |
+
cursor.executemany("""
|
| 39 |
+
INSERT INTO Departments (ID, Name, Manager)
|
| 40 |
+
VALUES (?, ?, ?)
|
| 41 |
+
""", [
|
| 42 |
+
(1, "Sales", "Alice"),
|
| 43 |
+
(2, "Engineering", "Bob"),
|
| 44 |
+
(3, "Marketing", "Charlie")
|
| 45 |
+
])
|
| 46 |
+
|
| 47 |
+
# Commit changes and close connection
|
| 48 |
+
conn.commit()
|
| 49 |
+
conn.close()
|
| 50 |
+
|
| 51 |
+
if __name__=="__main__":
|
| 52 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
langchain_groq
|
| 4 |
+
sqlalchemy
|
| 5 |
+
ipykernel
|
| 6 |
+
langchain_community
|
| 7 |
+
langchain_ollama
|