AIFeed

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

AI summary

可在对话过程中更新系统指令和工具,同时保留提示缓存。

Translation(model translation, not the source's own text)

文章标题:在 Gemini API 中引入对话中途指令

正文: 如今,对话中途的系统指令与工具变更已通过 Interactions API 在 Gemini API 中提供预览版。这让智能体能够在会话中途适应新的规则和工具,而无需重新读取整个对话,也不会推高成本。

长时间运行的智能体往往要跨越数十甚至数百个回合。在这一过程中,规则、权限和可用工具都会发生变化。在此之前,你无法在不使缓存失效的情况下,向模型提供新的系统指令或更新工具。

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

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

Interactions API 现在允许你在任意回合发送一个携带系统指令或工具变更的 system_input 步骤。它具备与顶层系统指令同等的权威性,并优先于用户消息,同时此前的回合仍保持缓存。

对话中途更新的内联选项 2 预览。在不重置缓存的情况下添加指令和工具
对话中途更新的内联选项 2 预览。在不重置缓存的情况下添加指令和工具

工作原理

要在对话中途发送指令,请在 previous_interaction_id 中传入上一回合的 ID,并将 system_input 步骤直接放在 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 都保持完全一致,从而复用 prompt 缓存。你可以在每一轮中于 user_input 之前直接发送一个 system_input 步骤,其中包含文本内容、工具更新或两者兼有。

对话中途的工具更新如何工作

工具声明位于 prompt 的开头,因此在轮次之间更改顶层 tools 列表会破坏 prompt 缓存。使用 system_input 时,我们建议你在每一轮都保留相同的顶层 tools 列表,并使用 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 之外。

你可以如何使用它

Session-dependent constraints:应用在对话中途发现的规则,例如工作区设置、账户权益或支出限额。

Long-horizon instruction reinforcement:在多轮会话深入、上下文增长时,再次强调关键的格式、语气或策略规则。

Dynamic permissions and mode switches:在身份验证或计划审批后启用写入或账户操作工具,并在返回只读审查时移除它们。

Live operational changes:当下游依赖离线时,在会话中途禁用某个工具并更新指引,待服务恢复后再将其恢复。

Large tool catalogs:从一小组工具开始,随着任务需要再添加更多工具,无需预先声明所有工具,也不会破坏缓存。

定价与支持的模型如何运作

对话中途系统指令不收取单独费用。system_input 步骤中的 token 按你所调用模型的标准输入 token 费率计费。当规则或工具发生变化时,此前的轮次仍可享受缓存 token 费率。

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

立即开始:阅读文档、比较 Gemini 模型以找到适合你工作负载的 Gemini 4 模型,并加入 AI Developer Forum 上的讨论。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Google AI 官方
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗