Spaces:
Sleeping
Sleeping
Sam
commited on
Commit
·
a6c6d28
1
Parent(s):
d7b8072
Enable streaming responses in Chainlit chatbot
Browse files
app.py
CHANGED
@@ -116,7 +116,7 @@ Question:
|
|
116 |
prompt = ChatPromptTemplate.from_template(template)
|
117 |
|
118 |
# Define the primary LLM
|
119 |
-
primary_llm = ChatOpenAI(model_name="gpt-4o", temperature=0)
|
120 |
|
121 |
#-----Creating a Retrieval Augmented Generation (RAG) Chain-----#
|
122 |
# The RAG chain:
|
@@ -161,11 +161,28 @@ async def start_chat():
|
|
161 |
async def handle_message(message: cl.Message):
|
162 |
settings = cl.user_session.get("settings")
|
163 |
|
164 |
-
response = retrieval_augmented_qa_chain.invoke({"question": message.content})
|
165 |
|
|
|
|
|
|
|
166 |
|
167 |
-
#
|
168 |
-
|
169 |
-
pretty_content = content.strip() # Remove any leading/trailing whitespace
|
170 |
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
prompt = ChatPromptTemplate.from_template(template)
|
117 |
|
118 |
# Define the primary LLM
|
119 |
+
primary_llm = ChatOpenAI(model_name="gpt-4o", temperature=0, streaming=True)
|
120 |
|
121 |
#-----Creating a Retrieval Augmented Generation (RAG) Chain-----#
|
122 |
# The RAG chain:
|
|
|
161 |
async def handle_message(message: cl.Message):
|
162 |
settings = cl.user_session.get("settings")
|
163 |
|
|
|
164 |
|
165 |
+
# Initialize the stream message in Chainlit
|
166 |
+
stream_msg = cl.Message(content="")
|
167 |
+
await stream_msg.send() # Send initial empty message to start the stream
|
168 |
|
169 |
+
# Create a generator from the RAG chain
|
170 |
+
response_generator = retrieval_augmented_qa_chain.stream({"question": message.content})
|
|
|
171 |
|
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 |
+
# Append the chunk to the streaming message content
|
177 |
+
stream_msg.content += chunk_content
|
178 |
+
await stream_msg.update() # Update the message in Chainlit
|
179 |
+
|
180 |
+
## Remove to stream the response
|
181 |
+
# response = retrieval_augmented_qa_chain.invoke({"question": message.content})
|
182 |
+
|
183 |
+
|
184 |
+
## # Extracting and sending just the content
|
185 |
+
## content = response["response"].content
|
186 |
+
## pretty_content = content.strip() # Remove any leading/trailing whitespace
|
187 |
+
|
188 |
+
## await cl.Message(content=pretty_content).send()
|