snrspeaks commited on
Commit
5b4a878
1 Parent(s): 0d7f0be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -47,9 +47,6 @@ async def start():
47
  # path = r'assets/ChatPDFAvatar.jpg'
48
  ).send()
49
 
50
-
51
- @cl.langchain_factory(use_async=True)
52
- async def init():
53
  files = None
54
 
55
  # Wait for the user to upload a file
@@ -120,14 +117,21 @@ async def init():
120
  cl.user_session.set("texts", pages)
121
 
122
  # Let the user know that the system is ready
123
- await msg.update(content=f''' "{file.name}" processed. You can now ask questions!''')
124
-
125
 
126
- return chain
127
 
128
 
129
- @cl.langchain_postprocess
130
  async def process_response(res):
 
 
 
 
 
 
 
131
  answer = res["answer"]
132
  source_documents = res['source_documents']
133
  content = [source_documents[i].page_content for i in range(len(source_documents))]
@@ -141,5 +145,8 @@ async def process_response(res):
141
  else:
142
  answer += "\n\nNo sources found"
143
 
144
- await cl.Message(content=answer, elements=source_elements).send()
145
- # await cl.Message(content=answer).send()
 
 
 
 
47
  # path = r'assets/ChatPDFAvatar.jpg'
48
  ).send()
49
 
 
 
 
50
  files = None
51
 
52
  # Wait for the user to upload a file
 
117
  cl.user_session.set("texts", pages)
118
 
119
  # Let the user know that the system is ready
120
+ msg.content = f''' "{file.name}" processed. You can now ask questions!'''
121
+ await msg.update()
122
 
123
+ cl.user_session.set('chain', chain)
124
 
125
 
126
+ @cl.on_message
127
  async def process_response(res):
128
+ chain = cl.user_session.get("chain")
129
+ cb = cl.AsyncLangchainCallbackHandler(
130
+ stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
131
+ )
132
+ cb.answer_reached = True
133
+ res = await chain.acall(message, callbacks=[cb])
134
+
135
  answer = res["answer"]
136
  source_documents = res['source_documents']
137
  content = [source_documents[i].page_content for i in range(len(source_documents))]
 
145
  else:
146
  answer += "\n\nNo sources found"
147
 
148
+ if cb.has_streamed_final_answer:
149
+ cb.final_stream.elements = source_elements
150
+ await cb.final_stream.update()
151
+ else:
152
+ await cl.Message(content=answer, elements=source_elements).send()