devchavda11 commited on
Commit
85920b7
·
verified ·
1 Parent(s): c4a55a3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +21 -11
src/streamlit_app.py CHANGED
@@ -139,17 +139,27 @@ if "current_chat_id" in st.session_state:
139
  st.write(user_input)
140
 
141
  with st.chat_message("assistant"):
 
142
  response_placeholder = st.empty()
143
  full_response = ""
144
 
145
- for message, metadata in workflow.stream(
146
- {"messages": [system, HumanMessage(user_input)]},
147
- config={"configurable": {"thread_id": st.session_state.current_chat_id}},
148
- stream_mode="messages",
149
- ):
150
- if isinstance(message, AIMessage):
151
- full_response += message.content or ""
152
- elif isinstance(message, ToolMessage):
153
- render_tool_message(message)
154
-
155
- response_placeholder.markdown(full_response)
 
 
 
 
 
 
 
 
 
 
139
  st.write(user_input)
140
 
141
  with st.chat_message("assistant"):
142
+ # ... (inside with st.chat_message("assistant"))
143
  response_placeholder = st.empty()
144
  full_response = ""
145
 
146
+ # Use stream_mode='values' for a cleaner loop
147
+ for chunk in workflow.stream(
148
+ {"messages": [HumanMessage(user_input)]}, # System message should already be in state
149
+ config=set_config()):
150
+ # The output of a streaming node is the key of the node itself
151
+ if "llmresponse" in chunk:
152
+ ai_message = chunk["llmresponse"]["messages"][-1]
153
+ full_response += ai_message.content
154
+ response_placeholder.markdown(full_response + "▌") # Add a cursor effect
155
+
156
+ # Handle tool calls if they appear in the stream
157
+ elif "tool_node" in chunk:
158
+ # Since we are streaming, render the tool message right away
159
+ # And clear the response placeholder for the next text chunk
160
+ response_placeholder.empty()
161
+ tool_messages = chunk["tool_node"]["messages"]
162
+ for tool_message in tool_messages:
163
+ render_tool_message(tool_message)
164
+
165
+ response_placeholder.markdown(full_response) # Final update without cursor