技术指南

微调嵌入模型以进行检索

微调嵌入模型意味着使用真实查询及其对应的文档对对其进行训练,以便在您的领域中,相关文本在向量空间中的距离比不相关文本更近。

听听这个指南AI叙述 · ~3 min

在本页4 分钟阅读
  1. 概述
  2. 深入探讨
  3. 现实世界的实施
  4. 用于检索的嵌入模型微调的未来
  5. 付诸实践
  6. 不断探索
  7. 常见问题

概述

这之所以重要,是因为检索质量决定了 RAG 质量的上限:如果没有检索到正确的段落,大语言模型就无法使用它,而通用嵌入模型往往会忽略领域词汇、缩写以及用户提问的方式。

深入探讨

An embedding model turns text into a vector so that semantically similar texts have similar vectors, usually measured by cosine similarity. In RAG, both queries and document chunks are embedded, and the nearest chunks are retrieved. General models are trained on broad web and question-answer data. They work reasonably well everywhere but can struggle with specialised terms, internal product names, and the gap between how a user asks and how a document is written. Fine-tuning closes that gap with training data in three forms. Positive pairs are a query and a passage that answers it. In-batch negatives use the other passages in the same training batch as examples of what should score lower. Hard negatives are passages that look relevant, sharing words or topic, but do not answer the query. Hard negatives teach the fine distinctions that matter most, because easy negatives are already separated by the base model. Good data sources include search logs with clicks, support tickets linked to articles, FAQ pages, and synthetic queries generated from your documents by a language model. Synthetic data is useful but should be filtered, since generated questions often copy the document's wording and make the task too easy. A frequent misconception is that fine-tuning is the first fix for poor retrieval. Often better chunking, adding keyword search such as BM25 in a hybrid setup, or adding a reranker gives larger gains with less effort. Fine-tuning is most worthwhile when you have measured a retrieval gap and have or can build at least a few thousand quality pairs. Another pitfall is false negatives: a mined hard negative that actually does answer the query. Training the model to push it away damages quality. Also note that changing the embedding model requires re-embedding the entire document collection, because old and new vectors are not comparable.

现实世界的实施

一家保险公司使用客户问题及回答这些问题的保险条款对来进行嵌入训练,以便“如果我把手机摔了,是否在保修范围内”能够检索到意外损坏条款。

一家软件公司利用其支持工单历史记录,将每个工单的问题与客服在回复中链接的帮助文章进行配对,作为免费的训练数据。

一个法律研究团队通过提取与查询共享关键词但针对不同司法管辖区的条款来挖掘困难负样本,从而教导模型将它们区分开来。

一个制药团队使用大语言模型从内部文档中生成合成问题,过滤掉低质量的问题,并利用这些问题对来训练领域嵌入模型。

用于检索的嵌入模型微调的未来

通用嵌入模型在 MTEB 等公共基准测试中不断改进,这缩小了但并未消除领域适应的好处,因为基准数据很少与私有语料库相匹配。利用大语言模型生成合成数据,使得没有大型标注数据集的团队也能够实际进行微调。预计将继续使用混合管线,其中微调的嵌入模型处理第一阶段召回,而重排器(reranker)处理精度。度量习惯最重要:跟踪真实查询召回率的团队会知道微调何时能带来回报。

付诸实践

为什么这很重要

  • 成本与预算. 多年来,架构决策决定着性能和运营成本。
  • 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
  • 质量控制. 更好的工程选择可以减少生产中的可靠性事故。

留意

  • 优化一项基准测试可以隐藏更广泛的系统弱点。
  • 基础设施和维护成本常常被低估。
  • 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。

后续步骤

  1. 在实施之前定义延迟、质量和成本目标。
  2. 在实际负载和数据条件下进行基准测试。
  3. 仪器监控错误、漂移和用户影响。
  4. 在扩展之前准备回滚和事件响应路径。

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

参加用于检索的嵌入模型微调测验

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

什么是用于检索的嵌入模型微调?

微调嵌入模型意味着使用真实查询及其对应的文档对对其进行训练,以便在您的领域中,相关文本在向量空间中的距离比不相关文本更近。这之所以重要,是因为检索质量决定了 RAG 质量的上限:如果没有检索到正确的段落,大语言模型就无法使用它,而通用嵌入模型往往会忽略领域词汇、缩写以及用户提问的方式。

什么是嵌入微调中的困难负样本?

困难负样本与查询共享词汇或主题但不正确,这教会了模型进行细微的区分。

为什么困难负样本比简单负样本更有用?

简单负样本已经距离查询很远;而困难负样本迫使模型学习微妙的差异。

在此语境下,什么是假阴性?

如果所谓的负样本实际上是相关的,那么训练会将正确答案推开并损害检索效果。

在查询-段落对上训练嵌入模型通常使用哪种损失函数?

对比损失会对批次内负样本和困难负样本的正样本进行评分,从而将正样本推至顶部。

切换到新微调的嵌入模型后,您必须对文档索引做什么?

不同模型的向量不可比,因此必须重新嵌入整个集合。