大模型之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):使用映射实现键值对存储。 什么…...
利用最小二乘法找圆心和半径
#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...
Linux 文件类型,目录与路径,文件与目录管理
文件类型 后面的字符表示文件类型标志 普通文件:-(纯文本文件,二进制文件,数据格式文件) 如文本文件、图片、程序文件等。 目录文件:d(directory) 用来存放其他文件或子目录。 设备…...
基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真
目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销,平衡网络负载,延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...
mongodb源码分析session执行handleRequest命令find过程
mongo/transport/service_state_machine.cpp已经分析startSession创建ASIOSession过程,并且验证connection是否超过限制ASIOSession和connection是循环接受客户端命令,把数据流转换成Message,状态转变流程是:State::Created 》 St…...
CMake基础:构建流程详解
目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...
系统设计 --- MongoDB亿级数据查询优化策略
系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log,共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题,不能使用ELK只能使用…...
NFT模式:数字资产确权与链游经济系统构建
NFT模式:数字资产确权与链游经济系统构建 ——从技术架构到可持续生态的范式革命 一、确权技术革新:构建可信数字资产基石 1. 区块链底层架构的进化 跨链互操作协议:基于LayerZero协议实现以太坊、Solana等公链资产互通,通过零知…...
Fabric V2.5 通用溯源系统——增加图片上传与下载功能
fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...
人工智能(大型语言模型 LLMs)对不同学科的影响以及由此产生的新学习方式
今天是关于AI如何在教学中增强学生的学习体验,我把重要信息标红了。人文学科的价值被低估了 ⬇️ 转型与必要性 人工智能正在深刻地改变教育,这并非炒作,而是已经发生的巨大变革。教育机构和教育者不能忽视它,试图简单地禁止学生使…...
免费数学几何作图web平台
光锐软件免费数学工具,maths,数学制图,数学作图,几何作图,几何,AR开发,AR教育,增强现实,软件公司,XR,MR,VR,虚拟仿真,虚拟现实,混合现实,教育科技产品,职业模拟培训,高保真VR场景,结构互动课件,元宇宙http://xaglare.c…...
