借助 Gemini Enterprise Agent Platform 快速模式,您可以快速试用 Gemini Enterprise Agent Platform 上提供的核心 生成式 AI 功能。本教程介绍了如何使用 Agent Platform API 快速模式执行以下任务:
- 安装并初始化 Google Gen AI SDK 快速模式。
向 Gemini for Google Cloud API 发送请求,包括以下内容:
- 非流式请求
- 流式请求
- 函数调用请求
准备工作
在执行本页中所述的任务之前,请先注册快速模式。
安装并初始化 Google Gen AI SDK 快速模式
借助 Google Gen AI SDK,您可以使用 Google 生成式 AI 模型和功能来构建 AI 赋能的应用。在快速模式下使用 Gemini Enterprise Agent Platform 时,请安装并初始化 google-genai 软件包,以便使用生成的 API 密钥进行身份验证。
安装
如需安装 Google Gen AI SDK 快速模式,请运行以下命令:
# Developer TODO: If you're using Colab, uncomment the following lines:
# from google.colab import auth
# auth.authenticate_user()
!pip install google-genai
!pip install --force-reinstall -qq "numpy<2.0"
如果您使用的是 Colab,请忽略任何依赖项冲突,并在安装后重启运行时。
初始化
配置快速模式的 API 密钥和环境变量。如需详细了解如何获取 API 密钥,请参阅 Gemini Enterprise Agent Platform 快速模式概览。
from google import genai
from google.genai import types
# Developer TODO: Replace YOUR_API_KEY with your API key.
API_KEY = "YOUR_API_KEY"
client = genai.Client(
vertexai=True, api_key=API_KEY
)
向 Gemini for Google Cloud API 发送请求
您可以向 Gemini for Google Cloud API 发送流式请求或非流式请求。流式请求会在处理请求时以块的形式返回响应。对于人类用户而言,流式响应可以减少延迟感。非流式请求会在处理完请求后以一个块的形式返回响应。
流式请求
如需发送流式请求,请设置 stream=True 并以块的形式输出响应。
from google import genai
from google.genai import types
def generate():
client = genai.Client(vertexai=True, api_key=YOUR_API_KEY)
config=types.GenerateContentConfig(
temperature=0,
top_p=0.95,
top_k=20,
candidate_count=1,
seed=5,
max_output_tokens=100,
stop_sequences=["STOP!"],
presence_penalty=0.0,
frequency_penalty=0.0,
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
for chunk in client.models.generate_content_stream(
model="gemini-2.5-flash-lite",
contents="Explain bubble sort to me",
config=config,
):
print(chunk.text)
generate()
非流式请求
以下代码示例定义了一个向 gemini-2.5-flash-lite 发送非流式请求的函数。它向您展示了如何配置基本请求参数和安全设置。
from google import genai
from google.genai import types
def generate():
client = genai.Client(vertexai=True, api_key=YOUR_API_KEY)
config=types.GenerateContentConfig(
temperature=0,
top_p=0.95,
top_k=20,
candidate_count=1,
seed=5,
max_output_tokens=100,
stop_sequences=["STOP!"],
presence_penalty=0.0,
frequency_penalty=0.0,
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
response = client.models.generate_content(
model="gemini-2.5-flash-lite",
contents="Explain bubble sort to me",
config=config,
)
print(response.text)
generate()
函数调用请求
以下代码示例声明了一个函数并将其作为工具传递,然后在响应中接收函数调用部分。从模型收到函数调用部分后,您可以调用该函数并获取响应,然后将响应传递给模型。
function_response_parts = [
{
'function_response': {
'name': 'get_current_weather',
'response': {
'name': 'get_current_weather',
'content': {'weather': 'super nice'},
},
},
},
]
manual_function_calling_contents = [
{'role': 'user', 'parts': [{'text': 'What is the weather in Boston?'}]},
{
'role': 'model',
'parts': [{
'function_call': {
'name': 'get_current_weather',
'args': {'location': 'Boston'},
}
}],
},
{'role': 'user', 'parts': function_response_parts},
]
function_declarations = [{
'name': 'get_current_weather',
'description': 'Get the current weather in a city',
'parameters': {
'type': 'OBJECT',
'properties': {
'location': {
'type': 'STRING',
'description': 'The location to get the weather for',
},
'unit': {
'type': 'STRING',
'enum': ['C', 'F'],
},
},
},
}]
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents=manual_function_calling_contents,
config=dict(tools=[{'function_declarations': function_declarations}]),
)
print(response.text)
清理
本教程不会创建任何 Google Cloud 资源,因此无需清理以避免产生费用。
后续步骤
- 试用 Agent Studio 教程,了解 Gemini Enterprise Agent Platform 的 快速模式。
- 查看 Gemini Enterprise Agent Platform 快速模式的完整 API 参考文档。