Sam commited on
Commit
12db87c
·
1 Parent(s): 102b12e

Addressing async with issue affecting response streaming

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -157,15 +157,25 @@ async def start_chat():
157
  # (2) Invokes the RAG chain with the user's message
158
  # (3) Extracts the content from the response and sends it back to the user
159
 
160
- @cl.on_message
161
  async def handle_message(message: cl.Message):
162
  settings = cl.user_session.get("settings")
163
 
164
- # Stream the response as it is generated
165
- async with cl.Message(content="") as msg:
166
- async for token in retrieval_augmented_qa_chain.stream({"question": message.content}):
167
- # Append each token to the message
168
- await msg.stream_token(token)
 
 
 
 
 
 
 
 
 
 
169
 
170
  ## Remove to stream the response
171
  # response = retrieval_augmented_qa_chain.invoke({"question": message.content})
 
157
  # (2) Invokes the RAG chain with the user's message
158
  # (3) Extracts the content from the response and sends it back to the user
159
 
160
+ @cl.on_message
161
  async def handle_message(message: cl.Message):
162
  settings = cl.user_session.get("settings")
163
 
164
+ # Initialize the stream message in Chainlit
165
+ stream_msg = cl.Message(content="")
166
+ await stream_msg.send() # Send initial empty message to start the stream
167
+
168
+ # Create a generator from the RAG chain
169
+ response_generator = retrieval_augmented_qa_chain.stream({"question": message.content})
170
+
171
+ # Stream each response chunk to Chainlit
172
+ async for response_chunk in response_generator:
173
+ # Extract the content from the chunk
174
+ chunk_content = response_chunk.get("response", {}).get("content", "")
175
+ if chunk_content:
176
+ # Stream the chunk to Chainlit
177
+ await stream_msg.stream_token(chunk_content)
178
+
179
 
180
  ## Remove to stream the response
181
  # response = retrieval_augmented_qa_chain.invoke({"question": message.content})