Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Guide de démarrage rapide : workflow d'évaluation de l'IA générative
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.
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 et Entertaining. Le code définit une métrique appelée custom_text_quality à l'aide de ces deux critères :
Ajoutez le code suivant pour préparer votre ensemble de données :
responses=[# An example of good custom_text_quality
"Lifeisarollercoaster,fullofupsanddowns,butit'sthethrillthatkeepsuscomingbackformore!",# An example of medium custom_text_quality
"Theweatherisnicetoday,nottoohot,nottoocold.",# An example of poor custom_text_quality
"Theweatheris,youknow,whatever.",]eval_dataset=pd.DataFrame({
"response" :responses,})
Exécuter l'évaluation avec votre ensemble de données
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[],[],null,["# Tutorial: Perform evaluation using the Python SDK\n=================================================\n\n| To see an example of Getting started with the Vertex AI Python SDK for Gen AI evaluation service,\n| run the \"Getting Started with the Vertex AI Python SDK for Gen AI evaluation service\" notebook in one of the following\n| environments:\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/evaluation/intro_to_gen_ai_evaluation_service_sdk.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fevaluation%2Fintro_to_gen_ai_evaluation_service_sdk.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fevaluation%2Fintro_to_gen_ai_evaluation_service_sdk.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/evaluation/intro_to_gen_ai_evaluation_service_sdk.ipynb)\n\nThis page shows you how to perform a model-based evaluation with Gen AI evaluation service using the Vertex AI SDK for Python.\n\nBefore you begin\n----------------\n\n1. Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n\n In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n\n\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project). \n In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n\n\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n2. Install the Vertex AI SDK for Python with Gen AI evaluation service dependency:\n\n !pip install google-cloud-aiplatform[evaluation]\n\n3. Set up your credentials. If you are running this quickstart in Colaboratory, run the following:\n\n from google.colab import auth\n auth.authenticate_user()\n\n For other environments, refer to [Authenticate to Vertex AI](/vertex-ai/docs/authentication#client-libraries).\n\nImport libraries\n----------------\n\nImport your libraries and set up your project and location. \n\n```python\nimport pandas as pd\n\nimport vertexai\nfrom vertexai.evaluation import EvalTask, PointwiseMetric, PointwiseMetricPromptTemplate\nfrom google.cloud import aiplatform\n\nPROJECT_ID = \"\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\"\nLOCATION = \"\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e\"\nEXPERIMENT_NAME = \"\u003cvar translate=\"no\"\u003eEXPERIMENT_NAME\u003c/var\u003e\"\n\nvertexai.init(\n project=PROJECT_ID,\n location=LOCATION,\n)\n```\n\nNote that \u003cvar translate=\"no\"\u003eEXPERIMENT_NAME\u003c/var\u003e can only contain lowercase alphanumeric characters and hyphens, up to a maximum of 127 characters.\n\nSet up evaluation metrics based on your criteria\n------------------------------------------------\n\nThe following metric definition evaluates the text quality generated from a large language model based on two criteria: `Fluency` and `Entertaining`. The code defines a metric called `custom_text_quality` using those two criteria: \n\n custom_text_quality = PointwiseMetric(\n metric=\"custom_text_quality\",\n metric_prompt_template=PointwiseMetricPromptTemplate(\n criteria={\n \"fluency\": (\n \"Sentences flow smoothly and are easy to read, avoiding awkward\"\n \" phrasing or run-on sentences. Ideas and sentences connect\"\n \" logically, using transitions effectively where needed.\"\n ),\n \"entertaining\": (\n \"Short, amusing text that incorporates emojis, exclamations and\"\n \" questions to convey quick and spontaneous communication and\"\n \" diversion.\"\n ),\n },\n rating_rubric={\n \"1\": \"The response performs well on both criteria.\",\n \"0\": \"The response is somewhat aligned with both criteria\",\n \"-1\": \"The response falls short on both criteria\",\n },\n ),\n )\n\nPrepare your dataset\n--------------------\n\nAdd the following code to prepare your dataset: \n\n responses = [\n # An example of good custom_text_quality\n \"Life is a rollercoaster, full of ups and downs, but it's the thrill that keeps us coming back for more!\",\n # An example of medium custom_text_quality\n \"The weather is nice today, not too hot, not too cold.\",\n # An example of poor custom_text_quality\n \"The weather is, you know, whatever.\",\n ]\n\n eval_dataset = pd.DataFrame({\n \"response\" : responses,\n })\n\nRun evaluation with your dataset\n--------------------------------\n\nRun the evaluation: \n\n eval_task = EvalTask(\n dataset=eval_dataset,\n metrics=[custom_text_quality],\n experiment=EXPERIMENT_NAME\n )\n\n pointwise_result = eval_task.evaluate()\n\nView the evaluation results for each response in the `metrics_table` Pandas DataFrame: \n\n pointwise_result.metrics_table\n\nClean up\n--------\n\n\nTo avoid incurring charges to your Google Cloud account for\nthe resources used on this page, follow these steps.\n\nDelete the `ExperimentRun` created by the evaluation: \n\n aiplatform.ExperimentRun(\n run_name=pointwise_result.metadata[\"experiment_run\"],\n experiment=pointwise_result.metadata[\"experiment\"],\n ).delete()\n\nWhat's next\n-----------\n\n- [Define your evaluation metrics](/vertex-ai/generative-ai/docs/models/determine-eval).\n\n- [Prepare your evaluation dataset](/vertex-ai/generative-ai/docs/models/evaluation-dataset)."]]