从“无法找到答案”到“一问一个准”! Contextual Embedding让chunk自带上下文,精准召回,效果立竿见影! 原创

发布于 2025-3-25 10:23
浏览
1收藏

背景

最近,公司的一个项目经理找我聊了个头疼的问题:他们给外部交付的项目POC效果不太理想,他发现从向量库中检索不到想要的信息。起初,我建议他换个更好的embedding模型,别再用​​text-embedding-ada-002​​​了。结果他反馈说,试了​​text-embedding-3-large​​​和​​bge-m3​​,效果也没啥显著提升。

我仔细看了他们的数据,发现他们上传了大量用户的文档,并对文档进行了切分,分成一个个​​chunk​​​,然后召回这些​​chunk​​​送给LLM生成回答。问题就在于他们切分chunk的方式用的是RecursiveCharacterTextSplitter,单独看一个切分后的​​chunk​​​,根本不知道它在讲什么。比如,有个​​chunk​​​提到了​​opening hours​​​,但因为递归切分的原因,缺少了主体信息。结果,即使召回了这个​​chunk​​,LLM也会回复“从提供的上下文中无法找到答案”。

我给了他一个建议:可以试试​​contextual-embedding​​​。引入这个方案不需要太多开发成本,而且配合​​prompt cache​​,还能有效减少LLM调用的开销。

什么是contextual embedding

在传统的RAG中,文档通常被分成更小的块以进行有效的检索。虽然这种方法对于许多应用程序都很有效,但当单个块缺乏足够的上下文时,它可能会导致问题。Contextual Embedding 通过使用LLM给每段chunk补充上下文信息,用户更精准召回和更高质量的回答。

举个简单的例子,比如有段chunk的内容如下:

The company's revenue grew by 3% over the previous quarter.

当我们提问:"What was the revenue growth for ACME Corp in Q2 2023?",虽然这段chunk是真实答案,但是却检索不到。这是因为原始chunk,是两个"The"对象,导致不管使用embedding还是BM25都抓不出来它。但是如果我们通过某种手段,给转换成下面这种contextualized_chunk,把上下文信息给注入到chunk里:

This chunk is from an SEC filing on ACME corp's performance in Q2 2023; the previous quarter's revenue was $314 million. The company's revenue grew by 3% over the previous quarter.

那么还是刚才的问题,就一问一个准了。

原理

从“无法找到答案”到“一问一个准”! Contextual Embedding让chunk自带上下文,精准召回,效果立竿见影!-AI.x社区

我们通过一个特定的提示生成每个分块的简洁上下文,生成的上下文通常为 50-100 个 token,然后 索引之前将其添加到分块中。对应的prompt示例:

system prompt

Here is the whole document: 
<document> 
{{WHOLE_DOCUMENT}} 
</document>

user prompt

Here is the chunk we want to situate within the whole document:
<chunk> 
{{CHUNK_CONTENT}} 
</chunk> 

Please give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk. Answer only with the succinct context and nothing else.

实战案例

这里我们还是以sentosa的一个网页为例:https://www.sentosa.com.sg/en/places-to-stay/amara-sanctuary-sentosa/。

从这个网页中,我们切分出了一段​​chunk​​,内容如下:

description: Bed: King 
Room Size: 37 sqm 
Maximum Occupants: 2 Adults or 2 Adults and 1 child age 11 and below 
Room Essentials  
Flat-screen TV with cable channel access  
Individually controlled air-conditioning  
Spacious bathroom with separate bathtub and shower facilities  
Luxury bathroom amenities  
Bathrobes and hair dryer  
Electronic safe  
Tea and coffee making facilities  
Iron and ironing board  
Baby cot is available on request (subject to availability)

name: Couple Suite

name: Courtyard Suite

name: Junior Suite

name: Verandah Suite

Opening Hours: 
Check in: from 3pm  
Check out: until 12pm

单独看这段​​chunk​​​,我们只能看出它在描述一些房间信息,但具体是哪些房间的信息,却并不清楚。于是,我们使用​​gpt-4o-mini​​​为这段​​chunk​​生成了上下文,结果如下:

This chunk provides detailed information about the room types and amenities available at Amara Sanctuary Sentosa, including the Deluxe Room specifications, other suite options, opening hours, accessibility features, and pet-friendly services, enhancing the overall description of the resort's accommodations.

接下来,我们将原始的​​chunk​​​和生成的上下文结合起来(使用\n\n 连接chunk),形成一个新的​​chunk​​:

description: Bed: King 
Room Size: 37 sqm 
Maximum Occupants: 2 Adults or 2 Adults and 1 child age 11 and below 
Room Essentials  
Flat-screen TV with cable channel access  
Individually controlled air-conditioning  
Spacious bathroom with separate bathtub and shower facilities  
Luxury bathroom amenities  
Bathrobes and hair dryer  
Electronic safe  
Tea and coffee making facilities  
Iron and ironing board  
Baby cot is available on request (subject to availability)

name: Couple Suite

name: Courtyard Suite

name: Junior Suite

name: Verandah Suite




Opening Hours: 
Check in: from 3pm  
Check out: until 12pm


This chunk provides detailed information about the room types and amenities available at Amara Sanctuary Sentosa, including the Deluxe Room specifications, other suite options, opening hours, accessibility features, and pet-friendly services, enhancing the overall description of the resort's accommodations.

这样一来,当我们再询问关于“Amara Sanctuary Sentosa的Deluxe Room”相关问题时,LLM就能准确回答上来了。这种方法不仅提升了信息的连贯性,还大大减少了LLM的误判率。

prompt cache

对于OpenAI模型,当你的提示(prompt)长度超过1,024个token时,API调用将自动受益于Prompt Caching功能。(deepseek也支持prompt cache)如果你重复使用具有相同前缀的提示,系统会自动应用Prompt Caching折扣,而你无需对API集成做任何修改。缓存通常在5-10分钟的不活动后被清除,并且无论如何都会在最后一次使用后的一小时内被移除。

当我们对某个文档进行切分,生成多个​​chunk​​​时,通常需要为每个​​chunk​​生成上下文信息。如果每次调用都传入全部文档信息,会导致重复计算,增加LLM的调用成本。这时,我们可以将全部文档信息放在system prompt中,利用Prompt Cache来节省费用。

以下是我调用LLM的Response中的​​usage​​字段,展示了Prompt Cache的实际效果:

CompletionUsage(
    completion_tokens=24, 
    prompt_tokens=1584, 
    total_tokens=1608, 
    completion_tokens_details=CompletionTokensDetails(
        accepted_prediction_tokens=0, 
        audio_tokens=0, 
        reasoning_tokens=0, 
        rejected_prediction_tokens=0
    ), 
    prompt_tokens_details=PromptTokensDetails(
        audio_tokens=0, 
        cached_tokens=1536  # 这里显示有1,536个token被缓存
    )
)

从上面的数据可以看出:

  • prompt_tokens: 1,584个token被用于提示。
  • cached_tokens: 1,536个token被缓存,这意味着这部分token的计算成本被节省了下来。
  • completion_tokens: 24个token用于生成回答。

通过将文档信息放在system prompt中,我们成功利用Prompt Cache减少了重复计算,显著降低了LLM的调用成本。

总结

传统的文档切分方法(如RecursiveCharacterTextSplitter)可能会导致chunk缺乏足够的上下文信息,从而影响检索效果。通过引入Contextual Embedding,我们能够为每个chunk补充上下文信息,显著提升检索的精准度和回答的质量。

总的来说,Contextual EmbeddingPrompt Cache的结合,为RAG系统提供了一种低成本、高效率的优化方案。尤其是在项目时间紧张、资源有限的情况下,这种方案能够快速提升系统的表现。


本文转载自公众号AI 博物院 作者:longyunfeigu

原文链接:​​https://mp.weixin.qq.com/s/I8muNOkLenngFn9I9U2ZQg​


©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏 1
回复
举报
回复
相关推荐