fix: improve error handling and logging in OpenAI client and chat message processing

This commit is contained in:
2025-03-25 17:32:23 +00:00
parent bef445baf4
commit f8dec1951f
2 changed files with 34 additions and 23 deletions

View File

@@ -12,22 +12,29 @@ def display_chat_messages():
def handle_user_input():
if prompt := st.chat_input("Type your message..."):
print(f"User input received: {prompt}") # Debug log
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
response_placeholder = st.empty()
full_response = ""
client = OpenAIClient()
for chunk in client.get_chat_response(st.session_state.messages):
if chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
response_placeholder.markdown(full_response + "")
response_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
try:
with st.chat_message("assistant"):
response_placeholder = st.empty()
full_response = ""
client = OpenAIClient()
print("Calling OpenAI API...") # Debug log
for chunk in client.get_chat_response(st.session_state.messages):
if chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
response_placeholder.markdown(full_response + "")
response_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
print("API call completed successfully") # Debug log
except Exception as e:
st.error(f"Error processing message: {str(e)}")
print(f"Error details: {str(e)}") # Debug log
def main():
st.title("Streamlit Chat App")