ogegadavis254 commited on
Commit
554fe45
·
verified ·
1 Parent(s): 091c4e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -40,10 +40,9 @@ def get_streamed_response(message, history):
40
  "content": "From now on, you are an AI assistant knowledgeable in general topics. You can respond with relevant information and provide concise, friendly replies. Always maintain a helpful and neutral tone. Ensure to be concise to keep the conversation flowing smoothly."
41
  }]
42
 
43
- for human, assistant in history:
44
- all_message.append({"role": "user", "content": human})
45
- all_message.append({"role": "assistant", "content": assistant})
46
-
47
  return all_message
48
 
49
  # Display chat messages from history on app rerun
@@ -61,7 +60,7 @@ if prompt := st.chat_input("Type your message here..."):
61
  st.session_state.messages.append({"role": "user", "content": prompt})
62
 
63
  # Prepare all messages for the conversation context
64
- history = [(msg["content"], next((m["content"] for m in st.session_state.messages if m["role"] == "assistant"), "")) for msg in st.session_state.messages]
65
 
66
  # Display assistant response in chat message container
67
  with st.chat_message("assistant"):
@@ -73,12 +72,15 @@ if prompt := st.chat_input("Type your message here..."):
73
  max_tokens=150 # Adjust the token limit as needed
74
  )
75
 
76
- st.markdown(response['choices'][0]['message']['content'])
77
-
 
 
 
 
 
78
  except Exception as e:
79
  st.markdown("An error occurred. Please try again later.")
80
  st.markdown("Error details:")
81
  st.markdown(str(e))
82
-
83
- # Add assistant's response to chat history
84
- st.session_state.messages.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
 
40
  "content": "From now on, you are an AI assistant knowledgeable in general topics. You can respond with relevant information and provide concise, friendly replies. Always maintain a helpful and neutral tone. Ensure to be concise to keep the conversation flowing smoothly."
41
  }]
42
 
43
+ for msg in history:
44
+ all_message.append(msg)
45
+ all_message.append({"role": "user", "content": message})
 
46
  return all_message
47
 
48
  # Display chat messages from history on app rerun
 
60
  st.session_state.messages.append({"role": "user", "content": prompt})
61
 
62
  # Prepare all messages for the conversation context
63
+ history = st.session_state.messages
64
 
65
  # Display assistant response in chat message container
66
  with st.chat_message("assistant"):
 
72
  max_tokens=150 # Adjust the token limit as needed
73
  )
74
 
75
+ # Ensure response handling is safe
76
+ if 'choices' in response and response['choices']:
77
+ st.markdown(response['choices'][0]['message']['content'])
78
+ st.session_state.messages.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
79
+ else:
80
+ st.markdown("No response received from the assistant.")
81
+
82
  except Exception as e:
83
  st.markdown("An error occurred. Please try again later.")
84
  st.markdown("Error details:")
85
  st.markdown(str(e))
86
+ st.session_state.messages.append({"role": "assistant", "content": "An error occurred. Please try again later."})