Tutorial: esegui la valutazione utilizzando il client GenAI nell'SDK Vertex AI

Questa pagina mostra come valutare i modelli e le applicazioni di AI generativa in una serie di casi d'uso utilizzando il client GenAI nell'SDK Vertex AI.

Prima di iniziare

  1. 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 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 (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:

    !pip install google-cloud-aiplatform[evaluation]
    
  3. Configura le tue credenziali. Se esegui questo tutorial in Colaboratory, esegui il seguente comando:

    from google.colab import auth
    auth.authenticate_user()
    

    Per altri ambienti, consulta Autenticazione in Vertex AI.

  4. Generare risposte

    Genera le risposte del modello per il tuo set di dati utilizzando run_inference():

    1. Prepara il set di dati come DataFrame Pandas:

      import pandas as pd
      
      eval_df = pd.DataFrame({
        "prompt": [
            "Explain software 'technical debt' using a concise analogy of planting a garden.",
            "Write a Python function to find the nth Fibonacci number using recursion with memoization, but without using any imports.",
            "Write a four-line poem about a lonely robot, where every line must be a question and the word 'and' cannot be used.",
            "A drawer has 10 red socks and 10 blue socks. In complete darkness, what is the minimum number of socks you must pull out to guarantee you have a matching pair?",
            "An AI discovers a cure for a major disease, but the cure is based on private data it analyzed without consent. Should the cure be released? Justify your answer."
        ]
      })
      
    2. Genera risposte del modello utilizzando run_inference():

      eval_dataset = client.evals.run_inference(
        model="gemini-2.5-flash",
        src=eval_df,
      )
      
    3. Visualizza i risultati dell'inferenza chiamando .show() sull'oggetto EvaluationDataset per esaminare gli output del modello insieme ai prompt e ai riferimenti originali:

      eval_dataset.show()
      

    La seguente immagine mostra il set di dati di valutazione con i prompt e le risposte generate corrispondenti:

    Una tabella che mostra un set di dati di valutazione con colonne per prompt e risposte.

    Esegui la valutazione

    Esegui evaluate() per valutare le risposte del modello:

    1. Valuta le risposte del modello utilizzando la metrica adattiva basata su rubrica predefinita GENERAL_QUALITY:

      eval_result = client.evals.evaluate(dataset=eval_dataset)
      
    2. Visualizza i risultati della valutazione chiamando .show() sull'oggetto EvaluationResult per visualizzare le metriche di riepilogo e i risultati dettagliati:

      eval_result.show()
      

    L'immagine seguente mostra un report di valutazione, che mostra le metriche di riepilogo e i risultati dettagliati per ogni coppia prompt-risposta.

    Un report di valutazione che mostra le metriche di riepilogo insieme ai risultati dettagliati per ogni coppia prompt-risposta.

    Esegui la pulizia

    Durante questo tutorial non vengono create risorse Vertex AI.

    Passaggi successivi