Flex 処理では、応答に時間がかかり、リソースを利用できない場合がある代わりに、Responses や Chat Completions のリクエストを低コストで処理できます。モデルの評価、データの拡充、非同期ワークロードなど、本番環境以外で実行するタスクや優先度の低いタスクに最適です。
トークンの料金にはバッチ API と同じ料金が適用され、プロンプトキャッシュによる追加割引も受けられます。
Flex 処理はベータ版で、利用できるモデルは限られています。
対応モデルは料金ページに記載されています。
Flex 処理を使用するには、API リクエストで service_tier パラメーターを flex に設定します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import OpenAI from "openai";
const client = new OpenAI({
timeout: 15 * 1000 * 60, // Increase default timeout to 15 minutes
});
const response = await client.responses.create(
{
model: "gpt-6-astra",
instructions: "List and describe all the metaphors used in this book.",
input: "<very long text of book here>",
service_tier: "flex",
},
{ timeout: 15 * 1000 * 60 }
);
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16from openai import OpenAI
client = OpenAI(
# increase default timeout to 15 minutes (from 10 minutes)
timeout=900.0
)
# you can override the max timeout per request as well
response = client.with_options(timeout=900.0).responses.create(
model="gpt-6-astra",
instructions="List and describe all the metaphors used in this book.",
input="<very long text of book here>",
service_tier="flex",
)
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
25package main
import (
"context"
"fmt"
"time"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient(option.WithRequestTimeout(15 * time.Minute))
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Instructions: openai.String("List and describe all the metaphors used in this book."),
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("<very long text of book here>")},
ServiceTier: responses.ResponseNewParamsServiceTierFlex,
})
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
20import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import java.time.Duration;
client = client.withOptions(options -> options.timeout(Duration.ofMinutes(15)));
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.input("<very long text of book here>")
.instructions("List and describe all the metaphors used in this book.")
.serviceTier(ResponseCreateParams.ServiceTier.FLEX)
.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
19using System.ClientModel;
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClientOptions clientOptions = new() { NetworkTimeout = TimeSpan.FromMinutes(15) };
ResponsesClient client = new(new ApiKeyCredential(key), clientOptions);
CreateResponseOptions options = new()
{
Model = "gpt-6-astra",
Instructions = "List and describe all the metaphors used in this book.",
ServiceTier = ResponseServiceTier.Flex,
};
options.InputItems.Add(ResponseItem.CreateUserMessageItem("<very long text of book here>"));
using CancellationTokenSource timeout = new(TimeSpan.FromMinutes(15));
ResponseResult response = await client.CreateResponseAsync(options, timeout.Token);
Console.WriteLine(response.GetOutputText());
1
2
3
4
5
6
7
8
9
10
11
12require "openai"
client = OpenAI::Client.new(timeout: 900.0)
response = client.responses.create(
model: "gpt-6-astra",
service_tier: :flex,
instructions: "List and describe all the metaphors used in this book.",
input: "<very long text of book here>"
)
puts(response.output_text)
1
2
3
4
5
6
7
8
9curl https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-astra",
"instructions": "List and describe all the metaphors used in this book.",
"input": "<very long text of book here>",
"service_tier": "flex"
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21import OpenAI from "openai";
const client = new OpenAI({
timeout: 15 * 1000 * 60,
});
const response = await client.chat.completions.create(
{
model: "gpt-6-astra",
messages: [
{
role: "developer",
content: "List and describe all the metaphors used in this book.",
},
{ role: "user", content: "<very long text of book here>" },
],
service_tier: "flex",
},
{ timeout: 15 * 1000 * 60 }
);
console.log(response.choices[0].message.content);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18from openai import OpenAI
client = OpenAI(timeout=900.0)
response = client.chat.completions.create(
model="gpt-6-astra",
messages=[
{
"role": "developer",
"content": "List and describe all the metaphors used in this book.",
},
{"role": "user", "content": "<very long text of book here>"},
],
service_tier="flex",
timeout=900.0,
)
print(response.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
26package main
import (
"context"
"fmt"
"time"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
)
func main() {
client := openai.NewClient(option.WithRequestTimeout(15 * time.Minute))
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-6-astra",
ServiceTier: openai.ChatCompletionNewParamsServiceTierFlex,
Messages: []openai.ChatCompletionMessageParamUnion{
openai.DeveloperMessage("List and describe all the metaphors used in this book."),
openai.UserMessage("<very long text of book here>"),
},
})
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
18import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import java.time.Duration;
client = client.withOptions(options -> options.timeout(Duration.ofMinutes(15)));
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-6-astra")
.addDeveloperMessage("List and describe all the metaphors used in this book.")
.addUserMessage("<very long text of book here>")
.serviceTier(ChatCompletionCreateParams.ServiceTier.FLEX)
.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
16
17
18
19
20
21
22using System.ClientModel;
using OpenAI;
using OpenAI.Chat;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-6-astra";
OpenAIClientOptions clientOptions = new() { NetworkTimeout = TimeSpan.FromMinutes(15) };
ChatClient client = new(model, new ApiKeyCredential(key), clientOptions);
ChatCompletionOptions options = new() { ServiceTier = ChatServiceTier.Flex };
using CancellationTokenSource timeout = new(TimeSpan.FromMinutes(15));
ChatCompletion completion = await client.CompleteChatAsync(
[
new DeveloperChatMessage("List and describe all the metaphors used in this book."),
new UserChatMessage("<very long text of book here>"),
],
options,
timeout.Token
);
Console.WriteLine(completion.Content[0].Text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20require "openai"
client = OpenAI::Client.new(timeout: 900.0)
completion = client.chat.completions.create(
model: "gpt-6-astra",
service_tier: :flex,
messages: [
{
role: :developer,
content: "List and describe all the metaphors used in this book."
},
{
role: :user,
content: "<very long text of book here>"
}
]
)
puts(completion.choices.fetch(0).message.content)
1
2
3
4
5
6
7
8curl 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": "List and describe all the metaphors used in this book."},
{"role": "user", "content": "<very long text of book here>"}
],
"service_tier": "flex"
}' --max-time 900
Flex 処理は処理速度が遅いため、リクエストがタイムアウトしやすくなります。タイムアウトに対処する際は、次の点を考慮してください。
- デフォルトのタイムアウト:公式の OpenAI SDK で API リクエストを行う場合、デフォルトのタイムアウトは 10 分 です。長いプロンプトや複雑なタスクでは、この時間を延長する必要がある場合があります。
- タイムアウトの設定:各 SDK には、タイムアウトを延長するためのパラメーターが用意されています。Python と JavaScript の SDK では、上記のコードサンプルに示すように
timeout を使用します。
- 自動再試行:OpenAI SDK は、エラーコード
408 Request Timeout が返されたリクエストを自動的に 2 回再試行してから例外をスローします。
Flex 処理では、リクエストを処理するためのリソースが不足し、エラーコード 429 Resource Unavailable が返される場合があります。 この場合、料金は発生しません。
リソース利用不可エラーへの対処として、次の方法の実装を検討してください。
-
指数バックオフによるリクエストの再試行:指数バックオフは、遅延を許容でき、コストを最小限に抑えたいワークロードに適しています。処理容量に余裕ができた時点で、リクエストを完了できるためです。実装の詳細は、こちらの Cookbookを参照してください。
-
標準処理によるリクエストの再試行:リソース利用不可エラーを受け取った際、ユースケース上、一時的にコストが高くなっても処理の確実な完了を優先したい場合は、標準処理で再試行する方法を実装してください。再試行するリクエストで service_tier を auto に設定するか、service_tier パラメーターを省略してプロジェクトのデフォルトモードを使用します。