预测输出(Predicted Outputs)让您能够在许多输出 token 预先已知的情况下,加快 聊天补全(Chat Completions) 的 API 响应速度。这在您重新生成包含少量修改的文本或代码文件时最为常见。您可以通过 聊天补全中的 prediction 请求参数 提供您的预测。
预测输出现已在最新的 gpt-4o、gpt-4o-mini、gpt-4.1、gpt-4.1-mini 和 gpt-4.1-nano 模型中可用。请继续阅读,了解如何使用预测输出来降低应用程序的延迟。
代码重构示例
预测输出对于重新生成包含细微修改的文本文档和代码文件特别有用。假设您希望 GPT-4o 模型重构一段 TypeScript 代码,并将 User 类的 username 属性改为 email:
1
2
3
4
5
6
7
class User {
firstName: string = "";
lastName: string = "";
username: string = "";
}
export default User;除了上面第 4 行外,文件的大部分内容将保持不变。如果您使用代码文件的当前文本作为预测,就可以以更低的延迟重新生成整个文件。对于较大的文件,这些时间节省会迅速累积。
以下是在我们的 SDK 中使用 prediction 参数的示例,通过预测模型的最终输出将与我们原始的代码文件非常相似(我们将其用作预测文本)来实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import OpenAI from "openai";
const code = `
class User {
firstName: string = "";
lastName: string = "";
username: string = "";
}
export default User;
`.trim();
const openai = new OpenAI();
const refactorPrompt = `
Replace the "username" property with an "email" property. Respond only
with code, and with no markdown formatting.
`;
const completion = await openai.chat.completions.create({
model: "gpt-4.1",
messages: [
{
role: "user",
content: refactorPrompt
},
{
role: "user",
content: code
}
],
store: true,
prediction: {
type: "content",
content: code
}
});
// Inspect returned data
console.log(completion);
console.log(completion.choices[0].message.content);除了重构后的代码外,模型响应还将包含看起来类似以下的数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
id: 'chatcmpl-xxx',
object: 'chat.completion',
created: 1730918466,
model: 'gpt-4o-2024-08-06',
choices: [ /* ...actual text response here... */],
usage: {
prompt_tokens: 81,
completion_tokens: 39,
total_tokens: 120,
prompt_tokens_details: { cached_tokens: 0, audio_tokens: 0 },
completion_tokens_details: {
reasoning_tokens: 0,
audio_tokens: 0,
accepted_prediction_tokens: 18,
rejected_prediction_tokens: 10
}
},
system_fingerprint: 'fp_159d8341cc'
}请注意 usage 对象中的 accepted_prediction_tokens 和 rejected_prediction_tokens。在本例中,预测中的 18 个 token 被用于加速响应,而 10 个被拒绝。
请注意,任何被拒绝的 token 仍会像 API 生成的其他补全 token 一样收费,因此预测输出可能会增加您的请求成本。
流式处理示例
当您为 API 响应使用流式处理(Streaming)时,预测输出带来的延迟收益会更加显著。以下是相同代码重构用例的示例,但这次使用的是 OpenAI SDK 中的流式处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import OpenAI from "openai";
const code = `
class User {
firstName: string = "";
lastName: string = "";
username: string = "";
}
export default User;
`.trim();
const openai = new OpenAI();
const refactorPrompt = `
Replace the "username" property with an "email" property. Respond only
with code, and with no markdown formatting.
`;
const completion = await openai.chat.completions.create({
model: "gpt-4.1",
messages: [
{
role: "user",
content: refactorPrompt
},
{
role: "user",
content: code
}
],
store: true,
prediction: {
type: "content",
content: code
},
stream: true
});
// Inspect returned data
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}响应中预测文本的位置
当提供预测文本时,您的预测可以出现在生成的响应中的任何位置,并且仍然能为响应提供延迟优化。假设您的预测文本是如下所示的简单 Hono 服务器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { serveStatic } from "@hono/node-server/serve-static";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
const app = new Hono();
app.get("/api", (c) => {
return c.text("Hello Hono!");
});
// You will need to build the client code first `pnpm run ui:build`
app.use(
"/*",
serveStatic({
rewriteRequestPath: (path) => `./dist${path}`,
})
);
const port = 3000;
console.log(`Server is running on port ${port}`);
serve({
fetch: app.fetch,
port,
});您可以提示模型重新生成文件,提示词如下:
Add a get route to this application that responds with
the text "hello world". Generate the entire application
file again with this route added, and with no other
markdown formatting.对该提示的响应可能看起来如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { serveStatic } from "@hono/node-server/serve-static";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
const app = new Hono();
app.get("/api", (c) => {
return c.text("Hello Hono!");
});
app.get("/hello", (c) => {
return c.text("hello world");
});
// You will need to build the client code first `pnpm run ui:build`
app.use(
"/*",
serveStatic({
rewriteRequestPath: (path) => `./dist${path}`,
})
);
const port = 3000;
console.log(`Server is running on port ${port}`);
serve({
fetch: app.fetch,
port,
});您仍然会看到响应中包含已接受的预测 token,即使预测文本出现在新添加内容的前后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
id: 'chatcmpl-xxx',
object: 'chat.completion',
created: 1731014771,
model: 'gpt-4o-2024-08-06',
choices: [ /* completion here... */],
usage: {
prompt_tokens: 203,
completion_tokens: 159,
total_tokens: 362,
prompt_tokens_details: { cached_tokens: 0, audio_tokens: 0 },
completion_tokens_details: {
reasoning_tokens: 0,
audio_tokens: 0,
accepted_prediction_tokens: 60,
rejected_prediction_tokens: 0
}
},
system_fingerprint: 'fp_9ee9e968ea'
}这一次,没有被拒绝的预测 token,因为我们预测的整个文件内容都被用于最终响应中。棒极了!🔥
限制
使用预测输出时,您应该考虑以下因素和限制。
- 预测输出仅支持 GPT-4o、GPT-4o-mini、GPT-4.1、GPT-4.1-mini 和 GPT-4.1-nano 系列模型。
- 提供预测时,任何不属于最终补全结果的提供 token 仍按补全 token 费率收费。请参阅
usage对象的rejected_prediction_tokens属性,查看有多少 token 未被最终响应使用。 - 使用预测输出时,不支持以下 API 参数:
n:不支持大于 1 的值logprobs:不支持presence_penalty:不支持大于 0 的值frequency_penalty:不支持大于 0 的值audio:预测输出与 音频输入和输出 不兼容modalities:仅支持text模态max_completion_tokens:不支持tools:预测输出目前不支持函数调用(Function calling)