跳到主要内容

使用 MLflow 和 DialoGPT 进行对话式 AI 入门

下载此笔记本

欢迎来到本教程,我们将学习如何将微软的 DialoGPT 与 MLflow 的 transformers flavor 集成,以探索对话式 AI。

学习目标

在本教程中,您将

  • 使用 Transformers 库中的 DialoGPT 设置对话式 AI 管道
  • 使用 MLflow 记录 DialoGPT 模型及其配置。
  • 推断 DialoGPT 模型的输入和输出签名
  • 从 MLflow 中加载已存储的 DialoGPT 模型以进行交互式使用。
  • 与聊天机器人模型进行交互,并理解对话式 AI 的细微之处。

通过本教程的学习,您将对使用 MLflow 管理和部署对话式 AI 模型有扎实的理解,从而增强您在自然语言处理方面的能力。

什么是 DialoGPT?

DialoGPT 是微软开发的一个对话模型,它在一个大型对话数据集上进行了微调,以生成类似人类的回复。作为 GPT 系列的一部分,DialoGPT 在自然语言理解和生成方面表现出色,使其成为聊天机器人的理想选择。

为什么在 DialoGPT 中使用 MLflow?

将 MLflow 与 DialoGPT 集成可以增强对话式 AI 模型开发

  • 实验跟踪:跟踪跨实验的配置和指标。
  • 模型管理:管理不同版本和配置的聊天机器人模型。
  • 可复现性:确保模型行为的可复现性。
  • 部署:简化在生产环境中部署对话式模型。
# Disable tokenizers warnings when constructing pipelines
%env TOKENIZERS_PARALLELISM=false

import warnings

# Disable a few less-than-useful UserWarnings from setuptools and pydantic
warnings.filterwarnings("ignore", category=UserWarning)
env: TOKENIZERS_PARALLELISM=false

设置对话式管道

我们首先使用 transformers 设置一个包含 DialoGPT 的对话式管道,并使用 MLflow 进行管理。

我们首先导入必要的库。Hugging Face 的 transformers 库提供了丰富的预训练模型集合,包括 DialoGPT,用于各种 NLP 任务。MLflow 是一个用于 ML 生命周期的综合工具,有助于实验跟踪、可复现性和部署。

初始化对话式管道

使用 transformers.pipeline 函数,我们设置了一个对话式管道。我们选择“microsoft/DialoGPT-medium”模型,该模型在性能和资源效率之间取得了平衡,非常适合对话式 AI。此步骤对于确保模型已准备好进行交互和集成到各种应用程序中至关重要。

使用 MLflow 推断模型签名

模型签名对于定义模型如何与输入数据交互至关重要。为了推断它,我们使用一个示例输入(“你好,聊天机器人!”)并利用 mlflow.transformers.generate_signature_output 来理解模型的输入-输出模式。此过程确保模型的数据要求和预测格式清晰,这对于无缝部署和使用至关重要。

此配置阶段为强大的对话式 AI 系统奠定了基础,利用 DialoGPT 和 MLflow 的优势实现高效且有效的对话交互。

import transformers

import mlflow

# Define our pipeline, using the default configuration specified in the model card for DialoGPT-medium
conversational_pipeline = transformers.pipeline(model="microsoft/DialoGPT-medium")

# Infer the signature by providing a representnative input and the output from the pipeline inference abstraction in the transformers flavor in MLflow
signature = mlflow.models.infer_signature(
"Hi there, chatbot!",
mlflow.transformers.generate_signature_output(conversational_pipeline, "Hi there, chatbot!"),
)
The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.

创建实验

我们创建一个新的 MLflow 实验,以便我们要将模型记录到的运行不会记录到默认实验,而是具有其自己的上下文相关条目。

# If you are running this tutorial in local mode, leave the next line commented out.
# Otherwise, uncomment the following line and set your tracking uri to your local or remote tracking server.

# mlflow.set_tracking_uri("http://127.0.0.1:8080")

# Set a name for the experiment that is indicative of what the runs being created within it are in regards to
mlflow.set_experiment("Conversational")
<Experiment: artifact_location='file:///Users/benjamin.wilson/repos/mlflow-fork/mlflow/docs/source/llms/transformers/tutorials/conversational/mlruns/370178017237207703', creation_time=1701292102618, experiment_id='370178017237207703', last_update_time=1701292102618, lifecycle_stage='active', name='Conversational', tags={}>

使用 MLflow 记录模型

现在,我们将使用 MLflow 记录我们的对话式 AI 模型,确保系统化的版本控制、跟踪和管理。

启动 MLflow 运行

我们的第一步是使用 mlflow.start_run() 启动一个 MLflow 运行。此操作会启动一个新的跟踪环境,并在唯一的运行 ID 下捕获所有与模型相关的数据。这是区分和组织不同建模实验的关键步骤。

记录对话式模型

我们使用 mlflow.transformers.log_model 记录我们的 DialoGPT 对话模型。此专用函数可高效记录 Transformer 模型,并需要几个关键参数:

  • transformers_model:我们将我们的 DialoGPT 对话管道传递进去。
  • artifact_path:MLflow 运行内的存储位置,恰当地命名为 “chatbot”
  • task:设置为 “conversational” 以反映模型的目的。
  • signature:推断出的模型签名,指示预期的输入和输出。
  • input_example:一个示例提示,例如 “一个聪明机智的问题”,以演示预期的用法。

通过此过程,MLflow 不仅跟踪我们的模型,还组织其元数据,从而便于未来的检索、理解和部署。

with mlflow.start_run():
model_info = mlflow.transformers.log_model(
transformers_model=conversational_pipeline,
name="chatbot",
task="conversational",
signature=signature,
input_example="A clever and witty question",
)

加载和交互聊天机器人模型

接下来,我们将加载 MLflow 记录的聊天机器人模型并与它进行交互,看看它的实际效果。

使用 MLflow 加载模型

我们使用 mlflow.pyfunc.load_model 加载我们的对话式 AI 模型。此函数是 MLflow Python 函数 flavor 的关键方面,它提供了一种与 Python 模型交互的通用方式。通过指定 model_uri=model_info.model_uri,我们可以精确地定位 MLflow 跟踪系统中存储的 DialoGPT 模型的位置。

与聊天机器人交互

加载后,模型(引用为 chatbot)即可进行交互。我们通过以下方式演示其对话能力:

  • 提问:向聊天机器人提出一个问题,例如“去南极的最佳方式是什么?”
  • 捕获响应:聊天机器人通过 predict 方法生成的响应,提供了其对话技能的实际示例。例如,它可能会建议乘船前往南极。

此演示突显了部署和使用 MLflow 记录的模型的可行性和便利性,尤其是在对话式 AI 等动态和交互式场景中。

# Load the model as a generic python function in order to leverage the integrated Conversational Context
# Note that loading a conversational model with the native flavor (i.e., `mlflow.transformers.load_model()`) will not include anything apart from the
# pipeline itself; if choosing to load in this way, you will need to manage your own Conversational Context instance to maintain state on the
# conversation history.
chatbot = mlflow.pyfunc.load_model(model_uri=model_info.model_uri)

# Validate that the model is capable of responding to a question
first = chatbot.predict("What is the best way to get to Antarctica?")
Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]
The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.
print(f"Response: {first}")
Response: I think you can get there by boat.

继续与聊天机器人对话

我们进一步探讨了 MLflow pyfunc 实现与 DialoGPT 聊天机器人模型的对话上下文状态保持性。

测试上下文记忆

我们提出了一个后续问题:“我应该使用哪种船?”来测试聊天机器人的上下文理解能力。我们收到的回答“一种可以去南极的船”,虽然简单,但展示了 MLflow pyfunc 模型能够保留和利用对话历史,通过 ConversationalPipeline 类型模型生成连贯的响应。

理解响应风格

响应的风格——诙谐且略带戏谑——反映了训练数据的性质,主要是来自 Reddit 的对话交流。这种训练来源显著影响了模型的语气和风格,导致响应可能既幽默又多样。

训练数据的含义

这次互动凸显了训练数据来源在塑造模型响应方面的重要性。在实际应用程序中部署此类模型时,了解和考虑训练数据对模型对话风格和知识库的影响至关重要。

# Verify that the PyFunc implementation has maintained state on the conversation history by asking a vague follow-up question that requires context
# in order to answer properly
second = chatbot.predict("What sort of boat should I use?")
The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.
print(f"Response: {second}")
Response: A boat that can go to Antarctica.

结论和要点

在本教程中,我们探讨了 MLflow 与对话式 AI 模型(特别是微软的 DialoGPT 模型)的集成。我们涵盖了许多对希望在实际应用环境中处理高级机器学习模型的任何人至关重要的重要方面和技术。

主要收获

  1. MLflow 用于模型管理:我们演示了如何有效使用 MLflow 来管理和部署机器学习模型。在机器学习工作流程中,能够记录模型、跟踪实验和管理不同模型版本是无价的。

  2. 对话式 AI:通过使用 DialoGPT 模型,我们深入了解了对话式 AI 的世界,展示了如何设置和与对话模型进行交互。这包括理解保持对话上下文的细微差别以及训练数据对模型响应的影响。

  3. 实际实现:通过实际示例,我们展示了如何在 MLflow 中记录模型、推断模型签名以及使用 pyfunc 模型 flavor 进行轻松部署和交互。这种动手方法旨在为您提供在自己的项目中实施这些技术所需的技能。

  4. 理解模型响应:我们强调了理解模型训练数据性质的重要性。这种理解对于解释模型的响应以及根据特定用例定制模型至关重要。

  5. 上下文历史:MLflow 的 transformers pyfunc 实现,用于 ConversationalPipelines,在无需您自己管理状态的情况下,会维护一个 Conversation 上下文。这使得创建聊天机器人变得非常轻松,因为状态会为您维护。

总结

随着本教程的结束,我们希望您对如何将 MLflow 与对话式 AI 模型集成以及部署这些模型所涉及的实际考虑因素有了更深入的了解。在此获得的技能和知识不仅适用于对话式 AI,也适用于更广泛的机器学习应用。

请记住,机器学习领域广阔且不断发展。持续学习和实验是保持更新并充分利用这些激动人心技术的关键。

感谢您与我们一起踏上这段 MLflow 和对话式 AI 之旅。我们鼓励您将这些学习内容应用到您自己独特的挑战和项目中。编码愉快!