透過 OpenAI API,你可以使用大型語言模型根據提示詞生成文字,就像使用 ChatGPT 一樣。模型幾乎能生成任何類型的文字回應,例如程式碼、數學方程式、結構化 JSON 資料,或如同人類撰寫的文章。
對於這類文字生成呼叫等直接向模型發出的請求,請使用 Responses API。
1
2
3
4
5
6
7
8
9import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-6-astra",
input: "Write a one-sentence bedtime story about a unicorn.",
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
input="Write a one-sentence bedtime story about a unicorn.",
)
print(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
23package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},
})
if err != nil {
panic(err.Error())
}
fmt.Println(resp.OutputText())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class Main {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
ResponseCreateParams params =
ResponseCreateParams.builder().input("Say this is a test").model("gpt-6-astra").build();
Response response = client.responses().create(params);
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> System.out.println(outputText.text()));
}
}
1
2
3
4
5
6
7
8
9
10
11
12using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
ResponseResult response = await client.CreateResponseAsync(
"gpt-6-astra",
"Say 'this is a test.'"
);
Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");
1
2
3
4
5
6
7
8
9
10require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-6-astra",
input: "Write a one-sentence bedtime story about a unicorn."
)
puts(response.output_text)
1
2
3
4
5openai responses create \
--model "gpt-6-astra" \
--input "Write a one-sentence bedtime story about a unicorn." \
--raw-output \
--transform 'output.#(type=="message").content.0.text'
1
2
3
4
5
6
7curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"input": "Write a one-sentence bedtime story about a unicorn."
}'
回應的 output 屬性包含一個陣列,其中存放模型生成的內容。在這個簡單的範例中,只有一個輸出項目,如下所示:
1234567891011121314[
{
"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": []
}
]
}
]
output 陣列通常包含不只一個項目! 其中可能包含工具呼叫、推理模型生成的推理 Token 相關資料,以及其他項目。不能假設模型的文字輸出一定會出現在 output[0].content[0].text。
為了方便使用,我們的部分官方 SDK 在模型回應中提供 output_text 屬性,將模型的所有文字輸出合併為單一字串。你可以透過這個屬性快速取得模型的文字輸出。
除了純文字,你也可以讓模型以 JSON 格式傳回結構化資料。這項功能稱為結構化輸出。
提示工程 是為模型撰寫有效指示的過程,目的是讓模型持續生成符合需求的內容。
由於模型生成的內容並非固定不變,想透過提示詞取得理想的輸出,既需要技巧,也需要科學方法。不過,運用適當的技術與最佳實務,就能持續獲得良好的結果。
有些提示工程技巧適用於所有模型,例如使用訊息角色。但不同模型可能需要不同的提示方式,才能產生最佳結果。即使是同一系列模型的不同快照,也可能產生不同結果。因此,在建構更複雜的應用程式時,我們強烈建議:
- 將正式環境中的應用程式固定使用特定的模型快照(例如
gpt-5.5-2026-04-23),以確保行為一致
- 建立測試與評估套件來衡量提示詞的表現,以便在反覆改進,或變更與升級模型版本時監控成效
接下來,讓我們看看有哪些工具與技巧可以協助你撰寫提示詞。
OpenAI 提供多種不同的模型與數個 API 供你選擇。推理模型(例如 gpt-6-astra)的行為與對話模型不同,適合的提示詞也有所不同。請留意,推理模型搭配 Responses API 使用時,表現更好,也能展現更高的智慧。
無論你要建構哪一種文字生成應用程式,我們都建議使用 Responses API,而非較舊的 Chat Completions API。如果你使用的是推理模型,遷移至 Responses 尤其有幫助。
你可以搭配使用 instructions API 參數與 訊息角色,向模型提供不同權威層級的指示。
instructions 參數用來向模型提供高層次指示,說明生成回應時應遵循的行為,包括語氣、目標與正確回應的範例。透過這種方式提供的任何指示,都會優先於 input 參數中的提示詞。
1
2
3
4
5
6
7
8
9
10
11import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-6-astra",
reasoning: { effort: "low" },
instructions: "Talk like a pirate.",
input: "Are semicolons optional in JavaScript?",
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
reasoning={"effort": "low"},
instructions="Talk like a pirate.",
input="Are semicolons optional in JavaScript?",
)
print(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
26
27
28
29package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Instructions: openai.String("Talk like a pirate."),
Reasoning: responses.ReasoningParam{
Effort: responses.ReasoningEffortLow,
},
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Are semicolons optional in JavaScript?"),
},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.Reasoning;
import com.openai.models.ReasoningEffort;
import com.openai.models.responses.ResponseCreateParams;
String semicolonsDevMsg = "Talk like a pirate.";
String semicolonsPrompt = "Are semicolons optional in JavaScript?";
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input(semicolonsPrompt)
.instructions(semicolonsDevMsg)
.reasoning(Reasoning.builder().effort(ReasoningEffort.LOW).build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new()
{
Model = "gpt-6-astra",
Instructions = "Talk like a pirate.",
ReasoningOptions = new ResponseReasoningOptions
{
ReasoningEffortLevel = ResponseReasoningEffortLevel.Low,
},
};
options.InputItems.Add(
ResponseItem.CreateUserMessageItem("Are semicolons optional in JavaScript?")
);
ResponseResult response = await client.CreateResponseAsync(options);
Console.WriteLine(response.GetOutputText());
1
2
3
4
5
6
7
8
9
10
11require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
instructions: "Talk like a pirate.",
reasoning: { effort: :low },
input: "Are semicolons optional in JavaScript?"
)
puts(response.output_text)
1
2
3
4
5
6
7
8
9curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"reasoning": {"effort": "low"},
"instructions": "Talk like a pirate.",
"input": "Are semicolons optional in JavaScript?"
}'
上述範例大致等同於在 input 陣列中使用下列輸入訊息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-6-astra",
reasoning: { effort: "low" },
input: [
{
role: "developer",
content: "Talk like a pirate.",
},
{
role: "user",
content: "Are semicolons optional in JavaScript?",
},
],
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
reasoning={"effort": "low"},
input=[
{"role": "developer", "content": "Talk like a pirate."},
{"role": "user", "content": "Are semicolons optional in JavaScript?"},
],
)
print(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
26
27
28
29
30
31
32
33
34
35
36
37package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Reasoning: responses.ReasoningParam{
Effort: responses.ReasoningEffortLow,
},
Input: responses.ResponseNewParamsInputUnion{
OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage(
"Talk like a pirate.",
responses.EasyInputMessageRoleDeveloper,
),
responses.ResponseInputItemParamOfMessage(
"Are semicolons optional in JavaScript?",
responses.EasyInputMessageRoleUser,
),
},
},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
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
37import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.Reasoning;
import com.openai.models.ReasoningEffort;
import com.openai.models.responses.EasyInputMessage;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputItem;
import java.util.List;
String semicolonsDevMsg = "Talk like a pirate.";
String semicolonsPrompt = "Are semicolons optional in JavaScript?";
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input(
ResponseCreateParams.Input.ofResponse(
List.of(
ResponseInputItem.ofEasyInputMessage(
EasyInputMessage.builder()
.role(EasyInputMessage.Role.DEVELOPER)
.content(semicolonsDevMsg)
.build()),
ResponseInputItem.ofEasyInputMessage(
EasyInputMessage.builder()
.role(EasyInputMessage.Role.USER)
.content(semicolonsPrompt)
.build()))))
.reasoning(Reasoning.builder().effort(ReasoningEffort.LOW).build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
CreateResponseOptions options = new()
{
Model = "gpt-6-astra",
ReasoningOptions = new ResponseReasoningOptions
{
ReasoningEffortLevel = ResponseReasoningEffortLevel.Low,
},
};
options.InputItems.Add(
ResponseItem.CreateDeveloperMessageItem("Talk like a pirate.")
);
options.InputItems.Add(
ResponseItem.CreateUserMessageItem("Are semicolons optional in JavaScript?")
);
ResponseResult response = await client.CreateResponseAsync(options);
Console.WriteLine(response.GetOutputText());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
reasoning: { effort: :low },
input: [
{
role: :developer,
content: "Talk like a pirate."
},
{
role: :user,
content: "Are semicolons optional in JavaScript?"
}
]
)
puts(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"reasoning": {"effort": "low"},
"input": [
{
"role": "developer",
"content": "Talk like a pirate."
},
{
"role": "user",
"content": "Are semicolons optional in JavaScript?"
}
]
}'
請注意,instructions 參數僅適用於目前這次回應生成請求。如果你使用 previous_response_id 參數來管理對話狀態,先前回合使用的 instructions 不會包含在上下文中。
OpenAI 模型規格說明了模型如何為不同角色的訊息賦予不同的優先順序。
| developer |
user |
assistant |
|---|
developer 訊息是應用程式開發者提供的指示,
優先於 user 訊息。
| user 訊息是終端使用者提供的指示,
優先順序低於 developer 訊息。
| 模型生成的訊息具有 assistant 角色。 |
多回合對話可能包含數則上述類型的訊息,以及你和模型提供的其他類型內容。請參閱對話狀態管理說明,瞭解更多資訊。
你可以將 developer 與 user 訊息想成程式語言中的函式與引數。
developer 訊息提供系統規則與業務邏輯,就像函式定義一樣。
user 訊息提供輸入與組態,讓 developer 訊息中的指示套用其上,就像傳入函式的引數一樣。
將正式環境使用的提示詞存放在應用程式碼中,而非建立可重複使用的提示詞物件。透過程式碼管理提示詞,你就能運用具型別的輸入、程式碼審查、測試與既有部署流程來調整模型行為。
OpenAI 正在棄用 API 中可重複使用的提示詞物件。自 2026 年 6 月 3 日起,
將逐步淡化提示詞建立功能,而 v1/prompts 預定於
2026 年 11 月 30 日停用。請參閱已棄用項目
頁面,瞭解目前的
時程。
開展新的文字生成工作時:
- 將提示詞建構器放在小型模組中,並讓模組位置靠近它所支援的功能。
- 針對客戶資料、檔案或任務選項等動態值,使用具型別的函式引數或結構描述。
- 將產生的
instructions 與 input 直接傳給 Responses API。
- 變更正式環境使用的提示詞前,先加入具代表性的測試資料、測試與評估檢查。
- 透過部署系統推出提示詞變更;需要分階段發布時,使用功能旗標或組態來控制。
如果你的整合已透過提示詞 ID 或版本呼叫已儲存的提示詞,請依照提示詞物件遷移指南,將該提示詞移至程式碼中。
瞭解文字輸入與輸出的基礎概念後,你可以接著參閱以下資源。
使用 Playground 開發並反覆改進提示詞。
確保模型輸出的 JSON 資料符合 JSON 結構描述。