Spestly commited on
Commit
f307f7f
·
verified ·
1 Parent(s): e88412a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -117,18 +117,24 @@ if prompt := st.chat_input("Message..."):
117
  try:
118
  co = cohere.ClientV2(api_key)
119
  with st.chat_message("assistant", avatar=AI_PFP):
120
- response = co.chat(
 
 
 
121
  model=selected_model,
122
  messages=st.session_state.messages
123
- )
124
- if hasattr(response, "message") and hasattr(response.message, "content"):
125
- content_items = response.message.content
126
- reply = "".join(getattr(item, 'text', '') for item in content_items)
127
- else:
128
- st.write(response)
129
- reply = "❗️Couldn't extract reply from the Cohere response."
130
- st.markdown(reply)
131
-
 
 
 
132
  st.session_state.messages.append({"role": "assistant", "content": reply})
133
 
134
  except Exception as e:
 
117
  try:
118
  co = cohere.ClientV2(api_key)
119
  with st.chat_message("assistant", avatar=AI_PFP):
120
+ message_placeholder = st.empty()
121
+ full_response = ""
122
+
123
+ for event in co.chat_stream(
124
  model=selected_model,
125
  messages=st.session_state.messages
126
+ ):
127
+ if event.type == "content-delta":
128
+ if hasattr(event.delta.message, "content") and event.delta.message.content:
129
+ for content_item in event.delta.message.content:
130
+ if hasattr(content_item, "text"):
131
+ delta_text = getattr(content_item, "text", "")
132
+ full_response += delta_text
133
+ message_placeholder.markdown(full_response + "▌")
134
+
135
+ message_placeholder.markdown(full_response)
136
+ reply = full_response
137
+
138
  st.session_state.messages.append({"role": "assistant", "content": reply})
139
 
140
  except Exception as e: