老金出海AI · GO GLOBAL
返回教程系列
Gemini API 对话中系统指令与工具更新指南
Google进阶发布于

Gemini API 对话中系统指令与工具更新指南

介绍 Gemini API 通过 Interactions API 在对话中更新系统指令和工具的方法,保持提示缓存有效,降低长会话成本与延迟,并说明操作步骤与限制。

查看官方参考
项目详情
提供方Google
难度进阶
发布日期2026-12-03
官方来源Google
主题#engineering · #llm

背景与价值

长时运行的智能体常常跨越数十甚至数百轮对话。在此过程中,规则、权限和可用工具会发生变化。此前,开发者无法在不使缓存失效的情况下向模型提供新的系统指令或更新工具。Gemini API 通过 Interactions API 提供对话中系统指令和工具变更的预览功能,使智能体能够在不重新读取整个对话或推高成本的情况下适应新规则和工具。

旧有变通方案的问题

开发者通常依赖两种变通方案:

  • 将指令放入用户消息:为保留缓存,可将新系统指令作为用户消息发送。但这些指令不具备顶层系统指令的同等权重,且允许用户尝试模仿它们。
  • 重写系统指令或工具:重写系统指令或更改工具会使提示缓存失效。模型会重新处理整个对话历史,增加成本和延迟。

核心机制

Interactions API 现在允许在任意轮次发送 system_input 步骤,携带系统指令或工具变更。它与顶层系统指令具有同等权威,并优先于用户消息,同时较早的轮次保持缓存。每个 system_input 作为独立步骤追加到对话历史,并在整个会话中与顶层 system_instruction 一同保持生效。覆盖不同主题的规则会同时生效。当较新的 system_input 与较早的指令冲突时,模型倾向于最新的指令。

操作步骤:发送对话中系统指令

  1. 在对话中发送指令时,在 previous_interaction_id 中传入上一轮的 ID。
  2. 在输入数组中,将 system_input 步骤直接放在 user_input 之前。
  3. 保持顶层 system_instruction 在各轮次中不变,以使提示前缀在缓存中保持有效。

示例代码(Python):

from google import genai

client = genai.Client(http_options={"api_version": "v1beta"})
system_instruction = "You are an infrastructure troubleshooting assistant."

turn1 = client.interactions.create(
  model="gemini-4-neon",
  system_instruction=system_instruction,
  input="Latency just spiked on our service. What should I check first?",
)

turn2 = client.interactions.create(
  model="gemini-4-neon",
  previous_interaction_id=turn1.id,
  system_instruction=system_instruction,
  input=[
    {
      "type": "system_input",
      "content": [
        {
          "type": "text",
          "text": (
            "A production deployment freeze is now active. "
            "Recommend read-only diagnostics and do not allow further rollouts."
          ),
        }
      ],
    },
    {
      "type": "user_input",
      "content": [
        {
          "type": "text",
          "text": "Give me the command to deploy a new release.",
        }
      ],
    },
  ],
)
print(turn2.output_text)

审计与调试

使用 client.interactions.get(id=turn2.id) 检索交互时,每个 system_input 步骤会按顺序与 user_input 和 model_output 一同出现,使多轮智能体轨迹易于审计和调试。

操作步骤:对话中工具更新

工具声明位于提示开头,因此在轮次之间更改顶层工具列表会破坏提示缓存。建议在每一轮保留相同的顶层工具列表,并使用 added_tools 和 removed_tools 更新模型可调用的函数。目前仅支持通过 added_tools 和 removed_tools 更新函数工具,不支持 MCP 服务器工具。

示例中,支持智能体初始拥有 get_store_hours 和 issue_refund。下一轮中,应用移除 issue_refund 并添加 create_escalation_ticket,同时保持顶层 tools 列表不变:

from google import genai
client = genai.Client(http_options={"api_version": "v1beta"})
tools = [
  {
    "type": "function",
    "name": "get_store_hours",
    "description": "Look up opening hours for a store.",
    "parameters": {
      "type": "object",
      "properties": {"store_id": {"type": "string"}},
      "required": ["store_id"],
    },
  },
  {
    "type": "function",
    "name": "issue_refund",
    "description": "Refund an order.",
    "parameters": {
      "type": "object",
      "properties": {"order_id": {"type": "string"}},
      "required": ["order_id"],
    },
  },
]
turn1 = client.interactions.create(
  model="gemini-4-neon",
  input="Hi, I have a question about my order.",
  tools=tools,
)
turn2 = client.interactions.create(
  model="gemini-4-neon",
  previous_interaction_id=turn1.id,
  tools=tools,
  input=[
    {
      "type": "system_input",
      "content": [
        {
          "type": "text",
          "text": (
            "This account is under billing review. Explain "
            "that refunds are paused and offer to escalate."
          ),
        }
      ],
      "removed_tools": [{"type": "function", "name": "issue_refund"}],
      "added_tools": [
        {
          "type": "function",
          "name": "create_escalation_ticket",
          "description": "Escalate a billing review issue to support.",
          "parameters": {
            "type": "object",
            "properties": {"order_id": {"type": "string"}},
            "required": ["order_id"],
          },
        }
      ],
    },
    {
      "type": "user_input",
      "content": [{"type": "text", "text": "Refund order 12345."}],
    },
  ],
)
print(turn2.output_text)

工具更新会一直生效,直到后续 system_input 更改它们。当单个步骤同时包含两个字段时,API 会先处理 removed_tools 再处理 added_tools,从而可以就地替换工具定义。移除工具时添加简短文本说明,也能为模型提供解释为何操作不可用的上下文。

限制与范围

  • 该功能目前处于预览阶段,通过 Interactions API 提供。
  • 仅支持通过 added_tools 和 removed_tools 更新函数工具,不支持 MCP 服务器工具。
  • 每轮只能发送一个 system_input 步骤,直接放在 user_input 之前,可包含文本内容、工具更新或两者。
  • 需保持顶层 system_instruction 和顶层 tools 列表在各轮次中不变,以维持提示缓存有效。
  • system_input、added_tools 和 removed_tools 用于引导模型行为并作为编排控制,应用层仍需自行实施相应控制。
说明:本文是老金出海对官方教程的原创导读与工程化补充,不替代厂商文档。模型名称、SDK、价格、配额和安全政策请以官方链接为准。

📚 来源与延伸

本页内容基于以下权威来源整理,可验证、可引用。

引用规范:请注明来源为老金出海(laojinchuhai.com)并保留原文链接。

老金出品 · 用 AI 提效

AllModelsAPI一个 key,调多家大模型

AllModelsAPI 是多模型 API 中转站:一个 key 调多家模型,OpenAI 兼容接口,现有代码改个 base_url 就能迁。它不是演示品——我们自己的生产环境每天都在用。

更多老金产品:Sellenca365AIOrg365Loopa365 Ops365Skill

相关内容

按主题 / 人物 / 专区自动互通