跳到主要内容

MLflow 与 DialoGPT 对话式 AI 简介

下载此 Notebook

欢迎来到本教程,我们将介绍如何将 Microsoft 的 DialoGPT 与 MLflow 的 transformers 风格集成,以探索对话式 AI。

学习目标

在本教程中,你将

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

在本教程结束时,你将对如何使用 MLflow 管理和部署对话式 AI 模型有扎实的理解,从而提升你在自然语言处理方面的能力。

什么是 DialoGPT?

DialoGPT 是 Microsoft 开发的一种对话模型,经过大量对话数据集的微调,可以生成类似人类的回复。作为 GPT 系列的一部分,DialoGPT 在自然语言理解和生成方面表现出色,使其非常适合聊天机器人。

为什么将 MLflow 与 DialoGPT 结合使用?

将 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 库提供了丰富的预训练模型集合,包括用于各种 NLP 任务的 DialoGPT。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 Experiment,这样我们将模型记录到的运行就不会记录到默认实验中,而是拥有自己的具有上下文相关的条目。

# 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:一个样本 Prompt,例如 "一个聪明且机智的问题",用于演示预期用法。

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

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

加载并与聊天机器人模型交互

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

使用 MLflow 加载模型

我们使用 mlflow.pyfunc.load_model 加载我们的对话式 AI 模型。此函数是 MLflow Python 函数风格的关键方面,提供了与 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 模型的集成,特别是使用了 Microsoft 的 DialoGPT 模型。我们涵盖了对于希望在实际、现实世界环境中处理高级机器学习模型的任何人来说都至关重要的几个重要方面和技术。

关键要点

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

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

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

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

  5. 上下文历史:MLflow 的 transformers pyfunc 实现针对 ConversationalPipelines 维护了一个 Conversation 上下文,无需你自己管理状态。这使得创建聊天机器人变得轻而易举,因为状态已经为你维护好了。

总结

在本教程结束之际,希望你对如何将 MLflow 与对话式 AI 模型集成以及部署这些模型涉及的实际考量有了更深入的了解。这里获得的技能和知识不仅适用于对话式 AI,也适用于更广泛的机器学习应用领域。

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

感谢你加入我们一起探索 MLflow 和对话式 AI 的世界。我们鼓励你将这些知识应用于你自己的独特挑战和项目中。祝你编程愉快!