跳到正文
实时
Google AI Blog·· 14 小时前精选60

Gemini API 推出对话中途修改系统指令与工具功能

Introducing mid-conversation instructions in the Gemini API

要点速览

Google 在 Gemini API 的 Interactions API 中以预览形式上线对话中途修改系统指令和工具的能力,此前这类改动会使 prompt 缓存失效、模型需重读全部对话历史。

Why it matters

原文给出了缓存保持机制和工具增删接口,读者可据此判断长会话智能体的成本与权限控制方式。

正文 · AI 翻译

2026年12月03日

|

开发者现在可以在对话过程中更新系统指令或工具,同时保持提示缓存完整。


Trey Nguyen

Gemini API 产品经理

Daniel Kyme

Google Cloud 产品经理


Mid-conversation instruction and tool changes preview

您的浏览器不支持音频元素。

听文章

[[duration]] 分钟

此内容由 Google AI 生成。生成式 AI 是实验性的

今天,对话中系统指令和工具的更改通过 Gemini API 的 Interactions API 提供预览。这允许代理在会话中途适应新规则和工具,而无需重新读取整个对话或增加成本。

长时间运行的代理通常跨越几十甚至几百轮。在此过程中,规则、权限和可用工具会发生变化。直到现在,你无法在不使缓存失效的情况下向模型提供新的系统指令或更新工具。

开发人员通常依赖两种解决方案:

  • 将指令放在用户消息中:为了保留缓存,你可以将新的系统指令作为用户消息发送。这些指令的权重与你的顶级系统指令相同,并允许用户尝试模仿它们。
  • 重写系统指令或工具:重写系统指令或更改工具会使提示缓存失效。模型重新处理整个对话历史记录,增加成本和延迟。

Interactions API 现在允许你在任何回合发送 system_input 步骤,其中包含系统指令或工具更改。它与你的顶级系统指令具有相同的权威性,并优先于用户消息,而较早的回合将保留在缓存中。

Preview of inline option 2 for min-conversation updates. Adding instructions and tools without resetting cache

工作原理

要在对话中途发送指令,请将前一个回合的 ID 传递到 previous_interaction_id,并在输入数组中将 system_input 步骤直接放在 user_input 步骤之前。确保在各个回合中保持顶层的 system_instruction 不变,以便提示前缀在缓存中保持有效:

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)

每个 system_input 都会作为其自己的步骤附加到对话历史记录中,并与顶层的 system_instruction 一起在会话中保持活动状态。涵盖不同主题的规则将一起保持有效。当较新的 system_input 与较早的指令冲突时,模型会优先考虑最新的指令。

当你使用 client.interactions.get(id=turn2.id) 检索交互时,每个 system_input 步骤会按顺序与 user_input 和 model_output 一起出现,使多回合代理跟踪变得简单易审计和调试:

{
  "steps": [
    {
      "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."
        }
      ]
    },
    {
      "type": "model_output",
      "content": [
        {
          "type": "text",
          "text": "A production deployment freeze is active, so we can't release. Here are some checks you can run..."
        }
      ]
    }
  ]
}

由于 system_input 在当前回合边界处附加,因此所有前面的标记在每个回合中保持相同,并重用提示缓存。你可以每回合直接在 user_input 之前发送一个 system_input 步骤,其中包含文本内容、工具更新或两者兼有。

对话中工具更新的工作原理

工具声明位于提示的开头,因此在不同回合之间更改顶层的工具列表会破坏提示缓存。使用 system_input 时,我们建议你在每个回合上保持相同的顶层工具列表,并使用 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,让你能在原地替换工具定义。移除工具时添加简短文本注释也能给模型上下文,解释为什么某个操作不可用。

请记住 system_input、added_tools 和 removed_tools 指导模型行为并作为编排控制。你的应用在执行前仍需在后台授权每个函数调用,并确保不受信任的外部或最终用户内容不进入 system_input。

如何使用它

依赖于会话的约束:在对话过程中中途应用发现的规则,例如工作区设置、账户授权或支出限额。

长周期指令强化:在多轮会话中随着上下文增长,重新强调关键格式、语气或政策规则。

动态权限和模式切换:在身份验证或计划批准后启用写入或账户操作工具,并在返回只读审核时移除它们。

实时操作变更:当下游依赖离线时禁用工具并会话中途更新指导,服务恢复后恢复它。

大型工具目录:从一个小的工具集开始,根据任务需要添加更多工具,无需一开始声明所有工具或破坏缓存。

定价和支持的模型

对话中期的系统指令不产生额外费用。system_input 步骤中的 token 按你调用的模型的标凈输入 token 速率计费。较早的回合在规则或工具变更时仍符合缓存的 token 速率。

对话中期的系统指令在 Interactions API(通过 v1beta 端点)中所有 Gemini 4 模型 的公共预览版中可用,在 Gemini API 中。对 Gemini 企业代理平台 的支持即将到来。

今天就通过阅读 文档、比较 Gemini 模型 找到适合你工作负载的 Gemini 4 模型,并 加入 AI 开发者论坛的讨论 开始使用吧。

来源: Google AI Blog · blog.google