Phaneendrabayi commited on
Commit
16de7f7
·
verified ·
1 Parent(s): 638bff1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -7,7 +7,7 @@ from langchain.memory.chat_message_histories import SQLChatMessageHistory
7
  from langchain_core.output_parsers import StrOutputParser
8
 
9
  # Configure Gemini API
10
- GEMINI_API_KEY = "AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo"
11
  model = ChatGoogleGenerativeAI(model="gemini-2.0-pro-exp", google_api_key=GEMINI_API_KEY)
12
  output_parser = StrOutputParser()
13
 
@@ -24,6 +24,21 @@ def save_chat(history_store, message, response):
24
  history_store.add_message(HumanMessage(content=message))
25
  history_store.add_message(AIMessage(content=response))
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  # AI Agent function using LangChain
28
  def chat_with_ai(user, message):
29
  history_store = get_history_store(user)
@@ -32,8 +47,11 @@ def chat_with_ai(user, message):
32
  for msg in history:
33
  memory.chat_memory.add_message(msg)
34
  memory.chat_memory.add_message(HumanMessage(content=message))
35
-
36
- conversation_history = "\n".join([f"User: {msg.content}" if isinstance(msg, HumanMessage) else f"AI: {msg.content}" for msg in memory.chat_memory.messages])
 
 
 
37
 
38
  response = model.invoke([SystemMessage(content=f"""
39
  You are a data science tutor. Answer ONLY data science-related questions.
@@ -44,13 +62,13 @@ def chat_with_ai(user, message):
44
 
45
  User: {user_input}
46
  """)] + memory.chat_memory.messages)
47
-
48
  parsed_response = output_parser.parse(response.content.strip())
49
  save_chat(history_store, message, parsed_response)
50
  return parsed_response
51
 
52
  # Streamlit UI
53
- st.title("Data Science Tutor")
54
 
55
  # User session management
56
  if "user" not in st.session_state:
@@ -58,14 +76,25 @@ if "user" not in st.session_state:
58
  if "messages" not in st.session_state:
59
  st.session_state.messages = []
60
 
61
- # Display chat history
 
 
 
 
 
62
  history_store = get_history_store(st.session_state.user)
63
  past_chats = get_chat_history(history_store)
64
  for chat in past_chats:
65
- if isinstance(chat, HumanMessage):
66
- st.chat_message("user").write(chat.content)
67
- elif isinstance(chat, AIMessage):
68
- st.chat_message("assistant").write(chat.content)
 
 
 
 
 
 
69
 
70
  # Chat input
71
  user_input = st.chat_input("Type your message...")
 
7
  from langchain_core.output_parsers import StrOutputParser
8
 
9
  # Configure Gemini API
10
+ GEMINI_API_KEY = "YOUR_GEMINI_API_KEY"
11
  model = ChatGoogleGenerativeAI(model="gemini-2.0-pro-exp", google_api_key=GEMINI_API_KEY)
12
  output_parser = StrOutputParser()
13
 
 
24
  history_store.add_message(HumanMessage(content=message))
25
  history_store.add_message(AIMessage(content=response))
26
 
27
+ # Delete chat history
28
+ def clear_chat_history(user):
29
+ history_store = get_history_store(user)
30
+ history_store.clear()
31
+ st.session_state.messages = []
32
+
33
+ # Delete selected message
34
+ def delete_chat_message(user, message_content):
35
+ history_store = get_history_store(user)
36
+ messages = history_store.messages
37
+ history_store.clear()
38
+ for msg in messages:
39
+ if msg.content != message_content:
40
+ history_store.add_message(msg)
41
+
42
  # AI Agent function using LangChain
43
  def chat_with_ai(user, message):
44
  history_store = get_history_store(user)
 
47
  for msg in history:
48
  memory.chat_memory.add_message(msg)
49
  memory.chat_memory.add_message(HumanMessage(content=message))
50
+
51
+ conversation_history = "\n".join([
52
+ f"User: {msg.content}" if isinstance(msg, HumanMessage) else f"AI: {msg.content}"
53
+ for msg in memory.chat_memory.messages
54
+ ])
55
 
56
  response = model.invoke([SystemMessage(content=f"""
57
  You are a data science tutor. Answer ONLY data science-related questions.
 
62
 
63
  User: {user_input}
64
  """)] + memory.chat_memory.messages)
65
+
66
  parsed_response = output_parser.parse(response.content.strip())
67
  save_chat(history_store, message, parsed_response)
68
  return parsed_response
69
 
70
  # Streamlit UI
71
+ st.title("Chat with AI (Gemini + LangChain)")
72
 
73
  # User session management
74
  if "user" not in st.session_state:
 
76
  if "messages" not in st.session_state:
77
  st.session_state.messages = []
78
 
79
+ # Clear chat button
80
+ if st.button("Clear Chat"):
81
+ clear_chat_history(st.session_state.user)
82
+ st.experimental_rerun()
83
+
84
+ # Display chat history with delete buttons
85
  history_store = get_history_store(st.session_state.user)
86
  past_chats = get_chat_history(history_store)
87
  for chat in past_chats:
88
+ col1, col2 = st.columns([0.9, 0.1])
89
+ with col1:
90
+ if isinstance(chat, HumanMessage):
91
+ st.chat_message("user").write(chat.content)
92
+ elif isinstance(chat, AIMessage):
93
+ st.chat_message("assistant").write(chat.content)
94
+ with col2:
95
+ if st.button("🗑️", key=chat.content[:20]):
96
+ delete_chat_message(st.session_state.user, chat.content)
97
+ st.experimental_rerun()
98
 
99
  # Chat input
100
  user_input = st.chat_input("Type your message...")