clementsan commited on
Commit
9733941
1 Parent(s): 1da1e92

Enable use of document references

Browse files
Files changed (1) hide show
  1. app.py +44 -11
app.py CHANGED
@@ -101,6 +101,7 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, pr
101
  progress(0.75, desc="Defining buffer memory...")
102
  memory = ConversationBufferMemory(
103
  memory_key="chat_history",
 
104
  return_messages=True
105
  )
106
  # retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})
@@ -113,7 +114,7 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, pr
113
  chain_type="stuff",
114
  memory=memory,
115
  # combine_docs_chain_kwargs={"prompt": your_prompt})
116
- # return_source_documents=True,
117
  # return_generated_question=True,
118
  # verbose=True,
119
  )
@@ -162,11 +163,20 @@ def conversation(message, history):
162
 
163
  # Generate response using QA chain
164
  response = qa_chain({"question": message, "chat_history": formatted_chat_history})
165
- # return response['answer']
 
 
 
 
 
 
 
 
166
 
167
  # Append user message and response to chat history
168
- new_history = history + [(message, response["answer"])]
169
- return gr.update(value=""), new_history
 
170
 
171
 
172
  def upload_file(file_obj):
@@ -188,7 +198,7 @@ def demo():
188
  """<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2>
189
  <h3>Ask any questions about your PDF documents, along with follow-ups</h3>
190
  <b>Note:</b> This AI assistant performs retrieval-augmented generation from your PDF documents. \
191
- When generating answers, it takes past questions into account (via conversational memory), and points to specific document sources for clarity purposes</i>
192
  <br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
193
  """)
194
  with gr.Tab("Step 1 - Document pre-processing"):
@@ -199,7 +209,7 @@ def demo():
199
  db_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")
200
  with gr.Accordion("Advanced options - Document text splitter", open=False):
201
  with gr.Row():
202
- slider_chunk_size = gr.Slider(minimum = 100, maximum = 1000, value=500, step=20, label="Chunk size", info="Chunk size", interactive=True)
203
  with gr.Row():
204
  slider_chunk_overlap = gr.Slider(minimum = 10, maximum = 200, value=40, step=10, label="Chunk overlap", info="Chunk overlap", interactive=True)
205
  with gr.Row():
@@ -222,6 +232,13 @@ def demo():
222
 
223
  with gr.Tab("Step 3 - Conversation with chatbot"):
224
  chatbot = gr.Chatbot(height=300)
 
 
 
 
 
 
 
225
  with gr.Row():
226
  msg = gr.Textbox(placeholder="Type message", container=True)
227
  with gr.Row():
@@ -230,13 +247,29 @@ def demo():
230
 
231
  # Preprocessing events
232
  #upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])
233
- db_btn.click(initialize_database, inputs=[document, slider_chunk_size, slider_chunk_overlap], outputs=[vector_db, db_progress])
234
- qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[llm_progress]).then(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
235
 
236
  # Chatbot events
237
- msg.submit(conversation, [msg, chatbot], [msg, chatbot], queue=False)
238
- submit_btn.click(conversation, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False)
239
- clear_btn.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
240
  demo.queue().launch(debug=True)
241
 
242
 
 
101
  progress(0.75, desc="Defining buffer memory...")
102
  memory = ConversationBufferMemory(
103
  memory_key="chat_history",
104
+ output_key='answer',
105
  return_messages=True
106
  )
107
  # retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})
 
114
  chain_type="stuff",
115
  memory=memory,
116
  # combine_docs_chain_kwargs={"prompt": your_prompt})
117
+ return_source_documents=True,
118
  # return_generated_question=True,
119
  # verbose=True,
120
  )
 
163
 
164
  # Generate response using QA chain
165
  response = qa_chain({"question": message, "chat_history": formatted_chat_history})
166
+ response_answer = response["answer"]
167
+ response_sources = response["source_documents"]
168
+ response_source1 = response_sources[0].page_content.strip()
169
+ response_source2 = response_sources[1].page_content.strip()
170
+ # Langchain sources are zero-based
171
+ response_source1_page = response_sources[0].metadata["page"] + 1
172
+ response_source2_page = response_sources[1].metadata["page"] + 1
173
+ # print ('chat response: ', response_answer)
174
+ # print('DB source', response_sources)
175
 
176
  # Append user message and response to chat history
177
+ new_history = history + [(message, response_answer)]
178
+ # return gr.update(value=""), new_history, response_sources[0], response_sources[1]
179
+ return gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page
180
 
181
 
182
  def upload_file(file_obj):
 
198
  """<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2>
199
  <h3>Ask any questions about your PDF documents, along with follow-ups</h3>
200
  <b>Note:</b> This AI assistant performs retrieval-augmented generation from your PDF documents. \
201
+ When generating answers, it takes past questions into account (via conversational memory), and includes document references for clarity purposes.</i>
202
  <br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
203
  """)
204
  with gr.Tab("Step 1 - Document pre-processing"):
 
209
  db_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")
210
  with gr.Accordion("Advanced options - Document text splitter", open=False):
211
  with gr.Row():
212
+ slider_chunk_size = gr.Slider(minimum = 100, maximum = 1000, value=600, step=20, label="Chunk size", info="Chunk size", interactive=True)
213
  with gr.Row():
214
  slider_chunk_overlap = gr.Slider(minimum = 10, maximum = 200, value=40, step=10, label="Chunk overlap", info="Chunk overlap", interactive=True)
215
  with gr.Row():
 
232
 
233
  with gr.Tab("Step 3 - Conversation with chatbot"):
234
  chatbot = gr.Chatbot(height=300)
235
+ with gr.Accordion("Advanced - Document references", open=False):
236
+ with gr.Row():
237
+ doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
238
+ source1_page = gr.Number(label="Page", scale=1)
239
+ with gr.Row():
240
+ doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)
241
+ source2_page = gr.Number(label="Page", scale=1)
242
  with gr.Row():
243
  msg = gr.Textbox(placeholder="Type message", container=True)
244
  with gr.Row():
 
247
 
248
  # Preprocessing events
249
  #upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])
250
+ db_btn.click(initialize_database, \
251
+ inputs=[document, slider_chunk_size, slider_chunk_overlap], \
252
+ outputs=[vector_db, db_progress])
253
+ qachain_btn.click(initialize_LLM, \
254
+ inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], \
255
+ outputs=[llm_progress]).then(lambda:[None,"",0,"",0], \
256
+ inputs=None, \
257
+ outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page], \
258
+ queue=False)
259
 
260
  # Chatbot events
261
+ msg.submit(conversation, \
262
+ inputs=[msg, chatbot], \
263
+ outputs=[msg, chatbot, doc_source1, source1_page, doc_source2, source2_page], \
264
+ queue=False)
265
+ submit_btn.click(conversation, \
266
+ inputs=[msg, chatbot], \
267
+ outputs=[msg, chatbot, doc_source1, source1_page, doc_source2, source2_page], \
268
+ queue=False)
269
+ clear_btn.click(lambda:[None,"",0,"",0], \
270
+ inputs=None, \
271
+ outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page], \
272
+ queue=False)
273
  demo.queue().launch(debug=True)
274
 
275