Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
180 lines (148 loc) · 7.31 KB
/
Copy pathapp.py
File metadata and controls
180 lines (148 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import sqlite3
from re import match as re_match, sub as re_sub
from time import time
from typing import Match
import streamlit as st
from pandas import DataFrame
from agents import sqlQueryGeneratorAgent, sqlQueryAccuracyJudgeAgent
from fetch_examples import getTopSentenceMatches, fetchUsedTables, fetchSentencePairing, fetchTableSchemas, \
loadSentences
def isQuerySafeToExecute(query: str) -> bool:
"""
Function to determine if a query is safe to execute (won't delete or modify any of the underlying data).
:param query: The query to analyze
:return: The boolean value indicating if the query is safe to execute
"""
restrictedKeywords: list = [
"DELETE", "DROP", "TRUNCATE", "ALTER", "INSERT", "UPDATE", "REPLACE", "MERGE"
]
return all(keyword not in query.upper() for keyword in restrictedKeywords)
# Page config
st.set_page_config(page_title="Pydantic AI Chatbot", layout="wide")
st.title("🤖 Pydantic AI Chatbot")
st.sidebar.markdown("### Chat History")
clearButton = st.sidebar.button("Clear Chat")
st.session_state.messages = []
if "messages" not in st.session_state or not st.session_state.get("messages"):
st.session_state.messages = []
if "previous_queries" not in st.session_state:
st.session_state.previous_queries = []
if clearButton:
st.session_state.messages = []
# Database options
dbOptions: dict = {
"Bakery Data": {
"path": 'databases/bakery_1/bakery_1.sqlite',
"description": "Database containing information about a bakery's operations, including products, sales, customers, and transactions.",
"db_id": "bakery_1"
},
"Concert Singer": {
"path": 'databases/concert_singer/concert_singer.sqlite',
"description": "Database containing information on concerts, singers, songs, and performances.",
"db_id": "concert_singer"
},
"Real Estate Properties": {
"path": 'databases/real_estate_properties/real_estate_properties.sqlite',
"description": "Database containing real estate listings, property details, owners, and agents.",
"db_id": "real_estate_properties"
},
"Tennis tournaments": {
"path": 'databases/wta_1/wta_1.sqlite',
"description": "Database containing information on WTA tournaments, including players, rankings, and matches.",
"db_id": "wta_1"
}
}
dbDisplayOptions: list = [f"{name} - {info['description']}" for name, info in dbOptions.items()]
selectedDbDisplay = st.selectbox("Select Database", dbDisplayOptions)
selectedDb = next(key for key, value in dbOptions.items() if f"{key} - {value['description']}" == selectedDbDisplay)
selectedDbId = dbOptions[selectedDb]['db_id']
# Display chat history
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# User input
userInput = st.chat_input("Ask me anything...")
if userInput:
st.session_state.messages.append({"role": "user", "content": userInput})
previouslyGeneratedQueries: list = []
with st.chat_message("user"):
st.markdown(userInput)
# Fetch example queries and table schemas
sentences = loadSentences(selectedDbId)
sentenceMatches = getTopSentenceMatches(userInput, sentences)
exampleQueries = fetchSentencePairing(sentenceMatches, selectedDbId)
tables = fetchUsedTables(sentenceMatches, selectedDbId)
tableSchemas = fetchTableSchemas(tables, selectedDbId)
error: None | str = None
failedQuery: None | str = None
with st.chat_message("assistant"):
# Generate and display 5 SQL queries
for _ in range(5):
retries: int = 3
while retries > 0:
generatedQuery = sqlQueryGeneratorAgent.run_sync(userInput, deps={
"prompt_query_pairings": exampleQueries,
"table_schemas": tableSchemas,
"previous_queries": previouslyGeneratedQueries,
"error": error if error else None,
"failed_query": failedQuery if failedQuery else None
})
# Reset error
error = None
cleanedQuery: str = re_sub(r'^sql\s*|$', '', generatedQuery.data).strip()
accuracyResponse = sqlQueryAccuracyJudgeAgent.run_sync(f"Generated Query: {cleanedQuery}",
deps={"original_prompt": userInput})
# Parse the response
matchResult: Match[str] | None = re_match(r'(\d\.\d+)\s*\|\s*\((.*)\)', accuracyResponse.data)
if matchResult:
accuracyScore: float = float(matchResult.group(1))
feedback: str = matchResult.group(2)
else:
accuracyScore: float = float(accuracyResponse.data)
feedback: str = ""
if accuracyScore >= 0.6:
if not isQuerySafeToExecute(cleanedQuery):
col1, col2 = st.columns([1, 2])
with col1:
st.markdown("### Query")
st.code(cleanedQuery, language='sql')
with col2:
st.markdown("### Warning")
st.markdown(
"Due to the nature of the query to be executed, the system can not show what the results would be.")
st.markdown(f"Accuracy Score: {accuracyScore}")
break
else:
try:
conn = sqlite3.connect(dbOptions[selectedDb]['path'])
cursor = conn.cursor()
# Measure execution time
startTime = time()
cursor.execute(cleanedQuery)
results = cursor.fetchall()
executionTime = time() - startTime
conn.close()
# Convert results to DataFrame and display
df = DataFrame(results, columns=[desc[0] for desc in cursor.description])
col1, col2 = st.columns([1, 2])
with col1:
st.markdown("### Query")
st.code(cleanedQuery, language='sql')
with col2:
st.markdown("### Query Results")
st.dataframe(df.head())
st.markdown(f"Accuracy Score: {accuracyScore} | Execution Time: {executionTime:.2f} seconds")
previouslyGeneratedQueries.append(cleanedQuery)
break
# Possible SQL error
except Exception as e:
retries -= 1
error = f"The following SQL error occurred when executing the query: {str(e)}"
failedQuery = cleanedQuery
# Confidence score too low
else:
retries -= 1
error = feedback
failedQuery = cleanedQuery
if retries == 0:
st.markdown("Failed to generate a valid query after 3 attempts.")