Sean-Case
commited on
Commit
•
d53332d
1
Parent(s):
ee77123
Added slider for changing number of passages out
Browse files- app.py +5 -3
- chatfuncs/chatfuncs.py +4 -4
app.py
CHANGED
@@ -236,13 +236,15 @@ with block:
|
|
236 |
ingest_embed_out = gr.Textbox(label="File/web page preparation progress")
|
237 |
|
238 |
with gr.Tab("Advanced features"):
|
|
|
239 |
with gr.Row():
|
240 |
model_choice = gr.Radio(label="Choose a chat model", value="Flan Alpaca (small, fast)", choices = ["Flan Alpaca (small, fast)", "Mistral Open Orca (larger, slow)"])
|
241 |
change_model_button = gr.Button(value="Load model", scale=0)
|
242 |
-
with gr.Accordion("Choose number of model layers to send to GPU (WARNING: please don't modify unless you have a GPU).", open = False):
|
243 |
gpu_layer_choice = gr.Slider(label="Choose number of model layers to send to GPU.", value=0, minimum=0, maximum=5, step = 1, visible=True)
|
244 |
|
245 |
load_text = gr.Text(label="Load status")
|
|
|
246 |
|
247 |
gr.HTML(
|
248 |
"<center>This app is based on the models Flan Alpaca and Mistral Open Orca. It powered by Gradio, Transformers, Ctransformers, and Langchain.</a></center>"
|
@@ -277,14 +279,14 @@ with block:
|
|
277 |
# Load in a webpage
|
278 |
|
279 |
# Click/enter to send message action
|
280 |
-
response_click = submit.click(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False, api_name="retrieval").\
|
281 |
then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
282 |
then(chatf.produce_streaming_answer_chatbot, inputs=[chatbot, instruction_prompt_out, model_type_state], outputs=chatbot)
|
283 |
response_click.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
|
284 |
then(chatf.add_inputs_answer_to_history,[message, chatbot, current_topic], [chat_history_state, current_topic]).\
|
285 |
then(lambda: chatf.restore_interactivity(), None, [message], queue=False)
|
286 |
|
287 |
-
response_enter = message.submit(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False).\
|
288 |
then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
289 |
then(chatf.produce_streaming_answer_chatbot, [chatbot, instruction_prompt_out, model_type_state], chatbot)
|
290 |
response_enter.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
|
|
|
236 |
ingest_embed_out = gr.Textbox(label="File/web page preparation progress")
|
237 |
|
238 |
with gr.Tab("Advanced features"):
|
239 |
+
out_passages = gr.Slider(minimum=1, value = 2, maximum=10, step=1, label="Choose number of passages to retrieve from the document. Numbers greater than 2 may lead to increased hallucinations or input text being truncated.")
|
240 |
with gr.Row():
|
241 |
model_choice = gr.Radio(label="Choose a chat model", value="Flan Alpaca (small, fast)", choices = ["Flan Alpaca (small, fast)", "Mistral Open Orca (larger, slow)"])
|
242 |
change_model_button = gr.Button(value="Load model", scale=0)
|
243 |
+
with gr.Accordion("Choose number of model layers to send to GPU (WARNING: please don't modify unless you are sure you have a GPU).", open = False):
|
244 |
gpu_layer_choice = gr.Slider(label="Choose number of model layers to send to GPU.", value=0, minimum=0, maximum=5, step = 1, visible=True)
|
245 |
|
246 |
load_text = gr.Text(label="Load status")
|
247 |
+
|
248 |
|
249 |
gr.HTML(
|
250 |
"<center>This app is based on the models Flan Alpaca and Mistral Open Orca. It powered by Gradio, Transformers, Ctransformers, and Langchain.</a></center>"
|
|
|
279 |
# Load in a webpage
|
280 |
|
281 |
# Click/enter to send message action
|
282 |
+
response_click = submit.click(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state, out_passages], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False, api_name="retrieval").\
|
283 |
then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
284 |
then(chatf.produce_streaming_answer_chatbot, inputs=[chatbot, instruction_prompt_out, model_type_state], outputs=chatbot)
|
285 |
response_click.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
|
286 |
then(chatf.add_inputs_answer_to_history,[message, chatbot, current_topic], [chat_history_state, current_topic]).\
|
287 |
then(lambda: chatf.restore_interactivity(), None, [message], queue=False)
|
288 |
|
289 |
+
response_enter = message.submit(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state, out_passages], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False).\
|
290 |
then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
291 |
then(chatf.produce_streaming_answer_chatbot, [chatbot, instruction_prompt_out, model_type_state], chatbot)
|
292 |
response_enter.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
|
chatfuncs/chatfuncs.py
CHANGED
@@ -312,7 +312,7 @@ def write_out_metadata_as_string(metadata_in):
|
|
312 |
metadata_string = [f"{' '.join(f'{k}: {v}' for k, v in d.items() if k != 'page_section')}" for d in metadata_in] # ['metadata']
|
313 |
return metadata_string
|
314 |
|
315 |
-
def generate_expanded_prompt(inputs: Dict[str, str], instruction_prompt, content_prompt, extracted_memory, vectorstore, embeddings): # ,
|
316 |
|
317 |
question = inputs["question"]
|
318 |
chat_history = inputs["chat_history"]
|
@@ -321,7 +321,7 @@ def generate_expanded_prompt(inputs: Dict[str, str], instruction_prompt, content
|
|
321 |
new_question_kworded = adapt_q_from_chat_history(question, chat_history, extracted_memory) # new_question_keywords,
|
322 |
|
323 |
|
324 |
-
docs_keep_as_doc, doc_df, docs_keep_out = hybrid_retrieval(new_question_kworded, vectorstore, embeddings, k_val = 25, out_passages =
|
325 |
vec_score_cut_off = 1, vec_weight = 1, bm25_weight = 1, svm_weight = 1)#,
|
326 |
#vectorstore=globals()["vectorstore"], embeddings=globals()["embeddings"])
|
327 |
|
@@ -356,7 +356,7 @@ def generate_expanded_prompt(inputs: Dict[str, str], instruction_prompt, content
|
|
356 |
|
357 |
return instruction_prompt_out, sources_docs_content_string, new_question_kworded
|
358 |
|
359 |
-
def create_full_prompt(user_input, history, extracted_memory, vectorstore, embeddings, model_type):
|
360 |
|
361 |
if not user_input.strip():
|
362 |
return history, "", "Respond with 'Please enter a question.' RESPONSE:"
|
@@ -373,7 +373,7 @@ def create_full_prompt(user_input, history, extracted_memory, vectorstore, embed
|
|
373 |
instruction_prompt, content_prompt = base_prompt_templates(model_type=model_type)
|
374 |
instruction_prompt_out, docs_content_string, new_question_kworded =\
|
375 |
generate_expanded_prompt({"question": user_input, "chat_history": history}, #vectorstore,
|
376 |
-
instruction_prompt, content_prompt, extracted_memory, vectorstore, embeddings)
|
377 |
|
378 |
|
379 |
history.append(user_input)
|
|
|
312 |
metadata_string = [f"{' '.join(f'{k}: {v}' for k, v in d.items() if k != 'page_section')}" for d in metadata_in] # ['metadata']
|
313 |
return metadata_string
|
314 |
|
315 |
+
def generate_expanded_prompt(inputs: Dict[str, str], instruction_prompt, content_prompt, extracted_memory, vectorstore, embeddings, out_passages = 2): # ,
|
316 |
|
317 |
question = inputs["question"]
|
318 |
chat_history = inputs["chat_history"]
|
|
|
321 |
new_question_kworded = adapt_q_from_chat_history(question, chat_history, extracted_memory) # new_question_keywords,
|
322 |
|
323 |
|
324 |
+
docs_keep_as_doc, doc_df, docs_keep_out = hybrid_retrieval(new_question_kworded, vectorstore, embeddings, k_val = 25, out_passages = out_passages,
|
325 |
vec_score_cut_off = 1, vec_weight = 1, bm25_weight = 1, svm_weight = 1)#,
|
326 |
#vectorstore=globals()["vectorstore"], embeddings=globals()["embeddings"])
|
327 |
|
|
|
356 |
|
357 |
return instruction_prompt_out, sources_docs_content_string, new_question_kworded
|
358 |
|
359 |
+
def create_full_prompt(user_input, history, extracted_memory, vectorstore, embeddings, model_type, out_passages):
|
360 |
|
361 |
if not user_input.strip():
|
362 |
return history, "", "Respond with 'Please enter a question.' RESPONSE:"
|
|
|
373 |
instruction_prompt, content_prompt = base_prompt_templates(model_type=model_type)
|
374 |
instruction_prompt_out, docs_content_string, new_question_kworded =\
|
375 |
generate_expanded_prompt({"question": user_input, "chat_history": history}, #vectorstore,
|
376 |
+
instruction_prompt, content_prompt, extracted_memory, vectorstore, embeddings, out_passages)
|
377 |
|
378 |
|
379 |
history.append(user_input)
|