chelleboyer commited on
Commit
16bf509
·
1 Parent(s): e4352d7

error handling

Browse files
Files changed (2) hide show
  1. README.md +1 -0
  2. app.py +37 -18
README.md CHANGED
@@ -5,6 +5,7 @@ colorFrom: purple
5
  colorTo: indigo
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
  # Paul Graham Essay Explorer 🤖
 
5
  colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
+ app_port: 7860
9
  ---
10
 
11
  # Paul Graham Essay Explorer 🤖
app.py CHANGED
@@ -168,13 +168,19 @@ async def start_chat():
168
 
169
  The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
170
  """
171
-
172
- lcel_rag_chain = (
173
- {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
174
- | rag_prompt | hf_llm
175
- )
176
-
177
- cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
 
 
 
 
 
 
178
 
179
  @cl.on_message
180
  async def main(message: cl.Message):
@@ -185,14 +191,27 @@ async def main(message: cl.Message):
185
 
186
  The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
187
  """
188
- lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
189
-
190
- msg = cl.Message(content="")
191
-
192
- for chunk in await cl.make_async(lcel_rag_chain.stream)(
193
- {"query": message.content},
194
- config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
195
- ):
196
- await msg.stream_token(chunk)
197
-
198
- await msg.send()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
170
  """
171
+ try:
172
+ lcel_rag_chain = (
173
+ {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
174
+ | rag_prompt | hf_llm
175
+ )
176
+
177
+ cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
178
+ except Exception as e:
179
+ await cl.Message(
180
+ content="I apologize, but I'm having trouble initializing the chat. Please refresh the page and try again.",
181
+ author="System"
182
+ ).send()
183
+ raise e # Re-raise the exception to prevent the chat from starting in a broken state
184
 
185
  @cl.on_message
186
  async def main(message: cl.Message):
 
191
 
192
  The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
193
  """
194
+ try:
195
+ lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
196
+
197
+ msg = cl.Message(content="")
198
+
199
+ async for chunk in await cl.make_async(lcel_rag_chain.stream)(
200
+ {"query": message.content},
201
+ config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
202
+ ):
203
+ await msg.stream_token(chunk)
204
+
205
+ await msg.send()
206
+ except Exception as e:
207
+ error_message = str(e)
208
+ if "Connection reset by peer" in error_message or "Connection aborted" in error_message:
209
+ await cl.Message(
210
+ content="I apologize, but I'm having trouble connecting to the language model right now. Please try again in a few moments. If the problem persists, the service might be temporarily unavailable.",
211
+ author="System"
212
+ ).send()
213
+ else:
214
+ await cl.Message(
215
+ content=f"An error occurred: {error_message}. Please try again.",
216
+ author="System"
217
+ ).send()