大模型之Chat Markup Language
背景
在笔者应用大模型的场景中,对话模型(即大模型-chat系列)通常具有比较重要的地位,我们通常基于与大模型进行对话来获取我们希望理解的知识。然而大模型对话是依据何种数据格式来进行训练的,他们的数据为什么这么来进行组织,本篇文章将进行总结。
Chat Markup Language
Chat Markup Language (CML) 是一种用于描述对话结构的标记语言。它可以帮助大模型和 AI 助手之间的对话更加结构化和清晰。CML 可以描述对话中的各种元素,例如对话的开始和结束、用户和 AI 助手的发言、对话中的问题和回答等等。使用 CML 可以使得对话的处理更加方便和高效,同时也可以提高对话的可读性和可维护性。
DeepMind的相关研究指出,相关研究指出,LLM可以通过选取合适的prompt)来转化为对话代理。这些文本提示通常包含一种所谓的“系统”信息来定义 LLM 的角色,以及一系列人机对话的示例。
对数话数据格式
一种简单对话数据的构建格式是,单纯的把系统信息和角色信息插入到每一个训练样本中,然后在对话用"序列结尾"的token(如)分隔开。如下所示:
Below is a dialogue between a human and AI assistant ...Human: Is it possible to imagine a society without law?
Assistant: It is difficult to imagine ...
Human: It seems like you ...
Assistant: You are correct ...
Human: Yeah, but laws are complicated ..
<EOS>
这种简单对话数据构建方法可能会导致对话推理过程中生成不必要的对话轮次,因此需要进行改进。一种更好的结构化方法是ChatML,它对每个对话轮次进行包装,并使用预定义的特殊Token来表示询问或回答的角色。这种方法可以更好地区分对话中不同角色的发言,并且可以更准确地捕捉对话的语境和上下文。相比于简单的插入系统信息和角色信息的方法,ChatML更加灵活和可扩展,可以适应不同类型的对话场景和任务。
ChatGPT的ChatML
根据OpenAI ChatML V0将每个角色(system,user,assistant)的文本按照如下进行拼接:
<|im_start|>system
You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.
Knowledge cutoff: 2021-09-01
Current date: 2023-03-01<|im_end|>
<|im_start|>user
How are you<|im_end|>
<|im_start|>assistant
I am doing well!<|im_end|>
<|im_start|>user
How are you now?<|im_end|>
其中<|im_start|>、<|im_end|>是special token,进行tokenizer encode的时候他们会被编码成对应的单个token id,他们专门用来表示一个角色说话的start和end。
千问-chat版本也是用的这种数据构建方法。https://arxiv.org/pdf/2309.16609.pdf
另一种的ChatML
我们使用一些特殊的标记来表示不同的信息类型:
- <|system|>:表示系统信息的开始,用于描述聊天机器人的身份和角色。
- <|user|>:表示用户说出的话语。
- <|assistant|>:表示机器人说出的话语。
- <|end|>:表示话语或系统信息的结尾。
案例
对话数据:
sample={"messages": [{"content": "Is it possible to imagine a society without law?","role": "user",},{"content": "It is difficult to imagine a society that is able to be maintained without any semblance of Law. Laws exists for the purpose of maintaining society and past human behavior suggests that there would people that have no interest in promoting social cohesion and norms so it's unlikely that complex social and economic systems could develop without a legal system.","role": "assistant",},{"content": 'It seems like you consider the absence of law equal to the absence of anything that could guide the behaviour of the individual, however there are many other such things, like religion, conscience, tradition to mention few important patterns which are known to have crucial influence on society even today, but also we could imagine more, like universal love if everyone suddenly became "awaken" in some spiritual sense, or simply just having an omnipotent leader who maintains a certain order without ever codifying the principles in law. Is it still difficult to imagine?',"role": "user",},{"content": "You are correct that there are other factors that can guide behavior in a society and play a role in shaping individuals' behavior and interactions with each other. However, even in societies where these factors are present, laws still serve an important role in maintaining social order and resolving conflicts. \n\nFor example, religious teachings may prohibit theft and murder, but laws provide a system of justice for those who violate these prohibitions. Personal morality and conscience may guide individuals to act in a certain way, but laws provide a framework for resolving disputes when conflicting moral beliefs arise. Furthermore, the other factors you mentioned, such as universal love or an omnipotent leader, are not necessarily stable or reliable guides for behavior. Universal love, while a noble ideal, may not be achievable in practice, and an omnipotent leader may be prone to corruption or abuse of power.\n\nLaws provide a crucial foundation for maintaining social order and ensuring justice. It is possible to imagine alternative systems of governance, but it is unlikely that a society without any sort of legal framework would be stable or functional in the long term.","role": "assistant",},{"content": "Yeah, but laws are complicated. Most people can't understand them in depth. Some would argue it is almost a self-serving system which put energy into growing itself(eg.: patent trolling). I think there must be a less complex system which keeps up order in society.","role": "user",},]
}
chatGPT chatGLM封装
im_start= "<|im_start|>"
im_end= "<|im_end|>"def prepare_dialogue(example):system_msg = "Below is a dialogue between a human and an AI assistant called StarChat."prompt = im_start + "system\n" + system_msg + im_end+ "\n"for message in example["messages"]:if message["role"] == "user":prompt += im_start+ "user\n" + message["content"] + im_end + "\n"else:prompt += im_start+ "assistant\n" + message["content"] + im_end + "\n"return promptprint(prepare_dialogue(sample))
另一种 chatGLM封装
system_token = "<|assistant|>"
user_token = "<|user|>"
assistant_token = "<|assistant|>"
end_token = "<|end|>"def prepare_dialogue(example):system_msg = "Below is a dialogue between a human and an AI assistant called StarChat."prompt = system_token + "\n" + system_msg + end_token + "\n"for message in example["messages"]:if message["role"] == "user":prompt += user_token + "\n" + message["content"] + end_token + "\n"else:prompt += assistant_token + "\n" + message["content"] + end_token + "\n"return promptprint(prepare_dialogue(sample))
将特殊字符添加到tokenizer中
openAI版本
tokenizer.add_special_tokens({"additional_special_tokens": ["<|im_start|>", "<|im_end|>"]})
print(tokenizer.additional_special_tokens)
tokenizer("<|im_start|>")
另一种版本
tokenizer.add_special_tokens({"additional_special_tokens": ["<|system|>", "<|assistant|>", "<|user|>", "<|end|>"]})
可见特殊字符被封装到单独一个 token 的 ID中
构建标签
我们可以遮盖掉来自用户话语部分的损失函数值。因为我们的模型是基于用户的话语进行训练的,只被训练去预测 AI 助手说话的部分(在模型推理时,只需要根据用户的话回答用户)。下面是一个简单的函数,用于遮盖掉用户部分的标签,并将所有用户部分的令牌转换为-100(接下来,-100将被损失函数忽略)。
tokenizer.add_special_tokens({"additional_special_tokens": ["<|system|>", "<|assistant|>", "<|user|>", "<|end|>"]})def mask_user_labels(tokenizer, labels):user_token_id = tokenizer.convert_tokens_to_ids(user_token)assistant_token_id = tokenizer.convert_tokens_to_ids(assistant_token)for idx, label_id in enumerate(labels):if label_id == user_token_id:current_idx = idxwhile labels[current_idx]!= assistant_token_id and current_idx < len(labels):labels[current_idx] = -100 # Ignored by the losscurrent_idx += 1dialogue = "<|user|>\nHello, can you help me?<|end|>\n<|assistant|>\nSure, what can I do for you?<|end|>\n"
input_ids = tokenizer(dialogue).input_ids
labels = input_ids.copy()
mask_user_labels(tokenizer, labels)
labels
tokenizer.add_special_tokens({"additional_special_tokens": ["<|im_start|>", "<|im_end|>"]})def mask_user_labels(tokenizer, labels):im_start_id = tokenizer.convert_tokens_to_ids("<|im_start|>")im_end_id = tokenizer.convert_tokens_to_ids("<|im_end|>")user_id = tokenizer.convert_tokens_to_ids("user")assitant_id = tokenizer.convert_tokens_to_ids("assitant")for idx, label_id in enumerate(labels):if label_id == im_start_id:if idx < len(labels):if labels[idx+1] == user_id:current_idx = idxwhile labels[current_idx+1]!= im_start_id:labels[current_idx] = -100 # Ignored by the losslabels[current_idx+1] = -100 # Ignored by the losscurrent_idx += 1dialogue = "<|im_start|>user\nHello, can you help me?<|im_end|>\n<|im_start|>assistant\nSure, what can I do for you?<|im_end|>\n"
input_ids = tokenizer(dialogue).input_ids
labels = input_ids.copy()
mask_user_labels(tokenizer, labels)
labels
所有用户输入的ID都被遮蔽了。在微调阶段,这些特殊的标记将学习到它们自己特定的嵌入。
相关文章:

大模型之Chat Markup Language
背景 在笔者应用大模型的场景中,对话模型(即大模型-chat系列)通常具有比较重要的地位,我们通常基于与大模型进行对话来获取我们希望理解的知识。然而大模型对话是依据何种数据格式来进行训练的,他们的数据为什么这么来进行组织,本…...

分布式链路追踪系统Skywalking的部署和应用
一,背景 随着业务的扩张,系统变得越来越复杂,由前端、app、api、微服务、数据库、缓存、消息队列、关系数据库、列式数据库等构成了繁杂的分布式网络。 当出现一个调用失败的问题时,要定位异常在哪个服务,需要进入每一…...

canvas绘制动态视频并且在视频上加上自定义logo
实现的效果:可以在画布上播放动态视频,并且加上自定义的图片logo放在视频的右下角 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta name"viewport" content"widthd…...

分类预测 | MATLAB实现基于BiGRU-AdaBoost双向门控循环单元结合AdaBoost多输入分类预测
分类预测 | MATLAB实现基于BiGRU-AdaBoost双向门控循环单元结合AdaBoost多输入分类预测 目录 分类预测 | MATLAB实现基于BiGRU-AdaBoost双向门控循环单元结合AdaBoost多输入分类预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 1.MATLAB实现基于BiGRU-AdaBoos…...
Kotlin 协程(线程)切换
常用协程切换函数 withContext 是Kotlin协程中的一个常用协程函数,它的作用是切换协程的执行上下文(线程或调度器)。具体来说,withContext 的主要功能如下: 切换执行上下文:withContext 允许你从一个执行上…...

分布式Trace:横跨几十个分布式组件的慢请求要如何排查?
目录 前言 一、问题的出现? 二、一体化架构中的慢请求排查如何做 三、分布式 Trace原理 四、如何来做分布式 Trace 前言 在分布式服务架构下,一个 Web 请求从网关流入,有可能会调用多个服务对请求进行处理,拿到最终结果。这个…...

【计算机毕设选题推荐】口腔助手小程序SpringBoot+Vue+小程序
前言:我是IT源码社,从事计算机开发行业数年,专注Java领域,专业提供程序设计开发、源码分享、技术指导讲解、定制和毕业设计服务 项目名 基于SpringBoot的口腔助手小程序 技术栈 SpringBootVue小程序MySQLMaven 文章目录 一、口腔…...

【C/C++笔试练习】初始化列表、构造函数、析构函数、两种排序方法、求最小公倍数
文章目录 C/C笔试练习1. 初始化列表(1)只能在列表初始化的变量 2.构造函数(2)函数体赋值(3)构造函数的概念(4)构造函数调用次数(5)构造函数调用次数ÿ…...

分享 | 对 电商API 平台的再思考
API 是推动现代企业数字化转型的基础。它不但连接了内部应用程序、合作伙伴和客户,同时也快速持续地向市场提供了各种新产品、版本和功能。 但当下还是以集中式的 API 交付为主。一个企业的对外 API 交付过程通常都是冗余而繁琐的,对企业内部的敏捷性、速…...

C语言--程序环境和预处理
前言 本章就是c语言的最后一个板块了,学完这章节,我们将知道写出的代码如何变成可执行程序的,这是非常重要的一个章节,那让我们一起进入本章的学习吧。 本章重点: 程序的翻译环境程序的执行环境详解:C语言程…...
深度学习笔记_5 经典卷积神经网络LeNet-5 解决MNIST数据集
1、定义LeNet-5模型,包括卷积层和全连接层。 import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms# 导入必要的库# 定义 LeNet-5 模型 class LeNet5(nn.Module):def __init__(self):super(LeNet5, self…...

国内智能客服机器人都有哪些?
随着人工智能技术的不断发展,智能客服机器人已经成为了企业客户服务的重要工具。国内的智能客服机器人市场也迎来了飞速发展,越来越多的企业开始采用智能客服机器人来提升客户服务效率和质量。 在这篇文章中,我将详细介绍国内知名的智能客服机…...

Matlab/C++源码实现RGB通道与HSV通道的转换(效果对比Halcon)
HSV通道的含义 HSV通道是指图像处理中的一种颜色模型,它由色调(Hue)、饱和度(Saturation)和明度(Value)三个通道组成。色调表示颜色的种类,饱和度表示颜色的纯度或鲜艳程度…...

【C进阶】动态内存管理
一、为什么存在动态内存分配 我们之前学的都是开辟固定大小的空间,但有时候需要空间的大小只有在程序运行时才能知道,那么就引入了动态内存开辟 内存分布所在: 二、动态内存函数的介绍 2.1malloc和free 动态内存开辟的函数 void * malloc…...
神经网络的梯度优化方法
神经网络的梯度优化是深度学习中至关重要的一部分,它有助于训练神经网络以拟合数据。下面将介绍几种常见的梯度优化方法,包括它们的特点、优缺点以及原理。 梯度下降法 (Gradient Descent): 特点: 梯度下降是最基本的优化算法,它试图通过迭代…...

linux 装机教程(自用备忘)
文章目录 安装 pyenv 管理多版本 python 环境安装使用使用 pyenv 和 virtualenv 管理虚拟 python 环境 vscode 连接远程服务器tmux 美化zsh 安装 pyenv 管理多版本 python 环境 安装 (教程参考:https://www.modb.pro/db/155036) sudo apt-…...

Tensorboard安装及简单使用
Tensorboard 1. tensorboard 简单介绍2. 安装必备环境3. Tensorboard安装4. 可视化命令 1. tensorboard 简单介绍 TensorBoard是一个可视化的模块,该模块功能强大,可用于深度学习网络模型训练查看模型结构和训练效果(预测结果、网络模型结构…...

SpringCloud 微服务全栈体系(二)
第三章 Eureka 注册中心 假如我们的服务提供者 user-service 部署了多个实例,如图: 思考几个问题: order-service 在发起远程调用的时候,该如何得知 user-service 实例的 ip 地址和端口?有多个 user-service 实例地址…...
flutter 常用组件:列表ListView
文章目录 总结#1、通过构造方法直接构建 ListView 提供了一个默认构造函数 ListView,我们可以通过设置它的 children 参数,很方便地将所有的子 Widget 包含到 ListView 中。 不过,这种创建方式要求提前将所有子 Widget 一次性创建好,而不是等到它们真正在屏幕上需要显示时才…...

十四天学会C++之第七天:STL(标准模板库)
1. STL容器 什么是STL容器,为什么使用它们。向量(vector):使用向量存储数据。列表(list):使用列表实现双向链表。映射(map):使用映射实现键值对存储。 什么…...
Nuxt.js 布局系统详解:构建可复用页面框架
Nuxt.js 是一个基于 Vue.js 的强大框架,旨在简化开发流程并提高项目的可维护性。布局系统是 Nuxt.js 项目结构中的一个重要组成部分,它位于 layouts 目录下,帮助开发者实现页面间的统一风格和结构复用。 什么是 Nuxt.js 布局系统 Nuxt.js 提…...

如何在没有 iTunes 的情况下备份 iPhone
我可以在没有 iTunes 的情况下将 iPhone 备份到电脑吗?虽然 iTunes 曾经是备份 iPhone 的主要方法,但它并不是 iOS 用户唯一的备份选项。您可以选择多种方便的替代方案来备份 iPhone,无需使用 iTunes。您可以在这里获得更灵活、更人性化的备份…...
损坏的RAID5 第十六次CCF-CSP计算机软件能力认证
纯大模拟 提前打好板子 我只通过4个用例点 然后就超时了。 #include<iostream> #include<cstring> #include<algorithm> #include<unordered_map> #include<bits/stdc.h> using namespace std; int n, s, l; unordered_map<int, string>…...

增量式网络爬虫通用模板
之前做过一个项目,他要求是只爬取新产生的或者已经更新的页面,避免重复爬取未变化的页面,从而节省资源和时间。这里我需要设计一个增量式网络爬虫的通用模板。可以继承该类并重写部分方法以实现特定的解析和数据处理逻辑。这样可以更好的节约…...

uniapp 安卓 APP 后台持续运行(保活)的尝试办法
在移动应用开发领域,安卓系统的后台管理机制较为复杂,应用在后台容易被系统回收,导致无法持续运行。对于使用 Uniapp 开发的安卓 APP 来说,实现后台持续运行(保活)是很多开发者面临的重要需求,比…...
leetcode 2434. 使用机器人打印字典序最小的字符串 中等
给你一个字符串 s 和一个机器人,机器人当前有一个空字符串 t 。执行以下操作之一,直到 s 和 t 都变成空字符串: 删除字符串 s 的 第一个 字符,并将该字符给机器人。机器人把这个字符添加到 t 的尾部。删除字符串 t 的 最后一个 字…...

读文献先读图:GO弦图怎么看?
GO弦图(Gene Ontology Chord Diagram)是一种用于展示基因功能富集结果的可视化工具,通过弦状连接可以更直观的展示基因与GO term(如生物过程、分子功能等)之间的关联。 GO弦图解读 ①内圈连线表示基因和生物过程之间的…...

学习设计模式《十二》——命令模式
一、基础概念 命令模式的本质是【封装请求】命令模式的关键是把请求封装成为命令对象,然后就可以对这个命令对象进行一系列的处理(如:参数化配置、可撤销操作、宏命令、队列请求、日志请求等)。 命令模式的定义:将一个…...

SDC命令详解:使用set_min_capacitance命令进行约束
相关阅读 SDC命令详解https://blog.csdn.net/weixin_45791458/category_12931432.html?spm1001.2014.3001.5482 目录 指定最小需驱动电容值 指定对象列表/集合 简单使用 写在最后 set_min_capacitance命令用于设置输入端口的最小需驱动电容(设置了输入端口的min_c…...
HttpServletRequest常用方法
方法说明示例String getMethod()获取请求的 HTTP 方法(如 GET、POST 等)。request.getMethod() 返回 "GET"String getRequestURI()获取请求的 URI(路径部分,不包括域名和协议)。请求 http://localhost:8080/…...