选择您的开发环境
首先,您需要一个安装了 Python 3.11 或更高版本的 Python 开发环境。此处的步骤已在 Google Colab 中进行了测试。
您还可以在其他开发环境(例如 Colab Enterprise、Cloud Shell 编辑器或本地机器)中调整此处显示的步骤。
环境准备就绪后,您可以继续安装 Agent Framework SDK。
安装 Agent Framework SDK
现在,您将安装 Agent Framework SDK。在非公开预览版期间,您需要列入许可名单才能访问该 SDK。请确保您已列入许可名单,然后再继续执行以下下载和安装步骤。
首先,使用 gcloud CLI 对您的 Google Cloud 账号进行身份验证。此步骤可确保您拥有访问 Google Cloud 服务所需的权限。如有需要,请参阅 gcloud 文档了解详情。
gcloud auth login
使用 gcloud CLI 将 Agent Framework SDK Python 软件包下载到您的开发环境:
gcloud storage cp gs://agent_framework/latest/google_genai_agents-0.0.2.dev20250204+723246417-py3-none-any.whl .
请注意,您需要列入许可名单,Agent Framework 才能访问上一个命令中的开发 build。
最后,使用 pip
安装下载的 Agent Framework SDK 软件包:
pip install google_genai_agents-0.0.2.dev20250204+723246417-py3-none-any.whl
安装 Agent Framework SDK 后,您现在可以导入必要的库,开始构建代理了。
导入所需的库
导入以下 Python 库,这些库是将此快速入门与 Agent Framework 搭配使用所必需的:
import os
from agents import Runner, Agent
from datetime import datetime
导入库后,下一步是配置对 Gemini 模型的访问权限。
配置对 Gemini API 的访问权限
Agent Framework 可配置为使用 Google 的 Gemini 模型。您可以通过 Google AI Studio(使用 API 密钥)或 Vertex AI(使用 Google Cloud 项目)配置对 Gemini 模型的访问权限。选择适合您需求的配置方法,然后按照以下相应说明操作。
如需使用 Google AI Studio 配置对 Gemini 模型的访问权限,请运行以下 Python 代码。将 YOUR_API_KEY
替换为您的实际 Google AI Studio API 密钥。
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "0"
os.environ["GOOGLE_API_KEY"] = "YOUR_API_KEY"
如需使用 Vertex AI 配置对 Gemini 模型的访问权限,请运行以下 Python 代码。将 YOUR_PROJECT_ID
替换为您的 Google Cloud 项目 ID。确保将 GOOGLE_CLOUD_LOCATION
设置为受支持的 Vertex AI 区域。
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "1"
os.environ["GOOGLE_CLOUD_PROJECT"] = "YOUR_PROJECT_ID"
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
配置 Gemini API 访问权限后,您现在可以定义客服人员将使用的工具了。
为您的智能体定义工具
代理通常使用工具与外部服务和数据进行交互。在此步骤中,您将定义一个名为 get_exchange_rate
的 Python 函数。此函数将作为一种工具,供客服人员从外部 API 检索实时货币汇率。
def get_exchange_rate(
currency_from: str,
currency_to: str,
currency_date: str,
):
"""Retrieves the exchange rate between two currencies on a specified date."""
import requests
response = requests.get(
f"https://api.frankfurter.app/{currency_date}",
params={"from": currency_from, "to": currency_to},
)
return response.json()
现在,您已为客服人员定义了一款工具。在下一部分中,您将定义代理本身,并将此工具纳入其中。
定义您的代理
现在,您将使用 Agent Framework SDK 定义 AI 智能体。智能体定义指定了它将使用的模型、其指令、它可以访问的工具以及其互动流程。
root_agent = Agent(
model="gemini-2.5-pro-exp-03-25",
name='currency_exchange_agent',
instruction="""
Help users retrieve exchange rates for different currencies
""",
tools=[
get_exchange_rate,
],
flow='single',
)
定义代理后,下一步是在 Agent Framework 中对其进行初始化。
初始化代理
在本部分中,您将初始化代理。以下代码会设置内存中的会话和制品服务,为代理实例化一个 Runner,并定义一个用于发送提示的辅助函数。
from agents.sessions.in_memory_session_service import InMemorySessionService
from agents.artifacts.in_memory_artifact_service import InMemoryArtifactService
from google.genai import types
session_service = InMemorySessionService()
artifact_service = InMemoryArtifactService()
runner = Runner(app_name="currency_exchange_agent", agent=root_agent, artifact_service=artifact_service, session_service=session_service)
session = session_service.create(app_name="currency_exchange_agent", user_id="1")
def run_prompt(new_message: str):
content = types.Content(role='user', parts=[types.Part.from_text(text=new_message)])
print (content)
for event in runner.run(
session=session,
new_message=content,
):
if event.content:
print(event.content.model_dump(exclude_none=True))
您的代理现已初始化完毕,可以接收提示了。在下一部分中,您将发送一个测试提示,以查看其实际效果。
向客服人员发送提示
现在可以测试您的代理了。使用您之前定义的 run_prompt 函数向代理发送以下提示。
run_prompt("What's the exchange rate from US dollars to Swedish currency today?")
使用提供的输入运行 run_prompt
函数,代理应生成类似以下的输出:
parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=None, inline_data=None, text="What's the exchange rate from US dollars to Swedish currency today?")] role='user'
{'parts': [{'function_call': {'args': {'currency_from': 'USD', 'currency_to': 'SEK', 'currency_date': '2025-02-21'}, 'name': 'get_exchange_rate'}}], 'role': 'model'}
{'parts': [{'function_response': {'name': 'get_exchange_rate', 'response': {'amount': 1.0, 'base': 'USD', 'date': '2025-02-21', 'rates': {'SEK': 10.6474}}}}], 'role': 'user'}
{'parts': [{'text': 'The exchange rate from US dollars to Swedish currency (SEK) today, 2025-02-21, is 1 USD to 10.6474 SEK.\n'}], 'role': 'model'}
此输出显示了代理的内部步骤,包括其所发出的函数调用和最终回答。
检查输出的最后一部分。您应该会看到客服人员发送的消息,其中包含检索到的汇率,表明客服人员成功使用了 get_exchange_rate
工具:
The exchange rate from US dollars to Swedish currency (SEK) today, 2025-02-21,
is 1 USD to 10.6474 SEK.
恭喜!您已成功向代理发送提示并观察到其回答。如需进一步探索 Agent 框架,请参阅下一部分,了解相关资源和后续步骤。
后续步骤和资源
恭喜,您已成功使用 Agent Framework 构建 AI 客服人员!
如需加深理解并探索 Agent 框架的更多高级功能,请参阅文档的其他部分、代码示例或记事。这些资源将引导您构建更复杂的代理并将其集成到应用中。