Tutoriel : Effectuer une évaluation à l'aide du SDK Python
Cette page vous explique comment effectuer une évaluation basée sur un modèle avec Gen AI Evaluation Service à l'aide du SDK Vertex AI pour Python.
Avant de commencer
-
Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
Installez le SDK Vertex AI pour Python avec la dépendance Gen AI Evaluation Service :
!pip install google-cloud-aiplatform[evaluation]
Configurez vos identifiants. Si vous exécutez ce guide de démarrage rapide dans Colaboratory, exécutez la commande suivante :
from google.colab import auth auth.authenticate_user()
Pour les autres environnements, consultez S'authentifier sur Vertex AI.
Importer des bibliothèques
Importez vos bibliothèques, puis configurez votre projet et votre emplacement.
import pandas as pd import vertexai from vertexai.evaluation import EvalTask, PointwiseMetric, PointwiseMetricPromptTemplate from google.cloud import aiplatform PROJECT_ID = "PROJECT_ID" LOCATION = "LOCATION" EXPERIMENT_NAME = "EXPERIMENT_NAME" vertexai.init( project=PROJECT_ID, location=LOCATION, )
Notez que
EXPERIMENT_NAME
ne peut contenir que des caractères alphanumériques en minuscules et des traits d'union, jusqu'à un maximum de 127 caractères.Configurer des métriques d'évaluation en fonction de vos critères
La définition de métrique suivante évalue la qualité du texte généré à partir d'un grand modèle de langage en fonction de deux critères :
Fluency
etEntertaining
. Le code définit une métrique appeléecustom_text_quality
à l'aide de ces deux critères :custom_text_quality = PointwiseMetric( metric="custom_text_quality", metric_prompt_template=PointwiseMetricPromptTemplate( criteria={ "fluency": ( "Sentences flow smoothly and are easy to read, avoiding awkward" " phrasing or run-on sentences. Ideas and sentences connect" " logically, using transitions effectively where needed." ), "entertaining": ( "Short, amusing text that incorporates emojis, exclamations and" " questions to convey quick and spontaneous communication and" " diversion." ), }, rating_rubric={ "1": "The response performs well on both criteria.", "0": "The response is somewhat aligned with both criteria", "-1": "The response falls short on both criteria", }, ), )
Préparer votre ensemble de données
Ajoutez le code suivant pour préparer votre ensemble de données :
responses = [ # An example of good custom_text_quality "Life is a rollercoaster, full of ups and downs, but it's the thrill that keeps us coming back for more!", # An example of medium custom_text_quality "The weather is nice today, not too hot, not too cold.", # An example of poor custom_text_quality "The weather is, you know, whatever.", ] eval_dataset = pd.DataFrame({ "response" : responses, })
Exécuter l'évaluation avec votre ensemble de données
Exécutez l'évaluation :
eval_task = EvalTask( dataset=eval_dataset, metrics=[custom_text_quality], experiment=EXPERIMENT_NAME ) pointwise_result = eval_task.evaluate()
Affichez les résultats de l'évaluation pour chaque réponse dans le DataFrame Pandas
metrics_table
:pointwise_result.metrics_table
Effectuer un nettoyage
Pour éviter que les ressources utilisées dans cette démonstration soient facturées sur votre compte Google Cloud , procédez comme suit :
Supprimez le
ExperimentRun
créé par l'évaluation :aiplatform.ExperimentRun( run_name=pointwise_result.metadata["experiment_run"], experiment=pointwise_result.metadata["experiment"], ).delete()
Étape suivante