samiee2213 commited on
Commit
f2eb2d4
1 Parent(s): b3ac93f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -989,32 +989,30 @@ elif page == "Sessions":
989
  memory = ConversationBufferWindowMemory(k=5, return_messages=True)
990
 
991
  def get_chatmodel_response(question):
992
- session['flowmessages'].append(HumanMessage(content=question))
993
- memory.save_context({"input": question}, {"output": ""}) # Save the input question to memory
994
- chat_history = "\n".join([f"User: {msg.content}" if isinstance(msg, HumanMessage) else f"Bot: {msg.content}" for msg in session['flowmessages']])
995
- prompt = CUSTOM_PROMPT.format(chat_history=chat_history, user_message=question, language=language)
996
- messages = [SystemMessage(content=prompt)]
997
- # Use st.session_state['chat'] here
998
- answer = st.session_state['chat'](messages) # Pass list of messages to chat
999
- session['flowmessages'].append(AIMessage(content=answer.content)) # Adjusted to handle response properly
1000
- save_session(session) # Ensure session is saved after adding new messages
1001
- return answer.content
1002
-
1003
-
1004
- input = st.text_input("Input: ", key="input")
1005
- submit = st.button("Ask the question")
1006
-
1007
- if submit:
1008
- response = get_chatmodel_response(input)
1009
- save_session(session)
1010
- st.experimental_rerun()
1011
- if "flowmessages" in session:
1012
- st.subheader("Chat")
1013
- for message in session['flowmessages']:
1014
- if isinstance(message, HumanMessage):
1015
- st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
1016
- elif isinstance(message, AIMessage):
1017
- st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
1018
 
1019
  # Session rename functionality
1020
  new_title = st.text_input("Rename session:", value=session["title"])
 
989
  memory = ConversationBufferWindowMemory(k=5, return_messages=True)
990
 
991
  def get_chatmodel_response(question):
992
+ # Append the user's question as a HumanMessage
993
+ session['flowmessages'].append(HumanMessage(content=question))
994
+
995
+ # Save the input question to memory
996
+ memory.save_context({"input": question}, {"output": ""})
997
+
998
+ # Construct chat history for the prompt
999
+ chat_history = "\n".join([f"User: {msg.content}" if isinstance(msg, HumanMessage) else f"Bot: {msg.content}" for msg in session['flowmessages']])
1000
+
1001
+ # Format the prompt with the chat history and the user's message
1002
+ prompt = CUSTOM_PROMPT.format(chat_history=chat_history, user_message=question, language=language)
1003
+
1004
+ # Create the messages list with the formatted prompt
1005
+ messages = [SystemMessage(content=prompt)] # Ensure this is formatted as required
1006
+
1007
+ # Use the chat model to get a response
1008
+ try:
1009
+ answer = st.session_state['chat'](messages) # Pass list of messages to chat
1010
+ session['flowmessages'].append(AIMessage(content=answer.content)) # Handle response properly
1011
+ save_session(session) # Ensure session is saved after adding new messages
1012
+ return answer.content
1013
+ except Exception as e:
1014
+ st.error(f"An error occurred: {str(e)}")
1015
+ return "Error generating response."
 
 
1016
 
1017
  # Session rename functionality
1018
  new_title = st.text_input("Rename session:", value=session["title"])