Gemini API 推出会话中途指令功能

2026年12月3日

|

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


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 会在当前轮次边界处追加内容,因此此前的所有 token 在各轮次间保持完全一致,并可复用提示词缓存。你可以在每轮发送一个 system_input 步骤,并将其直接置于 user_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 费率。

会话中途系统指令现已面向 所有 Gemini 4 模型 在 Interactions API 中开放公开预览(通过 v1beta 端点),该 API 位于 Gemini API中提供。对 Gemini Enterprise Agent Platform 的支持即将推出。

立即开始:阅读 文档, 比较 Gemini 模型 ,为你的工作负载找到合适的 Gemini 4 模型,并 加入 AI 开发者论坛的讨论.

发布于: