1. 标题提出
最近,META开源了Llama-2模型,受到了广泛的关注和好评,然而,在官方给的使用阐明中,并没有对使用方法进行特殊细节的介绍,尤其是对于对话任务,这就给我们在使用时带来了许多困扰。
以ChatGLM为例,在执行多轮对话时,需要将历史信息拼接到输入中,以供模型在天生时盘算历史token与当前query之间的交互(self-attn):
- # ChatGLM中对话prompt的产生:
- prompt = ""
- for i, (old_query, response) in enumerate(history_input):
- prompt += "[Round {}]\n问:{}\n答:{}\n".format(i, old_query, response)
- prompt += "[Round {}]\n问:{}\n答:".format(len(history_input), query_input)
复制代码 以是可以很自然的想到,如果使用Llama-2模型进行对话,应该也有这样一套模板,与训练过程中的对话形式相匹配。
于是经过简朴的搜刮后,在reddit论坛找到了Llama-2官方所提供的阐明:
https://www.reddit.com/r/LocalLLaMA/comments/155po2p/get_llama_2_prompt_format_right/
2. prompt的精确形式
根据官方账号给出的阐明,在对话时,用户所提供的prompt应当满意以下形式:
- <s>[INST] <<SYS>>
- {{ system_prompt }}
- <</SYS>>
- {{ user_message }} [/INST]
复制代码 其中,<s>,<\s>,<<SYS>>,<</SYS>>,[INST],以及[/INST]是特殊token,标记着prompt中各个部分的构成。
{{ system_prompt }}部分是整个对话中的通用前缀,一般用来给模型提供一个身份,作为对话的大配景。
{{ user_message }}部分是用户所提供的信息,可以理解为多轮对话中其中一轮对话的内容。
而且,其给出了一个样例:
[code]<s>[INST] <<SYS>>
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.
<</SYS>>
There's a llama in my garden |