Spaces:
Sleeping
Sleeping
Updated the SQL query generation and made the Answers of the chatbot a bit more Robust
#1
by
Hari-Prasath-M91
- opened
app.py
CHANGED
@@ -46,37 +46,56 @@ def generate_sql_from_nl(prompt):
|
|
46 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
47 |
|
48 |
table_struct = """
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
56 |
"""
|
57 |
|
58 |
response = client.chat.completions.create(
|
59 |
model="gpt-4o-mini",
|
60 |
messages=[
|
61 |
-
{"role": "system", "content": "You are an
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
]
|
67 |
)
|
68 |
return response.choices[0].message.content.strip()
|
69 |
|
70 |
# Function to convert SQL output to natural language
|
71 |
-
def generate_nl_from_sql_output(prompt):
|
72 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
73 |
|
74 |
response = client.chat.completions.create(
|
75 |
model="gpt-4o-mini",
|
76 |
messages=[
|
77 |
-
{"role": "system", "content": """You are
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
]
|
81 |
)
|
82 |
return response.choices[0].message.content.strip()
|
@@ -94,8 +113,10 @@ def fetch_data_from_sql(sql_query):
|
|
94 |
def answer_user_query(prompt):
|
95 |
initialize_roadmap_db()
|
96 |
sql = generate_sql_from_nl(prompt)
|
|
|
97 |
rows = fetch_data_from_sql(sql)
|
98 |
-
|
|
|
99 |
|
100 |
def initialize_roadmap_db():
|
101 |
if not os.path.exists("jee_roadmap.db"):
|
@@ -109,6 +130,7 @@ def initialize_roadmap_db():
|
|
109 |
cursor.execute("""
|
110 |
CREATE TABLE IF NOT EXISTS roadmap (
|
111 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
112 |
date TEXT,
|
113 |
subject TEXT,
|
114 |
chapter_name TEXT,
|
@@ -120,13 +142,15 @@ def initialize_roadmap_db():
|
|
120 |
|
121 |
for day in roadmap_data["schedule"]:
|
122 |
date = day["date"]
|
|
|
123 |
for subj in day["subjects"]:
|
124 |
subject = subj["name"]
|
125 |
for task in subj["tasks"]:
|
126 |
cursor.execute("""
|
127 |
-
INSERT INTO roadmap (date, subject, chapter_name, task_type, time, subtopic)
|
128 |
-
VALUES (?, ?, ?, ?, ?, ?)
|
129 |
""", (
|
|
|
130 |
date,
|
131 |
subject,
|
132 |
task["ChapterName"],
|
|
|
46 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
47 |
|
48 |
table_struct = """
|
49 |
+
CREATE TABLE IF NOT EXISTS roadmap (
|
50 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
51 |
+
day_num INTEGER,
|
52 |
+
date TEXT,
|
53 |
+
subject TEXT,
|
54 |
+
chapter_name TEXT,
|
55 |
+
task_type TEXT,
|
56 |
+
time TEXT,
|
57 |
+
subtopic TEXT
|
58 |
+
)
|
59 |
"""
|
60 |
|
61 |
response = client.chat.completions.create(
|
62 |
model="gpt-4o-mini",
|
63 |
messages=[
|
64 |
+
{"role": "system", "content": f""""You are an helper who runs in the background of an AI agent,
|
65 |
+
which helps students for their JEE Preparation. Now your Job is to analyze the users prompt and
|
66 |
+
create an SQL query to extract the related Information from an sqlite3 database with the table
|
67 |
+
structure: {table_struct}.
|
68 |
+
|
69 |
+
Note: For the time column, the data is formatted like '0.5 hour', '1 hour', '2 hours' and
|
70 |
+
so on. So make sure create queries that compare just the numbers within the text.
|
71 |
+
|
72 |
+
You will also make sure multiple times that you give an SQL
|
73 |
+
Query that adheres to the given table structure, and you Output just the SQL query.
|
74 |
+
Do not include anyting else like new line statements, ```sql or any other text. Your output
|
75 |
+
is going to be directly fed into a Python script to extract the required information. So,
|
76 |
+
please follow all the given Instructions."""},
|
77 |
+
{"role": "user", "content": f"""Keeping the table structure in mind: {table_struct},
|
78 |
+
Convert this prompt to an SQL query for the given table: {prompt}. Make sure your
|
79 |
+
output is just the SQL query, which can directly be used to extract required content"""}
|
80 |
]
|
81 |
)
|
82 |
return response.choices[0].message.content.strip()
|
83 |
|
84 |
# Function to convert SQL output to natural language
|
85 |
+
def generate_nl_from_sql_output(prompt, data):
|
86 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
87 |
|
88 |
response = client.chat.completions.create(
|
89 |
model="gpt-4o-mini",
|
90 |
messages=[
|
91 |
+
{"role": "system", "content": f"""You are an helpful AI chatbot working under the roadmap
|
92 |
+
section of an AI Agent, whose role is to aid students in their preparation for the JEE examination.
|
93 |
+
You are going to play a very crucial role of a Roadmap Assistant, who helps the student out with whatever query
|
94 |
+
they have related to their roadmap, the data required to answer the users query is already extracted
|
95 |
+
from the Roadmap table of a SQLite3 database and given to you here {data}. Analyse the users query deeply and
|
96 |
+
reply to it with the relevant information from the given data in a supportive manner."""},
|
97 |
+
{"role": "user", "content": f"""Answer to this users query using the data given to you, while keeping
|
98 |
+
your role in mind: {prompt}"""}
|
99 |
]
|
100 |
)
|
101 |
return response.choices[0].message.content.strip()
|
|
|
113 |
def answer_user_query(prompt):
|
114 |
initialize_roadmap_db()
|
115 |
sql = generate_sql_from_nl(prompt)
|
116 |
+
st.write(sql)
|
117 |
rows = fetch_data_from_sql(sql)
|
118 |
+
st.write(rows)
|
119 |
+
return generate_nl_from_sql_output(prompt, rows)
|
120 |
|
121 |
def initialize_roadmap_db():
|
122 |
if not os.path.exists("jee_roadmap.db"):
|
|
|
130 |
cursor.execute("""
|
131 |
CREATE TABLE IF NOT EXISTS roadmap (
|
132 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
133 |
+
day_num INTEGER,
|
134 |
date TEXT,
|
135 |
subject TEXT,
|
136 |
chapter_name TEXT,
|
|
|
142 |
|
143 |
for day in roadmap_data["schedule"]:
|
144 |
date = day["date"]
|
145 |
+
day_num = day["dayNumber"]
|
146 |
for subj in day["subjects"]:
|
147 |
subject = subj["name"]
|
148 |
for task in subj["tasks"]:
|
149 |
cursor.execute("""
|
150 |
+
INSERT INTO roadmap (day_num, date, subject, chapter_name, task_type, time, subtopic)
|
151 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
152 |
""", (
|
153 |
+
day_num,
|
154 |
date,
|
155 |
subject,
|
156 |
task["ChapterName"],
|