מדריך: הערכה באמצעות Python SDK
בדף הזה מוסבר איך לבצע הערכה מבוססת-מודל באמצעות שירות ההערכה של AI גנרטיבי, בעזרת Agent Platform SDK ל-Python.
לפני שמתחילים
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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 Cloud de Confiance 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 תווים לכל היותר.
הגדרת מדדי הערכה על סמך הקריטריונים שלכם
ההגדרה הבאה של המדד מעריכה את איכות הטקסט שנוצר ממודל שפה גדול (LLM) על סמך שני קריטריונים: 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()
מציגים את תוצאות ההערכה של כל תשובה ב-Pandas DataFrame של metrics_table:
pointwise_result.metrics_table
הסרת המשאבים
כדי לא לצבור חיובים בחשבון Cloud de Confiance על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.
מוחקים את ה-ExperimentRun שנוצר על ידי ההערכה:
aiplatform.ExperimentRun(
run_name=pointwise_result.metadata["experiment_run"],
experiment=pointwise_result.metadata["experiment"],
).delete()