当前位置: 首页 > news >正文

网站建设优化教程网站伪静态设置

网站建设优化教程,网站伪静态设置,济南百度推广公司电话,淘宝上做淘宝客的网站NLP实践——Llama-2 多轮对话prompt构建 1. 问题提出2. prompt的正确形式3. 效果测试4. 结尾 1. 问题提出 最近,META开源了Llama-2模型,受到了广泛的关注和好评,然而,在官方给的使用说明中,并没有对使用方法进行特别细…

NLP实践——Llama-2 多轮对话prompt构建

  • 1. 问题提出
  • 2. prompt的正确形式
  • 3. 效果测试
  • 4. 结尾

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 }}部分是用户所提供的信息,可以理解为多轮对话中其中一轮对话的内容。

并且,其给出了一个样例:

<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 😱 What should I do? [/INST]

然而,这个简单的样例并不能很好的反映多轮对话的场景,因为它只描述了用户怎样提供第一轮输入,对于几个特殊token如何使用,也没有很好的说明。

幸运的是,在评论区有用户@Evening_Ad6637给出了更加完整的样例以供我们参考:

假设输入是以下形式的对话:

<s>[INST] <<SYS>>You are are a helpful... bla bla.. assistant<</SYS>>Hi there! [/INST] Hello! How can I help you today? </s><s>[INST] What is a neutron star? [/INST] A neutron star is a ... </s><s> [INST] Okay cool, thank you! [/INST]

那么当这个整体作为prompt输入给模型去进行generate时,模型的输出应该是类似于You're welcome! 之类的话。

更详细的解释一下:

    1. 每一组<s></s>之间是一个相对完整的单元,可以理解为一个对话轮次(如果直接给一个文本作为输入,也可以看到模型的输入结果分别是以这两个BOS和EOS token作为结尾的)。
    1. [INST][/INST]用于区分在当前这一轮的对话(历史)中,用户输入的部分与模型返回的部分。位于[INST]之后,/[INST]之前的文本,是用户在这一轮次对话中所输入的query,而/[INST]之后的文本,是模型针对这一query所作出的回答。
    1. 在对话中的第一组单元,可以提供整个对话的背景信息,并以<<SYS>><</SYS>>作为特殊标记,位于它们之间的,是对话的背景信息。
    1. 需要额外注意,有些特殊标记与文本之间是有空格的,容易遗漏。

3. 效果测试

为了验证prompt是否有效,我们做一个简单的实验,场景是询问动物相关的问题。

(1)首先假设Llama-2模型是一个动物学家,以此作为背景:

You are a zoologist, you will answer my questions about animals.

(2)然后提供一轮对话历史:

- What is an elephant?
- Ah, an excellent question! Elephants are fascinating creatures, and I'm happy to share my knowledge with you.\nAn elephant is a large, intelligent, and social mammal that belongs to the family Elephantidae.

(3)接下来,提供新一轮对话的query

How much does it weigh?

注意,在这里我回避了使用elephant这个词,而是直接用it进行指代。

(4)完整的prompt
根据前文所述的prompt格式,可以将当前轮次输入给模型进行generate的完整prompt构造如下:

text = """<s>[INST] <<SYS>>
You are a zoologist, you will answer my questions about animals. 
<</SYS>>What is an elephant? [/INST] Ah, an excellent question! Elephants are fascinating creatures, and I'm happy to share my knowledge with you.\nAn elephant is a large, intelligent, and social mammal that belongs to the family Elephantidae.</s><s>[INST] How much does it weigh? [/INST]"""

(5)返回结果
经过generate(生成参数为:{“max_length”: 4096, “num_beams”: 3, “temperature”: 0.9})并解码之后,模型生成结果为:

"<s><s> [INST] <<SYS>>\nYou are a zoologist, you will answer my questions about animals. \n<</SYS>>\n\nWhat is an elephant? [/INST] Ah, an excellent question! Elephants are fascinating creatures, and I'm happy to share my knowledge with you.\nAn elephant is a large, intelligent, and social mammal that belongs to the family Elephantidae.</s><s> [INST] How much does it weigh? [/INST]  Great question! The weight of an elephant can vary depending on the species and the individual animal. Here are the average weights for the two main species of elephants:\n* African elephants: Males typically weigh between 5,000 to 14,000 pounds (2,268 to 6,350 kilograms), while females weigh between 4,000 to 9,000 pounds (1,814 to 4,082 kilograms).\n* Asian elephants: Males typically weigh between 3,000 to 6,000 pounds (1,361 to 2,722 kilograms), while females weigh between 2,000 to 3,000 pounds (907 to 1,361 kilograms).\nIt's worth noting that these are just averages, and individual elephants can vary significantly in weight depending on various factors such as age, sex, and nutrition.</s>"

可以看到对话的效果还是不错的,可以比较好的回答出当前轮次关于大象体重的问题。

4. 结尾

本文介绍了如何构建Llama多轮对话的prompt,以适应模型的训练过程中的结构。如果需要搭建一个对话工具,还需要进行一些其他额外的工作,例如对模型生成结果进行截断、对中间kv进行缓存等。

chat与stream_chat的实现可以参考chatGLM的写法:
https://huggingface.co/THUDM/chatglm2-6b/blob/main/modeling_chatglm.py

如何在对话过程中进行显存控制,可以参考我之前的这一篇博客:
https://blog.csdn.net/weixin_44826203/article/details/130401177

此外,还有关于特殊token的一点小疑问,我实验过程中所使用的Llama-2-7b-chat-hf模型,只将<s><\s>列为了特殊token,而<<SYS>><</SYS>>[INST],以及[/INST]都只是普通字符,在tokenize的时候会被切分开,不知道是我用错了模型还是什么其他的原因,希望官方在后续能够提供更加详细的说明介绍。

如果本文对你的学习和工作有所帮助的话,记得点一个免费的赞。转载请注明出处。

http://www.yayakq.cn/news/652573/

相关文章:

  • 做网站到哪里做网页制作工具按其制作方式分可以分为
  • 阜阳建设部网站鞍山市人力资源招聘信息网
  • 长安网站设计免费行情软件网站大全
  • 周口市做网站网站登录界面图片用什么软件做
  • 哪个公司建网站最好网站做资讯需要获取许可证吗
  • p2p网站建设报价网络营销平台的主要功能
  • 贵阳专业做网站的公司海外全网推广
  • 哪个网站建设好wordpress注册不发送邮件
  • 兰州网站建设科技公司wordpress指定文章评论
  • 怎么自己做网站表白开网店的流程视频
  • 网站模板去哪下载山东枣庄滕州网站建设
  • 网站集群建设参数人才网站怎么建设
  • 中文域名是网站名称吗网页设计与制作实例教程方其桂
  • 国家住房部和城乡建设部 网站首页淘宝网站的建设目的是什么意思
  • 哪里有建网站的公司网站运行时错误如何做
  • 网络推广最好的网站在线网页设计培训机构
  • 讲述做网站的电影建设网站中期
  • 上海做网站优化西安哪有做网站的
  • 如何做网站公众号推广网站制作开发的步骤和方法
  • 网站图标代码开通网站流程
  • 做网站先做前台还是后台WordPress 磁力
  • html网站开发 工具福州百度首页优化
  • 字体设计比较好的网站网站建设与推广的实训报告
  • 网站搭建设计是什么企业网阳江一中成绩查询
  • 重庆网站seo设计哪里有做美食的视频网站
  • 网站开发者工具post乐之网站制作
  • 商务网站建设的一般流程图广告商对接平台
  • 哪个网站可以做装修效果图建设银行纪念币网站
  • 谷歌网站建设代理zencart外贸网站建设
  • 如何自己做电影网站wordpress 砍柴网