Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -57,77 +57,81 @@ def query_rag_agent(query: str):
|
|
57 |
logger.error("Graph recursion limit reached; query processing failed.")
|
58 |
return "Graph recursion limit reached. No SQL result generated.", ""
|
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 |
with st.expander("SQL Query", expanded=True):
|
109 |
-
st.code(
|
110 |
-
|
111 |
-
#
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
# Append assistant response to session state
|
127 |
-
st.session_state.messages.append(
|
128 |
-
{
|
129 |
-
"role": "assistant",
|
130 |
-
"output": output_text,
|
131 |
-
"sql_query": sql_query,
|
132 |
-
}
|
133 |
-
)
|
|
|
57 |
logger.error("Graph recursion limit reached; query processing failed.")
|
58 |
return "Graph recursion limit reached. No SQL result generated.", ""
|
59 |
|
60 |
+
def main():
|
61 |
+
with st.sidebar:
|
62 |
+
st.header("About Project")
|
63 |
+
st.markdown(
|
64 |
+
"""
|
65 |
+
RAG (Retrieval-Augmented Generation) Agent SQL is an approach that combines retrieval techniques with text generation to create more relevant and contextualised answers from data,
|
66 |
+
particularly in SQL databases. RAG-Agent SQL uses two main components:
|
67 |
+
- Retrieval: Retrieving relevant information from the database based on a given question or input.
|
68 |
+
- Augmented Generation: Using natural language models (e.g., LLMs such as GPT or LLaMA) to generate more detailed answers, using information from the retrieval results.
|
69 |
+
|
70 |
+
to see the architecture can be seen here [Github](https://github.com/fahmiaziz98/sql_agent/tree/main/002sql-agent-ra)
|
71 |
+
"""
|
72 |
+
)
|
73 |
+
|
74 |
+
st.header("Example Question")
|
75 |
+
st.markdown(
|
76 |
+
"""
|
77 |
+
- How many different aircraft models are there? And what are the models?
|
78 |
+
- What is the aircraft model with the longest range?
|
79 |
+
- Which airports are located in the city of Basel?
|
80 |
+
- Can you please provide information on what I asked before?
|
81 |
+
- What are the fare conditions available on Boeing 777-300?
|
82 |
+
- What is the total amount of bookings made in April 2024?
|
83 |
+
- What is the scheduled arrival time of flight number QR0051?
|
84 |
+
- Which car rental services are available in Basel?
|
85 |
+
- Which seat was assigned to the boarding pass with ticket number 0060005435212351?
|
86 |
+
- Which trip recommendations are related to history in Basel?
|
87 |
+
- How many tickets were sold for Business class on flight 30625?
|
88 |
+
- Which hotels are located in Zurich?
|
89 |
+
"""
|
90 |
+
)
|
91 |
+
|
92 |
+
# Main Application Title
|
93 |
+
st.title("RAG SQL-Agent")
|
94 |
+
|
95 |
+
# Initialize session state for storing chat messages
|
96 |
+
if "messages" not in st.session_state:
|
97 |
+
st.session_state.messages = []
|
98 |
+
|
99 |
+
# Display conversation history from session state
|
100 |
+
for message in st.session_state.messages:
|
101 |
+
role = message.get("role", "assistant")
|
102 |
+
with st.chat_message(role):
|
103 |
+
if "output" in message:
|
104 |
+
st.markdown(message["output"])
|
105 |
+
if "sql_query" in message and message["sql_query"]:
|
106 |
+
with st.expander("SQL Query", expanded=True):
|
107 |
+
st.code(message["sql_query"])
|
108 |
+
|
109 |
+
# Input form for user prompt
|
110 |
+
if prompt := st.chat_input("What do you want to know?"):
|
111 |
+
st.chat_message("user").markdown(prompt)
|
112 |
+
st.session_state.messages.append({"role": "user", "output": prompt})
|
113 |
+
|
114 |
+
# Fetch response from RAG agent function directly
|
115 |
+
with st.spinner("Searching for an answer..."):
|
116 |
+
output_text, sql_query = query_rag_agent(prompt)
|
117 |
+
|
118 |
+
# Display assistant response and SQL query
|
119 |
+
st.chat_message("assistant").markdown(output_text)
|
120 |
+
if sql_query:
|
121 |
with st.expander("SQL Query", expanded=True):
|
122 |
+
st.code(sql_query)
|
123 |
+
|
124 |
+
# Append assistant response to session state
|
125 |
+
st.session_state.messages.append(
|
126 |
+
{
|
127 |
+
"role": "assistant",
|
128 |
+
"output": output_text,
|
129 |
+
"sql_query": sql_query,
|
130 |
+
}
|
131 |
+
)
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
URL = "https://storage.googleapis.com/benchmarks-artifacts/travel-db/travel2.sqlite"
|
135 |
+
download_sqlite_db(URL)
|
136 |
+
main()
|
137 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|