第五步 - RAG 模式介绍
在这一步中,我们将介绍 RAG 模式,并在我们的 AI 服务中实现它。RAG(检索增强生成)模式是一种扩展 AI 服务中使用的 LLM 知识的方法。
实际上,LLM 是在非常大的数据集上训练的。但是这个数据集是通用的,不包含关于您的公司、您的专业领域或任何可能频繁更改的信息的特定信息。RAG 模式允许您向 LLM 添加知识库。
RAG 模式由两部分组成
- 摄取:这是将数据存储在知识库中的部分。
- 增强:这是将检索到的信息添加到 LLM 输入的部分。
我们将在接下来的步骤中看到这两部分,但首先让我们使用 EasyRag{:target=”blank”} 开始并了解 RAG 模式。 EasyRag 抽象了实现 RAG 模式的大部分复杂性。基本上,您将数据放入配置的目录中,然后就完成了!
如果您想查看此步骤的最终结果,可以查看 step-05
目录。
添加 Easy Rag 依赖
首先,我们需要将 EasyRag 依赖项添加到我们的项目中。将以下依赖项添加到您的 pom.xml
文件
<dependency>
<groupId>io.quarkiverse.langchain4j</groupId>
<artifactId>quarkus-langchain4j-easy-rag</artifactId>
</dependency>
重新加载
如果您的应用程序以开发模式运行,它将使用新的依赖项自动重新启动。
添加一些数据
RAG 模式允许使用您自己的数据扩展 LLM 知识。所以,让我们添加一些数据。
在 src/main/resources
目录中创建一个名为 rag
的目录。然后,在 rag
目录中创建一个名为 miles-of-smiles-terms-of-use.txt
的文件,内容如下
Miles of Smiles Car Rental Services Terms of Use
1. Introduction
These Terms of Service (“Terms”) govern the access or use by you, an individual, from within any country in the world, of applications, websites, content, products, and services (“Services”) made available by Miles of Smiles Car Rental Services, a company registered in the United States of America.
2. The Services
Miles of Smiles rents out vehicles to the end user. We reserve the right to temporarily or permanently discontinue the Services at any time and are not liable for any modification, suspension or discontinuation of the Services.
3. Bookings
3.1 Users may make a booking through our website or mobile application.
3.2 You must provide accurate, current and complete information during the reservation process. You are responsible for all charges incurred under your account.
3.3 All bookings are subject to vehicle availability.
4. Cancellation Policy
4.1 Reservations can be cancelled up to 11 days prior to the start of the booking period.
4.2 If the booking period is less than 4 days, cancellations are not permitted.
5. Use of Vehicle
5.1 All cars rented from Miles of Smiles must not be used:
for any illegal purpose or in connection with any criminal offense.
for teaching someone to drive.
in any race, rally or contest.
while under the influence of alcohol or drugs.
6. Liability
6.1 Users will be held liable for any damage, loss, or theft that occurs during the rental period.
6.2 We do not accept liability for any indirect or consequential loss, damage, or expense including but not limited to loss of profits.
7. Governing Law
These terms will be governed by and construed in accordance with the laws of the United States of America, and any disputes relating to these terms will be subject to the exclusive jurisdiction of the courts of United States.
8. Changes to These Terms
We may revise these terms of use at any time by amending this page. You are expected to check this page from time to time to take notice of any changes we made.
9. Acceptance of These Terms
By using the Services, you acknowledge that you have read and understand these Terms and agree to be bound by them.
If you do not agree to these Terms, please do not use or access our Services.
step-05/src/main/resources/rag
目录复制 miles-of-smiles-terms-of-use.txt
文件。请注意,我们正在添加一个文件,但您可以在 rag
目录中添加任意数量的文件。此外,它不仅限于文本文件,您可以使用 PDF、Word 或任何其他格式。有关更多信息,请参阅 EasyRag 文档。
配置 EasyRag
现在我们有了一些数据,我们需要配置 EasyRag 来摄取它。在 src/main/resources/application.properties
文件中,添加以下配置:
quarkus.langchain4j.easy-rag.path=src/main/resources/rag
quarkus.langchain4j.easy-rag.max-segment-size=100
quarkus.langchain4j.easy-rag.max-overlap-size=25
quarkus.langchain4j.easy-rag.max-results=3
让我们看一下配置
quarkus.langchain4j.easy-rag.path
:包含数据文件的目录的路径。quarkus.langchain4j.easy-rag.max-segment-size
:段中的最大令牌数。实际上,每个文档都被分成段(块)以被 LLM 摄取。此参数定义了段中的最大令牌数。quarkus.langchain4j.easy-rag.max-overlap-size
:两个段之间重叠的最大令牌数。因此,每个段都与前一个段重叠此令牌数。这允许 LLM 在两个段之间具有上下文。quarkus.langchain4j.easy-rag.max-results
:查询知识库时返回的最大结果数。
测试 RAG 模式
让我们测试 RAG 模式。确保应用程序正在运行并在 https://:8080 打开浏览器。
摄取和嵌入
当您启动应用程序时,您应该在日志中看到以下行
INFO [io.qua.lan.eas.run.EasyRagIngestor] (Quarkus Main Thread) Ingesting documents from path: src/main/resources/rag, path matcher = glob:**, recursive = true
INFO [io.qua.lan.eas.run.EasyRagIngestor] (Quarkus Main Thread) Ingested 1 files as 8 documents
来自 rag
目录的数据正在被摄取。从配置的目录读取文件,分成段,并存储在知识库中。在我们的例子中,知识库是内存中的。我们将在接下来的步骤中看到如何使用持久知识库。
这些段不是按原样存储在知识库中的。它们被转换为向量,也称为嵌入。这是一种以数字形式表示文本的方式。因此,在知识库中,我们有文本和相应的嵌入。这些嵌入是使用嵌入模型计算的。现在,我们使用 OpenAI 提供的默认嵌入模型。我们将在接下来的步骤中看到如何使用您自己的嵌入模型。
让我们看看我们的知识库的内容。在 https://:8080/q/dev-ui 打开浏览器。这是 Quarkus Dev UI,包含开发 Quarkus 应用程序所需的一切的工具箱。找到LangChain4j 磁贴,然后单击嵌入存储链接:
然后,查找搜索相关嵌入
部分。在 搜索文本
字段中输入查询,例如 Cancellation
,然后单击 搜索
按钮
您应该会看到接近搜索文本的段。您可以可视化这些段,以及它们的分数,即它们与搜索文本的接近程度。
为了找到相关的段,它计算搜索文本的嵌入,并将它们与段的嵌入进行比较。它使用距离计算(如 余弦相似度)应用相似性搜索。嵌入越接近,分数越高。
增强
现在让我们回到我们的聊天机器人并测试 RAG 模式。在 https://:8080 打开浏览器。提出一个与使用条款相关的问题:
正如您所看到的,AI 能够回答问题,并使用来自知识库的相关段。
让我们看一下日志。您应该看到以下行
{
"role" : "user",
"content" : "What can you tell me about your cancellation policy?\n\nAnswer using the following information:\nYou are responsible for all charges incurred under your account.\n\n3.3 All bookings are subject to vehicle availability.\n\n4. Cancellation Policy\n4.1 Reservations can be cancelled up to 11 days prior to the start of the booking period.\n4.2 If the booking period is less than 4 days, cancellations are not permitted.\n\n4.2 If the booking period is less than 4 days, cancellations are not permitted.\n\n5. Use of Vehicle\n5.1 All cars rented from Miles of Smiles must not be used:\nfor any illegal purpose or in connection with any criminal offense.\nfor teaching someone to drive.\nin any race, rally or contest.\nwhile under the influence of alcohol or drugs.\n\n3. Bookings\n3.1 Users may make a booking through our website or mobile application.\n3.2 You must provide accurate, current and complete information during the reservation process. You are responsible for all charges incurred under your account.\n3.3 All bookings are subject to vehicle availability."
}
content
以用户查询开始,但随后 AI 服务添加了来自知识库的相关段。它使用相关信息扩展了提示。这是 RAG 模式的增强部分。LLM 接收到扩展的提示,并且可以提供更准确的响应。
结论
在这一步中,我们介绍了 RAG 模式,并在我们的 AI 服务中实现了它。我们使用了 EasyRAG 来简化设置。在下一步中,我们将开始解构 RAG 模式,以了解其工作原理以及如何自定义它。