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-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
10 from 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
23 package 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
20 import 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
12 using 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
10 require "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
5 openai 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
7 curl "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 プロパティに配列として格納されます。この簡単な例では、出力は次の 1 件だけです。
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" : []
}
]
}
] output 配列には、複数の項目が含まれることがよくあります。 ツール呼び出しや、リーズニングモデル が生成した推論トークンに関するデータなども含まれる場合があります。モデルのテキスト出力が必ず output[0].content[0].text にあると想定するのは安全ではありません。
一部の公式 SDK では、利便性のためにモデルのレスポンスに output_text プロパティを用意しています。このプロパティは、モデルのすべてのテキスト出力を 1 つの文字列にまとめます。モデルのテキスト出力を手軽に取得する方法として役立ちます。
モデルはプレーンテキストに加えて、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.5" ,
messages: [
{
role: "user" ,
content: "Write a one-sentence bedtime story about a unicorn." ,
},
],
});
console. log (completion.choices[ 0 ].message.content); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-5.5",
messages=[
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn.",
}
],
)
print(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
21
22
23
24
25
26
27 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
completion, err := client.Chat.Completions.New(
context.Background(),
openai.ChatCompletionNewParams{
Model: "gpt-6-astra",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Write a one-sentence bedtime story about a unicorn."),
},
},
)
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
} 1
2
3
4
5
6
7
8
9
10
11
12
13 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-6-astra")
.addUserMessage("Write a one-sentence bedtime story about a unicorn.")
.build();
client.chat().completions().create(params).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println); 1
2
3
4
5
6
7
8
9
10
11 using OpenAI.Chat;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-6-astra";
ChatClient client = new(model, key);
ChatCompletion completion = await client.CompleteChatAsync(
new UserChatMessage("Write a one-sentence bedtime story about a unicorn.")
);
Console.WriteLine(completion.Content[0].Text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14 require "openai"
client = OpenAI::Client.new
completion = client.chat.completions.create(
model: "gpt-6-astra",
messages: [
{
role: :user,
content: "Write a one-sentence bedtime story about a unicorn."
}
]
)
puts(completion.choices.fetch(0).message.content) 1
2
3
4
5
6
7
8
9
10
11
12 curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"messages": [
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}' モデルが生成したコンテンツは、レスポンスの choices プロパティに配列として格納されます。この簡単な例では、出力は次の 1 件だけです。
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-6-astra を選ぶとよいでしょう。
プロンプトエンジニアリング とは、要件に合ったコンテンツをモデルが安定して生成できるように、効果的な指示を作成することです。
モデルが生成するコンテンツは非決定的なため、望む出力を得るためのプロンプト作成には、創意工夫と体系的な手法の両方が必要です。ただし、適切な手法やベストプラクティスを適用すれば、安定して良い結果を得られます。
メッセージのロールの活用など、どのモデルにも有効なプロンプトエンジニアリングの手法があります。一方、最良の結果を得るには、リーズニングモデルと GPT モデルのように、モデルの種類に応じてプロンプトの作成方法を変える必要がある場合もあります。同じファミリーのモデルでも、スナップショットが違えば結果が異なることがあります。そのため、複雑なアプリケーションを構築する際は、次の対応を強くお勧めします。
動作の一貫性を確保するため、本番環境のアプリケーションで使用するモデルのスナップショット を特定のもの(例:gpt-4.1-2025-04-14)に固定すること
プロンプトに対する動作を測定するテストや評価スイートを構築し、プロンプトの改善時やモデルのバージョン変更・アップグレード時に性能を監視できるようにすること
ここからは、プロンプトの作成に使えるツールや手法を見ていきます。
API の instructions パラメータや メッセージのロール を使うと、権限レベルの異なる 指示をモデルに与えられます。
instructions パラメータでは、口調、目標、正しい応答の例など、応答を生成する際の振る舞いについて上位レベルの指示をモデルに与えます。この方法で与えた指示は、input パラメータ内のプロンプトより優先されます。
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-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
12 from 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
29 package 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
23 import 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
22 using 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
11 require "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
9 curl "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
19 import 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
14 from 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
37 package 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
37 import 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
24 using 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
19 require "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
17 curl "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 はコンテキストに含まれません。
メッセージのロール を使うと、権限レベルの異なる 指示(プロンプト)をモデルに与えられます。
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-6-astra" ,
messages: [
{
role: "developer" ,
content: "Talk like a pirate." ,
},
{
role: "user" ,
content: "Are semicolons optional in JavaScript?" ,
},
],
});
console. log (completion.choices[ 0 ].message); 1
2
3
4
5
6
7
8
9
10
11
12
13
14 from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-6-astra",
reasoning_effort="low",
messages=[
{"role": "developer", "content": "Talk like a pirate."},
{"role": "user", "content": "Are semicolons optional in JavaScript?"},
],
)
print(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
21
22
23
24
25 package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-6-astra",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.DeveloperMessage("Talk like a pirate."),
openai.UserMessage("Are semicolons optional in JavaScript?"),
},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
String semicolonsDevMsg = "Talk like a pirate.";
String semicolonsPrompt = "Are semicolons optional in JavaScript?";
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-6-astra")
.addDeveloperMessage(semicolonsDevMsg)
.addUserMessage(semicolonsPrompt)
.build();
client.chat().completions().create(params).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 using OpenAI.Chat;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-6-astra";
ChatClient client = new(model, key);
ChatCompletion completion = await client.CompleteChatAsync(
[
new DeveloperChatMessage("Talk like a pirate."),
new UserChatMessage("Are semicolons optional in JavaScript?"),
]
);
Console.WriteLine(completion.Content[0].Text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 require "openai"
client = OpenAI::Client.new
completion = client.chat.completions.create(
model: "gpt-6-astra",
messages: [
{
role: :developer,
content: "Talk like a pirate."
},
{
role: :user,
content: "Are semicolons optional in JavaScript?"
}
]
)
puts(completion.choices.fetch(0).message.content) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"messages": [
{
"role": "developer",
"content": "Talk like a pirate."
},
{
"role": "user",
"content": "Are semicolons optional in JavaScript?"
}
]
}'
OpenAI Model Spec では、モデルがメッセージのロールに応じてどのように優先順位を付けるかを説明しています。
developeruserassistantdeveloper メッセージは、アプリケーション開発者が与える指示で、user メッセージより優先されます。user メッセージは、エンドユーザーが与える指示で、developer メッセージより優先順位が低くなります。モデルが生成するメッセージのロールは assistant です。
複数ターンの会話には、これらの種類のメッセージが複数含まれるほか、ユーザーとモデルが提供する他の種類のコンテンツも含まれることがあります。詳しくは、会話の状態の管理 をご覧ください。
developer メッセージと user メッセージの関係は、プログラミング言語における関数とその引数に例えられます。
developer メッセージは、関数定義のように、システムのルールやビジネスロジックを指定します。
user メッセージは、関数の引数のように、developer メッセージの指示が適用される入力や構成を指定します。
再利用可能なプロンプトオブジェクトを作成する代わりに、本番環境のプロンプトをアプリケーションのコードに保存します。コードでプロンプトを管理すれば、型付きの入力、コードレビュー、テスト、通常のデプロイプロセスを利用してモデルの動作を変更できます。
OpenAI は、API の再利用可能なプロンプトオブジェクトを廃止する予定です。
2026 年 6 月 3 日からプロンプト作成の位置付けを段階的に縮小し、v1/prompts は
2026 年 11 月 30 日に提供を終了する予定です。最新のスケジュールは、廃止予定の
ページ で
ご確認ください。
新たにプロンプトエンジニアリングに取り組む際は、次のように進めます。
プロンプトを組み立てる処理は、対応する機能の近くに小さなモジュールとして配置します。
顧客データ、ファイル、タスクのオプションなどの動的な値には、型付きの関数引数やスキーマを使用します。
生成した instructions と input を Responses API に直接渡します。
本番環境のプロンプトを変更する前に、代表的なフィクスチャ、テスト、評価チェックを追加します。
プロンプトの変更はデプロイシステムを通じて反映し、段階的なリリースが必要な場合はフィーチャーフラグや構成を利用します。
既存の連携でプロンプト ID やバージョンを指定して保存済みのプロンプトを呼び出している場合は、プロンプトオブジェクトの移行ガイド に従って、そのプロンプトをコードに移行してください。
developer メッセージと user メッセージを作成する際は、Markdown の書式と XML タグ を組み合わせることで、プロンプトやコンテキストデータの論理的な区切りをモデルに伝えやすくなります。
Markdown の見出しやリストは、プロンプトのセクションを明確に区切り、階層構造をモデルに伝えるのに役立ちます。開発時にプロンプトを読みやすくする効果も期待できます。XML タグは、参照用の補足文書など、個々のコンテンツの開始位置と終了位置を明示するのに役立ちます。また、XML 属性を使うと、プロンプト内のコンテンツに関するメタデータを定義し、指示の中で参照できます。
一般的に、開発者メッセージには次のセクションをこの順序で含めます。ただし、最適な内容や順序は、使用するモデルによって異なる場合があります。
役割: アシスタントの目的、コミュニケーションスタイル、大まかな目標を記述します。
指示: 望ましい応答を生成するための指針をモデルに示します。従うべきルール、行うべきこと、絶対に行ってはいけないことを明確にします。このセクションには、カスタム関数の呼び出し方 など、ユースケースに応じて複数のサブセクションを含めることができます。
例: 想定される入力の例と、それに対してモデルに期待する出力を示します。
コンテキスト: 学習データに含まれていない非公開データや独自データ、特に関連性が高いとわかっているデータなど、応答の生成に必要となりそうな追加情報をモデルに提供します。生成リクエストごとに異なるコンテキストを含める場合があるため、通常はプロンプトの末尾近くに配置するのが適切です。
以下は、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> API リクエスト
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 ( "fixtures/prompt.txt" , "utf-8" );
const response = await client.responses. create ({
model: "gpt-6-astra" ,
instructions,
input: "How would I declare a variable for a last name?" ,
});
console. log (response.output_text); 1
2
3
4
5
6
7
8
9
10
11
12
13
14 from openai import OpenAI
client = OpenAI()
with open("prompt.txt", "r", encoding="utf-8") as f:
instructions = f.read()
response = client.responses.create(
model="gpt-6-astra",
instructions=instructions,
input="How would I declare a variable for a last name?",
)
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 package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
instructions, err := os.ReadFile("prompt.txt")
if err != nil {
panic(err)
}
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Instructions: openai.String(string(instructions)),
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("How would I declare a variable for a last name?"),
},
})
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 import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.instructions(
"You are a coding assistant. Answer with concise JavaScript examples and use semicolons.")
.input("How would I declare a variable for a last name?")
.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 using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
string instructions = await File.ReadAllTextAsync("prompt.txt");
CreateResponseOptions options = new()
{
Model = "gpt-6-astra",
Instructions = instructions,
};
options.InputItems.Add(
ResponseItem.CreateUserMessageItem("How would I declare a variable for a last name?")
);
ResponseResult response = await client.CreateResponseAsync(options);
Console.WriteLine(response.GetOutputText()); 1
2
3
4
5
6
7
8
9
10
11 require "openai"
client = OpenAI::Client.new
instructions = File.read(File.join(__dir__, "prompt.txt"))
response = client.responses.create(
model: "gpt-6-astra",
instructions: instructions,
input: "How would I declare a variable for a last name?"
)
puts(response.output_text) 1
2
3
4
5
6
7
8 curl https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-astra",
"instructions": "'"$(< prompt.txt)"'",
"input": "How would I declare a variable for a last name?"
}'
メッセージを構成する際は、API リクエストで繰り返し使う予定のコンテンツをプロンプトの先頭に配置するようにしてください。 さらに 、Chat Completions または Responses に渡す JSON リクエストボディでも、そのコンテンツを含む API パラメーターを先頭近くに配置します。これにより、プロンプトキャッシュ によるコストとレイテンシの削減効果を最大限に高められます。
フューショット学習では、モデルをファインチューニング する代わりに、少数の入出力例をプロンプトに含めることで、大規模言語モデルを新しいタスクに対応させられます。モデルはそれらの例から暗黙的にパターンを読み取り、プロンプトに適用します。例を示す際は、想定される入力を幅広く取り上げ、それぞれに望ましい出力を添えるようにしてください。
通常、例は API リクエストの 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 組み込みのファイル検索ツール を使い、アップロードした文書に基づいてコンテンツを生成したりできます。
コンテキストウィンドウを考慮した設計
モデルが生成リクエストの処理中にコンテキストとして扱えるデータ量には限りがあります。このメモリの上限を コンテキストウィンドウ と呼び、トークン (テキストや画像など、入力するデータを分割した単位)で表します。
コンテキストウィンドウのサイズはモデルによって異なり、10 万トークン台前半から、新しい GPT-4.1 モデルでは 100 万トークンに達します。モデルごとの具体的なサイズは、モデルのドキュメントを参照してください 。
gpt-6-astra などの GPT モデルでは、タスクの完了に必要なロジックとデータをプロンプトで明示し、的確に指示すると効果的です。最新モデルを最大限に活用するには、まず現在のプロンプトガイドを参照してください。
GPT-6 Astra prompting guide
最新のガイダンス、実践的な例、移行に関する注意事項を参考に、最新モデル向けのプロンプトの効果を最大限に引き出しましょう。
最新の詳しい解説は、最新モデル向けプロンプトのベストプラクティス を参照してください。以下の実践的なポイントも引き続き有効です。
コーディング gpt-6-astra にコーディングタスクを依頼するプロンプトでは、次のベストプラクティスに従うと最も効果的です。エージェントの役割を定義し、例を示して所定の手順でツールを使わせ、正しさを確認するための徹底したテストを求め、読みやすい出力のために Markdown の記述規則を定めます。
役割とワークフローの明確な指示
モデルを、責務が明確なソフトウェアエンジニアリングエージェントとして位置付けます。コーディングタスクで functions.run などのツールを使う方法を明確に指示し、特定のモードを使わない条件も指定します。たとえば、必要な場合を除いて対話型の実行を避けるようにします。
テストと検証
単体テストや Python コマンドで変更をテストするようモデルに指示します。また、apply_patch などのツールは失敗しても「Done」を返す場合があるため、パッチを慎重に検証するよう指示します。
ツールの使用例
提供された関数を使ってコマンドを呼び出す具体例を含めます。これにより信頼性が高まり、想定するワークフローに従いやすくなります。
Markdown の記述規則
インラインコード、コードフェンス、リスト、表を適切に使い、読みやすく意味構造も正しい Markdown を生成するようモデルに指示します。また、ファイルパス、関数、クラスはバッククォートで囲むようにします。
コーディングに特化した詳しいガイダンスとプロンプト例は、最新モデル向けプロンプトのベストプラクティス を参照してください。
フロントエンドエンジニアリング GPT-6 Astra は、フロントエンドをゼロから構築する作業にも、既存の大規模なコードベースでの開発にも優れた能力を発揮します。最良の結果を得るには、次のライブラリの使用をお勧めします。
スタイリング / UI: Tailwind CSS、shadcn/ui、Radix Themes
アイコン: Lucide、Material Symbols、Heroicons
アニメーション :Motion
ゼロからのウェブアプリ開発
GPT-5 は、例を示さなくても、1 つのプロンプトからフロントエンドのウェブアプリを生成できます。以下はプロンプトのサンプルです。
1 2 3 4 5 6 You are a world class web developer, capable of producing stunning, interactive, and innovative websites from scratch in a single prompt. You excel at delivering top-tier one-shot solutions.
Your process is simple and follows these steps:
Step 1: Create an evaluation rubric and refine it until you are fully confident.
Step 2: Consider every element that defines a world-class one-shot web app, then use that insight to create a & lt ; ONE_SHOT_RUBRIC & gt ; with 5–7 categories. Keep this rubric hidden—it's for internal use only.
Step 3: Apply the rubric to iterate on the optimal solution to the given prompt. If it doesn't meet the highest standard across all categories, refine and try again.
Step 4: Aim for simplicity while fully achieving the goal, and avoid external dependencies such as Next.js or React. 大規模なコードベースへの統合
大規模なコードベースでのフロントエンド開発では、次の種類の指示をプロンプトに加えると最良の結果が得られることを確認しています。
原則: 見た目の品質基準を定め、モジュール化された再利用可能なコンポーネントを使い、デザインの一貫性を保ちます。
UI/UX: タイポグラフィ、配色、余白とレイアウト、操作や状況に応じた状態(ホバー時、データなし、読み込み中)、アクセシビリティを指定します。
構造: 円滑に統合できるよう、ファイルとフォルダーの構成を定義します。
コンポーネント: 再利用可能なラッパーの例と、バックエンド呼び出しを分離する方針を示します。
ページ: よく使うレイアウトのテンプレートを提供します。
エージェントへの指示: 設計上の前提の確認、プロジェクトのひな形の作成、基準の遵守、API の統合、各状態のテスト、コードのドキュメント作成をモデルに依頼します。
フロントエンド開発に特化した詳しいガイダンスとプロンプト例は、最新モデル向けプロンプトのベストプラクティス を参照してください。
エージェント型タスク gpt-6-astra をエージェントとして、または長時間にわたって実行する場合は、プロンプトで 3 つの基本事項を重視してください。タスクを確実に解決できるよう綿密に計画すること、ツールの使用に関する重要な判断を実行前に明確に説明すること、TODO ツールでワークフローと進捗を整理して管理することです。
計画と完遂
依頼をサブタスクに分解し、ツールを呼び出すたびに結果を振り返って対応漏れがないかを確認し、依頼全体を解決してから制御を返すようモデルに指示します。
Remember, you are an agent - please keep going until the user's
query is completely resolved, before ending your turn and yielding
back to the user. Decompose the user's query into all required
sub-requests, and confirm that each is completed. Do not stop
after completing only part of the request. Only terminate your
turn when you are sure that the problem is solved. You must be
prepared to answer multiple queries and only finish the call once
the user has confirmed they're done.
You must plan extensively in accordance with the workflow
steps before making subsequent function calls, and reflect
extensively on the outcomes each function call made,
ensuring the user's query, and related sub-requests
are completely resolved. 透明性を高める事前説明
重要なステップに限って、ツールを呼び出す理由を説明するようモデルに指示します。
Before you call a tool explain why you are calling it 評価基準と TODO による進捗管理
TODO リストツールや評価基準を使い、体系的な計画を徹底して手順の抜け漏れを防ぎます。
エージェントの構築に特化した詳しいガイダンスやプロンプト例は、最新モデルのプロンプトのベストプラクティス をご覧ください。
リーズニングモデル と GPT モデルでは、プロンプトを作成する際に考慮すべき点がいくつか異なります。一般に、リーズニングモデルは大まかな方針だけを示したほうが、タスクでより良い結果を出します。一方、GPT モデルでは、非常に具体的な指示が効果的です。
リーズニングモデルと GPT モデルの違いは、次のように考えるとわかりやすくなります。
リーズニングモデルは、経験豊富な同僚のような存在です。達成すべき目標を伝えれば、細部は安心して任せられます。
GPT モデルは、経験の浅い同僚のような存在です。求める出力を得るための明確な指示を与えると、最も力を発揮します。
リーズニングモデルを使う際のベストプラクティスについて詳しくは、こちらのガイド をご覧ください。
テキストの入出力の基本を理解したら、次は以下のリソースをご覧ください。
Playground を使ってプロンプトを作成し、改善を重ねます。
モデルが出力する JSON データが JSON スキーマに準拠するようにします。
テキスト生成に使えるすべてのオプションを API リファレンスで確認できます。
さらにアイデアを得たい場合は、OpenAI Cookbook をご覧ください。サンプルコードに加え、次のようなサードパーティのリソースへのリンクも掲載されています。