使用 OpenAI API,你可以利用 大型语言模型 通过提示生成文本,就像你使用 ChatGPT。模型可以生成几乎任何类型的文本响应——例如代码、数学公式、结构化的 JSON 数据或拟人的散文。
这里有一个使用 Responses API.
1
2
3
4
5
6
7
8
9
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.5",
input: "Write a one-sentence bedtime story about a unicorn."
});
console.log(response.output_text);模型生成的内容数组位于响应的 output 属性中。在这个简单的示例中,我们只有一个输出,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"id": "msg_67b73f697ba4819183a15cc17d011509",
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "Under the soft glow of the moon, Luna the unicorn danced through fields of twinkling stardust, leaving trails of dreams for every child asleep.",
"annotations": []
}
]
}
]The output 数组通常会包含多个项目! 它可以包含工具调用、由其生成的推理 token 的相关数据 推理模型,以及其他项目。不能安全地假设模型的文本输出存在于 output[0].content[0].text.
我们的一些 官方 SDK 为了方便起见,在模型响应中包含了 output_text 属性,它将模型的所有文本输出聚合为一个字符串。这可作为获取模型文本输出的快捷方式,非常有用。
除了纯文本,你还可以让模型以 JSON 格式返回结构化数据 - 此功能称为 结构化输出.
这里有一个使用 Chat Completions API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import OpenAI from "openai";
const client = new OpenAI();
const completion = await client.chat.completions.create({
model: "gpt-5",
messages: [
{
role: "user",
content: "Write a one-sentence bedtime story about a unicorn.",
},
],
});
console.log(completion.choices[0].message.content);模型生成的内容数组位于响应的 choices 属性中。在这个简单的示例中,我们只有一个输出,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
[
{
"index": 0,
"message": {
"role": "assistant",
"content": "Under the soft glow of the moon, Luna the unicorn danced through fields of twinkling stardust, leaving trails of dreams for every child asleep.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
]除了纯文本,你还可以让模型以 JSON 格式返回结构化数据 - 此功能称为 结构化输出.
选择模型
通过 API 生成内容时需要做出的一个关键选择是你要使用哪个模型 - 即上述代码示例中的 model 参数。 你可以在此处找到可用模型的完整列表。以下是选择文本生成模型时需要考虑的几个因素。
- 推理模型 生成内部思维链来分析输入提示,并在理解复杂任务和多步规划方面表现出色。它们通常比 GPT 模型更慢且使用成本更高。
- GPT 模型 速度快、成本效益高且非常智能,但在明确指示如何完成任务方面会获益更多。
- 大模型与小模型(mini 或 nano) 在速度、成本和智能水平上各有取舍。大型模型在跨领域理解提示和解决问题方面更胜一筹,而小型模型通常速度更快、使用成本更低。
如果不确定如何选择, gpt-5.5 可作为通用文本生成和提示迭代的一个强力默认选项。
提示工程
提示工程 是为模型编写有效指令的过程,使其能够持续生成满足你要求的内容。
由于模型生成的内容是非确定性的,因此通过提示来获得所需输出是一门艺术与科学的结合。不过,你可以运用一些技巧和最佳实践来持续获得良好的结果。
某些提示工程技巧适用于所有模型,例如使用消息角色。但不同的模型类型(如推理模型与 GPT 模型)可能需要不同的提示方式才能产生最佳结果。即使是同一家族中不同版本的模型快照也可能产生不同的结果。因此,随着你构建更复杂的应用程序,我们强烈建议:
- 将生产环境中的应用程序固定到特定的 模型快照 (如
gpt-4.1-2025-04-14例如)以确保行为一致 - 构建 评估 以衡量提示词的行为表现,从而让您在迭代过程中,或在更改和升级模型版本时,能够监控提示词的性能
现在,让我们来看看一些可用于构建提示词的工具和技术。
消息角色与指令遵循
你可以使用 不同级别的权限 使用 instructions API 参数或 消息角色.
The instructions 参数为模型提供了关于其生成回复时应如何行为的高级指令,包括语气、目标和正确回复的示例。以这种方式提供的任何指令都将优先于 input parameter.
1
2
3
4
5
6
7
8
9
10
11
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
reasoning: { effort: "low" },
instructions: "Talk like a pirate.",
input: "Are semicolons optional in JavaScript?",
});
console.log(response.output_text);上面的示例大致等同于在 input array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
reasoning: { effort: "low" },
input: [
{
role: "developer",
content: "Talk like a pirate."
},
{
role: "user",
content: "Are semicolons optional in JavaScript?",
},
],
});
console.log(response.output_text);请注意,由于诸多原因, instructions 参数仅适用于当前的响应生成请求。如果你在 管理对话状态 with the previous_response_id 参数, instructions 之前轮次中使用的将不会出现在上下文中。
你可以使用以下方式向模型提供指令(提示): 不同级别的权限 使用 消息角色.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import OpenAI from "openai";
const client = new OpenAI();
const completion = await client.chat.completions.create({
model: "gpt-5",
messages: [
{
role: "developer",
content: "Talk like a pirate."
},
{
role: "user",
content: "Are semicolons optional in JavaScript?",
},
],
});
console.log(completion.choices[0].message);The OpenAI 模型规范 描述了我们的模型如何为具有不同角色的消息分配不同的优先级。
developer | user | assistant |
|---|---|---|
developer 消息是由应用开发者提供的指令,优先级高于 user messages. | user 消息是由最终用户提供的指令,优先级低于 developer messages. | 模型生成的消息具有 assistant role. |
多轮对话可能包含多条这些类型的消息,以及由你和模型提供的其他内容类型。了解更多关于 在此管理对话状态.
你可以将 developer and user 消息想象成编程语言中的函数及其参数。
developermessages 提供了系统的规则和业务逻辑,类似于函数定义。usermessages 提供了输入和配置,developermessage 的指令会应用于这些输入和配置,类似于函数的参数。
可复用提示词
在 OpenAI 控制台中,你可以开发可复用的 提示词配合使用 ,以便在 API 请求中使用,而无需在代码中指定提示词的内容。这样,你可以更轻松地构建和评估提示词,并在不更改集成代码的情况下部署改进后的提示词版本。
目前,可复用提示词仅在 Responses API 中受支持。它们在 Chat Completions API 中不可用。
工作原理如下:
- 创建可复用提示词 in the 仪表板 with placeholders like
{{customer_name}}. - 使用提示词 通过
prompt参数在你的 API 请求中。该提示词参数对象包含三个可配置的属性:id— 提示词的唯一标识符,可在控制台中找到version— 提示词的特定版本(默认为控制台中指定的“当前”版本)variables— 用于替换提示词中变量的值的映射。替换值可以是字符串,也可以是其他 Response 输入消息类型,例如input_imageorinput_file. 查看完整的 API 参考文档.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
prompt: {
id: "pmpt_abc123",
version: "2",
variables: {
customer_name: "Jane Doe",
product: "40oz juice box"
}
}
});
console.log(response.output_text);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 fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
// Upload a PDF we will reference in the prompt variables
const file = await client.files.create({
file: fs.createReadStream("draconomicon.pdf"),
purpose: "user_data",
});
const response = await client.responses.create({
model: "gpt-5",
prompt: {
id: "pmpt_abc123",
variables: {
topic: "Dragons",
reference_pdf: {
type: "input_file",
file_id: file.id,
},
},
},
});
console.log(response.output_text);使用 Markdown 和 XML 格式化消息
在编写 developer and user messages 时,你可以结合使用 Markdown 格式和 XML 标签,帮助模型理解提示词和上下文数据的逻辑边界。.
Markdown 标题和列表有助于标记提示词的不同部分,并向模型传达层级结构。它们还可能使你的提示词在开发过程中更具可读性。XML 标签可以帮助界定一段内容的起止位置(例如用于参考的支持文档)。XML 属性还可用于定义提示词中有关内容的元数据,以便你的指令进行引用。
通常,开发者消息将包含以下几个部分,且通常遵循以下顺序(尽管具体的最优内容和顺序可能因你使用的模型而异):
- Identity: 描述助手的目的、沟通风格和高层级目标。
- Instructions: 为模型提供关于如何生成你所需回复的指导。它应该遵循哪些规则?模型应该做什么,绝对不能做什么?根据你的具体用例,此部分可以包含多个子部分,例如关于模型应该如何 调用自定义函数.
- Examples: 提供可能的输入示例,以及期望的模型输出。
- Context: 为模型提供其生成响应可能需要的任何附加信息,例如训练数据之外的私有/专有数据,或您知道与当前生成特别相关的任何其他数据。这些内容通常最好放置在提示词的末尾,因为您可能会为不同的生成请求提供不同的上下文。
下面是一个使用 Markdown 和 XML 标签构建的示例, developer 该消息包含不同的部分和支持的示例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Identity
You are coding assistant that helps enforce the use of snake case
variables in JavaScript code, and writing code that will run in
Internet Explorer version 6.
# Instructions
* When defining variables, use snake case names (e.g. my_variable)
instead of camel case names (e.g. myVariable).
* To support old browsers, declare variables using the older
"var" keyword.
* Do not give responses with Markdown formatting, just return
the code as requested.
# Examples
<user_query>
How do I declare a string variable for a first name?
</user_query>
<assistant_response>
var first_name = "Anna";
</assistant_response>1
2
3
4
5
6
7
8
9
10
11
12
13
import fs from "fs/promises";
import OpenAI from "openai";
const client = new OpenAI();
const instructions = await fs.readFile("prompt.txt", "utf-8");
const response = await client.responses.create({
model: "gpt-5",
instructions,
input: "How would I declare a variable for a last name?",
});
console.log(response.output_text);通过提示词缓存节省成本和延迟
在构建消息时,您应该尽量将期望在 API 请求中反复使用的内容放在提示的开头, and 即您在传给 JSON 请求体的首批 API 参数中。这使您能够最大化节省 Chat Completions or 响应的成本和延迟。 提示缓存.
少样本学习
少样本学习允许您通过在提示词中包含少量输入/输出示例,而非 微调 该模型。模型会隐式地“领会”这些示例中的模式,并将其应用于提示。提供示例时,尽量展示多样化的可能输入及其期望输出。
通常,你会将示例作为以下内容的一部分提供: developer 消息。这是一个示例 developer 一条包含示例的消息,向模型展示如何将客户服务评价分类为正面或负面。
# Identity
You are a helpful assistant that labels short product reviews as
Positive, Negative, or Neutral.
# Instructions
* Only output a single word in your response with no additional formatting
or commentary.
* Your response should only be one of the words "Positive", "Negative", or
"Neutral" depending on the sentiment of the product review you are given.
# Examples
<product_review id="example-1">
I absolutely love this headphones — sound quality is amazing!
</product_review>
<assistant_response id="example-1">
Positive
</assistant_response>
<product_review id="example-2">
Battery life is okay, but the ear pads feel cheap.
</product_review>
<assistant_response id="example-2">
Neutral
</assistant_response>
<product_review id="example-3">
Terrible customer service, I'll never buy from them again.
</product_review>
<assistant_response id="example-3">
Negative
</assistant_response>包含相关的上下文信息
在你给模型的提示中包含模型可用于生成响应的额外上下文信息,通常很有用。这样做有几个常见原因:
- 为模型提供专有数据,或任何模型训练数据集之外的其他数据的访问权限。
- 为了将模型的响应限制为您认为最有效的一组特定资源。
向模型生成请求中添加额外相关上下文的技术有时被称为 检索增强生成 (RAG)。您可以通过多种不同的方式向提示词中添加额外的上下文,例如查询向量数据库并将返回的文本包含在提示词中,或者使用 OpenAI 内置的 文件搜索工具 根据上传的文档生成内容。
规划上下文窗口
模型在生成请求期间所能处理的上下文数据量是有限的。此内存限制称为 上下文窗口,其定义基于 token (你传入的数据块,从文本到图像)。
模型的上下文窗口大小各不相同,从较低的 100k 范围一直到较新的 GPT-4.1 模型的百万 token。 请参考模型文档 以获取每个模型的特定上下文窗口大小。
提示当前 GPT-5 系列模型
像 GPT 这样的模型 gpt-5.5 得益于明确的指令,这些指令需在提示中明确提供完成任务所需的逻辑和数据。为了充分发挥最新 GPT-5 系列模型的优势,请从当前的提示指南开始。
通过当前指南、实用示例和迁移说明,充分利用最新 GPT-5 系列模型的提示功能。
针对最新 GPT-5 系列模型的提示最佳实践
如需完整的最新说明,请使用 提示指南 指南。以下实用提醒仍然适用。
对推理模型进行 Prompting
在对 推理模型 和 GPT 模型进行 Prompting 时,需要考虑一些差异。一般而言,推理模型在仅有高层指导的任务上会产生更好的结果。这与 GPT 模型不同,后者受益于非常精确的指令
你可以这样理解推理模型和 GPT 模型之间的区别
- 推理模型就像一位资深同事。你可以给他们一个要达成的目标,并相信他们能自行解决细节问题
- GPT 模型就像一位初级同事。如果为他们提供明确的指令以生成特定的输出,他们的表现会最好
有关使用推理模型最佳实践的更多信息, 请参阅此指南.
后续步骤
既然你已经了解了文本输入和输出的基础知识,接下来你可能想查看以下这些资源。
使用 Playground 来开发和迭代提示词。
确保模型输出的 JSON 数据符合 JSON schema。
请在 API 参考文档中查看文本生成的所有选项。
其他资源
获取更多灵感,请访问 OpenAI Cookbook,其中包含示例代码,并链接至以下第三方资源: