适用于 ADK 代理的 Google Gemini 模型¶
ADK 支持 Google Gemini 系列的生成式 AI 模型,这些模型具备强大的功能与广泛的应用场景。ADK 支持 Gemini 的许多功能,包括…… 代码执行, Google 搜索, 上下文缓存(Context caching), 计算机的使用 以及 交互 API(Interactions API).
立即开始使用吧!¶
以下代码示例展示了如何在您的代理程序中使用 Gemini 模型进行基本实现:
from google.adk.agents import LlmAgent
# --- Example using a stable Gemini Flash model ---
agent_gemini_flash = LlmAgent(
# Use the latest stable Flash model identifier
model="gemini-flash-latest",
name="gemini_flash_agent",
instruction="You are a fast and helpful Gemini assistant.",
# ... other agent parameters
)
import {LlmAgent} from '@google/adk';
// --- Example #2: using a powerful Gemini Pro model with API Key in model ---
export const rootAgent = new LlmAgent({
name: 'hello_time_agent',
model: 'gemini-flash-latest',
description: 'Gemini flash agent',
instruction: `You are a fast and helpful Gemini assistant.`,
});
import (
"google.golang.org/adk/v2/agent/llmagent"
"google.golang.org/adk/v2/model/gemini"
"google.golang.org/genai"
)
// --- Example using a stable Gemini Flash model ---
modelFlash, err := gemini.NewModel(ctx, "gemini-2.0-flash", &genai.ClientConfig{})
if err != nil {
log.Fatalf("failed to create model: %v", err)
}
agentGeminiFlash, err := llmagent.New(llmagent.Config{
// Use the latest stable Flash model identifier
Model: modelFlash,
Name: "gemini_flash_agent",
Instruction: "You are a fast and helpful Gemini assistant.",
// ... other agent parameters
})
if err != nil {
log.Fatalf("failed to create agent: %v", err)
}
// --- Example #1: using a stable Gemini Flash model with ENV variables---
LlmAgent agentGeminiFlash =
LlmAgent.builder()
// Use the latest stable Flash model identifier
.model("gemini-flash-latest") // Set ENV variables to use this model
.name("gemini_flash_agent")
.instruction("You are a fast and helpful Gemini assistant.")
// ... other agent parameters
.build();
import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.models.Gemini
// --- Example using a stable Gemini Flash model ---
val agentGeminiFlash = LlmAgent(
// Use the latest stable Flash model identifier
name = "gemini_flash_agent",
model = Gemini(name = "gemini-flash-latest"),
instruction = Instruction("You are a fast and helpful Gemini assistant."),
// ... other agent parameters
)
注意:Gemini模型选择器 gemini-flash-latest
ADK文档中的大多数代码示例都使用了…… gemini-flash-latest 用于选择
最新可用版本
这是 Gemini Flash 的版本。不过,如果您通过某个区域性的终端(regionally-defined endpoint)来访问 Gemini,那么…… us-central1如果这个选择字符串无法正常使用,那么请使用该模型对应的特定版本号字符串。
Gemini系列模型 “page” 或 “Google Cloud” Gemini系列模型 list.
Gemini模型的身份验证(Authentication)¶
当通过某些服务(例如 Gemini API 或 Google Cloud 上的 Gemini Enterprise Agent Platform)使用 AI 模型时,您必须提供 API 密钥或通过该服务进行身份验证。提供这些信息的最直接方法是使用环境变量。 .env 以下示例展示了配置代理以用于 Gemini API 或 Gemini Enterprise Agent Platform 的最常见方法。
有关将 ADK 代理连接到由 Google Cloud 托管的模型和服务的更多详细信息(包括 Gemini Enterprise Agent Platform),请参阅…… 连接到 Google Cloud 和 Agent Platform。 guide.
支持语音和视频流传输¶
为了在 ADK 中使用语音/视频流功能,您需要使用支持 Live API 的 Gemini 模型。您可以在相关文档或资源中找到这些模型的详细信息。 模型 ID(Model IDs) 在文档中提供了对 Gemini Live API 的支持信息:
Gemini Interactions API¶
双子座(The Gemini) 交互 API(Interactions API)
它是……的另一种选择。 generateContent 推理(Inference)API:该API提供了支持“有状态对话”(stateful conversations)的功能,使您能够通过该API串联多个交互操作(interactions)。
previous_interaction_id 与其在每次请求时都发送完整的对话记录,不如使用这一功能。对于较长的对话来说,这种方式会更加高效。
您可以通过设置相关参数来启用“Interactions API”。 use_interactions_api=True
在 Gemini 模型配置中,该参数的作用如下所示(代码片段如下):
from google.adk.agents.llm_agent import Agent
from google.adk.models.google_llm import Gemini
from google.adk.tools.google_search_tool import GoogleSearchTool
root_agent = Agent(
model=Gemini(
model="gemini-flash-latest",
use_interactions_api=True, # Enable Interactions API
),
name="interactions_test_agent",
tools=[
GoogleSearchTool(bypass_multi_tools_limit=True), # Converted to function tool
get_current_weather, # Custom function tool
],
)
有关完整的代码示例,请参见…… 交互 API 示例.
启用 Interactions API 同时还允许您为每次运行选择相应的服务层级(即服务器资源的使用级别)。 有关如何将模型调用安排在非高峰时段执行的详细信息,请参阅…… 延迟调度(Deferred scheduling).
已知的局限性¶
“Interactions API” 不;并非如此 支持将自定义的函数调用工具与内置工具(例如……)结合使用。
Google 搜索在同一代理(agent)内部,您可以通过配置内置工具来使其以自定义工具的方式运行,从而规避这一限制。 bypass_multi_tools_limit
parameter:
在这个例子中,该选项会将内置的功能(或组件)进行转换。 google_search 通过调用工具(via)将数据传递给某个函数。 GoogleSearchAgentTool),这使得它能够与自定义函数工具一起使用。
故障排除¶
错误代码 429 – 资源耗尽(Resource Exhausted)¶
这个错误通常发生在你的请求数量超过了系统为处理请求而分配的容量时。
为了缓解这个问题,您可以采取以下措施之一:
-
请为您尝试使用的模型申请更高的配额限制。
-
启用客户端端的重试功能。通过重试,客户端可以在延迟一段时间后自动重新发送请求;如果配额问题只是暂时的,这种方式会非常有用。
有两种方法可以设置重试选项:
选项 1: 将重试选项设置为代理(Agent)的配置的一部分。
generate_content_config.如果您将模型作为字符串传递,并让 ADK 为您生成模型适配器,那么就可以使用这个选项。
import com.google.adk.agents.LlmAgent; import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.HttpOptions; import com.google.genai.types.HttpRetryOptions; // ... LlmAgent rootAgent = LlmAgent.builder() .model("gemini-flash-latest") // ... .generateContentConfig(GenerateContentConfig.builder() // ... .httpOptions(HttpOptions.builder() // ... .retryOptions(HttpRetryOptions.builder().initialDelay(1.0).attempts(2).build()) // ... .build()) // ... .build()) .build();选项 2: 此模型适配器提供的重试选项。
如果您需要自己手动创建适配器的实例,那么就可以使用这个选项。
import com.google.adk.agents.LlmAgent; import com.google.adk.models.Gemini; import com.google.genai.Client; import com.google.genai.types.HttpOptions; import com.google.genai.types.HttpRetryOptions; // ... LlmAgent agent = LlmAgent.builder() .model(Gemini.builder() .modelName("gemini-flash-latest") .apiClient(Client.builder() .httpOptions(HttpOptions.builder() .retryOptions(HttpRetryOptions.builder().initialDelay(1.0).attempts(2).build()) .build()) .build()) .build()) .build();在 Kotlin 中,你可以通过创建相应的代码来实现这一目标。
Client将自己实例化(即创建一个自己的对象),然后将其传递给其他函数或方法。Geminiconstructor.import com.google.adk.kt.agents.LlmAgent import com.google.adk.kt.models.Gemini import com.google.genai.Client import com.google.genai.types.HttpOptions import com.google.genai.types.HttpRetryOptions val client = Client.builder() .apiKey("YOUR_API_KEY") .httpOptions(HttpOptions.builder() .retryOptions(HttpRetryOptions.builder().initialDelay(1.0).attempts(2).build()) .build()) .build() val model = Gemini(client = client, name = "gemini-flash-latest") val agent = LlmAgent( name = "my_agent", model = model // ... )