| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  | import streamlit as st | 
					
						
							|  |  |  | from agent import Agent | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  | def main() -> None: | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  |     st.set_page_config(page_title="AI Chat Assistant", page_icon="🤖") | 
					
						
							|  |  |  |     st.title("AI Chat Assistant 🤖") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # adding agent object to session state to persist across sessions | 
					
						
							|  |  |  |     # stramlit reruns the script on every user interaction | 
					
						
							|  |  |  |     if "agent" not in st.session_state: | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  |         st.session_state["agent"] = Agent() | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # initialize chat history | 
					
						
							|  |  |  |     if "messages" not in st.session_state: | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  |         st.session_state["messages"] = [] | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # displying chat history messages | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  |     for message in st.session_state["messages"]: | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  |         with st.chat_message(message["role"]): | 
					
						
							|  |  |  |             st.markdown(message["content"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     prompt = st.chat_input("Type a message...") | 
					
						
							|  |  |  |     if prompt: | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  |         st.session_state["messages"].append({"role": "user", "content": prompt}) | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  |         with st.chat_message("user"): | 
					
						
							|  |  |  |             st.markdown(prompt) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 14:06:59 -08:00
										 |  |  |         response = asyncio.run(st.session_state["agent"].chat(prompt)) | 
					
						
							|  |  |  |         st.session_state["messages"].append({"role": "assistant", "content": response}) | 
					
						
							| 
									
										
										
										
											2025-01-31 14:19:16 -06:00
										 |  |  |         with st.chat_message("assistant"): | 
					
						
							|  |  |  |             st.markdown(response) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     main() |