当前位置: 首页 > news >正文

LangChain4j(13)——RAG使用3

本文介绍RAG的一些高级用法(Advanced RAG),很多语法都需要借助检索增强对象RetrievalAugmentor实现。

RetrievalAugmentor是 RAG 管道的入口,它负责对用户提出问题的检索信息进行增强。

下面我们通过一些案例介绍下RAG的高级语法。

查询压缩

查询转换器(QueryTransformer) 将提出的问题进行转换,其目标是通过修改或扩展原始内容来提高检索质量。

如果不做设置,使用的是默认的转换器(DefaultQueryTransformer)。

进行查询压缩,需要借助CompressingQueryTransformer对象实现。

通过查询压缩转换器,大模型将给定的会话和之前的会话压缩到一个独立的会话中 。当用户提出的新问题,需要引用先前问题或答案中信息时,会对问题进行进一步补充完善,通过该方式,提高RAG检索质量。

比如如下会话中:

User: Tell me about John Doe
AI: John Doe was a ...
User: Where did he live?

该会话中,"Where did he live"中的"he"指的是John Doe,如果可以将其转换为"Where did John Doe live",RAG进行内容检索时,检索出的信息将会更加准确。

测试代码

package com.renr.langchain4jnew.app5;import com.renr.langchain4jnew.app4.NaiveRAG;
import com.renr.langchain4jnew.constant.CommonConstants;
import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentParser;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.embedding.onnx.bgesmallenv15q.BgeSmallEnV15QuantizedEmbeddingModel;
import dev.langchain4j.rag.DefaultRetrievalAugmentor;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.rag.query.transformer.CompressingQueryTransformer;
import dev.langchain4j.rag.query.transformer.QueryTransformer;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;public class AdvancedRAG01_QueryCompression {public static void main(String[] args) throws URISyntaxException {// 获取待加载的文档路径URL fileUrl = AdvancedRAG01_QueryCompression.class.getClassLoader().getResource("document/biography-of-john-doe.txt");Path path = Paths.get(fileUrl.toURI());// 指定文档解析器DocumentParser documentParser = new TextDocumentParser();// 加载文档数据Document document = FileSystemDocumentLoader.loadDocument(path, documentParser);EmbeddingModel embeddingModel = new BgeSmallEnV15QuantizedEmbeddingModel();EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder().documentSplitter(DocumentSplitters.recursive(300, 0)).embeddingModel(embeddingModel).embeddingStore(embeddingStore).build();ingestor.ingest(document);ChatLanguageModel chatModel = ZhipuAiChatModel.builder()// 模型key.apiKey(CommonConstants.API_KEY)// 精确度.temperature(0.9).model("GLM-4-Flash").maxRetries(3).callTimeout(Duration.ofSeconds(60)).connectTimeout(Duration.ofSeconds(60)).writeTimeout(Duration.ofSeconds(60)).readTimeout(Duration.ofSeconds(60)).logRequests(true).logResponses(true).build();// 用钱用于查询压缩的转换器QueryTransformer queryTransformer = new CompressingQueryTransformer(chatModel);// 内容检索对象ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder().embeddingStore(embeddingStore).embeddingModel(embeddingModel).maxResults(2).minScore(0.6).build();// 创建检索增强器对象,指定使用的查询压缩转换器RetrievalAugmentor retrievalAugmentor = DefaultRetrievalAugmentor.builder().queryTransformer(queryTransformer).contentRetriever(contentRetriever).build();// 通过retrievalAugmentor属性设置检索增强对象,不再需要设置contentRetriever属性Assistant assistant = AiServices.builder(Assistant.class).chatLanguageModel(chatModel)// .contentRetriever(contentRetriever).retrievalAugmentor(retrievalAugmentor).chatMemory(MessageWindowChatMemory.withMaxMessages(10)).build();String answer = assistant.chat("Tell me about John Doe");System.out.println(answer);String answer2 = assistant.chat("Where did he live?");System.out.println(answer2);}}

执行过程

第一次发送请求:

13:12:12.553 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDUzODY5MzI0NTQsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NTM4NTEzMjQ1NA0KfQ.HMKFRo2H_AgMQdJdnujEuliSA6Nbm6oqCxMpT0nuUzk]
- body: {"model" : "GLM-4-Flash","messages" : [ {"role" : "user","content" : "Tell me about John Doe\n\nAnswer using the following information:\nJohn Doe: An Imaginary Luminary\n\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation."} ],"stream" : false,"temperature" : 0.9,"max_tokens" : 512,"tool_choice" : "auto"
}

通过content,可以看出,这是我们提出的第一个问题。 

第二次发送请求:

13:12:18.377 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDUzODY5MzI0NTQsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NTM4NTEzMjQ1NA0KfQ.HMKFRo2H_AgMQdJdnujEuliSA6Nbm6oqCxMpT0nuUzk]
- body: {"model" : "GLM-4-Flash","messages" : [ {"role" : "user","content" : "Read and understand the conversation between the User and the AI. Then, analyze the new query from the User. Identify all relevant details, terms, and context from both the conversation and the new query. Reformulate this query into a clear, concise, and self-contained format suitable for information retrieval.\n\nConversation:\nUser: Tell me about John Doe\n\nAnswer using the following information:\nJohn Doe: An Imaginary Luminary\n\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\nAI: John Doe, an Imaginary Luminary, is a fictional character whose story begins in the early 1980s. Born on April 1, 1980, in the picturesque town of Fictionville, USA, John was the only child of Jane and Joe Doe. From a young age, John displayed a remarkable talent for creativity and innovation, which would later shape his life's trajectory.\n\nGrowing up in Fictionville, a town known for its serene atmosphere and close-knit community, John was nurtured in an environment that fostered imagination and curiosity. His parents, Jane and Joe Doe, were proud of their son's early inclinations and encouraged him to explore his creative passions.\n\nAs John matured, his education became a cornerstone of his development. He attended the local schools in Fictionville, where he honed his skills and began to lay the groundwork for his future endeavors. It was during these formative years that John's innate creativity and innovative spirit began to take root, setting the stage for his eventual rise to prominence in the world of imagination and innovation.\n\nUser query: Where did he live?\n\nIt is very important that you provide only reformulated query and nothing else! Do not prepend a query with anything!"} ],"stream" : false,"temperature" : 0.9,"max_tokens" : 512,"tool_choice" : "auto"
}

content中的内容比较长,通过content,可以看出这个问题不是我们问的,而是LangChain4j通过上下文,针对下一个问题,给出的思考。

下面我们看看content中的内容:

"Read and understand the conversation between the User and the AI. Then, analyze the new query from the User. Identify all relevant details, terms, and context from both the conversation and the new query. Reformulate this query into a clear, concise, and self-contained format suitable for information retrieval.\n\n

翻译:阅读并理解用户与 AI 之间的对话。然后,分析来自 User 的新查询。从对话和新查询中识别所有相关的详细信息、术语和上下文。将此查询重新表述为适合信息检索的清晰、简洁且自包含的格式。

下面是将上下文信息也放入提示词:

Conversation:\n

User: Tell me about John Doe\n\n

Answer using the following information:\nJohn Doe: An Imaginary Luminary\n\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\nAI: John Doe, an Imaginary Luminary, is a fictional character whose story begins in the early 1980s. Born on April 1, 1980, in the picturesque town of Fictionville, USA, John was the only child of Jane and Joe Doe. From a young age, John displayed a remarkable talent for creativity and innovation, which would later shape his life's trajectory.\n\nGrowing up in Fictionville, a town known for its serene atmosphere and close-knit community, John was nurtured in an environment that fostered imagination and curiosity. His parents, Jane and Joe Doe, were proud of their son's early inclinations and encouraged him to explore his creative passions.\n\nAs John matured, his education became a cornerstone of his development. He attended the local schools in Fictionville, where he honed his skills and began to lay the groundwork for his future endeavors. It was during these formative years that John's innate creativity and innovative spirit began to take root, setting the stage for his eventual rise to prominence in the world of imagination and innovation.\n\n

content中还包含下一个问题

User query: Where did he live?\n\n

It is very important that you provide only reformulated query and nothing else! Do not prepend a query with anything!"
翻译:请务必仅提供重新构建的 query,而不提供任何其他内容!不要在查询前面加上任何内容!

第二次请求的响应:

{"choices": [{"finish_reason": "stop","index": 0,"message": {"content": "John Doe's place of residence throughout his life.","role": "assistant"}}],"created": 1745385140,"id": "202504231312195e711186c3f34537","model": "GLM-4-Flash","request_id": "202504231312195e711186c3f34537","usage": {"completion_tokens": 12,"prompt_tokens": 384,"total_tokens": 396}
}

该响应中,大模型将我们下一个问题,进行了转换,转换为:John Doe's place of residence throughout his life.

第三次请求:

13:12:18.983 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDUzODY5MzgzNzUsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NTM4NTEzODM3NQ0KfQ.RlbvjNPx71uKtwQs7-U2iHc2vUT89R66XblGOkeZTlU]
- body: {"model" : "GLM-4-Flash","messages" : [ {"role" : "user","content" : "Tell me about John Doe\n\nAnswer using the following information:\nJohn Doe: An Imaginary Luminary\n\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation."}, {"role" : "assistant","content" : "John Doe, an Imaginary Luminary, is a fictional character whose story begins in the early 1980s. Born on April 1, 1980, in the picturesque town of Fictionville, USA, John was the only child of Jane and Joe Doe. From a young age, John displayed a remarkable talent for creativity and innovation, which would later shape his life's trajectory.\n\nGrowing up in Fictionville, a town known for its serene atmosphere and close-knit community, John was nurtured in an environment that fostered imagination and curiosity. His parents, Jane and Joe Doe, were proud of their son's early inclinations and encouraged him to explore his creative passions.\n\nAs John matured, his education became a cornerstone of his development. He attended the local schools in Fictionville, where he honed his skills and began to lay the groundwork for his future endeavors. It was during these formative years that John's innate creativity and innovative spirit began to take root, setting the stage for his eventual rise to prominence in the world of imagination and innovation."}, {"role" : "user","content" : "Where did he live?\n\nAnswer using the following information:\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\n\nJohn Doe: An Imaginary Luminary"} ],"stream" : false,"temperature" : 0.9,"max_tokens" : 512,"tool_choice" : "auto"
}

针对"Where did he live",通过content,可以看出检索的内容如下:

Answer using the following information:\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\n\nJohn Doe: An Imaginary Luminary

第三次请求的响应:

{"choices": [{"finish_reason": "stop","index": 0,"message": {"content": "John Doe, as an Imaginary Luminary, lived in the quaint town of Fictionville, USA. This town served as the backdrop for his early life and education, shaping his formative years and nurturing his early inclination towards creativity and innovation. Being the only child of Jane and Joe Doe, his surroundings in Fictionville would have played a significant role in the development of his personality and talents.","role": "assistant"}}],"created": 1745385142,"id": "2025042313122054c1ad256e0d4150","model": "GLM-4-Flash","request_id": "2025042313122054c1ad256e0d4150","usage": {"completion_tokens": 81,"prompt_tokens": 358,"total_tokens": 439}
}

不使用CompressingQueryTransformer的对比

第一次请求

13:40:04.468 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDUzODg2MDQzNTQsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NTM4NjgwNDM1NA0KfQ.c22g_UY7JkJ2J-_GRGobaEMurph_cu22A4DHyEBNPaA]
- body: {"model" : "GLM-4-Flash","messages" : [ {"role" : "user","content" : "Tell me about John Doe\n\nAnswer using the following information:\nJohn Doe: An Imaginary Luminary\n\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation."} ],"stream" : false,"temperature" : 0.9,"max_tokens" : 512,"tool_choice" : "auto"
}

第二次请求

13:40:11.970 [main] DEBUG dev.langchain4j.community.model.zhipu.RequestLoggingInterceptor - Request:
- method: POST
- url: https://open.bigmodel.cn/api/paas/v4/chat/completions
- headers: [Authorization: Bearer ey...fQ.ew0KICAiZXhwIiA6IDE3NDUzODg2MDQzNTQsDQogICJhcGlfa2V5IiA6ICJkMjMzNGIyZTk4NTE0YWUwOTIzMDQ4YTM4NDEzNTMwNiIsDQogICJ0aW1lc3RhbXAiIDogMTc0NTM4NjgwNDM1NA0KfQ.c22g_UY7JkJ2J-_GRGobaEMurph_cu22A4DHyEBNPaA]
- body: {"model" : "GLM-4-Flash","messages" : [ {"role" : "user","content" : "Tell me about John Doe\n\nAnswer using the following information:\nJohn Doe: An Imaginary Luminary\n\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation."}, {"role" : "assistant","content" : "John Doe, referred to as an Imaginary Luminary, is a character shrouded in mystery and creativity. He was born on April 1, 1980, in the picturesque and whimsical town of Fictionville, USA. As the only child of Jane and Joe Doe, young John's early life was marked by his evident affinity for creative expression and innovative thinking.\n\nFrom a young age, it was apparent that John had a distinct taste for the unusual, and this curiosity translated into a profound love for the arts. As he grew up in the close-knit community of Fictionville, John absorbed the town's idyllic charm, which likely influenced the direction of his life and work. While details about his educational background are not specified, it can be inferred that he would have nurtured his nascent creative spark and probably engaged in activities that fostered imagination and innovation. Through his formative years, John Doe began to carve his path as a luminary whose work and presence would one day capture the imagination of others, even though his story remains largely hypothetical and unverified."}, {"role" : "user","content" : "Where did he live?\n\nAnswer using the following information:\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\n\nIn his personal life, John is known to be a private individual.\nHe married his college sweetheart, Emily, and they have two children.\nThe family resides in a sustainable home in Fictionville, where Doe enjoys gardening and mentoring young entrepreneurs."} ],"stream" : false,"temperature" : 0.9,"max_tokens" : 512,"tool_choice" : "auto"
}

第二次请求的响应

{"choices": [{"finish_reason": "stop","index": 0,"message": {"content": "John Doe lived in the quaint town of Fictionville, USA. This town, steeped in charm and likely reflective of a nurturing environment, was where he was born on April 1, 1980. As he grew, John continued to reside in Fictionville, where he married his college sweetheart, Emily. The couple has two children, and their family now resides in a sustainable home within the same town. John's deep connection to Fictionville is evidenced by his enjoyment of activities such as gardening and mentoring young entrepreneurs, further indicating his commitment to the local community and his desire to contribute positively to the environment and the next generation.","role": "assistant"}}],"created": 1745386816,"id": "20250423134013be0fab7819a24647","model": "GLM-4-Flash","request_id": "20250423134013be0fab7819a24647","usage": {"completion_tokens": 129,"prompt_tokens": 402,"total_tokens": 531}
}

两次检索的对比

使用CompressingQueryTransformer:

Where did he live?\n\nAnswer using the following information:\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\n\nJohn Doe: An Imaginary Luminary

不使用 CompressingQueryTransformer:

Where did he live?\n\nAnswer using the following information:\nEarly Life and Education\nJohn Doe was born on April 1, 1980, in the quaint town of Fictionville, USA.\nThe only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation.\n\nIn his personal life, John is known to be a private individual.\nHe married his college sweetheart, Emily, and they have two children.\nThe family resides in a sustainable home in Fictionville, where Doe enjoys gardening and mentoring young entrepreneurs."

对比发现,使用CompressingQueryTransformer后,检索的结果相对更准确,占用token也少。

两次最终结果的对比

使用CompressingQueryTransformer:

John Doe, as an Imaginary Luminary, lived in the quaint town of Fictionville, USA. This town served as the backdrop for his early life and education, shaping his formative years and nurturing his early inclination towards creativity and innovation. Being the only child of Jane and Joe Doe, his surroundings in Fictionville would have played a significant role in the development of his personality and talents.

不使用 CompressingQueryTransformer:

John Doe lived in the quaint town of Fictionville, USA. This town, steeped in charm and likely reflective of a nurturing environment, was where he was born on April 1, 1980. As he grew, John continued to reside in Fictionville, where he married his college sweetheart, Emily. The couple has two children, and their family now resides in a sustainable home within the same town. John's deep connection to Fictionville is evidenced by his enjoyment of activities such as gardening and mentoring young entrepreneurs, further indicating his commitment to the local community and his desire to contribute positively to the environment and the next generation.

对比发现,使用CompressingQueryTransformer后,返回的结果即符合需求,占用token也少。

相关文章:

  • vscode+cmake高阶使用
  • SimVG论文精读
  • php基础
  • Nacos自动刷新配置源码解析
  • Ubuntu22.04新版本谷歌无法使用搜狗输入法/中文不显示
  • 组织级项目管理OPM
  • Spark 集群搭建:Standalone 模式详解
  • 天梯赛补题
  • Kafka 详解
  • Qt creator 16.0.1 语言家失效解决方法
  • 使用 VSCode 编写 Markdown 文件
  • Vscode已经打开的python项目,如何使用已经建立的虚拟环境
  • 局部最小实验--用最小成本确保方向正确
  • 信息学奥赛一本通 1505:【例 2】双调路径 | 洛谷 P5530 [BalticOI 2002] 双调路径
  • 03-谷粒商城笔记
  • MongoDB(docker版)备份还原
  • 八大排序——选择排序/堆排序
  • Android APP 爬虫操作
  • 海外产能达产,威尔高一季度营收利润双双大增
  • 【k8s】docker、k8s、虚拟机的区别以及使用场景
  • 著名文学评论家、清华大学中文系教授蓝棣之逝世
  • 融入长三角一体化发展,苏南名城镇江的优势和机遇何在
  • 世界读书日丨阅读与行走,都是理解世界的方式
  • 《哪吒2》票房已达157亿,光线传媒一季度净利增至20亿元
  • 41岁知名学者陈昊因病骤然离世,北大再发讣告缅怀
  • 大学2025丨本科专业大调整,教育专家:化解就业难背后供需错配