您可以在快捷模式下使用 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 參考資料,請參閱這篇文章。