LangChain 与 MLflow 使用介绍
欢迎来到本互动教程,旨在向您介绍 LangChain 及其与 MLflow 的集成。本教程以笔记本形式组织,旨在通过 LangChain 最简单、最核心的功能提供动手实践的学习体验。
您将学到什么
- 理解 LangChain:了解 LangChain 的基础知识以及它如何用于开发由语言模型驱动的应用程序。
- LangChain 中的链:探索 LangChain 中
chains
(链)的概念,它们是为执行复杂任务而编排的一系列动作或操作。 - 与 MLflow 集成:学习 LangChain 如何与 MLflow 集成,MLflow 是一个用于管理机器学习生命周期(包括日志、跟踪和部署模型)的平台。
- 实际应用:运用您的知识构建一个 LangChain 链,充当副厨师长,专注于食谱的准备步骤。
LangChain 背景
LangChain 是一个基于 Python 的框架,它简化了使用语言模型开发应用程序的过程。它旨在增强应用程序的上下文感知和推理能力,从而实现更复杂和交互式的功能。
什么是链?
- 链定义:在 LangChain 中,
chain
(链)指的是一系列相互连接的组件或步骤,旨在完成特定任务。 - 链示例:在本教程中,我们将创建一个模拟副厨师长准备食谱食材和工具的链。
教程概述
在本教程中,您将
- 设置 LangChain 和 MLflow:初始化并配置 LangChain 和 MLflow。
- 创建副厨师长链:开发一个 LangChain 链,列出食材、描述准备技术、组织食材摆放,并详细说明给定食谱的烹饪工具准备。
- 日志和加载模型:利用 MLflow 记录链模型,然后加载它进行预测。
- 运行预测:执行链,查看它如何为特定数量的顾客准备餐厅菜肴。
在本教程结束时,您将对 LangChain 与 MLflow 的使用有一个扎实的基础,并了解如何构建和管理链以用于实际应用。
让我们深入探索 LangChain 和 MLflow 的世界!
先决条件
为了开始本教程,我们首先需要一些东西。
- 一个 OpenAI API 账户。您可以在这里注册以获取访问权限,开始以编程方式访问地球上领先的高度复杂的 LLM 服务之一。
- 一个 OpenAI API 密钥。创建账户后,您可以通过导航到API 密钥页面来访问它。
- OpenAI SDK。它在 PyPI 上这里可用。在本教程中,我们将使用 0.28.1 版本(1.0 版本之前的最后一个版本)。
- LangChain 包。您可以在PyPI 上这里找到它。
Notebook 兼容性
对于像 langchain
这样快速变化的库,示例可能会很快过时并且不再起作用。为了演示目的,以下是建议使用的关键依赖项,以有效运行此笔记本。
软件包 | 版本 |
---|---|
langchain | 0.1.16 |
langchain-community | 0.0.33 |
langchain-openai | 0.0.8 |
openai | 1.12.0 |
tiktoken | 0.6.0 |
mlflow | 2.12.1 |
如果您尝试使用不同的版本执行此 Notebook,它可能可以正常运行,但建议使用上述精确版本,以确保您的代码正确执行。
要安装依赖包,只需运行
pip install openai==1.12.0 tiktoken==0.6.0 langchain==0.1.16 langchain-openai==0.0.33 langchain-community==0.0.33 mlflow==2.12.1
注意:本教程不支持 openai<1,并且不保证与 langchain<1.16.0 版本一起使用
API 密钥安全概述
API 密钥,特别是 SaaS 大型语言模型 (LLM) 的 API 密钥,由于与计费相关联,因此与财务信息一样敏感。
如果您有兴趣了解更多关于安全管理访问密钥的替代 MLflow 解决方案,请在此处阅读 MLflow AI Gateway。
基本实践:
- 保密性:始终保持 API 密钥的私密性。
- 安全存储:首选环境变量或安全服务。
- 频繁轮换:定期更新密钥以避免未经授权的访问。
配置 API 密钥
为了安全使用,将 API 密钥设置为环境变量。
macOS/Linux:有关详细说明,请参阅Apple 关于在终端中使用环境变量的指南。
Windows:遵循Microsoft 关于环境变量的文档中概述的步骤。
import os
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_openai import OpenAI
import mlflow
assert "OPENAI_API_KEY" in os.environ, "Please set the OPENAI_API_KEY environment variable."
注意:如果您想将 Azure OpenAI 与 LangChain 结合使用,您需要安装
openai>=1.10.0
和langchain-openai>=0.0.6
,并指定以下凭据和参数
# NOTE: Only run this cell if you are using Azure interfaces with OpenAI. If you have a direct account with
# OpenAI, ignore this cell.
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>",
)
在 LangChain 中配置 OpenAI 补全模型
在本教程的这一部分,我们已经配置了 OpenAI 模型,并设置了适用于生成语言补全的特定参数。我们使用的是补全模型,而不是聊天补全模型,这意味着每个请求都是独立的,每次都需要包含整个提示才能生成响应。
理解补全模型
-
补全模型:此模型不跨请求维护上下文信息。它非常适合每个请求都是独立的且不依赖于过去交互的任务。为各种非对话应用程序提供了灵活性。
-
无上下文记忆:缺乏对先前交互的记忆意味着该模型最适合一次性请求或不需要对话连续性的场景。
-
与聊天补全模型类型的比较:专为对话式 AI 量身定制,维护跨多个交流的上下文以实现连续对话。适用于聊天机器人或对话历史至关重要的应用程序。
在本教程中,我们使用补全模型,因为它简单有效,能够处理独立的单个请求,这与我们教程侧重于烹饪前的准备步骤相符。
llm = OpenAI(temperature=0.1, max_tokens=1000)
副厨师长模拟模板指令解释
在本教程的这一部分,我们精心制作了一个详细的提示模板,模拟高级餐厅副厨师长的角色。此模板旨在指导 LangChain 模型准备菜肴,专门专注于准备工作(mise-en-place)过程。
模板指令分解
-
副厨师长角色扮演:该提示将语言模型置于副厨师长的角色,强调细致的准备工作。
-
任务大纲:
- 列出食材:指示模型列出给定菜肴所需的所有食材。
- 准备技巧:要求模型描述食材准备所需的技巧,例如切割和加工。
- 食材摆放:要求模型提供每种食材的详细摆放说明,考虑使用顺序和时间。
- 烹饪工具准备:指导模型列出并准备菜肴准备阶段所需的所有烹饪工具。
-
范围限制:模板明确设计为只停留在准备阶段,不涉及实际烹饪过程。它专注于设置厨师开始烹饪所需的一切。
-
动态输入:模板可以根据不同的食谱和顾客数量进行调整,如占位符
{recipe}
和{customer_count}
所示。
此模板指令是本教程的关键组成部分,它演示了如何利用 LangChain 声明性指令提示,并带有参数化功能,以适应单一目的的补全式应用程序。
template_instruction = (
"Imagine you are a fine dining sous chef. Your task is to meticulously prepare for a dish, focusing on the mise-en-place process."
"Given a recipe, your responsibilities are: "
"1. List the Ingredients: Carefully itemize all ingredients required for the dish, ensuring every element is accounted for. "
"2. Preparation Techniques: Describe the techniques and operations needed for preparing each ingredient. This includes cutting, "
"processing, or any other form of preparation. Focus on the art of mise-en-place, ensuring everything is perfectly set up before cooking begins."
"3. Ingredient Staging: Provide detailed instructions on how to stage and arrange each ingredient. Explain where each item should be placed for "
"efficient access during the cooking process. Consider the timing and sequence of use for each ingredient. "
"4. Cooking Implements Preparation: Enumerate all the cooking tools and implements needed for each phase of the dish's preparation. "
"Detail any specific preparation these tools might need before the actual cooking starts and describe what pots, pans, dishes, and "
"other tools will be needed for the final preparation."
"Remember, your guidance stops at the preparation stage. Do not delve into the actual cooking process of the dish. "
"Your goal is to set the stage flawlessly for the chef to execute the cooking seamlessly."
"The recipe you are given is for: {recipe} for {customer_count} people. "
)
构建 LangChain 链
我们首先在 LangChain 中设置一个 PromptTemplate
,专门用于我们的副厨师场景。该模板旨在动态接受食谱名称和顾客数量等输入。然后,我们通过将 OpenAI 语言模型与提示模板相结合来初始化一个 LLMChain
,创建一个可以模拟副厨师准备过程的链。
在 MLflow 中记录链
链准备就绪后,我们将其记录在 MLflow 中。这在 MLflow 运行中完成,不仅将链模型记录在指定名称下,还跟踪模型的各种详细信息。记录过程确保链的所有方面都被记录下来,从而实现高效的版本控制和未来的检索。
prompt = PromptTemplate(
input_variables=["recipe", "customer_count"],
template=template_instruction,
)
chain = LLMChain(llm=llm, prompt=prompt)
mlflow.set_experiment("Cooking Assistant")
with mlflow.start_run():
model_info = mlflow.langchain.log_model(chain, name="langchain_model")
如果我们导航到 MLflow UI,我们将看到我们记录的 LangChain 模型。
使用 MLflow 加载模型并进行预测
在本教程的这一部分,我们演示了使用 MLflow 记录的 LangChain 模型的实际应用。我们加载模型并对特定菜肴进行预测,展示了模型辅助烹饪准备的能力。
模型加载与执行
在用 MLflow 记录了 LangChain 链后,我们接着使用 MLflow 的 pyfunc.load_model
函数加载模型。这一步至关重要,因为它将我们之前记录的模型带入可执行状态。
然后,我们将一个特定的食谱和顾客数量输入到我们的模型中。在这种情况下,我们使用“勃艮第牛肉”的食谱,并指定它适用于 12 位顾客。模型作为副厨师长,处理这些信息并生成详细的准备说明。
模型输出
模型输出提供了关于准备“勃艮第牛肉”的全面指南,涵盖了几个关键方面
- 食材清单:详细列出所有必需的食材,并根据指定的顾客数量进行量化和定制。
- 准备技巧:关于如何准备每种食材的分步说明,遵循烹饪准备的原则。
- 食材摆放:关于如何组织和摆放食材的指导,确保在烹饪过程中高效获取和使用。
- 烹饪工具准备:关于准备必要的烹饪工具和器具的说明,从锅碗瓢盆到碗和滤网。
这个例子展示了 LangChain 和 MLflow 在实际场景中结合的力量和实用性。它强调了这种集成如何有效地将复杂的需求转化为可操作的步骤,协助需要精确和周密计划的任务。
loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)
dish1 = loaded_model.predict({"recipe": "boeuf bourginon", "customer_count": "4"})
print(dish1[0])
1. Ingredients: - 2 pounds beef chuck, cut into 1-inch cubes - 6 slices of bacon, diced - 2 tablespoons olive oil - 1 onion, diced - 2 carrots, diced - 2 cloves of garlic, minced - 1 tablespoon tomato paste - 1 bottle of red wine - 2 cups beef broth - 1 bouquet garni (thyme, bay leaf, parsley) - 1 pound pearl onions, peeled - 1 pound mushrooms, quartered - Salt and pepper to taste - Chopped parsley for garnish 2. Preparation Techniques: - Cut the beef chuck into 1-inch cubes and set aside. - Dice the bacon and set aside. - Peel and dice the onion and carrots. - Mince the garlic cloves. - Prepare the bouquet garni by tying together a few sprigs of thyme, a bay leaf, and a few sprigs of parsley with kitchen twine. - Peel the pearl onions and quarter the mushrooms. 3. Ingredient Staging: - Place the beef cubes in a bowl and season with salt and pepper. - In a large Dutch oven, heat the olive oil over medium-high heat. - Add the diced bacon and cook until crispy. - Remove the bacon from the pot and set aside. - In the same pot, add the seasoned beef cubes and cook until browned on all sides. - Remove the beef from the pot and set aside. - In the same pot, add the diced onion and carrots and cook until softened. - Add the minced garlic and cook for an additional minute. - Stir in the tomato paste and cook for another minute. - Add the beef and bacon back into the pot. - Pour in the red wine and beef broth. - Add the bouquet garni and bring to a simmer. - Cover the pot and let it simmer for 2 hours, stirring occasionally. - After 2 hours, add the pearl onions and mushrooms to the pot. - Continue to simmer for an additional hour, or until the beef is tender. - Remove the bouquet garni and discard. - Taste and adjust seasoning with salt and pepper if needed. - Garnish with chopped parsley before serving. 4. Cooking Implements Preparation: - Large Dutch oven or heavy-bottomed pot - Kitchen twine - Cutting board - Chef's knife - Wooden spoon - Measuring cups and spoons - Bowls for prepped ingredients - Tongs for handling meat - Ladle for serving - Serving dishes for the final dish.
dish2 = loaded_model.predict({"recipe": "Okonomiyaki", "customer_count": "12"})
print(dish2[0])
Ingredients: - 2 cups all-purpose flour - 2 teaspoons baking powder - 1/2 teaspoon salt - 2 eggs - 1 1/2 cups water - 1/2 head cabbage, thinly sliced - 1/2 cup green onions, thinly sliced - 1/2 cup carrots, grated - 1/2 cup red bell pepper, thinly sliced - 1/2 cup cooked shrimp, chopped - 1/2 cup cooked bacon, chopped - 1/2 cup pickled ginger, chopped - 1/2 cup tenkasu (tempura flakes) - 1/2 cup mayonnaise - 1/4 cup okonomiyaki sauce - 1/4 cup katsuobushi (dried bonito flakes) - Vegetable oil for cooking Preparation Techniques: 1. In a large mixing bowl, combine the flour, baking powder, and salt. 2. In a separate bowl, beat the eggs and water together. 3. Slowly pour the egg mixture into the flour mixture, stirring until well combined. 4. Set the batter aside to rest for 10 minutes. 5. Thinly slice the cabbage, green onions, and red bell pepper. 6. Grate the carrots. 7. Chop the cooked shrimp, bacon, and pickled ginger. 8. Prepare the tenkasu, mayonnaise, okonomiyaki sauce, and katsuobushi. Ingredient Staging: 1. Place the sliced cabbage, green onions, carrots, red bell pepper, shrimp, bacon, and pickled ginger in separate bowls. 2. Arrange the tenkasu, mayonnaise, okonomiyaki sauce, and katsuobushi in small dishes. 3. Set up a large griddle or non-stick pan for cooking the okonomiyaki. Cooking Implements Preparation: 1. Make sure the griddle or pan is clean and dry. 2. Heat the griddle or pan over medium heat. 3. Have a spatula, tongs, and a large plate ready for flipping and serving the okonomiyaki. 4. Prepare a large plate or platter for serving the finished okonomiyaki. Remember, mise-en-place is key to a successful dish. Make sure all ingredients are prepped and ready to go before starting the cooking process. Happy cooking!
结论
在教程的最后一步,我们使用 LangChain 模型执行另一次预测。这次,我们探讨为 12 位顾客准备日本料理“大阪烧”。这展示了模型在各种菜系中的适应性和多功能性。
使用已加载模型进行额外预测
模型处理“大阪烧”的输入并输出详细的准备步骤。这包括列出食材、解释准备技巧、指导食材摆放以及详细说明所需的烹饪工具,展示了模型精确处理各种食谱的能力。
我们学到了什么
- 模型多功能性:本教程强调了 LangChain 框架如何组装基本 LLM 应用程序的组件,将特定指令提示链接到补全式 LLM。
- MLflow 在模型管理中的作用:LangChain 与 MLflow 的集成展示了有效的模型生命周期管理,从创建、记录到预测执行。
结束语
本教程提供了一次深入的旅程,讲解了如何使用 MLflow 创建、管理和利用 LangChain 模型进行烹饪准备。它展示了 LangChain 在复杂场景中的实际应用和适应性。我们希望这次经历为您提供了宝贵的知识,并鼓励您在您的项目中进一步探索和创新 LangChain 和 MLflow。祝您编码愉快!
下一步是什么?
要继续学习 MLflow 和 LangChain 在更复杂示例中的功能,我们鼓励您通过其他 LangChain 教程继续学习。