מדריך: ביצוע הערכה באמצעות Python SDK
בדף הזה מוסבר איך לבצע הערכה מבוססת-מודל באמצעות שירות ההערכה של AI גנרטיבי באמצעות Agent Platform SDK ל-Python.
לפני שמתחילים
-
נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
מתקינים את Agent Platform SDK ל-Python עם תלות בשירות ההערכה של AI גנרטיבי:
!pip install google-cloud-aiplatform[evaluation]מגדירים את פרטי הכניסה. אם אתם מפעילים את המדריך למתחילים הזה ב-Colaboratory, מריצים את הפקודה הבאה:
from google.colab import auth auth.authenticate_user()לסביבות אחרות, אפשר לעיין במאמר בנושא אימות ב-Agent Platform.
ייבוא ספריות
מייבאים את הספריות ומגדירים את הפרויקט והמיקום.
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, )
שימו לב: EXPERIMENT_NAME יכול להכיל רק תווים אלפאנומריים באותיות קטנות ומקפים, עד 127 תווים.
הגדרת מדדי הערכה על סמך הקריטריונים
ההגדרה הבאה של מדד מעריכה את איכות הטקסט שנוצר ממודל שפה גדול על סמך שני קריטריונים: Fluency ו-Entertaining. הקוד מגדיר מדד בשם custom_text_quality באמצעות שני הקריטריונים האלה:
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",
},
),
)
הכנת מערך הנתונים
מוסיפים את הקוד הבא כדי להכין את מערך הנתונים:
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,
})
הרצת הערכה עם קבוצת הנתונים
מריצים את ההערכה:
eval_task = EvalTask(
dataset=eval_dataset,
metrics=[custom_text_quality],
experiment=EXPERIMENT_NAME
)
pointwise_result = eval_task.evaluate()
אפשר לראות את תוצאות ההערכה של כל תשובה ב-metrics_table Pandas DataFrame:
pointwise_result.metrics_table
הסרת המשאבים
כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, פועלים לפי השלבים הבאים:
מחיקת ExperimentRun שנוצר על ידי ההערכה:
aiplatform.ExperimentRun(
run_name=pointwise_result.metadata["experiment_run"],
experiment=pointwise_result.metadata["experiment"],
).delete()