fahmiaziz commited on
Commit
cbe205b
·
verified ·
1 Parent(s): 01e29bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -73
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
- URL = "https://storage.googleapis.com/benchmarks-artifacts/travel-db/travel2.sqlite"
61
- download_sqlite_db(URL)
62
-
63
- with st.sidebar:
64
- st.header("About Project")
65
- st.markdown(
66
- """
67
- 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,
68
- particularly in SQL databases. RAG-Agent SQL uses two main components:
69
- - Retrieval: Retrieving relevant information from the database based on a given question or input.
70
- - 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.
71
-
72
- to see the architecture can be seen here [Github](https://github.com/fahmiaziz98/sql_agent/tree/main/002sql-agent-ra)
73
- """
74
- )
75
-
76
- st.header("Example Question")
77
- st.markdown(
78
- """
79
- - How many different aircraft models are there? And what are the models?
80
- - What is the aircraft model with the longest range?
81
- - Which airports are located in the city of Basel?
82
- - Can you please provide information on what I asked before?
83
- - What are the fare conditions available on Boeing 777-300?
84
- - What is the total amount of bookings made in April 2024?
85
- - What is the scheduled arrival time of flight number QR0051?
86
- - Which car rental services are available in Basel?
87
- - Which seat was assigned to the boarding pass with ticket number 0060005435212351?
88
- - Which trip recommendations are related to history in Basel?
89
- - How many tickets were sold for Business class on flight 30625?
90
- - Which hotels are located in Zurich?
91
- """
92
- )
93
-
94
- # Main Application Title
95
- st.title("RAG SQL-Agent")
96
-
97
- # Initialize session state for storing chat messages
98
- if "messages" not in st.session_state:
99
- st.session_state.messages = []
100
-
101
- # Display conversation history from session state
102
- for message in st.session_state.messages:
103
- role = message.get("role", "assistant")
104
- with st.chat_message(role):
105
- if "output" in message:
106
- st.markdown(message["output"])
107
- if "sql_query" in message and message["sql_query"]:
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  with st.expander("SQL Query", expanded=True):
109
- st.code(message["sql_query"])
110
-
111
- # Input form for user prompt
112
- if prompt := st.chat_input("What do you want to know?"):
113
- st.chat_message("user").markdown(prompt)
114
- st.session_state.messages.append({"role": "user", "output": prompt})
115
-
116
- # Fetch response from RAG agent function directly
117
- with st.spinner("Searching for an answer..."):
118
- output_text, sql_query = query_rag_agent(prompt)
119
-
120
- # Display assistant response and SQL query
121
- st.chat_message("assistant").markdown(output_text)
122
- if sql_query:
123
- with st.expander("SQL Query", expanded=True):
124
- st.code(sql_query)
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
+