LangChain系列之如何使用LangChain4j构建RAG应用3
<<<<书接上文
创建内容检索器
在检索增强生成(RAG)应用中,**内容检索器(Content Retriever)**负责根据用户的查询,从数据源中获取最相关的内容。
LangChain4J 提供了对这一过程的抽象,使我们能够将嵌入存储(embedding store)和嵌入模型(embedding model)连接起来,实现高效的内容检索。
具体来说,我们使用
EmbeddingStoreContentRetriever,它通过对用户查询进行嵌入,再在嵌入存储中查找最相关的匹配内容,从而实现精准检索。
package com.mongodb;
public class LangChainRagApp {
public static void main(String[] args) {
try {
// ...
// Content Retriever
ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
.embeddingStore(embeddingStore)
.embeddingModel(embeddingModel)
.maxResults(5)
.minScore(0.75)
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我们来拆解一下这个内容检索器(Content Retriever)的组成部分:
- embeddingStore:这是我们之前搭建的数据语料库,存放了所有文档的向量化表示。
- embeddingModel:这是检索过程的核心“大脑”,即之前用于生成嵌入向量的模型(例如 text-embedding-ada-002)。在这里使用同一个模型,确保用户的查询和存储的内容被映射到相同的“向量语言”空间,从而实现准确匹配。
- maxResults:将 maxResults 设置为 5 意味着检索器会为你的查询返回最多五个最相关的匹配结果。
- minScore:这是你的质量筛选器。通过设置 minScore 为 0.75,我们的意思是“不要显示任何不高度相关的内容。”如果没有结果达到这个阈值,我们将得到一个空列表,而不是杂乱无关的数据。
通过调整这些参数,我们可以微调检索器的表现,确保它准确地提供我们需要的内容!
提问
现在将各个部分组合起来了。我们需要一种方式把所有组件整合起来,以便查询我们增强后的大语言模型。首先,我们需要为我们的Assistant类创建一个接口。请创建如下所示的接口。
package com.mongodb;
public interface Assistant {
String answer(String question);
}
非常简单,我们只想提供一个answer方法并获得答案。接下来,我们需要在主类中创建并调用我们的Assistant。
package com.mongodb;
public class LangChainRagApp {
public static void main(String[] args) {
try {
// ...
// Assistant
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(chatModel)
.contentRetriever(contentRetriever)
.build();
String output = assistant.answer("How to use Atlas Triggers and AI to summarise AirBnB reviews?");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这个实现中,我保持了非常简单的方式,现在,让我们来看一下回复:
To summarise Airbnb reviews using MongoDB Atlas Triggers and OpenAI, follow these steps:
1. **Prerequisites**: Set up an App Services application to link to the cluster with Airbnb data. Also, create an OpenAI account with API access.
2. **Set up Secrets and Values**: In App Services, create a secret named `openAIKey` with your OpenAI API key. Then, create a linked value named `OpenAIKey` and link it to the secret.
3. **Trigger code**: The trigger listens for changes in the sample_airbnb.listingsAndReviews collection. When a new review is detected, it samples up to 50 reviews, sends them to OpenAI's API for summarisation, and updates the original document with the summarised content and tags. The trigger reacts to updates that are marked with the `"process" : false` flag, which indicates that a summary hasn't been created for the batch of reviews yet.
4. **Sample Reviews Function**: To avoid overloading the API with too many reviews, a function called `sampleReviews` is defined that randomly samples up to 50 reviews.
5. **API Interaction**: Using the `context.http.post` method, the API request is sent to the OpenAI API.
6. **Updating the Original Document**: Once a successful response from the API is received, the trigger updates the original document with the summarised content, negative tags (neg_tags), positive tags (pos_tags), and a process flag set to true.
7. **Displaying the Data**: Once the data is added to the documents, it can be displayed in a VUE application by adding an HTML template.
By combining MongoDB Atlas triggers with OpenAI's powerful models, large volumes of reviews can be processed and analysed in real-time. This not only provides concise summaries of reviews but also categorises them into positive and negative tags, offering valuable insights to property hosts and potential renters.
回答内容很丰富,实际上引用了原始教程中的内容:《使用 MongoDB Atlas Triggers 与 OpenAI 总结 Airbnb 评论》。
结论
到这里为止——我们已经使用 MongoDB 和 LangChain4j 创建了一个简单的 RAG 应用。LangChain4j 在整个过程中为我们简化了许多步骤,包括数据分段、连接 MongoDB 数据库以及嵌入模型等。
相关文章
- Java官方宣布:32位系统用户,你们被抛弃了!
- Java二十周年特别策划--谈谈我与Java的那些年、这些事
- Java二十周年特别策划——谈谈我与Java的那些年、这些事
- Java 25 在 JEP 519 中集成了紧凑对象头
- Java动态代理
- JAVA入门教程-第1章 概述
- 那些让你望而却步的Java概念,其实没那么难!
- CBN x ASEAN Watch丨Labubu-mania: The unlikely cultural sensation sweeping Southeast Asia
- CBN Correspondent丨Coffee shirt, methanol bus, 100% green power…Boao goes all-in on zero-carbon push
- CBN丨Policy supports to shore up foreign investors' confidence