snrspeaks commited on
Commit
4028cfa
1 Parent(s): e367153

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -21,14 +21,11 @@ system_template = """Use the following pieces of context to answer the users que
21
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
22
  ALWAYS return a "SOURCES" part in your answer.
23
  The "SOURCES" part should be a reference to the source of the document from which you got your answer.
24
-
25
  Example of your response should be:
26
-
27
  ```
28
  The answer is foo
29
  SOURCES: xyz
30
  ```
31
-
32
  Begin!
33
  ----------------
34
  {summaries}"""
@@ -47,6 +44,9 @@ async def start():
47
  # path = r'assets/ChatPDFAvatar.jpg'
48
  ).send()
49
 
 
 
 
50
  files = None
51
 
52
  # Wait for the user to upload a file
@@ -117,21 +117,14 @@ async def start():
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(message):
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,8 +138,5 @@ async def process_response(message):
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()
 
21
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
22
  ALWAYS return a "SOURCES" part in your answer.
23
  The "SOURCES" part should be a reference to the source of the document from which you got your answer.
 
24
  Example of your response should be:
 
25
  ```
26
  The answer is foo
27
  SOURCES: xyz
28
  ```
 
29
  Begin!
30
  ----------------
31
  {summaries}"""
 
44
  # path = r'assets/ChatPDFAvatar.jpg'
45
  ).send()
46
 
47
+
48
+ @cl.langchain_factory(use_async=True)
49
+ async def init():
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
+ await msg.update(content=f''' "{file.name}" processed. You can now ask questions!''')
 
121
 
 
122
 
123
+ return chain
124
 
125
+
126
+ @cl.langchain_postprocess
127
+ async def process_response(res):
 
 
 
 
 
 
128
  answer = res["answer"]
129
  source_documents = res['source_documents']
130
  content = [source_documents[i].page_content for i in range(len(source_documents))]
 
138
  else:
139
  answer += "\n\nNo sources found"
140
 
141
+ await cl.Message(content=answer, elements=source_elements).send()
142
+ # await cl.Message(content=answer).send()