预测输出 可帮助您加速 Chat Completions 的 API 响应,这在提前已知许多输出 Token 时非常有用。当您对文本或代码文件进行少量修改并重新生成时,这种情况最为常见。您可以使用 prediction 请求参数在 Chat Completions 中提供您的预测.
预测输出目前已支持最新的 gpt-4o, gpt-4o-mini, gpt-4.1, gpt-4.1-mini,且 gpt-4.1-nano 模型。请继续阅读,了解如何在您的应用中使用预测输出来降低延迟。
代码重构示例
预测输出非常适合用于重新生成进行了少量修改的文本文档和代码文件。假设您希望 GPT-4o 模型 重构一段 TypeScript 代码,并将 username 类的 User 属性转换为 email instead:
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'
}请注意 accepted_prediction_tokens and rejected_prediction_tokens in the usage 对象。在此示例中,有 18 个预测 Token 被用于加速响应,同时有 10 个被拒绝。
请注意,任何被拒绝的 token 仍会像 API 生成的其他补全 token 一样计费,因此 Predicted Outputs 可能会增加您的请求成本。
流式示例
当您对 API 响应使用流式传输时,Predicted Outputs 带来的延迟降低会更加显著。以下是相同代码重构用例的示例,但在 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,因为我们预测的文件的全部内容都被用于最终响应中。太棒了!🔥
限制
在使用 Predicted Outputs 时,您应考虑以下因素和限制。
- Predicted Outputs 仅受 GPT-4o、GPT-4o-mini、GPT-4.1、GPT-4.1-mini 和 GPT-4.1-nano 系列模型支持。
- 在提供预测时,所提供的任何未包含在最终补全中的 token,仍将按补全 token 费率进行计费。请参见
rejected_prediction_tokens类的usage对象 以查看最终响应中未使用的 token 数量。 - The following API 参数 在使用 Predicted Outputs 时不受支持:
n:不支持大于 1 的值logprobs:不支持presence_penalty:不支持大于 0 的值frequency_penalty:不支持大于 0 的值audio:预测输出不兼容 音频输入和输出modalities:仅text模态受支持max_completion_tokens:不支持tools:预测输出当前不支持函数调用