Skip to content

Commit 22d34ac

Browse files
Merge pull request #6 from seanpedrick-case/dev
Corrected Gemma model prompt format, made prompts in general ask for more detail. Wording changes.
2 parents 91e0b76 + a0e9486 commit 22d34ac

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def load_model(model_type:str, gpu_layers:int, gpu_config:dict=gpu_config, cpu_c
223223

224224
gr.Markdown("<h1><center>Lightweight PDF / web page QA bot</center></h1>")
225225

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.""")
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.""")
227227

228228
with gr.Row():
229229
current_source = gr.Textbox(label="Current data source(s)", value=DEFAULT_DATA_SOURCE, scale = 10)
@@ -252,7 +252,7 @@ def load_model(model_type:str, gpu_layers:int, gpu_config:dict=gpu_config, cpu_c
252252

253253
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)
254254

255-
with gr.Tab("Load in a different file/webpage"):
255+
with gr.Tab("Change data source"):
256256
with gr.Accordion("PDF file", open = False):
257257
in_pdf = gr.File(label="Upload pdf", file_count="multiple", file_types=['.pdf'])
258258
load_pdf = gr.Button(value="Load in file", variant="secondary", scale=0)

chatfuncs/chatfuncs.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from langchain.text_splitter import RecursiveCharacterTextSplitter
3333
from langchain.docstore.document import Document
3434

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
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
3636
from chatfuncs.model_load import temperature, max_new_tokens, sample, repetition_penalty, top_p, top_k, torch_device, CtransGenGenerationConfig, max_tokens
3737
from chatfuncs.config import GEMINI_API_KEY, AWS_DEFAULT_REGION, LARGE_MODEL_NAME, SMALL_MODEL_NAME, RUN_AWS_FUNCTIONS, FEEDBACK_LOGS_FOLDER
3838

@@ -136,11 +136,11 @@ def base_prompt_templates(model_type:str = SMALL_MODEL_NAME):
136136
# The main prompt:
137137

138138
if model_type == SMALL_MODEL_NAME:
139-
INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_qwen, input_variables=['question', 'summaries'])
139+
INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_gemma, input_variables=['question', 'summaries'])
140140
elif model_type == LARGE_MODEL_NAME:
141141
INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_phi3, input_variables=['question', 'summaries'])
142142
else:
143-
INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_template_orca, input_variables=['question', 'summaries'])
143+
INSTRUCTION_PROMPT=PromptTemplate(template=instruction_prompt_template_gemini_aws, input_variables=['question', 'summaries'])
144144

145145

146146
return INSTRUCTION_PROMPT, CONTENT_PROMPT
@@ -507,7 +507,6 @@ def produce_streaming_answer_chatbot(
507507
new_text = ""
508508
history[-1]['content'] += new_text
509509
NUM_TOKENS += 1
510-
history[-1]['content'] = history[-1]['content'].replace('<|im_end|>','')
511510
yield history
512511
except Exception as e:
513512
print(f"Error during text generation: {e}")
@@ -543,7 +542,6 @@ def produce_streaming_answer_chatbot(
543542
if "choices" in out and len(out["choices"]) > 0 and "text" in out["choices"][0]:
544543
history[-1]['content'] += out["choices"][0]["text"]
545544
NUM_TOKENS+=1
546-
history[-1]['content'] = history[-1]['content'].replace('<|im_end|>','')
547545
yield history
548546
else:
549547
print(f"Unexpected output structure: {out}")
@@ -557,7 +555,7 @@ def produce_streaming_answer_chatbot(
557555
print(f'Time per token: {(time_generate/NUM_TOKENS)*1000}ms')
558556

559557
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."
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."
561559

562560
print("full_prompt:", full_prompt)
563561

@@ -595,13 +593,11 @@ def produce_streaming_answer_chatbot(
595593
elif GEMINI_API_KEY: gemini_api_key = GEMINI_API_KEY
596594
else: raise Exception("Gemini API key not found. Please enter a key on the Advanced settings page or select another model type")
597595

598-
print("Using Gemini model:", model_type)
599-
print("full_prompt:", full_prompt)
600-
596+
601597
if isinstance(full_prompt, str):
602598
full_prompt = [full_prompt]
603599

604-
system_prompt = "You are answering questions from the user based on source material. Respond with short, factually correct answers."
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."
605601

606602
model, config = construct_gemini_generative_model(gemini_api_key, temperature, model_type, system_prompt, max_tokens)
607603

chatfuncs/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def add_folder_to_path(folder_path: str):
216216

217217
DEFAULT_DATA_SOURCE = get_or_create_env_var('DEFAULT_DATA_SOURCE', "https://seanpedrick-case.github.io/doc_redaction/README.html")
218218

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?"]')
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?"]')
220220
#
221221
# ') # ["What were the five pillars of the previous borough plan?",
222222
#"What is the vision statement for Lambeth?",

chatfuncs/prompts.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@
7373
Answer:<|im_end|>
7474
<|im_start|>assistant\n"""
7575

76-
instruction_prompt_gemma = """Answer the QUESTION using information from the following CONTENT. Respond with short answers that directly answer the question.
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.
7784
CONTENT: {summaries}
7885
QUESTION: {question}
79-
assistant:"""
86+
Answer:"""

docker_build_run_commands.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)