seanpedrickcase commited on
Commit
a0e9486
·
1 Parent(s): 14d074b

Corrected Gemma model prompt format, made prompts in general ask for more detail. Wording changes.

Browse files
app.py CHANGED
@@ -223,7 +223,7 @@ with app:
223
 
224
  gr.Markdown("<h1><center>Lightweight PDF / web page QA bot</center></h1>")
225
 
226
- gr.Markdown(f"""Chat with PDF, web page or (new) csv/Excel documents. The default is a small model ({SMALL_MODEL_NAME}), that can only answer specific questions that are answered in the text. It cannot give overall impressions of, or summarise the document. The alternative ({LARGE_MODEL_NAME}, if available), can reason a little better, but is much slower (See Advanced settings tab).\n\nBy default '[{DEFAULT_DATA_SOURCE_NAME}]({DEFAULT_DATA_SOURCE})' is loaded.If you want to talk about another document or web page, please select from the second tab. If switching topic, please click the 'Clear chat' button.\n\nCaution: This is a public app. Please ensure that the document you upload is not sensitive is any way as other users may see it! Also, please note that LLM chatbots may give incomplete or incorrect information, so please use with care.""")
227
 
228
  with gr.Row():
229
  current_source = gr.Textbox(label="Current data source(s)", value=DEFAULT_DATA_SOURCE, scale = 10)
@@ -252,7 +252,7 @@ with app:
252
 
253
  current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here", visible=False)
254
 
255
- with gr.Tab("Load in a different file/webpage"):
256
  with gr.Accordion("PDF file", open = False):
257
  in_pdf = gr.File(label="Upload pdf", file_count="multiple", file_types=['.pdf'])
258
  load_pdf = gr.Button(value="Load in file", variant="secondary", scale=0)
 
223
 
224
  gr.Markdown("<h1><center>Lightweight PDF / web page QA bot</center></h1>")
225
 
226
+ gr.Markdown(f"""Chat with PDFs, web pages or data files (.csv / .xlsx). The default is a small model ({SMALL_MODEL_NAME}), that can only answer specific questions that are answered in the text. It cannot give overall impressions of, or summarise the document. Go to Advanced settings to change model to e.g. a choice of Gemini models that are available on [their very generous free tier](https://ai.google.dev/gemini-api/docs/pricing) (needs an API key), or AWS Bedrock/larger local models if activated.\n\nBy default '[{DEFAULT_DATA_SOURCE_NAME}]({DEFAULT_DATA_SOURCE})' is loaded as a data source. If you want to query another data source, please upload it on the 'Change data source' tab. If switching topic, please click the 'Clear chat' button. 'Stop generating' will halt the language model during its response.\n\n**Caution: On Hugging Face, this is a public app. Please ensure that the document you upload is not sensitive is any way as other users may see it!** Also, please note that AI chatbots may give incomplete or incorrect information, so please use with care and ensure that you verify any outputs before further use.""")
227
 
228
  with gr.Row():
229
  current_source = gr.Textbox(label="Current data source(s)", value=DEFAULT_DATA_SOURCE, scale = 10)
 
252
 
253
  current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here", visible=False)
254
 
255
+ with gr.Tab("Change data source"):
256
  with gr.Accordion("PDF file", open = False):
257
  in_pdf = gr.File(label="Upload pdf", file_count="multiple", file_types=['.pdf'])
258
  load_pdf = gr.Button(value="Load in file", variant="secondary", scale=0)
chatfuncs/chatfuncs.py CHANGED
@@ -32,7 +32,7 @@ from langchain_community.retrievers import SVMRetriever
32
  from langchain.text_splitter import RecursiveCharacterTextSplitter
33
  from langchain.docstore.document import Document
34
 
35
- from chatfuncs.prompts import instruction_prompt_template_alpaca, instruction_prompt_mistral_orca, instruction_prompt_phi3, instruction_prompt_llama3, instruction_prompt_qwen, instruction_prompt_template_orca, instruction_prompt_gemma
36
  from chatfuncs.model_load import temperature, max_new_tokens, sample, repetition_penalty, top_p, top_k, torch_device, CtransGenGenerationConfig, max_tokens
37
  from chatfuncs.config import GEMINI_API_KEY, AWS_DEFAULT_REGION, LARGE_MODEL_NAME, SMALL_MODEL_NAME, RUN_AWS_FUNCTIONS, FEEDBACK_LOGS_FOLDER
38
 
@@ -136,11 +136,11 @@ def base_prompt_templates(model_type:str = SMALL_MODEL_NAME):
136
  # The main prompt:
137
 
138
  if model_type == SMALL_MODEL_NAME:
139
- INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_qwen, input_variables=['question', 'summaries'])
140
  elif model_type == LARGE_MODEL_NAME:
141
  INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_phi3, input_variables=['question', 'summaries'])
142
  else:
143
- INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_template_orca, input_variables=['question', 'summaries'])
144
 
145
 
146
  return INSTRUCTION_PROMPT, CONTENT_PROMPT
@@ -507,7 +507,6 @@ def produce_streaming_answer_chatbot(
507
  new_text = ""
508
  history[-1]['content'] += new_text
509
  NUM_TOKENS += 1
510
- history[-1]['content'] = history[-1]['content'].replace('<|im_end|>','')
511
  yield history
512
  except Exception as e:
513
  print(f"Error during text generation: {e}")
@@ -543,7 +542,6 @@ def produce_streaming_answer_chatbot(
543
  if "choices" in out and len(out["choices"]) > 0 and "text" in out["choices"][0]:
544
  history[-1]['content'] += out["choices"][0]["text"]
545
  NUM_TOKENS+=1
546
- history[-1]['content'] = history[-1]['content'].replace('<|im_end|>','')
547
  yield history
548
  else:
549
  print(f"Unexpected output structure: {out}")
@@ -557,7 +555,7 @@ def produce_streaming_answer_chatbot(
557
  print(f'Time per token: {(time_generate/NUM_TOKENS)*1000}ms')
558
 
559
  elif "claude" in model_type:
560
- system_prompt = "You are answering questions from the user based on source material. Respond with short, factually correct answers."
561
 
562
  print("full_prompt:", full_prompt)
563
 
@@ -595,13 +593,11 @@ def produce_streaming_answer_chatbot(
595
  elif GEMINI_API_KEY: gemini_api_key = GEMINI_API_KEY
596
  else: raise Exception("Gemini API key not found. Please enter a key on the Advanced settings page or select another model type")
597
 
598
- print("Using Gemini model:", model_type)
599
- print("full_prompt:", full_prompt)
600
-
601
  if isinstance(full_prompt, str):
602
  full_prompt = [full_prompt]
603
 
604
- system_prompt = "You are answering questions from the user based on source material. Respond with short, factually correct answers."
605
 
606
  model, config = construct_gemini_generative_model(gemini_api_key, temperature, model_type, system_prompt, max_tokens)
607
 
 
32
  from langchain.text_splitter import RecursiveCharacterTextSplitter
33
  from langchain.docstore.document import Document
34
 
35
+ from chatfuncs.prompts import instruction_prompt_template_alpaca, instruction_prompt_mistral_orca, instruction_prompt_phi3, instruction_prompt_llama3, instruction_prompt_qwen, instruction_prompt_template_orca, instruction_prompt_gemma, instruction_prompt_template_gemini_aws
36
  from chatfuncs.model_load import temperature, max_new_tokens, sample, repetition_penalty, top_p, top_k, torch_device, CtransGenGenerationConfig, max_tokens
37
  from chatfuncs.config import GEMINI_API_KEY, AWS_DEFAULT_REGION, LARGE_MODEL_NAME, SMALL_MODEL_NAME, RUN_AWS_FUNCTIONS, FEEDBACK_LOGS_FOLDER
38
 
 
136
  # The main prompt:
137
 
138
  if model_type == SMALL_MODEL_NAME:
139
+ INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_gemma, input_variables=['question', 'summaries'])
140
  elif model_type == LARGE_MODEL_NAME:
141
  INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_phi3, input_variables=['question', 'summaries'])
142
  else:
143
+ INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_template_gemini_aws, input_variables=['question', 'summaries'])
144
 
145
 
146
  return INSTRUCTION_PROMPT, CONTENT_PROMPT
 
507
  new_text = ""
508
  history[-1]['content'] += new_text
509
  NUM_TOKENS += 1
 
510
  yield history
511
  except Exception as e:
512
  print(f"Error during text generation: {e}")
 
542
  if "choices" in out and len(out["choices"]) > 0 and "text" in out["choices"][0]:
543
  history[-1]['content'] += out["choices"][0]["text"]
544
  NUM_TOKENS+=1
 
545
  yield history
546
  else:
547
  print(f"Unexpected output structure: {out}")
 
555
  print(f'Time per token: {(time_generate/NUM_TOKENS)*1000}ms')
556
 
557
  elif "claude" in model_type:
558
+ system_prompt = "You are answering questions from the user based on source material. Make sure to fully answer the questions with all required detail."
559
 
560
  print("full_prompt:", full_prompt)
561
 
 
593
  elif GEMINI_API_KEY: gemini_api_key = GEMINI_API_KEY
594
  else: raise Exception("Gemini API key not found. Please enter a key on the Advanced settings page or select another model type")
595
 
596
+
 
 
597
  if isinstance(full_prompt, str):
598
  full_prompt = [full_prompt]
599
 
600
+ system_prompt = "You are answering questions from the user based on source material. Make sure to fully answer the questions with all required detail."
601
 
602
  model, config = construct_gemini_generative_model(gemini_api_key, temperature, model_type, system_prompt, max_tokens)
603
 
chatfuncs/config.py CHANGED
@@ -216,7 +216,7 @@ DEFAULT_DATA_SOURCE_NAME = get_or_create_env_var('DEFAULT_DATA_SOURCE_NAME', "Do
216
 
217
  DEFAULT_DATA_SOURCE = get_or_create_env_var('DEFAULT_DATA_SOURCE', "https://seanpedrick-case.github.io/doc_redaction/README.html")
218
 
219
- DEFAULT_EXAMPLES = get_or_create_env_var('DEFAULT_EXAMPLES', '[ "How can I make a custom deny list?", "How can I find page duplicates?", "How can I review and modify existing redactions?", "How can I export my review files to Adobe?"]')
220
  #
221
  # ') # ["What were the five pillars of the previous borough plan?",
222
  #"What is the vision statement for Lambeth?",
 
216
 
217
  DEFAULT_DATA_SOURCE = get_or_create_env_var('DEFAULT_DATA_SOURCE', "https://seanpedrick-case.github.io/doc_redaction/README.html")
218
 
219
+ DEFAULT_EXAMPLES = get_or_create_env_var('DEFAULT_EXAMPLES', '[ "How can I make a custom deny list?", "How can I find duplicate pages in a document?", "How can I review and modify existing redactions?", "How can I export my review files to Adobe?"]')
220
  #
221
  # ') # ["What were the five pillars of the previous borough plan?",
222
  #"What is the vision statement for Lambeth?",
chatfuncs/prompts.py CHANGED
@@ -73,7 +73,14 @@ QUESTION: {question}\n
73
  Answer:<|im_end|>
74
  <|im_start|>assistant\n"""
75
 
76
- instruction_prompt_gemma = """Answer the QUESTION using information from the following CONTENT. Respond with short answers that directly answer the question.
 
 
 
 
 
 
 
77
  CONTENT: {summaries}
78
  QUESTION: {question}
79
- assistant:"""
 
73
  Answer:<|im_end|>
74
  <|im_start|>assistant\n"""
75
 
76
+ instruction_prompt_gemma = """<start_of_turn>user
77
+ Answer the QUESTION using information from the following CONTENT. Make sure to fully answer the question with all required detail.
78
+ CONTENT: {summaries}
79
+ QUESTION: {question}<end_of_turn>
80
+ <start_of_turn>model
81
+ """
82
+
83
+ instruction_prompt_template_gemini_aws = """Answer the QUESTION with a using information from the following CONTENT. Make sure to fully answer the question with all required detail.
84
  CONTENT: {summaries}
85
  QUESTION: {question}
86
+ Answer:"""
docker_build_run_commands.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ docker build -t qa_chatbot .
2
+ docker run -p 7860:7860 -e HF_TOKEN=<token> qa_chatbot # HF_TOKEN is required to download Gemma 3