ADK 代理接入 Gemini 模型的技术要点

ADK 在 Python、TypeScript、Go、Java、Kotlin 中支持接入 Gemini 模型,需通过环境变量或 .env 文件提供 API 密钥或完成服务认证。语音与视频流式能力依赖支持 Live API 的模型 ID,Interactions API 则用 previous_interaction_id 维持有状态对话。

## Google 用于 ADK 代理的双子座模型[¶](<https://adk.dev/agents/models/google-gemini/#google-gemini-models-for-adk-agents>) 在ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.2.0Kotlin v0.1.0中支持 互联网档案馆的存檔,存档日期2011-09-02. ADK支持Google双子座家族基因化的AI模型,这些模型提供了一套具有广泛特性的强大模型. ADK为许多双子座特征提供了支持,其中包括: [代码执行](<https://adk.dev/integrations/code-execution/>), [谷歌搜索](<https://adk.dev/integrations/google-search/>), [上下文缓存](<https://adk.dev/context/caching/>), [计算机使用](<https://adk.dev/integrations/computer-use/>) 页:1 [互动 API](<https://adk.dev/agents/models/google-gemini/#interactions-api>). ## 开始吧[¶](<https://adk.dev/agents/models/google-gemini/#get-started>) 以下代码示例显示在您的代理中使用双子体模型的基本执行 : PythonTypeScriptGoJavaKotlin ``` 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-flash-latest` ADK文档使用中的大多数代码示例 `gemini-flash-latest` 选择 [最新可用数据](<https://ai.google.dev/gemini-api/docs/models#latest>) 双子闪光版本。 但是, 如果您从区域端点访问双子闪光, 例如 `us-central1`,此选择字符串可能行不通。在这种情况下,使用来自 [双子座模型](<https://ai.google.dev/gemini-api/docs/models>) 页面或 Google 云 [双子座模型](<https://docs.cloud.google.com/vertex-ai/generative-ai/docs/models>) 列表中选项。 ## 双子座模型认证[¶](<https://adk.dev/agents/models/google-gemini/#gemini-model-authentication>) 在通过服务使用AI模型时,如双子座API或双子座企业代理平台在Google Cloud上,您必须提供API密钥或认证服务。提供这种信息的最直接方式是使用环境变量或一个 `.env` 。以下示例显示了用于双子公司API或双子企业代理平台的最常用的配置代理方式。 双子APIGoogle 云代理平台 ``` # .env configuration file GOOGLE_API_KEY="PASTE_YOUR_GEMINI_API_KEY_HERE" ``` ``` # .env configuration file GOOGLE_CLOUD_PROJECT=your-project-id GOOGLE_CLOUD_LOCATION=location-code # example: us-central1 GOOGLE_GENAI_USE_ENTERPRISE=True ``` 关于将ADK代理连接到Google Cloud托管模型和服务,包括双子企业代理平台的更多细节,请参见: [连接到 Google 云和代理平台](<https://adk.dev/get-started/google-cloud/>) 向导。 ## 语音和视频流支持[¶](<https://adk.dev/agents/models/google-gemini/#voice-and-video-streaming-support>) 为了在 ADK 中使用语音/视频流, 您需要使用支持 Live 的双子模型 API 。 您可以找到 。 **型号 ID(s)** 在文档中支持双子座 Live API 的: - [Google AI 工作室:双子直播 API](<https://ai.google.dev/gemini-api/docs/models#live-api>) - [代理平台:双子座Live API](<https://cloud.google.com/vertex-ai/generative-ai/docs/live-api>) ## 双子体相互作用 API[¶](<https://adk.dev/agents/models/google-gemini/#interactions-api>) 支持于 ADKPython v1.21.0 双子座 [互动 API](<https://ai.google.dev/gemini-api/docs/interactions>) 是一种替代 ***generateContent*** 推论 API 提供了状态性的对话能力,允许您使用一个 `previous_interaction_id` 而不是发送每个请求的全部对话历史 。 使用此特性可以更高效地进行长对话 。 您可以通过设置 API 来启用交互 `use_interactions_api=True` 双子座模型配置中的参数,如下代码片段所示: Python 键 ``` 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 样本](<https://github.com/google/adk-python/tree/main/contributing/samples/models/interactions_api>). 启用 Interactives API 也允许您为每次运行选择一个服务级。 有关排队模式的详细信息, 请查看 。 [推迟的时间安排](<https://adk.dev/agents/models/google-gemini/deferred-schedule/>). ### 已知的限制[¶](<https://adk.dev/agents/models/google-gemini/#known-limitations>) 互动 API **没有** 支持将自定义函数调用工具与内置工具混合,例如 [谷歌搜索](<https://adk.dev/integrations/google-search/>),工具,在同一代理工具内。您可以通过配置内置工具来围绕这一限制工作,以便使用该工具作为自定义工具运行。 `bypass_multi_tools_limit` 参数 : Python 键 ``` # Use bypass_multi_tools_limit=True to convert google_search to a function tool GoogleSearchTool(bypass_multi_tools_limit=True) ``` 在此示例中, 此选项转换内置 `google_search` 到函数调用工具(通过 `GoogleSearchAgentTool`),它允许它与自定义函数工具一起工作。 ## 解决问题[¶](<https://adk.dev/agents/models/google-gemini/#troubleshooting>) ### 错误代码 429 - RESOURCE_EXHAUSTED[¶](<https://adk.dev/agents/models/google-gemini/#error-code-429-resource_exhausted>) 如果您的请求数量超过为处理请求而分配的能力,通常会出现此错误 。 为了缓解这种情况,你可以做以下之一: 1. 请为您尝试使用的模型申请更高的配额限制 。 2. 启用客户端重试。 重试允许客户端在延迟后自动重试请求, 如果配额问题是暂时的, 则会有所帮助 。 有两种方法可以设置重试选项: **选项 1 :** 设置代理服务器上的重试选项作为其中的一部分 `generate_content_config`. 如果您将模型作为命名字符串通过,并让ADK为您创建模型适配器,您将会使用此选项 。 PythonJava ``` from google.genai import types # ... root_agent = Agent( model='gemini-flash-latest', # ... generate_content_config=types.GenerateContentConfig( # ... http_options=types.HttpOptions( # ... retry_options=types.HttpRetryOptions(initial_delay=1, attempts=2), # ... ), # ... ), ) ``` ``` 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 :** 在此模型适配器上重试选项 。 如果你是自己即时地使用适配器的话,你会使用这个选项。 PythonJavaKotlin ``` from google.genai import types # ... agent = Agent( model=Gemini( retry_options=types.HttpRetryOptions(initial_delay=1, attempts=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(); ``` 在科特林,你可以通过创建 `Client` 以你为例,并把它传给 `Gemini` 建筑师。 ``` 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 // ... ) ```

查看原始来源