跳到主要内容

MLflow 与 LangChain 的 RAG 入门

下载此笔记本

教程概述

欢迎来到本教程,我们将探讨检索增强生成 (RAG) 与 MLflow 和 LangChain 的集成。我们的重点是演示如何创建高级 RAG 系统,并展示 MLflow 在这些应用中提供的独特功能。

理解 RAG 以及如何使用 MLflow 开发 RAG

检索增强生成 (RAG) 将语言模型的生成能力与信息检索相结合,使语言模型能够访问和整合外部数据。这种方法极大地丰富了模型响应,提供了详细且针对特定上下文的信息。

MLflow 在此过程中发挥着关键作用。作为一个开源平台,它有助于复杂模型的日志记录、跟踪和部署,包括 RAG 链。有了 MLflow,集成 LangChain 变得更加顺畅,从而增强了 RAG 模型的开发、评估和部署过程。

注意:在本教程中,我们将使用 GPT-3.5 作为基础语言模型。需要注意的是,RAG 系统获得的响应结果将与直接与 GPT 模型交互获得的结果不同。RAG 将外部数据检索与语言模型生成相结合的独特方法,能够产生更细致、更具上下文丰富性的响应。

学习目标

在本教程结束时,您将学到:

  • 如何使用 LangChain 和 MLflow 建立 RAG 链。
  • 抓取和处理文档以供 RAG 系统使用的技术。
  • 部署和使用 RAG 模型回答复杂查询的最佳实践。
  • 理解使用 RAG 与直接语言模型交互相比,在实际应用中的影响和响应差异。

设置我们的检索器依赖项

为了存储我们经过验证的数据(我们将检索的信息),我们将使用向量数据库。我们选择使用的框架(由于其简单性、功能性和免费使用的特性)是来自 **Meta** 的 FAISS。

本教程的 FAISS 安装

理解 FAISS

在本教程中,我们将使用 FAISS(Facebook AI Similarity Search,由 Meta AI 研究团队开发和维护),这是一个高效的相似性搜索和聚类库。它是一个非常有用的库,可以轻松处理大型数据集,并能够执行最近邻搜索等操作,这对于检索增强生成 (RAG) 系统至关重要。有许多其他向量数据库解决方案可以执行类似的功能;在本教程中,我们使用 FAISS 是因为它的简单性、易用性和出色的性能。

Notebook 兼容性

随着像 langchain 这样快速发展的库,示例会很快过时并且不再起作用。为了演示的目的,以下是建议使用的关键依赖项,以有效地运行此笔记本:

软件包版本
langchain0.1.16
lanchain-community0.0.33
langchain-openai0.0.8
openai1.12.0
tiktoken0.6.0
mlflow2.12.1
faiss-cpu1.7.4

如果您尝试使用不同的版本执行此 Notebook,它可能可以正常运行,但建议使用上述精确版本,以确保您的代码正确执行。

安装要求

在继续本教程之前,请确保您已通过 pip 安装了 FAISS 和 Beautiful Soup。其他软件包的版本说明符保证与此笔记本兼容。这些软件包的其他版本可能由于其 API 的重大更改而无法正常工作。

    pip install beautifulsoup4 faiss-cpu==1.7.4 langchain==0.1.16 langchain-community==0.0.33 langchain-openai==0.0.8 openai==1.12.0 tiktoken==0.6.0

注意:如果您想使用 GPU 运行此程序,可以安装 faiss-gpu

import os
import shutil
import tempfile

import requests
from bs4 import BeautifulSoup
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain_openai import OpenAI, OpenAIEmbeddings

import mlflow

assert "OPENAI_API_KEY" in os.environ, "Please set the OPENAI_API_KEY environment variable."

注意:如果您想在 LangChain 中使用 Azure OpenAI,则需要安装 openai>=1.10.0langchain-openai>=0.0.6,并指定以下凭据和参数:

from langchain_openai import AzureOpenAI, AzureOpenAIEmbeddings

# Set this to `azure`
os.environ["OPENAI_API_TYPE"] = "azure"
# The API version you want to use: set this to `2023-05-15` for the released version.
os.environ["OPENAI_API_VERSION"] = "2023-05-15"
assert "AZURE_OPENAI_ENDPOINT" in os.environ, (
"Please set the AZURE_OPENAI_ENDPOINT environment variable. It is the base URL for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource."
)
assert "OPENAI_API_KEY" in os.environ, (
"Please set the OPENAI_API_KEY environment variable. It is the API key for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource."
)

azure_openai_llm = AzureOpenAI(
deployment_name="<your-deployment-name>",
model_name="gpt-4o-mini",
)
azure_openai_embeddings = AzureOpenAIEmbeddings(
azure_deployment="<your-deployment-name>",
)

抓取联邦文件以供 RAG 处理

在本教程的这一部分,我们将演示如何从联邦文件网页抓取内容以供我们的 RAG 系统使用。我们将专注于提取网页特定部分的文本,然后将这些文本用于我们的检索增强生成 (RAG) 模型。此过程对于为 RAG 系统提供相关的外部数据至关重要。

函数概述

  • fetch_federal_document 函数旨在抓取并返回特定联邦文件的文本。
  • 它接受两个参数:url(网页 URL)和 div_class(包含文本的 div 元素的类名)。
  • 该函数处理 Web 请求,解析 HTML 内容,并提取所需的文本。

此步骤对于构建依赖于外部、特定上下文数据的 RAG 系统是不可或缺的。通过有效地获取和处理这些数据,我们可以使用直接来自权威文件的准确信息来丰富模型的响应。

注意:在实际场景中,您的特定文本数据将存储在某个磁盘位置(本地或云提供商),并将嵌入式数据加载到向量搜索数据库的过程将完全独立于下面显示的活动抓取。我们仅为演示目的在此处展示整个过程,以展示与 RAG 模型交互的完整端到端工作流程。

def fetch_federal_document(url, div_class):
"""
Scrapes the transcript of the Act Establishing Yellowstone National Park from the given URL.

Args:
url (str): URL of the webpage to scrape.

Returns:
str: The transcript text of the Act.
"""
# Sending a request to the URL
response = requests.get(url)
if response.status_code == 200:
# Parsing the HTML content of the page
soup = BeautifulSoup(response.text, "html.parser")

# Finding the transcript section by its HTML structure
transcript_section = soup.find("div", class_=div_class)
if transcript_section:
transcript_text = transcript_section.get_text(separator="
", strip=True)
return transcript_text
else:
return "Transcript section not found."
else:
return f"Failed to retrieve the webpage. Status code: {response.status_code}"

文档抓取和 FAISS 数据库创建

在接下来的部分,我们将重点介绍两个关键流程:

  1. 文档抓取:

    • 我们使用 fetch_and_save_documents 从指定的 URL 检索文档。
    • 此函数以 URL 列表和文件路径作为输入。
    • 从 URL 抓取的每个文档都会被附加到给定路径下的单个文件中。
  2. FAISS 数据库创建:

    • create_faiss_database 负责从上一步保存的文档创建 FAISS 数据库。
    • 该函数利用 TextLoader 加载文本,CharacterTextSplitter 进行文档拆分,并使用 OpenAIEmbeddings 生成嵌入。
    • 生成的 FAISS 数据库(用于高效相似性搜索)将保存到指定的目录并返回供进一步使用。

这些函数简化了收集相关文档和设置 FAISS 数据库的过程,这对于在 MLflow 中实现高级检索增强生成 (RAG) 应用至关重要。通过模块化这些步骤,我们确保了代码的可重用性和可维护性。

def fetch_and_save_documents(url_list, doc_path):
"""
Fetches documents from given URLs and saves them to a specified file path.

Args:
url_list (list): List of URLs to fetch documents from.
doc_path (str): Path to the file where documents will be saved.
"""
for url in url_list:
document = fetch_federal_document(url, "col-sm-9")
with open(doc_path, "a") as file:
file.write(document)


def create_faiss_database(document_path, database_save_directory, chunk_size=500, chunk_overlap=10):
"""
Creates and saves a FAISS database using documents from the specified file.

Args:
document_path (str): Path to the file containing documents.
database_save_directory (str): Directory where the FAISS database will be saved.
chunk_size (int, optional): Size of each document chunk. Default is 500.
chunk_overlap (int, optional): Overlap between consecutive chunks. Default is 10.

Returns:
FAISS database instance.
"""
# Load documents from the specified file
document_loader = TextLoader(document_path)
raw_documents = document_loader.load()

# Split documents into smaller chunks with specified size and overlap
document_splitter = CharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
document_chunks = document_splitter.split_documents(raw_documents)

# Generate embeddings for each document chunk
embedding_generator = OpenAIEmbeddings()
faiss_database = FAISS.from_documents(document_chunks, embedding_generator)

# Save the FAISS database to the specified directory
faiss_database.save_local(database_save_directory)

return faiss_database

设置工作环境和 FAISS 数据库

本教程的这一部分将介绍我们检索增强生成 (RAG) 应用的设置。我们将建立工作环境并创建必要的 FAISS 数据库。

  1. 临时目录创建:

    • 使用 tempfile.mkdtemp() 创建一个临时目录。此目录将用作存储我们的文档和 FAISS 数据库的工作区。
  2. 文档路径和 FAISS 索引目录:

    • 用于存储抓取的文档和 FAISS 数据库的路径在此临时目录中定义。
  3. 文档抓取:

    • 我们有一个包含需要抓取的文档的 URL 列表 (url_listings)。
    • fetch_and_save_documents 函数用于从这些 URL 检索文档并将其保存到位于 doc_path 的单个文件中。
  4. FAISS 数据库创建:

    • 然后调用 create_faiss_database 函数,使用默认的 chunk_sizechunk_overlap 值,从保存的文档创建 FAISS 数据库。
    • 此数据库 (vector_db) 对于 RAG 过程至关重要,因为它能够对加载的文档进行高效的相似性搜索。

通过此过程,我们将所有文档合并到一个位置,并准备好 FAISS 数据库,以便在 MLflow 启用的 RAG 应用中用于检索。

temporary_directory = tempfile.mkdtemp()

doc_path = os.path.join(temporary_directory, "docs.txt")
persist_dir = os.path.join(temporary_directory, "faiss_index")

url_listings = [
"https://www.archives.gov/milestone-documents/act-establishing-yellowstone-national-park#transcript",
"https://www.archives.gov/milestone-documents/sherman-anti-trust-act#transcript",
]

fetch_and_save_documents(url_listings, doc_path)

vector_db = create_faiss_database(doc_path, persist_dir)

建立 RetrievalQA 链并使用 MLflow 进行日志记录

在这一最终设置阶段,我们将重点放在创建 RetrievalQA 链并将其与 MLflow 集成。

  1. 初始化 RetrievalQA 链:

    • 使用 OpenAI 语言模型 (llm) 和来自先前创建的 FAISS 数据库的检索器 (vector_db.as_retriever()) 初始化 RetrievalQA 链。
    • 此链将使用 OpenAI 模型生成响应,并使用 FAISS 检索器进行基于文档的信息检索。
  2. 用于检索的加载器函数:

    • 定义了一个 load_retriever 函数,用于从指定目录中保存的 FAISS 数据库加载检索器。
    • 此函数在模型以后使用时重新加载检索器至关重要。
  3. 使用 MLflow 记录模型:

    • 使用 mlflow.langchain.log_model 记录 RetrievalQA 链。
    • 此过程包括指定 artifact_path、检索器的 loader_fn 和存储 FAISS 数据库的 persist_dir
    • 使用 MLflow 记录模型可确保其被跟踪并且可以轻松地为将来的使用进行检索。

通过这些步骤,我们成功地将一个复杂的 RAG 应用与 MLflow 集成,展示了其处理高级 NLP 任务的能力。

mlflow.set_experiment("Legal RAG")

retrievalQA = RetrievalQA.from_llm(llm=OpenAI(), retriever=vector_db.as_retriever())


# Log the retrievalQA chain
def load_retriever(persist_directory):
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.load_local(
persist_directory,
embeddings,
allow_dangerous_deserialization=True, # This is required to load the index from MLflow
)
return vectorstore.as_retriever()


with mlflow.start_run() as run:
model_info = mlflow.langchain.log_model(
retrievalQA,
name="retrieval_qa",
loader_fn=load_retriever,
persist_dir=persist_dir,
)

重要提示:为了加载一个已存储的向量存储实例,例如我们上面的 FAISS 实例,我们需要在加载函数中将参数 allow_dangeous_deserialization 设置为 True,以便加载成功。这是因为在 langchain 中引入了用于加载使用 picklecloudpickle 序列化的对象的安全警告。虽然远程代码执行的风险在使用 MLflow 时不存在,因为序列化和反序列化完全通过 API 并在您的环境中进行,但必须设置此参数以防止在加载时抛出异常。

MLflow UI 中的 RAG 应用

Our Model in the UI

测试我们的 RAG 模型

现在模型已存储在 MLflow 中,我们可以将其作为 pyfunc 加载,看看它如何回答关于美国国会法案的一些关键问题。

loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)
def print_formatted_response(response_list, max_line_length=80):
"""
Formats and prints responses with a maximum line length for better readability.

Args:
response_list (list): A list of strings representing responses.
max_line_length (int): Maximum number of characters in a line. Defaults to 80.
"""
for response in response_list:
words = response.split()
line = ""
for word in words:
if len(line) + len(word) + 1 <= max_line_length:
line += word + " "
else:
print(line)
line = word + " "
print(line)

让我们确保这个东西能用

让我们通过发送一个相当简单但故意含糊的问题来试用我们的检索器模型。

answer1 = loaded_model.predict([{"query": "What does the document say about trespassers?"}])

print_formatted_response(answer1)
The document states that all persons who shall locate or settle upon or occupy 
the land reserved for the public park, except as provided, shall be considered 
trespassers and removed from the park.

理解 RetrievalQA 响应

通过此模型,我们的方法将从数据库进行的文本检索与语言模型生成相结合,以回答特定查询。

工作原理:

  • RetrievalQA 模型:此模型是 LangChain 套件的一部分,旨在首先从预定义的数据库中检索相关信息,然后使用语言模型根据此信息生成响应。
  • 数据库集成:在本例中,我们使用《黄石国家公园建立法案》等历史文件创建了一个 FAISS 数据库。此数据库由 RetrievalQA 模型用于查找相关的文本部分。
  • 查询处理:当我们执行 loaded_model.predict([{"query": "What does the document say about trespassers?"}]) 时,模型首先在数据库中搜索与“侵入者”查询最相关的文档部分。

为什么响应不同?

  • 特定上下文的答案:与直接查询 GPT-3.5 不同(GPT-3.5 可能会根据其训练数据生成答案,而没有特定上下文),RetrievalQA 模型提供的响应直接源自数据库中的特定文档。
  • 准确且相关:响应更加准确且与上下文相关,因为它基于正在查询的特定文档的实际内容。
  • 无泛化:响应中的泛化或假设较少。RetrievalQA 模型不是根据其训练进行“猜测”,而是直接提供源自文档的信息。

关键要点:

  • 这种方法证明了 MLflow 和 LangChain 如何实现复杂的 RAG 用例,其中与历史或特定文本的直接交互可获得比通用语言模型预测更精确的答案。
  • 该教程重点介绍了在需要将响应 grounding 在特定文本或文档中的场景中,利用 RAG 的实用性,展示了检索和生成能力的强大结合。

分析马道查询响应

本教程的这一部分展示了 RetrievalQA 模型在处理涉及特定信息检索和额外上下文生成查询方面的有趣方面。

查询和响应细分:

  • 查询:用户问道:“什么是马道?我可以在黄石公园使用它吗?”
  • 响应:模型回答了马道的定义,并根据法案确认可以在黄石公园使用马道。

理解响应动态:

  1. 结合文档数据和 LLM 上下文:

    • 关于马道的查询在建立黄石公园的法案中没有直接回答。
    • 模型利用其语言理解能力提供马道的定义。
    • 然后,它将这些信息与从 FAISS 数据库中检索到的关于该法案(特别是关于公园内道路和马道的建设)的上下文合并。
  2. 增强的上下文理解:

    • RetrievalQA 模型通过其语言模型,能够通过其他上下文补充数据库中的直接信息。
    • 这种方法提供了更全面的答案,与用户的查询一致,显示了文档特定数据和一般知识的融合。
  3. 为何如此值得注意:

    • 与标准的 LLM 响应不同,RetrievalQA 模型不单单依赖其训练数据来获取通用响应。
    • 它有效地整合了特定文档信息和更广泛的上下文理解,提供了更细致的答案。

关键要点:

  • 这个例子强调了 MLflow 和 LangChain 如何通过 RetrievalQA 模型实现复杂的响应机制。该模型不仅检索相关的文档信息,还智能地用其自身的语言理解能力填补空白。
  • 这种响应机制在处理需要特定文档引用和额外上下文信息的查询时特别有用,展示了 RAG 在实际应用中的高级功能。
answer2 = loaded_model.predict(
[{"query": "What is a bridle-path and can I use one at Yellowstone?"}]
)

print_formatted_response(answer2)
A bridle-path is a narrow path or trail designed for horseback riding. Yes, you 
can use a bridle-path at Yellowstone as it is designated for the enjoyment and 
benefit of the people visiting the park. However, it may be subject to certain 
regulations and restrictions set by the Secretary of the Interior.

一个非常严肃的问题

在本教程的这一部分,我们将深入探讨一个异想天开的荒谬查询,以及我们的模型如何以准确性和一丝幽默感来处理它。

查询概述:

  • 查询:“我能从联邦政府购买黄石公园来经营一家以野牛为主题的水疗中心吗?”
  • 响应:模型一本正经地回答:“不行,你不能从联邦政府购买黄石公园来经营一家以野牛为主题的水疗中心。”

一窥模型的思考过程:

  1. 直接且不废话的响应:

    • 尽管查询带有喜剧色彩,但模型给出了直接且明确的答复。就像模型在说:“试试可以,但不行。”
    • 这突显了模型即使面对一个明显更幽默而不是严肃的问题,也能保持事实的能力。
  2. 理解法律界限:

    • 响应尊重国家公园的法律和监管神圣性。看来我们的模型非常重视保护黄石公园这样的国家宝藏!
    • 模型在法律和常识方面的训练有助于提供准确的响应,尽管这个问题有些戏谑。
  3. 与传统 LLM 响应的对比:

    • 传统的 LLM 可能会给出更通用的答案。相比之下,我们的模型配备了特定上下文数据,立即驳斥了购买国家公园用于水疗中心的异想天开的想法。

学习中的一丝幽默:

  • 这个查询虽然荒谬,但它作为一个有趣的例子,展示了模型提供与最离奇问题相关的上下文答案的能力。
  • 它提醒我们,学习可以既有信息量又有趣。在这种情况下,模型扮演着一本正经的喜剧演员的角色,用坚定而滑稽的“不”来回应一个充满想象力的商业提案。

所以,虽然您不能购买黄石公园来实现您的野牛主题水疗中心的梦想,但您当然可以享受公园的自然美景……只是作为一名游客,而不是水疗中心老板!

answer3 = loaded_model.predict(
[
{
"query": "Can I buy Yellowstone from the Federal Government to set up a buffalo-themed day spa?"
}
]
)

print_formatted_response(answer3)
No, you cannot buy Yellowstone from the Federal Government to set up a 
buffalo-themed day spa. The land near the headwaters of the Yellowstone River 
has been reserved and withdrawn from settlement, occupancy, or sale under the 
laws of the United States and dedicated and set apart as a public park for the 
benefit and enjoyment of the people. The Secretary of the Interior has control 
over the park and is responsible for making and publishing rules and 
regulations for its management. Leases for building purposes are only granted 
for small parcels of ground for the accommodation of visitors, and the proceeds 
are used for the management of the park and the construction of roads and 
bridle-paths. Additionally, the wanton destruction of fish and game within the 
park is prohibited. Furthermore, the Act to protect trade and commerce against 
unlawful restraints and monopolies (approved July 2, 1890) states that any 
contract, combination, or conspiracy in restraint of trade or commerce in any 
Territory or the District of Columbia is illegal. Thus, buying land to set up a 
buffalo-themed day spa would likely be considered a violation of this act.

保持冷静:回答另一个异想天开的查询

在本教程的这一部分,我们将探讨另一个关于在黄石公园租赁土地以经营野牛主题水疗中心的有趣问题。让我们看看我们的模型如何以不可动摇的镇定态度回应这个奇怪的询问。

查询和响应:

  • 查询:“我能从联邦政府租用一小块土地,用于为公园游客经营一家小型野牛主题水疗中心吗?”
  • 响应:“不行,你不能从联邦政府租用一小块土地,用于为公园游客经营一家小型野牛主题水疗中心……”

模型响应的见解:

  1. 事实且坚定:

    • 尽管继续进行离谱的提问,我们的模型仍然保持冷静。它耐心地解释了在黄石公园租赁土地的限制和实际用途。
    • 该响应引用了法案第 2 条,为反驳增添了法律上的精确性。
  2. 律师的耐心受到考验?:

    • 想象一下,这个问题如果问的是一位真正的律师。到这时,他们可能会揉太阳穴!但我们的模型却不为所动,继续提供事实答案。
    • 这展示了模型在面对重复和不寻常的查询时,不会失去“冷静”的能力。

总之,虽然我们的模型坚决拒绝了野牛主题水疗中心的梦想,但它这样做是以信息丰富的方式进行的,展示了它无论查询多么富有想象力,都能保持方向的能力。

answer4 = loaded_model.predict(
[
{
"query": "Can I lease a small parcel of land from the Federal Government for a small "
"buffalo-themed day spa for visitors to the park?"
}
]
)

print_formatted_response(answer4)
No, according to the context provided, the Secretary of the Interior may grant 
leases for building purposes for terms not exceeding ten years, but only for 
the accommodation of visitors. It does not specifically mention a 
buffalo-themed day spa as a possible use for the leased land.

再次尝试野牛主题水疗中心之梦

我们再次向模型提出了一个富有想象力的问题,这次将酒店添加到野牛主题水疗中心的情景中。修改此查询的原因是为了评估 RAG 应用是否能够辨别我们加载的两个法案故意模糊措辞的细微差别。让我们看看响应,以确定它是否能解决这两条信息!

快速要点:

  • 模型坚持其信息丰富的性质,根据法案的规定澄清了租赁方面。
  • 它有趣地将查询与另一项关于贸易和商业的法案联系起来,显示了它交叉引用相关法律文件的能力。
  • 此响应展示了模型即使在面对古怪和假设的情景时,也能提供详细、相关信息的能力。
answer5 = loaded_model.predict(
[
{
"query": "Can I lease a small parcel of land from the Federal Government for a small "
"buffalo-themed day spa and hotel for visitors to stay in and relax at while visiting the park?"
}
]
)
print_formatted_response(answer5)
No, it is not possible to lease land from the Federal Government for a 
commercial purpose within the designated park area. The Secretary of the 
Interior may grant leases for building purposes for terms not exceeding ten 
years, but only for small parcels of ground for the accommodation of visitors. 
Additionally, all proceeds from leases and other revenues must be used for the 
management of the park and the construction of roads and bridle-paths.

那么,我还能做什么呢?

要点:

  • 响应令人安心地确认,只要遵守公园规则和规定,人们就可以享受黄石公园的自然美景。
  • 这说明了模型能够响应简单、现实世界的问题,提供直接、实用的建议。它显然拥有原始法案的上下文,并且能够推断出什么被允许(享受保留地)。
answer6 = loaded_model.predict(
[{"query": "Can I just go to the park and peacefully enjoy the natural splendor?"}]
)

print_formatted_response(answer6)
Yes, according to the context, the park was set apart as a public park or 
pleasuring-ground for the benefit and enjoyment of the people. However, the 
park is under the exclusive control of the Secretary of the Interior who has 
the authority to make and publish rules and regulations for the care and 
management of the park. Therefore, it is important to follow any rules or 
regulations set by the Secretary to ensure the preservation and protection of 
the park for future enjoyment.

本教程的这一部分展示了 RetrievalQA 模型集成和解释来自多个法律文档的上下文的复杂能力。模型面临一个需要综合来自不同来源的信息的查询。这次测试特别有趣,因为它展示了模型在以下方面的熟练程度:

  1. 上下文集成:模型熟练地提取不同文档中的相关法律细节,这表明它能够处理多个信息源。

  2. 法律解释:它解释与查询相关的法律含义,这突显了模型对复杂法律语言和概念的理解。

  3. 跨文档推断:模型从多个文档池中辨别和提取最相关信息的能力,证明了其在多文档场景中的高级功能。

这次评估清晰地展示了模型在处理需要深入、细致地理解各种数据源的复杂查询方面的潜力。

answer7 = loaded_model.predict(
[
{
"query": "Can I start a buffalo themed day spa outside of Yellowstone National Park and stifle any competition?"
}
]
)

print_formatted_response(answer7)
No, according to the context, any attempt to monopolize trade or commerce in 
the Yellowstone area or in any territory of the United States is considered 
illegal. Additionally, the park is under the exclusive control of the Secretary 
of the Interior, who may grant leases for building purposes but is expected to 
prevent the destruction of natural resources and the exploitation of fish and 
game for profit. Therefore, it would not be possible to start a buffalo themed 
day spa outside of Yellowstone National Park and stifle competition without 
violating the law and risking consequences from the Secretary of the Interior.

清理:删除临时目录:

  • 在我们问完检索器模型一堆愚蠢的问题后,使用 shutil.rmtree 清理了之前创建的临时目录。
# Clean up our temporary directory that we created with our FAISS instance
shutil.rmtree(temporary_directory)

结论:精通 MLflow 的 RAG

在本教程中,我们深入探讨了由 MLflow 启用和简化的检索增强生成 (RAG) 应用。以下是我们旅程的总结和关键要点:

  1. RAG 应用开发简便性:我们了解到 MLflow 如何通过简化大型语言模型与外部数据源的集成过程来促进 RAG 应用的开发。我们的实践经验展示了在 MLflow 框架内管理文档抓取、处理和嵌入 FAISS 数据库的过程。

  2. 高级查询处理:通过我们的测试,我们观察到 MLflow 包装的 LangChain RAG 模型如何熟练处理复杂查询,从多个文档中提取信息以提供丰富的上下文和准确的响应。这展示了 RAG 模型在处理和理解需要多源数据集成的查询方面的潜力。

  3. MLflow 在部署和管理中的作用:MLflow 的健壮性体现在它如何简化复杂模型的日志记录、部署和管理。它跟踪实验、管理工件和简化部署过程的能力,凸显了它在机器学习生命周期中的不可或缺性。

  4. 实际应用见解:我们的查询虽然有时很幽默,但它们有效地说明了 RAG 模型的实际能力。从法律解释到假设情景,我们看到了这些模型如何应用于现实世界,提供有见地的、与上下文相关的响应。

  5. RAG 和 MLflow 的未来:本教程强调了将 RAG 与 MLflow 的简化管理功能相结合的潜力。随着 RAG 的不断发展,MLflow 作为利用其强大功能的关键工具脱颖而出,使高级 NLP 应用更加易于访问和高效。

总而言之,我们通过本教程的旅程不仅使我们掌握了有效开发和部署 RAG 应用的知识,还为我们打开了在高级 NLP 领域中蕴藏的广阔前景,而 MLflow 使这一切更加触手可及。

下一步是什么?

如果您想了解更多关于 MLflow 和 LangChain 如何集成的信息,请参阅 MLflow LangChain flavor 的其他高级教程