Tutorial: eseguire la valutazione utilizzando l'SDK Python

Questa pagina mostra come eseguire una valutazione basata su modelli con Gen AI evaluation service utilizzando l'SDK Vertex AI Python.

Prima di iniziare

  1. Accedi al tuo Google Cloud account. Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei workload.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

    Verify that billing is enabled for your Google Cloud project.

  2. Installa l'SDK Vertex AI Python con la dipendenza Gen AI evaluation service:

    !pip install google-cloud-aiplatform[evaluation]
    
  3. Configura le credenziali. Se esegui questa guida rapida in Colaboratory, esegui quanto segue:

    from google.colab import auth
    auth.authenticate_user()
    

    Per altri ambienti, consulta Autenticarsi alla piattaforma agente.

Importa le librerie

Importa le librerie e configura il progetto e la località.

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,
)

Tieni presente che EXPERIMENT_NAME può contenere solo caratteri alfanumerici minuscoli e trattini, fino a un massimo di 127 caratteri.

Configura le metriche di valutazione in base ai tuoi criteri

La seguente definizione di metrica valuta la qualità del testo generato da un modello linguistico di grandi dimensioni in base a due criteri: Fluency e Entertaining. Il codice definisce una metrica denominata custom_text_quality utilizzando questi due criteri:

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",
        },
    ),
)

Prepara il set di dati

Aggiungi il seguente codice per preparare il set di dati:

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,
})

Esegui la valutazione con il set di dati

Esegui la valutazione:

eval_task = EvalTask(
    dataset=eval_dataset,
    metrics=[custom_text_quality],
    experiment=EXPERIMENT_NAME
)

pointwise_result = eval_task.evaluate()

Visualizza i risultati della valutazione per ogni risposta nel DataFrame Pandas metrics_table:

pointwise_result.metrics_table

Libera spazio

Per evitare che al tuo Google Cloud account vengano addebitati costi relativi alle risorse utilizzate in questa pagina, segui questi passaggi.

Elimina ExperimentRun creato dalla valutazione:

aiplatform.ExperimentRun(
    run_name=pointwise_result.metadata["experiment_run"],
    experiment=pointwise_result.metadata["experiment"],
).delete()

Passaggi successivi