{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction to Conversational AI with MLflow and DialoGPT\n", "\n", "Welcome to our tutorial on integrating [Microsoft's DialoGPT](https://huggingface.co/microsoft/DialoGPT-medium) with MLflow's transformers flavor to explore conversational AI." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Download this Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Learning Objectives\n", "\n", "In this tutorial, you will:\n", "\n", "- Set up a conversational AI **pipeline** using DialoGPT from the Transformers library.\n", "- **Log** the DialoGPT model along with its configurations using MLflow.\n", "- Infer the input and output **signature** of the DialoGPT model.\n", "- **Load** a stored DialoGPT model from MLflow for interactive usage.\n", "- Interact with the chatbot model and understand the nuances of conversational AI.\n", "\n", "By the end of this tutorial, you will have a solid understanding of managing and deploying conversational AI models with MLflow, enhancing your capabilities in natural language processing.\n", "\n", "#### What is DialoGPT?\n", "DialoGPT is a conversational model developed by Microsoft, fine-tuned on a large dataset of dialogues to generate human-like responses. Part of the GPT family, DialoGPT excels in natural language understanding and generation, making it ideal for chatbots.\n", "\n", "#### Why MLflow with DialoGPT?\n", "Integrating MLflow with DialoGPT enhances conversational AI model development:\n", "\n", "- **Experiment Tracking**: Tracks configurations and metrics across experiments.\n", "- **Model Management**: Manages different versions and configurations of chatbot models.\n", "- **Reproducibility**: Ensures the reproducibility of the model's behavior.\n", "- **Deployment**: Simplifies deploying conversational models in production." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: TOKENIZERS_PARALLELISM=false\n" ] } ], "source": [ "# Disable tokenizers warnings when constructing pipelines\n", "%env TOKENIZERS_PARALLELISM=false\n", "\n", "import warnings\n", "\n", "# Disable a few less-than-useful UserWarnings from setuptools and pydantic\n", "warnings.filterwarnings(\"ignore\", category=UserWarning)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting Up the Conversational Pipeline\n", "\n", "We begin by setting up a conversational pipeline with DialoGPT using `transformers` and managing it with MLflow.\n", "\n", "We start by importing essential libraries. The `transformers` library from Hugging Face offers a rich collection of pre-trained models, including DialoGPT, for various NLP tasks. MLflow, a comprehensive tool for the ML lifecycle, aids in experiment tracking, reproducibility, and deployment.\n", "\n", "#### Initializing the Conversational Pipeline\n", "Using the `transformers.pipeline` function, we set up a conversational pipeline. We choose the \"`microsoft/DialoGPT-medium`\" model, balancing performance and resource efficiency, ideal for conversational AI. This step is pivotal for ensuring the model is ready for interaction and integration into various applications.\n", "\n", "#### Inferring the Model Signature with MLflow\n", "Model signature is key in defining how the model interacts with input data. To infer it, we use a sample input (\"`Hi there, chatbot!`\") and leverage `mlflow.transformers.generate_signature_output` to understand the model's input-output schema. This process ensures clarity in the model's data requirements and prediction format, crucial for seamless deployment and usage.\n", "\n", "This configuration phase sets the stage for a robust conversational AI system, leveraging the strengths of DialoGPT and MLflow for efficient and effective conversational interactions." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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.\n", "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n", "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.\n" ] } ], "source": [ "import transformers\n", "\n", "import mlflow\n", "\n", "# Define our pipeline, using the default configuration specified in the model card for DialoGPT-medium\n", "conversational_pipeline = transformers.pipeline(model=\"microsoft/DialoGPT-medium\")\n", "\n", "# Infer the signature by providing a representnative input and the output from the pipeline inference abstraction in the transformers flavor in MLflow\n", "signature = mlflow.models.infer_signature(\n", " \"Hi there, chatbot!\",\n", " mlflow.transformers.generate_signature_output(conversational_pipeline, \"Hi there, chatbot!\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating an experiment\n", "\n", "We create a new MLflow Experiment so that the run we're going to log our model to does not log to the default experiment and instead has its own contextually relevant entry." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# If you are running this tutorial in local mode, leave the next line commented out.\n", "# Otherwise, uncomment the following line and set your tracking uri to your local or remote tracking server.\n", "\n", "# mlflow.set_tracking_uri(\"http://127.0.0.1:8080\")\n", "\n", "# Set a name for the experiment that is indicative of what the runs being created within it are in regards to\n", "mlflow.set_experiment(\"Conversational\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Logging the Model with MLflow\n", "\n", "We'll now use MLflow to log our conversational AI model, ensuring systematic versioning, tracking, and management.\n", "\n", "#### Initiating an MLflow Run\n", "Our first step is to start an MLflow run with `mlflow.start_run()`. This action initiates a new tracking environment, capturing all model-related data under a unique run ID. It's a crucial step to segregate and organize different modeling experiments.\n", "\n", "#### Logging the Conversational Model\n", "We log our DialoGPT conversational model using `mlflow.transformers.log_model`. This specialized function efficiently logs Transformer models and requires several key parameters:\n", "\n", "- **transformers_model**: We pass our DialoGPT conversational pipeline.\n", "- **artifact_path**: The storage location within the MLflow run, aptly named `\"chatbot\"`.\n", "- **task**: Set to `\"conversational\"` to reflect the model's purpose.\n", "- **signature**: The inferred model signature, dictating expected inputs and outputs.\n", "- **input_example**: A sample prompt, like `\"A clever and witty question\"`, to demonstrate expected usage.\n", "\n", "Through this process, MLflow not only tracks our model but also organizes its metadata, facilitating future retrieval, understanding, and deployment." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "with mlflow.start_run():\n", " model_info = mlflow.transformers.log_model(\n", " transformers_model=conversational_pipeline,\n", " artifact_path=\"chatbot\",\n", " task=\"conversational\",\n", " signature=signature,\n", " input_example=\"A clever and witty question\",\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading and Interacting with the Chatbot Model\n", "\n", "Next, we'll load the MLflow-logged chatbot model and interact with it to see it in action.\n", "\n", "#### Loading the Model with MLflow\n", "We use `mlflow.pyfunc.load_model` to load our conversational AI model. This function is a crucial aspect of MLflow's Python function flavor, offering a versatile way to interact with Python models. By specifying `model_uri=model_info.model_uri`, we precisely target the stored location of our DialoGPT model within MLflow's tracking system.\n", "\n", "#### Interacting with the Chatbot\n", "Once loaded, the model, referenced as `chatbot`, is ready for interaction. We demonstrate its conversational capabilities by:\n", "\n", "- **Asking Questions**: Posing a question like \"What is the best way to get to Antarctica?\" to the chatbot.\n", "- **Capturing Responses**: The chatbot's response, generated through the `predict` method, provides a practical example of its conversational skills. For instance, it might respond with suggestions about reaching Antarctica by boat.\n", "\n", "This demonstration highlights the practicality and convenience of deploying and using models logged with MLflow, especially in dynamic and interactive scenarios like conversational AI." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f46b4d8422cd4fac874bc5b87e85e474", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading checkpoint shards: 0%| | 0/3 [00:00