撰寫、審查、編輯程式碼,以及回答程式碼相關問題,是目前 OpenAI 模型的主要使用案例之一。本指南介紹如何運用 gpt-6-astra 與 Codex 生成程式碼,以及有哪些選擇。
Codex 是 OpenAI 專為軟體開發打造的程式設計智慧體,可協助你撰寫、審查程式碼及進行偵錯。你可以透過多種介面與 Codex 互動:在 IDE 中、透過 CLI、使用網頁與行動版網站,或透過 SDK 在 CI/CD 流程中使用。Codex 是在專案中導入智慧體式軟體工程的最佳方式。
Codex 搭配最新的通用模型(例如 gpt-5.6)時,表現最佳。我們也提供一系列專為 Codex 等程式設計智慧體打造的模型,例如 gpt-5.3-codex,但對於大多數程式碼生成任務,我們建議使用最新的通用模型。
請參閱 ChatGPT 文件,取得設定指南、參考資料、定價及更多資訊。
對於大多數透過 API 生成程式碼的情境,建議從 gpt-6-astra 開始。它能同時處理通用工作與程式設計,因此,當應用程式需要在同一處撰寫程式碼、推敲需求、查閱文件及處理更廣泛的工作流程時,它是理想的預設選擇。
以下範例示範如何使用 Responses API 生成程式碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import OpenAI from "openai";
const openai = new OpenAI();
const result = await openai.responses.create({
model: "gpt-6-astra",
input: `Find the null pointer exception in this code:
def display_name(user):
return user.profile.name
print(display_name(None))
`,
reasoning: { effort: "high" },
});
console.log(result.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17from openai import OpenAI
client = OpenAI()
result = client.responses.create(
model="gpt-6-astra",
input="""Find the null pointer exception in this code:
def display_name(user):
return user.profile.name
print(display_name(None))
""",
reasoning={"effort": "high"},
)
print(result.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
28package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
"github.com/openai/openai-go/v3/shared"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String(`Find the null pointer exception in this code:
def display_name(user):
return user.profile.name
print(display_name(None))`)},
Reasoning: shared.ReasoningParam{Effort: shared.ReasoningEffortHigh},
})
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
26import 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 code =
"""
def display_name(user):
return user.profile.name
print(display_name(None))
""";
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("Find the null pointer exception in this code:\n\n" + code)
.reasoning(Reasoning.builder().effort(ReasoningEffort.HIGH).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
25
26
27
28
29using 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.High,
},
};
options.InputItems.Add(
ResponseItem.CreateUserMessageItem(
"""
Find the null pointer exception in this code:
def display_name(user):
return user.profile.name
print(display_name(None))
"""
)
);
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
17require "openai"
client = OpenAI::Client.new
code = <<~PYTHON
def display_name(user):
return user.profile.name
print(display_name(None))
PYTHON
response = client.responses.create(
model: "gpt-6-astra",
input: "Find the null pointer exception in this code:\n\n#{code}",
reasoning: { effort: :high }
)
puts(response.output_text)
1
2
3
4
5
6
7
8curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"input": "Find the null pointer exception in this code:\n\ndef display_name(user):\n return user.profile.name\n\nprint(display_name(None))\n",
"reasoning": { "effort": "high" }
}'
我們的 GPT-5 系列模型特別擅長前端開發,搭配 Codex 等程式設計智慧體任務執行框架時,表現尤其出色。
以下示範應用程式都是一次生成的成果,也就是僅憑一則提示詞生成,沒有手動撰寫程式碼。你可以用這些範例評估前端生成品質,以及適合以 UI 為主的程式碼生成工作流程的提示詞模式。