# 用我的 Telegram 消息微调大语言模型：一段经验分享 [译]

- Link: https://paooo.com/ai-study/7033/
- Published: 2024-02-28T18:17:22+08:00
- Author: 宝玉的分享

⚡ Prompt 语宙 AI 导读 · 30 秒核心看点用 Telegram 聊天记录做微调语料，核心在于把真实对话整理成可训练格式。文章展示了多轮闲聊与技术讨论混合数据，说明清洗和结构化比堆量更关键。实测经验强调：微调效果取决于语料一致性，适合做个性化对话模型参考。&lt;|im_start|&gt;John Smith&gt;&gt;&gt; 真是糟糕，总是卡在 135 分钟的时间限制上&gt;&gt;&gt; 尝试高效完成任务，但似乎总是不行&lt;|im_end|&gt;&lt;|im_start|&gt;Alexander Smirnov&gt;&gt;&gt; 我也有同样的困扰&gt;&gt;&gt; 你还在坚持最初的方案吗？&lt;|im_end|&gt;&lt;|im_start|&gt;John Smith&gt;&gt;&gt; 不确定，我觉得我们想法是一致的&gt;&gt;&gt; 就像你说的&gt;&gt;&gt; 我正在试着用倒置字符串的方法，希望能在那里发现些什么&gt;&gt;&gt; 但这似乎行不通，因为 z 函数把一切都搞砸了……………………&lt;|im_end|&gt;&lt;|im_start|&gt;Alexander Smirnov&gt;&gt;&gt; 我不明白 z 函数在这里怎么用&lt;|im_end|&gt;&lt;|im_start|&gt;John Smith&gt;&gt;&gt; 我也不太清楚，感觉不管怎样我都是在迭代地做，但是是的，为了构建 z 函数需要反转一些字符串&gt;&gt;&gt; 这只是从讨论中随便找的一个解决方案&gt;&gt;&gt; 来自讨论&lt;|im_end|&gt;&lt;|im_start|&gt;Alexander Smirnov&gt;&gt;&gt; 明白了&lt;|im_end|&gt; &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; I’m good, what about you?&gt;&gt;&gt; nothing much&gt;&gt;&gt; what did you do today?&gt;&gt;&gt; I worked.&gt;&gt;&gt; then I did a few tasks in Python and studied data processing libraries.&gt;&gt;&gt; in the evening I’ll do wrestling training.&gt;&gt;&gt; have you ever been into sports or do you have any hobbies?&gt;&gt;&gt; I see&gt;&gt;&gt; want to watch a movie today?&gt;&gt;&gt; maybe, if I find the time.&gt;&gt;&gt; what’s your favorite music? Electronic, rock, or other genres? &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; well, everything is fine so far. what’s new with you?&gt;&gt;&gt; spent the whole day programming&gt;&gt;&gt; what did you do today?&gt;&gt;&gt; finished at work&gt;&gt;&gt; and did some housework, now I’m resting. are you also resting or still working?&gt;&gt;&gt; I’m already resting&gt;&gt;&gt; well, that’s good. can we talk about something interesting or do you have any questions? &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hi) all good&gt;&gt;&gt; you?)&gt;&gt;&gt; ok as well&gt;&gt;&gt; what you were doing today?&gt;&gt;&gt; had an amazon interview and solved problems&gt;&gt;&gt; had to solve 3&gt;&gt;&gt; cool&gt;&gt;&gt; hard?&gt;&gt;&gt; don’t know what&gt;&gt;&gt; hadn’t looked yet&gt;&gt;&gt; I hadn’t started yet&gt;&gt;&gt; chatting whole day with the team and meet new ones&gt;&gt;&gt; how are they?&gt;&gt;&gt; ok&gt;&gt;&gt; same coworking as you have)) &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; ok, we show yesterday’s product to searchcom, today we write endpoints for them so that someone else can request our api&gt;&gt;&gt; cool&gt;&gt;&gt; what problems do we have now?&gt;&gt;&gt; no problems&gt;&gt;&gt; everything is working&gt;&gt;&gt; I added an option to choose index&gt;&gt;&gt; so now we can attach several indexes&gt;&gt;&gt; and separate collections within single index&gt;&gt;&gt; what we have to do?&gt;&gt;&gt; test&gt;&gt;&gt; yes ok&gt;&gt;&gt; are they always will be sending post-script? &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; Hi, good)&gt;&gt;&gt; you?&gt;&gt;&gt; ok as well&gt;&gt;&gt; what do you do at the evening?&gt;&gt;&gt; we have meeting with office collegues at 8 30 msk&gt;&gt;&gt; after it I can&gt;&gt;&gt; can what?&gt;&gt;&gt; haha)&gt;&gt;&gt; didn’t get it&gt;&gt;&gt; I don’t know( &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hi)&gt;&gt;&gt; seems good&gt;&gt;&gt; was working, than went to the call with Nadya&gt;&gt;&gt; now reading normally&gt;&gt;&gt; you?&gt;&gt;&gt; good as well&gt;&gt;&gt; any plans for the evening?&gt;&gt;&gt; after work wendesday I would love to go running&gt;&gt;&gt; and then have a lunch&gt;&gt;&gt; your plans?&gt;&gt;&gt; I though about watching movie&gt;&gt;&gt; aa&gt;&gt;&gt; ooo&gt;&gt;&gt; to be honest I like when it is raining&gt;&gt;&gt; when you don’t feel like going anywhere &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; Hi, great!&gt;&gt;&gt; Sorry will be 20-30 min late&gt;&gt;&gt; ok&gt;&gt;&gt; why?&gt;&gt;&gt; yes, I just got up and forgot to press the «exit sleep» button&gt;&gt;&gt; my telegram didn’t close on its own&gt;&gt;&gt; I thought it behaved like this when you logging off the Internet, but no&gt;&gt;&gt; I still logged off the Internet, but telegram still didn’t respond&gt;&gt;&gt; and it was strange, because yesterday there was also such a case when I sent you a message and did not receive an answer&gt;&gt;&gt; today I looked at mine, but not yours, but yours was displayed)&gt;&gt;&gt; ok&gt;&gt;&gt; any plans?&gt;&gt;&gt; yes, we can chat a little&gt;&gt;&gt; and I can tell you that the method for generating answers worked really well for me&gt;&gt;&gt; he gives respectable results in training&gt;&gt;&gt; but not on the test &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hey, hi&gt;&gt;&gt; Good, you?)&gt;&gt;&gt; Haven’t gone to bed yet)0)0)0)&gt;&gt;&gt; Today I get up and go to a programming course&gt;&gt;&gt; To tighten up the base, etc.&gt;&gt;&gt; nice&gt;&gt;&gt; what did you do yesterday?&gt;&gt;&gt; Yesterday there was already the same course in which it seemed to me that I did very poorly&gt;&gt;&gt; Then I wanted to solve hw but I don’t care&gt;&gt;&gt; Otherwise I’m fucked because I can’t sleep)&gt;&gt;&gt; Really fucked up&gt;&gt;&gt; It will probably be better now, since I’m already maximally overtrained&gt;&gt;&gt; cool man&gt;&gt;&gt; what are your plans for life in general?&gt;&gt;&gt; Fuck, get a job interviews first&gt;&gt;&gt; And if everything is ok, then actually order a computer&gt;&gt;&gt; Like, I think you should order a computer when it’s more profitable than working at work&gt;&gt;&gt; But I don’t care &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hey) good&gt;&gt;&gt; you?&gt;&gt;&gt; good as well&gt;&gt;&gt; what you were doing today?&gt;&gt;&gt; played a board game&gt;&gt;&gt; I’ll go home now&gt;&gt;&gt; nice&gt;&gt;&gt; what kind of game?&gt;&gt;&gt; catan&gt;&gt;&gt; strategy&gt;&gt;&gt; very good&gt;&gt;&gt; who was there?&gt;&gt;&gt; Max Yavich and Fedya&gt;&gt;&gt; who won?&gt;&gt;&gt; Max &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hi, good&gt;&gt;&gt; let’s meet in half an hour?&gt;&gt;&gt; yep&gt;&gt;&gt; what did you want?&gt;&gt;&gt; damn I haven’t finished the script for recalculating models yet&gt;&gt;&gt; and I need to finish it to send it to them&gt;&gt;&gt; this recalculation takes a lot of time, so I just want to turn around and start doing it and not waste time on preparation&gt;&gt;&gt; got it&gt;&gt;&gt; what is the plan for today?&gt;&gt;&gt; meet in half an hour to discuss what we are doing&gt;&gt;&gt; if decided, then I send an application to them and wait for a response&gt;&gt;&gt; if we haven’t decided, then we continue to work&gt;&gt;&gt; who do you mean by them?&gt;&gt;&gt; investors&gt;&gt;&gt; https://www.ycombinator.com/howtoapply&gt;&gt;&gt; there are two tests) &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hey we’re sitting and solving problems&gt;&gt;&gt; what kind of problems?&gt;&gt;&gt; data analysis +-&gt;&gt;&gt; and for what?&gt;&gt;&gt; https://yandex.ru/cup/profile/&gt;&gt;&gt; it seems it just became more interesting&gt;&gt;&gt; cool&gt;&gt;&gt; what are you planning this evening?&gt;&gt;&gt; idk yet&gt;&gt;&gt; and on the weekend?&gt;&gt;&gt; apparently I’ll also finish solving problems and get some work done &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; hey)&gt;&gt;&gt; good&gt;&gt;&gt; you?&gt;&gt;&gt; that’s okay too&gt;&gt;&gt; what did you do today?&gt;&gt;&gt; well, I worked, of course&gt;&gt;&gt; and also&gt;&gt;&gt; met with Alena&gt;&gt;&gt; ate at the ramen shop with everyone&gt;&gt;&gt; and went home&gt;&gt;&gt; by the way, we booked a place for 14 people&gt;&gt;&gt; for the evening))&gt;&gt;&gt; cool&gt;&gt;&gt; and what’s about university?&gt;&gt;&gt; yeah&gt;&gt;&gt; studied a lot&gt;&gt;&gt; in general, every day I want to study more&gt;&gt;&gt; what is the hardest thing now?&gt;&gt;&gt; well I don’t know&gt;&gt;&gt; it just takes a very long time&gt;&gt;&gt; there are some optimization methods that need to be rewritten&gt;&gt;&gt; so I think maybe it’s not worth optimizing &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; Hey, good, what about you?&gt;&gt;&gt; Are you already in Bangkok?&gt;&gt;&gt; yeah, already here&gt;&gt;&gt; what are your plans for today?&gt;&gt;&gt; Well, I would like to work on these pioneer tasks, if you don’t mind, we can do it together&gt;&gt;&gt; Or you can skip, I’ll finish the livechat tomorrow&gt;&gt;&gt; and what about live chat?&gt;&gt;&gt; Damn right now I launched the front on the prod without this shit that generates this thing with a logo, and it swears, says to connect js from google analytics&gt;&gt;&gt; I understand that this is a fucking cringe, but I still tried to remember where to get it&gt;&gt;&gt; yeah got it&gt;&gt;&gt; what will you do on weekends?&gt;&gt;&gt; We are meeting with friends here before leaving, tomorrow we need to go to the hotel by 6 am to spend the day in the best traditions&gt;&gt;&gt; There’s another party in the evening&gt;&gt;&gt; Actually, let’s have a call, I’ll tell you &gt;&gt;&gt; hey&gt;&gt;&gt; what’s up?&gt;&gt;&gt; Hi, haven’t you called yet?)&gt;&gt;&gt; Hey&gt;&gt;&gt; ok)&gt;&gt;&gt; I’m going to play Dota now&gt;&gt;&gt; Join if you want to)&gt;&gt;&gt; mb a little later)&gt;&gt;&gt; what are your plans for weekends?&gt;&gt;&gt; Lot of gyms/flexes/parks&gt;&gt;&gt; + maybe come to your place&gt;&gt;&gt; hmm well mb mb)&gt;&gt;&gt; what’s with your job btw?&gt;&gt;&gt; Well, I’m waiting for the info&gt;&gt;&gt; did they say what deadlines are?&gt;&gt;&gt; He said that there will be a call next week to discuss it&gt;&gt;&gt; remind me what this position is&gt;&gt;&gt; Data Scientist&gt;&gt;&gt; The team is engaged in recommending products and other shit&gt;&gt;&gt; Creating model&gt;&gt;&gt; As part of this position, you will have to develop skills in machine learning, statistical data analysis, programming and much more&gt;&gt;&gt; It is how I understood it📋 一键复制 Prompt 咒语 用我的 Telegram 消息微调大语言模型：一段经验分享 2023 年 11 月 27 日阅读目录用我的 Telegram 消息微调大语言模型：一段经验分享实施方法数据准备评估计划LoRA完整精调 (Full fine-tuning)结束感想代码 对大部分认识我的人来说，我大多数时间像是一个文本处理程序。既然输入输出都这么简单，一个模型能不能取代我呢？为了实现这一点，模型不只需要模仿我的写作风格，还得对我有深入了解。而我的 Telegram 使用记录是最好的信息源，我每天都用它，它几乎记录了我所有的想法和行动。 实施方法 最简单的方法是提取我所有的消息，导入到 ChatGPT 的上下文中，并让它学习这些信息，以此模仿我回复新消息的风格。不过，这种方法受限于上下文窗口的大小，意味着我得预处理消息，提炼关键内容。为了避免这个繁琐的步骤，我考虑使用检索增强生成 (RAG) 来在需要时检索相关信息。但根据我的经验，要从多样化的数据，比如聊天记录中检索信息，通常需要对检索模型进行有监督的微调，这是我不愿意去做的。因此，对我来说，微调是最好的选择，因为它不仅能够捕捉我的写作风格，还能从我所有的消息中积累知识，而无需我去筛选重要内容。 OpenAI 提供了微调功能，但因为我打算使用私人信息，我不想依赖任何第三方的微调服务。因此，我需要挑选一个合适的基础模型。根据 Hugging Face Open LLM 排行榜，Mistral 7B（链接）是目前排名靠前的小型模型之一（参数不超过 13B），它的表现甚至优于 Llama 2 13B（链接）。现在，我在考虑是否采用 LoRA 足以应对我的需求，或者我是否需要进行完整的微调。一些比较研究 [1] [2] 显示，尽管 LoRA 在某些情况下不如完全微调，但大多数时候仍然有效。但对于像我这样的特定任务（俄语 + 聊天），我找到了一篇 论文，研究者们在中文环境下对 Llama 进行了指令微调，这与我的目标有相似的复杂性。他们发现，对于未经过指令微调的基础模型，采用 LoRA 微调的效果不及完全微调。然而，在已经针对指令进行了微调的模型上，LoRA 微调可以达到与完全微调相媲美的效果。对我来说，这意味着要么在一个基础模型上进行完全微调，要么在已经针对俄语聊天进行过微调的模型上采用 LoRA。鉴于我找不到专门针对俄语聊天微调的模型，我打算在一个已针对英语聊天进行过微调的 Mistral 模型 Dolphin 上尝试 LoRA。 因此，我的计划是： 首先在 Dolphin 上尝试 LoRA，Dolphin 是一个已针对英语聊天进行过微调的 Mistral 模型。 如果效果不理想，考虑在 Mistral 上进行完全微调。 数据准备 在应用程序如 Telegram 的信息传递中，有一个与电子邮件截然不同的特点：对话流的自然性。在这样的对话中，消息通常不是你和联系人依次回复，而是你可能连续发出几条信息，然后对方连续回复。这些信息一般都比较简短。为了在我的数据中保留这种真实的对话感觉，我进行了一些特别的处理。 利用 Telegram 的内置导出功能，我导出了所有的聊天记录为 JSON 格式。经过筛选和将消息编组成不同的会话后，我汇总了过去五年使用 Telegram 的数据。这包括 15,789 个会话，来自 466 个聊天群，平均每个会话有 8.51 条信息。在数据结构上，我选用了 ChatML 格式作为参考。下面是一个例子，展示了一个从俄语翻译过来的会话： 我的数据整合工具只在收到某人的回应时计算损失值。预测下一个发言者相对简单，我们不想让模型专注于这个学习点。因此，对话中涉及损失计算的部分会加粗显示。 你会发现，不仅仅是我的回应，其他人的回应也被用来计算损失值。这样做是有意为之。通过这种方式，模型不仅能扮演我，还能模仿我常交流的朋友们！ 评估计划 我将通过两种方式来测试模型。首先，让模型扮演我，然后我会从我不同朋友的视角与自己对话。接着，我会以自己的身份聊天，让模型扮演我的朋友。我的开场白总是相同的两条信息：“hey”和“what’s up?”（俄语：“прив”和“как дела?”）。模型扮演的角色和生成的短语会被突出显示。所有对话最初将使用俄语，并可通过点击“原始”详细信息按钮查看。测试时，我会使用 oobabooga/text-generation-webui。 首先，我想看看没有我事先训练的情况下，通用对话微调的 Mistral 模型如何处理这个任务。 朋友 1 对 亚历山大·斯米尔诺夫 亚历山大·斯米尔诺夫 对 朋友 1 原文 好的，它能够组织出连贯的句子。最大的问题是它对对话背景的理解不足，导致回答显得单调和普通。消息缺乏独特的风格，感觉非常基础。另一个问题是，模型的俄语表达能力不强。这是可以预见的，因为这个模型太小，不足以很好地适应英语之外的其他语言。此外，模型过于积极，几乎每句话都以问题结束，这并不符合人们在即时通讯中的真实沟通方式。 让我们试着解决这些问题！ LoRA LoRA 提供了一个既节省时间又节省硬件资源的训练方法。它仅训练了总权重的大约 1%。我采用的是 1024 的序列长度和 8 的批次大小。这个训练在 RTX 3090 显卡上占用了 20GB 显存，进行了三个周期，总共用时 5.5 小时。我在 vast.ai 上进行了训练，GPU 的费用是每小时 0.362，总共花费了0.362，总共花费了 0.362，总共花费了2（不包括做实验和修复错误所花的时间）。 以下是实验结果： Friend 1 对比  Alexander Smirnov Friend 2 对比  Alexander Smirnov Friend 3 对比  Alexander Smirnov Alexander Smirnov 对比  Friend 1 Alexander Smirnov 对比  Friend 2 Alexander Smirnov 对比  Friend 3 这个效果显著提升。它不仅捕捉到了它所回应的人的风格，还能识别出特定两人间最常讨论的主题。比如，与 Friend 2 的对话更多聚焦在工作上。但是，其语法仍存在问题，并且很快就会偏离对话的主题。我相信在英语环境中 LoRA 可以达到合理的质量，可能不需要完整的微调。但考虑到俄语并非模型的原生语言，我们接下来尝试完整的微调。 完整精调 (Full fine-tuning) 由于涉及多 GPU 训练，完整精调过程颇具挑战。常用的方法包括使用ZeRO &amp; DeepSpeed 3] 或 FSDP 4]，其中 FSDP 实际上是基于 ZeRO3 5] 的。我选择了 FSDP 方法。 在构建训练流程时，我借鉴了 斯坦福大学 Alpaca 精调代码 和 Anton Bacaj 的 Mistral 精调代码。 我使用半精度的 FSDP 全分片模式，设置序列长度为 1024，微批大小为 2，在每个 80 GB A100 GPU 上需用到 63GB VRAM。这样的训练持续了三个时期，仅需 20 分钟。虚拟机的总费用为每小时 8.88 美元，实际花费为 3 美元，不包括实验和调试的时间。 对话： 朋友 1 vs Alexander Smirnov 朋友 2 vs Alexander Smirnov 朋友 3 vs Alexander Smirnov Alexander Smirnov vs 朋友 1 Alexander Smirnov vs 朋友 2 Alexander Smirnov vs 朋友 3 尽管对话变得更加生动有趣，但仍存在丢失上下文的可能性。俄语表现有所改善，但仍存在一些错误。我认为，在进行特定任务的精调（如我的情况，数据量有限）之前，最好先在大量俄语文本上进行无监督的模型精调。此外，把常见对话伙伴的名字作为单独的 Token 加入可能会进一步提高对话质量。 我认为它与 LoRA 相比并没有显著的优势。专注于分析单一人物的回应，仅根据这些回应来计算损失可能会更有效，而不是尝试去理解每一个对话对象。 结束感想 当然，在展示结果时我进行了精挑细选。这并非因为模型的大部分回复不合适，而是因为许多回复过于简单，如“我稍后给你打电话”、“我很忙”和“好的”，这些在对话中非常常见。尽管如此，这个模型在模仿它所模拟人物的风格方面表现出色。它还能捕捉到两个人之间常讨论的主题。然而，它在对话的上下文理解上有明显不足。在没有充分上下文的情况下，回应像“嘿，怎么样？”或“周末有什么计划？”这样的问题显然是挑战性的。或许利用像 Rewind 这样的系统，它可以捕捉用户在电脑上的所有活动，这可能会有所帮助。 代码 你可以在我的 GitHub 仓库中找到这个项目的代码，以及如何在你自己的 Telegram 转储上实现它的详细说明 my github repo。你还可以在 WandB 上查看训练日志。 🧭 文中关联 AI 工具官网与参数卡（语宙 AI 导航）Telegram ↗Telegram 的核心是一个以安全、速度和云同步为基础的跨平台即时通讯应用。它提供了端到端加密的私密聊天、可自❓ 常见问题解答（FAQ）Telegram 消息可以直接拿来微调大模型吗？不能直接用，通常要先清洗、去噪并统一成对话格式。原始聊天里会混有表情、口语、重复内容和无关片段，若不处理会影响训练质量。用聊天记录微调模型最重要的是什么？最重要的是数据质量和一致性。需要保留有价值的上下文、统一角色标记，并尽量剔除无意义内容。比起单纯增加数量，干净且风格一致的语料更有效。这种微调适合做什么场景？更适合做个性化对话风格、特定语气模仿或小范围知识表达优化，不适合替代通用能力训练。若语料来自个人聊天，模型更容易学到说话习惯而非通用知识。🔬 语宙深潜追问 · 进阶实操拆解Telegram 消息怎么转成可训练语料？先按会话切分，再统一角色标注、时间顺序和上下文窗口，去掉转发、链接和无意义寒暄。最好保留少量高价值上下文，让模型学到你的语气、节奏和回复偏好。个人风格微调最容易失败在哪？最大问题不是模型不够大，而是数据太杂、太少或标签不一致。若样本里混入大量无关主题，模型会学到“泛聊天”而不是你的风格，甚至出现过拟合和复读。🗳️ 读者观点微投票 · 您的实战立场是？用聊天记录微调模型，最该先清洗什么？先删噪声脏数据投票先统一对话格式投票先看样本量够不够投票🕸️ 语宙向量知识图谱 · 语义强关联深度内链🧬 所属权威专题：RAG 知识库与模型微调 →TeleChat2-115B – 中国电信AI研究院推出的开源星辰语义大模型基于大语言模型的智能代理[译]微软开源UserLM-8b：用AI模拟真实用户，颠覆对话模型评测与数据生成利用语言模型模拟观众，让人际沟通更上一层楼 [译] 🏛️ 宝玉的分享 · 前沿大模型翻译与提示词工程实战分享专栏，聚焦海外一手 AI 技术动态与工程实践。📄 Markdown 纯净版 💡 这篇内容对您有启发吗？👍 干货满满🔍 期待更深拆解📡 RSS 订阅✨ 提交 AI 工具 / 合作 🚀 Prompt 语宙 AI 创作者生态：探索 2,800+ 全球实用 AI 工具与高精度生图提示词咒语库🧭 语宙 AI 导航 (ai.paooo.com)🎨 AI 生图 Prompt 库📚 核心专题导航⚡ AIGC 早报生成器
