快速模式下的 Gemini Enterprise Agent Platform 可让您试用 Agent Platform 上提供的核心生成式 AI 功能。本教程将介绍如何使用快速模式下的 Agent Platform API 完成以下任务:
- 安装并初始化 Google Gen AI SDK(快速模式)。
向 Gemini for Google Cloud API 发送请求,其中包含以下内容:
- 流式请求
- 非流式传输请求
- 函数调用请求
准备工作
在执行本文档中所述的任务之前,请注册使用快速模式。
安装并初始化快速模式的 SDK
借助 Google Gen AI SDK,您可以使用 Google 生成式 AI 模型和功能来构建 AI 赋能的应用。在快速模式下使用 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 --upgrade google-genai
如果您使用的是 Colaboratory,请在安装后根据提示重启运行时。
初始化
为快速模式配置 API 密钥,并使用 enterprise=True 初始化客户端。如需详细了解如何获取 API 密钥,请参阅快速模式下的 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(
enterprise=True, api_key=API_KEY
)
向 Gemini for Google Cloud API 发送请求
您可以向 Gemini for Google Cloud API 发送流式传输请求或非流式传输请求。流式传输请求会在处理请求时以块的形式返回响应。非流式请求会在处理完成后返回完整响应。
流式请求
如需发送流式请求,请调用 client.models.generate_content_stream() 并打印每个到达的块:
from google import genai
from google.genai import types
def generate():
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="MEDIUM",
),
seed=5,
max_output_tokens=1024,
stop_sequences=["STOP!"],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
for chunk in client.models.generate_content_stream(
model="gemini-3.8-flash",
contents="Explain bubble sort to me",
config=config,
):
print(chunk.text, end="")
generate()
非流式传输请求
以下代码示例定义了一个向 Gemini 3.8 Flash (gemini-3.8-flash) 发送非流式请求的函数。它展示了如何配置思维水平、输出参数和安全设置:
from google import genai
from google.genai import types
def generate():
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="MEDIUM",
),
seed=5,
max_output_tokens=1024,
stop_sequences=["STOP!"],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
response = client.models.generate_content(
model="gemini-3.8-flash",
contents="Explain bubble sort to me",
config=config,
)
print(response.text)
generate()
函数调用请求
以下代码示例声明了一个函数工具,向 3.8 Flash 发送初始提示,接收响应中的函数调用部分,然后将匹配的 FunctionResponse 发送回模型以生成最终响应:
from google import genai
from google.genai import types
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
get_weather_declaration = types.FunctionDeclaration(
name="get_current_weather",
description="Gets the current weather in a given city.",
parameters={
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, such as Boston, MA.",
},
"unit": {
"type": "STRING",
"enum": ["C", "F"],
},
},
"required": ["location"],
},
)
tools = [types.Tool(function_declarations=[get_weather_declaration])]
prompt = "What is the weather in Boston?"
first_response = client.models.generate_content(
model="gemini-3.8-flash",
contents=prompt,
config=types.GenerateContentConfig(tools=tools),
)
function_call = first_response.function_calls[0]
contents = [
types.Content(role="user", parts=[types.Part(text=prompt)]),
first_response.candidates[0].content,
types.Content(
role="user",
parts=[
types.Part(
function_response=types.FunctionResponse(
id=function_call.id,
name=function_call.name,
response={"weather": "sunny", "temperature": "72F"},
)
)
],
),
]
response = client.models.generate_content(
model="gemini-3.8-flash",
contents=contents,
config=types.GenerateContentConfig(tools=tools),
)
print(response.text)
清理
本教程不会创建任何 Google Cloud 资源,因此无需清理即可避免产生费用。
后续步骤
- 不妨尝试 Agent Platform 的快速模式 Agent Studio 教程。
- 如需了解快速模式下的 Agent Platform 的完整 API 参考文档,请点击此处。