MLflow 和 DialoGPT 会话式 AI 简介
欢迎阅读本教程,了解如何将 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
库提供了丰富的预训练模型集合,包括 DialoGPT,用于各种 NLP 任务。MLflow 是一个用于 ML 生命周期的综合工具,有助于实验跟踪、可复现性和部署。
初始化会话管道
使用 transformers.pipeline
函数,我们设置了一个会话管道。我们选择“microsoft/DialoGPT-medium
”模型,它平衡了性能和资源效率,是会话式 AI 的理想选择。这一步对于确保模型准备好进行交互并集成到各种应用程序中至关重要。
使用 MLflow 推断模型签名
模型签名是定义模型如何与输入数据交互的关键。为了推断它,我们使用一个样本输入(“Hi there, chatbot!
”)并利用 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 函数特性的关键方面,提供了一种与 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 模型)的集成。我们涵盖了几个重要方面和技术,这些对于任何希望在实际、真实环境中处理高级机器学习模型的人来说都至关重要。
主要收获
-
MLflow 用于模型管理:我们演示了 MLflow 如何有效用于管理和部署机器学习模型。记录模型、跟踪实验以及管理不同版本的模型的能力在机器学习工作流中是无价的。
-
会话式 AI:通过使用 DialoGPT 模型,我们深入研究了会话式 AI 的世界,展示了如何设置会话模型并与之交互。这包括理解维护会话上下文的细微差别以及训练数据对模型响应的影响。
-
实际实施:通过实际示例,我们展示了如何在 MLflow 中记录模型、推断模型签名以及使用
pyfunc
模型特性进行轻松部署和交互。这种动手实践方法旨在为您提供在您自己的项目中实施这些技术所需的技能。 -
理解模型响应:我们强调了理解模型训练数据性质的重要性。这种理解对于解释模型的响应以及根据特定用例定制模型至关重要。
-
上下文历史:MLflow 的
transformers
pyfunc
实现适用于ConversationalPipelines
,它维护一个Conversation
上下文,而无需您自己管理状态。这使得聊天机器人可以以最小的努力创建,因为状态已为您维护。
总结
在本教程结束时,我们希望您对如何将 MLflow 与会话式 AI 模型集成以及部署这些模型所涉及的实际考虑因素有了更深入的了解。此处获得的技能和知识不仅适用于会话式 AI,也适用于更广泛的机器学习应用程序。
请记住,机器学习领域广阔且不断发展。持续学习和实验是保持更新并充分利用这些令人兴奋的技术的关键。
感谢您与我们一起踏上 MLflow 和会话式 AI 之旅。我们鼓励您将这些学习应用于您自己独特的挑战和项目。祝您编码愉快!