编写、审查、编辑代码以及解答代码相关问题,是目前 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 为主的代码生成工作流的提示模式。