> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inputapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 预测输出

> 为已知大部分内容的文本修改提供 prediction，明确渠道与转换限制。

## 适用场景

例如修改已有代码或文档中的少量内容，可以把预计保留的原文作为 `prediction: {type: content, content: ...}` 发送给支持它的 Chat 模型。它是候选输出提示，不是强制模板、输出校验器，也不是提示词缓存或答案缓存。每次调用仍需等待真正的完整回答。

项目 `GeneralOpenAIRequest.Prediction` 使用原始 JSON 字段，普通 OpenAI Chat 适配器可以转发；目前 Chat → Responses 转换器没有对应映射。模型支持、原生 Chat 路径和最终参数都需确认，不能假定任何推理模型或所有 OpenAI 兼容服务支持该功能。

本例只发送文本与 `prediction`，不混合工具、音频、logprobs 或生成上限。上游对组合参数有额外限制，见 [预测输出说明](https://developers.openai.com/api/docs/guides/predicted-outputs)。没有固定加速比例或成本节省保证；不要直接把 OpenRouter 的性能或路由承诺套到当前部署。

## 运行与验证

脚本内置一段无敏感数据的示例源码，要求将 Hello 改成 Welcome，把原文同时放进输入与 prediction。它只验证 Chat 的完成状态和非空文本，输出 `proposed_source` JSON 供审阅，不覆盖文件、不执行返回的代码，也不证明修改语义正确。

返回可能带可选的接受/拒绝预测 token 统计；当前网关还可能重建 usage，不能把统计缺失当作零或反推功能必然未执行。本示例不依赖该元数据，也不根据它计算账单。真实收益应在同一模型与路径上独立评估，而不是从本地 fixture 耗时推断。

如果参数报错，先确认上游支持及最终协议，再单独移除 prediction 比较基础请求；不要自动循环提交以掩盖配置问题。已有 [生成参数](/zh/guides/generation-parameters) 与 [缓存](/zh/guides/prompt-caching) 指南说明相关功能的不同边界。

实现依据：`relaykit/dto/openai_request.go`、`relay/channel/openai/adaptor.go`、`relaykit/relayconvert/internal/oai_chat/to_oai_responses_req.go`。参见 [Chat API](/zh/api-reference/chat-completions)。

## 可运行示例

从文档目录运行。配置见 [REST 运行时](/zh/guides/http-runtime)；复制脚本时保留同目录的 `http-client.mjs`。

```sh theme={null}
export API_ROOT_URL='https://gateway.example.com'
export API_KEY='YOUR_API_KEY'
export API_MODEL='YOUR_MODEL_ID'
node examples/predicted-output.mjs
```

```javascript theme={null}
import { requestJSON } from './http-client.mjs';

try {
  const model = process.env.API_MODEL;
  if (!model?.trim()) throw new Error('Set model.');
  const original = 'export function greet(name) {\n  return `Hello, ${name}!`;\n}\n';
  const result = await requestJSON('/v1/chat/completions', {
    model,
    messages: [
      { role: 'system', content: 'Return the complete revised source only, without Markdown fences or explanations.' },
      { role: 'user', content: `Replace Hello with Welcome in this source. Keep everything else unchanged.\n${original}` },
    ],
    prediction: { type: 'content', content: original },
  });
  const choice = result.choices?.[0];
  const message = choice?.message;
  if (!Array.isArray(result.choices) || result.choices.length !== 1 || choice?.index !== 0
    || choice.finish_reason !== 'stop' || message?.role !== 'assistant'
    || message.refusal != null || message.tool_calls != null || message.function_call != null
    || typeof message.content !== 'string' || !message.content.trim()) throw new Error('Incomplete text.');
  // Generated code is returned for review, never written over source files or executed.
  console.log(JSON.stringify({ proposed_source: message.content }));
} catch {
  console.error('Prediction example failed. Check native Chat support and completion status. No automatic retry.');
  process.exitCode = 1;
}
```

[Read in English](/en/guides/predicted-outputs)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.