{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Evaluate a 🤗 Hugging Face LLM with mlflow.evaluate()\n", "\n", "This guide will show how to load a pre-trained Hugging Face pipeline, log it to MLflow, and use `mlflow.evaluate()` to evaluate builtin metrics as well as custom LLM-judged metrics for the model.\n", "\n", "For detailed information, please read the documentation on [using MLflow evaluate](https://mlflow.org/docs/latest/llms/llm-evaluate/index.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Start MLflow Server\n", "\n", "You can either:\n", "\n", "- Start a local tracking server by running `mlflow server` within the same directory that your notebook is in.\n", "- Use a tracking server, as described in [this overview](https://mlflow.org/docs/latest/getting-started/tracking-server-overview/index.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Install necessary dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install -q mlflow transformers torch torchvision evaluate datasets openai tiktoken fastapi rouge_score textstat" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Necessary imports\n", "import warnings\n", "\n", "import pandas as pd\n", "from datasets import load_dataset\n", "from transformers import pipeline\n", "\n", "import mlflow\n", "from mlflow.metrics.genai import EvaluationExample, answer_correctness, make_genai_metric" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Disable FutureWarnings\n", "warnings.filterwarnings(\"ignore\", category=FutureWarning)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load a pretrained Hugging Face pipeline\n", "\n", "Here we are loading a text generation pipeline, but you can also use either a text summarization or question answering pipeline." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f258c308f1504fb0937f81475dc5fffd", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading checkpoint shards: 0%| | 0/2 [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mpt_pipeline = pipeline(\"text-generation\", model=\"mosaicml/mpt-7b-chat\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Log the model using MLflow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We log our pipeline as an MLflow Model, which follows a standard format that lets you save a model in different \"flavors\" that can be understood by different downstream tools. In this case, the model is of the transformers \"flavor\"." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Successfully registered model 'mpt-7b-chat'.\n", "Created version '1' of model 'mpt-7b-chat'.\n" ] } ], "source": [ "mlflow.set_experiment(\"Evaluate Hugging Face Text Pipeline\")\n", "\n", "# Define the signature\n", "signature = mlflow.models.infer_signature(\n", " model_input=\"What are the three primary colors?\",\n", " model_output=\"The three primary colors are red, yellow, and blue.\",\n", ")\n", "\n", "# Log the model using mlflow\n", "with mlflow.start_run():\n", " model_info = mlflow.transformers.log_model(\n", " transformers_model=mpt_pipeline,\n", " name=\"mpt-7b\",\n", " signature=signature,\n", " registered_model_name=\"mpt-7b-chat\",\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load Evaluation Data\n", "\n", "Load in a dataset from Hugging Face Hub to use for evaluation." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | instruction | \n", "input | \n", "output | \n", "text | \n", "
|---|---|---|---|---|
| 0 | \n", "Give three tips for staying healthy. | \n", "\n", " | 1.Eat a balanced diet and make sure to include... | \n", "Below is an instruction that describes a task.... | \n", "
| 1 | \n", "What are the three primary colors? | \n", "\n", " | The three primary colors are red, blue, and ye... | \n", "Below is an instruction that describes a task.... | \n", "
| 2 | \n", "Describe the structure of an atom. | \n", "\n", " | An atom is made up of a nucleus, which contain... | \n", "Below is an instruction that describes a task.... | \n", "
| 3 | \n", "How can we reduce air pollution? | \n", "\n", " | There are a number of ways to reduce air pollu... | \n", "Below is an instruction that describes a task.... | \n", "
| 4 | \n", "Describe a time when you had to make a difficu... | \n", "\n", " | I had to make a difficult decision when I was ... | \n", "Below is an instruction that describes a task.... | \n", "
| 5 | \n", "Identify the odd one out. | \n", "Twitter, Instagram, Telegram | \n", "Telegram | \n", "Below is an instruction that describes a task,... | \n", "
| 6 | \n", "Explain why the following fraction is equivale... | \n", "4/16 | \n", "The fraction 4/16 is equivalent to 1/4 because... | \n", "Below is an instruction that describes a task,... | \n", "
| 7 | \n", "Write a short story in third person narration ... | \n", "\n", " | John was at a crossroads in his life. He had j... | \n", "Below is an instruction that describes a task.... | \n", "
| 8 | \n", "Render a 3D model of a house | \n", "\n", " | <nooutput> This type of instruction cannot be ... | \n", "Below is an instruction that describes a task.... | \n", "
| 9 | \n", "Evaluate this sentence for spelling and gramma... | \n", "He finnished his meal and left the resturant | \n", "He finished his meal and left the restaurant. | \n", "Below is an instruction that describes a task,... | \n", "
An evaluation metric encapsulates any quantitative or qualitative measure you want to calculate for your model. For each model type, mlflow.evaluate() will automatically calculate some set of builtin metrics. Refer here for which builtin metrics will be calculated for each model type. You can also pass in any other metrics you want to calculate as extra metrics. MLflow provides a set of predefined metrics that you can find here, or you can define your own custom metrics. In the example here, we will use the predefined metric mlflow.metrics.genai.answer_correctness.\n",
"
To create a custom metric using make_metric, you will need to define an evaluation function that:
Given such an evaluation function, for example named my_metric_eval_fn, all we need to do to create our custom metric is call make_metric with greater_is_better set appropriately, as shown below:\n",
"
\n",
" custom_metric = make_metric(eval_fn=my_metric_eval_fn, greater_is_better=False)\n",
"
make_metric.\n",
" \n",
" If we want to create a custom GenAI metric, we can use make_genai_metric instead.
Let's explore how we would create a GenAI metric for our use case, where we want to measure the quality of the answer based on its fluency, clarity, and conciseness. We have four important things to define for our metric: the name, definition, grading prompt, and examples. There are other optional parameters for make_genai_metric which you can explore here, but we will start with the essential parameters (examples are optional but highly recommended).
All we have to do is call make_genai_metric with the above parameters, and we'll have a custom GenAI metric that we can use! Note that our predefined GenAI metrics (such as answer_correctness, which we use below) use the same prompt as custom GenAI metrics created with make_genai_metric, the only difference being that we have already defined the name, definition, and grading prompt. When using a predefined GenAI metric, any other parameters (such as examples) can be customized.
| \n", " | instruction | \n", "input | \n", "text | \n", "output | \n", "outputs | \n", "token_count | \n", "toxicity/v1/score | \n", "flesch_kincaid_grade_level/v1/score | \n", "ari_grade_level/v1/score | \n", "answer_correctness/v1/score | \n", "answer_correctness/v1/justification | \n", "answer_quality/v1/score | \n", "answer_quality/v1/justification | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "Give three tips for staying healthy. | \n", "\n", " | Below is an instruction that describes a task.... | \n", "1.Eat a balanced diet and make sure to include... | \n", "Give three tips for staying healthy.\\n1. Eat a... | \n", "19 | \n", "0.000446 | \n", "4.1 | \n", "4.0 | \n", "2 | \n", "The output provided by the model only includes... | \n", "3 | \n", "The output is understandable and fluent but it... | \n", "
| 1 | \n", "What are the three primary colors? | \n", "\n", " | Below is an instruction that describes a task.... | \n", "The three primary colors are red, blue, and ye... | \n", "What are the three primary colors?\\nThe three ... | \n", "19 | \n", "0.000217 | \n", "5.0 | \n", "4.9 | \n", "5 | \n", "The output provided by the model is completely... | \n", "5 | \n", "The model's output is fluent, clear, and conci... | \n", "
| 2 | \n", "Describe the structure of an atom. | \n", "\n", " | Below is an instruction that describes a task.... | \n", "An atom is made up of a nucleus, which contain... | \n", "Describe the structure of an atom.\\nAn atom is... | \n", "18 | \n", "0.000139 | \n", "3.1 | \n", "2.2 | \n", "1 | \n", "The output provided by the model is incomplete... | \n", "2 | \n", "The output is incomplete and lacks clarity, ma... | \n", "
| 3 | \n", "How can we reduce air pollution? | \n", "\n", " | Below is an instruction that describes a task.... | \n", "There are a number of ways to reduce air pollu... | \n", "How can we reduce air pollution?\\nThere are ma... | \n", "18 | \n", "0.000140 | \n", "5.0 | \n", "5.5 | \n", "1 | \n", "The output provided by the model is completely... | \n", "1 | \n", "The output is entirely incomprehensible and ca... | \n", "
| 4 | \n", "Describe a time when you had to make a difficu... | \n", "\n", " | Below is an instruction that describes a task.... | \n", "I had to make a difficult decision when I was ... | \n", "Describe a time when you had to make a difficu... | \n", "18 | \n", "0.000159 | \n", "5.2 | \n", "2.9 | \n", "1 | \n", "The output provided by the model is completely... | \n", "2 | \n", "The output is incomplete and lacks clarity, ma... | \n", "
| 5 | \n", "Identify the odd one out. | \n", "Twitter, Instagram, Telegram | \n", "Below is an instruction that describes a task,... | \n", "Telegram | \n", "Identify the odd one out.\\n\\n1. A car\\n2. A tr... | \n", "18 | \n", "0.072345 | \n", "0.1 | \n", "-5.4 | \n", "1 | \n", "The output provided by the model is completely... | \n", "2 | \n", "The output is not clear and lacks fluency. The... | \n", "
| 6 | \n", "Explain why the following fraction is equivale... | \n", "4/16 | \n", "Below is an instruction that describes a task,... | \n", "The fraction 4/16 is equivalent to 1/4 because... | \n", "Explain why the following fraction is equivale... | \n", "23 | \n", "0.000320 | \n", "6.4 | \n", "7.6 | \n", "1 | \n", "The output provided by the model is completely... | \n", "2 | \n", "The output is not clear and does not answer th... | \n", "
| 7 | \n", "Write a short story in third person narration ... | \n", "\n", " | Below is an instruction that describes a task.... | \n", "John was at a crossroads in his life. He had j... | \n", "Write a short story in third person narration ... | \n", "20 | \n", "0.000247 | \n", "10.7 | \n", "11.1 | \n", "1 | \n", "The output provided by the model is completely... | \n", "1 | \n", "The output is exactly the same as the input, a... | \n", "
| 8 | \n", "Render a 3D model of a house | \n", "\n", " | Below is an instruction that describes a task.... | \n", "<nooutput> This type of instruction cannot be ... | \n", "Render a 3D model of a house in Blender - Blen... | \n", "19 | \n", "0.003694 | \n", "5.2 | \n", "2.7 | \n", "1 | \n", "The output provided by the model is completely... | \n", "2 | \n", "The output is partially understandable but lac... | \n", "
| 9 | \n", "Evaluate this sentence for spelling and gramma... | \n", "He finnished his meal and left the resturant | \n", "Below is an instruction that describes a task,... | \n", "He finished his meal and left the restaurant. | \n", "Evaluate this sentence for spelling and gramma... | \n", "18 | \n", "0.003260 | \n", "4.2 | \n", "6.4 | \n", "1 | \n", "The output provided by the model is completely... | \n", "4 | \n", "The output is fluent and clear, but it is not ... | \n", "