本快速入门将引导您设置 Google Cloud 项目、安装代理开发套件 (ADK)、设置基本代理,以及运行其开发者界面。
本快速入门假定您使用的是具有 Python 3.10 及更高版本和终端访问权限的本地 IDE(VS Code、PyCharm 等)。该代理完全在您的机器上运行,适用于本地应用开发。
准备工作
请完成以下步骤:
设置 Google Cloud 项目
设置您的 Google Cloud 项目并启用 Vertex AI API。
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
设置凭据
在本地终端上,使用 Google Cloud CLI 进行设置和身份验证。如果您熟悉 Google AI Studio 中的 Gemini API,请注意,Vertex AI Gemini API 使用 Identity and Access Management(而不是 API 密钥)来管理访问权限。
-
安装并初始化 Google Cloud CLI。
-
如果您之前安装了 gcloud CLI,请运行以下命令,确保您的
gcloud
组件已更新。gcloud components update
-
运行以下命令以生成本地应用默认凭据 (ADC) 文件。您的代理将在本地应用开发期间使用这些凭据访问 Vertex AI。
gcloud auth application-default login
如需了解详情,请参阅设置应用默认凭据。
设置虚拟环境并安装 ADK
创建并激活虚拟环境(推荐):
# Create python -m venv .venv # Activate (uncomment the line relevant to your environment) # macOS/Linux: source .venv/bin/activate # Windows CMD: .venv\Scripts\activate.bat # Windows PowerShell: .venv\Scripts\Activate.ps1
安装 ADK:
pip install google-adk
创建代理
使用终端创建文件夹结构:
mkdir multi_tool_agent/
touch \
multi_tool_agent/__init__.py \
multi_tool_agent/agent.py \
multi_tool_agent/.env
您的结构:
parent_folder/
multi_tool_agent/
__init__.py
agent.py
.env
将以下代码复制并粘贴到您创建的以下三个文件中:
__init__.py
from . import agent
.env
# If using Gemini via Vertex AI on Google CLoud
GOOGLE_CLOUD_PROJECT="your-project-id"
GOOGLE_CLOUD_LOCATION="your-location" #e.g. us-central1
GOOGLE_GENAI_USE_VERTEXAI="True"
agent.py
from google.adk.agents import Agent
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Returns:
dict: A dictionary containing the weather information with a 'status' key ('success' or 'error') and a 'report' key with the weather details if successful, or an 'error_message' if an error occurred.
"""
if city.lower() == "new york":
return {"status": "success",
"report": "The weather in New York is sunny with a temperature of 25 degrees Celsius (41 degrees Fahrenheit)."}
else:
return {"status": "error",
"error_message": f"Weather information for '{city}' is not available."}
def get_current_time(city:str) -> dict:
"""Returns the current time in a specified city.
Args:
dict: A dictionary containing the current time for a specified city information with a 'status' key ('success' or 'error') and a 'report' key with the current time details in a city if successful, or an 'error_message' if an error occurred.
"""
import datetime
from zoneinfo import ZoneInfo
if city.lower() == "new york":
tz_identifier = "America/New_York"
else:
return {"status": "error",
"error_message": f"Sorry, I don't have timezone information for {city}."}
tz = ZoneInfo(tz_identifier)
now = datetime.datetime.now(tz)
return {"status": "success",
"report": f"""The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}"""}
root_agent = Agent(
name="weather_time_agent",
model="gemini-2.0-flash",
description="Agent to answer questions about the time and weather in a city.",
instruction="I can answer your questions about the time and weather in a city.",
tools=[get_weather, get_current_time]
)
该代理配备了两个具有模拟实现的函数工具。
运行和测试代理
在终端中,前往代理的父级目录(例如,使用
cd ..
):parent_folder/ <-- navigate to this directory multi_tool_agent/ __init__.py agent.py .env
运行以下命令可启动开发者 Web 界面。
adk web
在浏览器中打开提供的网址(通常为
http://localhost:8000
或http://127.0.0.1:8000
)。此连接完全在本地机器上进行。选择multi_tool_agent
并与客服人员互动。
建议尝试的提示示例
您可以尝试使用以下提示:
- 纽约的天气怎么样?
- 纽约现在是几点?
- 巴黎的天气怎么样?
- 巴黎现在是几点?