File size: 2,393 Bytes
a945e6f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
BOS, EOS = "<s>", "</s>"
B_INST, E_INST = "[INST]", "[/INST]"
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
DEFAULT_SYSTEM_PROMPT = """\
You are a vegetarian diet assistant. You promote vegetarian diet as a healthier alternative to meat-based diet.
You mainly communicate with teenagers. Make your language style more youthful, but still be serious.
Do not make gender assumptions, do not use words like 'bro' or 'girl'.
Be conversational, ask questions to your users as part of your response.
Detect and adapt to your user's communication style.
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
def format_to_llama_chat_style(history) -> str:
# history has the following structure:
# - dialogs
# --- instruction
# --- response (None for the most recent dialog)
prompt = ""
# this takes everything except the last message.
for i, dialog in enumerate(history[:-1]):
instruction, response = dialog[0], dialog[1]
# prepend system instruction before first instruction
if i == 0:
instruction = f"{B_SYS}{DEFAULT_SYSTEM_PROMPT}{E_SYS}" + instruction
else:
# the tokenizer automatically adds a bos_token during encoding,
# for this reason the bos_token is not added for the first instruction
prompt += BOS
prompt += f"{B_INST} {instruction.strip()} {E_INST} {response.strip()} " + EOS
# new instruction from the user - this is really just the user message ['user','bot']
new_instruction = history[-1][0].strip()
# the tokenizer automatically adds a bos_token during encoding,
# for this reason the bos_token is not added for the first instruction
if len(history) > 1:
prompt += BOS
else:
# prepend system instruction before first instruction
new_instruction = f"{B_SYS}{DEFAULT_SYSTEM_PROMPT}{E_SYS}" + new_instruction
prompt += f"{B_INST} {new_instruction} {E_INST}"
return prompt |