Anda dapat menggunakan layanan evaluasi AI generatif untuk mengevaluasi kemampuan agen dalam menyelesaikan tugas dan sasaran untuk kasus penggunaan tertentu.
Halaman ini menunjukkan cara membuat dan men-deploy agen dasar serta menggunakan layanan evaluasi AI Generatif untuk mengevaluasi agen:
Mengembangkan agen: Tentukan agen dengan fungsi alat dasar.
Men-deploy agen: Men-deploy agen ke Vertex AI Agent Engine Runtime.
Jalankan inferensi agen: Tentukan set data evaluasi dan jalankan inferensi agen untuk membuat respons.
Buat proses evaluasi: Buat proses evaluasi untuk melakukan evaluasi.
Melihat hasil evaluasi: Melihat hasil evaluasi melalui proses evaluasi.
Sebelum memulai
-
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 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.
Instal Vertex AI SDK untuk Python:
%pip install google-cloud-aiplatform[adk,agent_engines] %pip install --upgrade --force-reinstall -q google-cloud-aiplatform[evaluation]Siapkan kredensial Anda. Jika Anda menjalankan tutorial ini di Colaboratory, jalankan perintah berikut:
from google.colab import auth auth.authenticate_user()Untuk lingkungan lain, lihat Mengautentikasi ke Vertex AI.
Menginisialisasi Klien GenAI di Vertex AI SDK:
import vertexai from vertexai import Client from google.genai import types as genai_types GCS_DEST = "gs://BUCKET_NAME/output-path" vertexai.init( project=PROJECT_ID, location=LOCATION, ) client = Client( project=PROJECT_ID, location=LOCATION, http_options=genai_types.HttpOptions(api_version="v1beta1"), )Ganti kode berikut:
BUCKET_NAME: Nama bucket Cloud Storage. Lihat Membuat bucket untuk mempelajari lebih lanjut cara membuat bucket.
PROJECT_ID: Project ID Anda.
LOCATION: Region yang Anda pilih.
Buat respons model untuk set data Anda menggunakan
run_inference():Siapkan set data sebagai DataFrame Pandas. Perintah harus spesifik untuk agen Anda. Input sesi diperlukan untuk rekaman aktivitas. Untuk mengetahui informasi selengkapnya, lihat Sesi: Melacak Percakapan Individual.
import pandas as pd from vertexai import types session_inputs = types.evals.SessionInput( user_id="user_123", state={}, ) agent_prompts = [ "Search for 'noise-cancelling headphones'.", "Show me the details for product 'B08H8H8H8H'.", "Add one pair of 'B08H8H8H8H' to my shopping cart.", "Find 'wireless earbuds' and then add the first result to my cart.", "I need a new laptop for work, can you find one with at least 16GB of RAM?", ] agent_dataset = pd.DataFrame({ "prompt": agent_prompts, "session_inputs": [session_inputs] * len(agent_prompts), })Membuat respons model menggunakan
run_inference():agent_dataset_with_inference = client.evals.run_inference( agent=agent_engine_resource_name, src=agent_dataset, )Visualisasikan hasil inferensi Anda dengan memanggil
.show()pada objekEvaluationDatasetuntuk memeriksa output model bersama dengan perintah dan referensi asli Anda:agent_dataset_with_inference.show()Gambar berikut menampilkan set data evaluasi dengan perintah dan
intermediate_eventssertaresponsesyang dihasilkan yang sesuai:
Ambil
agent_infomenggunakan fungsi bantuan bawaan:agent_info = types.evals.AgentInfo.load_from_agent( my_agent, agent_engine_resource_name )Mengevaluasi respons model menggunakan metrik berbasis rubrik adaptif spesifik per agen (
FINAL_RESPONSE_QUALITY,TOOL_USE_QUALITY, danHALLUCINATION):evaluation_run = client.evals.create_evaluation_run( dataset=agent_dataset_with_inference, agent_info=agent_info, metrics=[ types.RubricMetric.FINAL_RESPONSE_QUALITY, types.RubricMetric.TOOL_USE_QUALITY, types.RubricMetric.HALLUCINATION, types.RubricMetric.SAFETY, ], dest=GCS_DEST, )- Mengembangkan agen.
- Men-deploy agen.
- Gunakan agen.
- Pelajari lebih lanjut layanan evaluasi AI Generatif
Mengembangkan agen
Kembangkan agen Agent Development Kit (ADK) dengan menentukan model, petunjuk, dan kumpulan alat. Untuk mengetahui informasi selengkapnya tentang cara mengembangkan agen, lihat Mengembangkan agen Agent Development Kit.
from google.adk import Agent
# Define Agent Tools
def search_products(query: str):
"""Searches for products based on a query."""
# Mock response for demonstration
if "headphones" in query.lower():
return {"products": [{"name": "Wireless Headphones", "id": "B08H8H8H8H"}]}
else:
return {"products": []}
def get_product_details(product_id: str):
"""Gets the details for a given product ID."""
if product_id == "B08H8H8H8H":
return {"details": "Noise-cancelling, 20-hour battery life."}
else:
return {"error": "Product not found."}
def add_to_cart(product_id: str, quantity: int):
"""Adds a specified quantity of a product to the cart."""
return {"status": f"Added {quantity} of {product_id} to cart."}
# Define Agent
my_agent = Agent(
model="gemini-2.5-flash",
name='ecommerce_agent',
instruction='You are an ecommerce expert',
tools=[search_products, get_product_details, add_to_cart],
)
Men-deploy agen
Deploy agen Anda ke Vertex AI Agent Engine Runtime. Proses ini dapat memerlukan waktu hingga 10 menit. Mengambil nama resource dari agen yang di-deploy.
def deploy_adk_agent(root_agent):
"""Deploy agent to agent engine.
Args:
root_agent: The ADK agent to deploy.
"""
app = vertexai.agent_engines.AdkApp(
agent=root_agent,
)
remote_app = client.agent_engines.create(
agent=app,
config = {
"staging_bucket": gs://BUCKET_NAME,
"requirements": ['google-cloud-aiplatform[adk,agent_engines]'],
"env_vars": {"GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY": "true"}
}
)
return remote_app
agent_engine = deploy_adk_agent(my_agent)
agent_engine_resource_name = agent_engine.api_resource.name
Untuk mendapatkan daftar agen yang di-deploy ke Vertex AI Agent Engine, lihat Mengelola agen yang di-deploy.
Membuat respons
Menjalankan evaluasi agen
Jalankan create_evaluation_run() untuk mengevaluasi respons agen.
Melihat hasil evaluasi agen
Anda dapat melihat hasil evaluasi menggunakan Vertex AI SDK.
Ambil proses evaluasi dan visualisasikan hasil evaluasi dengan memanggil
.show() untuk menampilkan metrik ringkasan dan hasil mendetail:
evaluation_run = client.evals.get_evaluation_run(
name=evaluation_run.name,
include_evaluation_items=True
)
evaluation_run.show()
Gambar berikut menampilkan laporan evaluasi, yang menunjukkan metrik ringkasan, informasi agen, dan hasil mendetail untuk setiap pasangan perintah-respons. Hasil mendetail juga mencakup rekaman aktivitas yang menunjukkan interaksi agen. Untuk mengetahui informasi selengkapnya tentang rekaman aktivitas, lihat Merekam aktivitas agen.

Langkah berikutnya
Coba notebook berikut: