OpenAI Flex 处理
Flex处理以较慢响应及偶尔资源不足,换取更低的Responses或Chat Completions费用。它适合非生产或低优先级工作,例如模型评测、数据补充和异步任务。
词元采用Batch费率,提示缓存还可提供进一步折扣,详见价格。Flex仍处于Beta,模型范围有限,见Flex支持列表。
API用法
在请求中设置service_tier:"flex":
来源示例 1(javascript):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
import 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);
来源示例 2(python):
## [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
from 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)
来源示例 3(go):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
package 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())
}
来源示例 4(java):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
import 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()));
来源示例 5(csharp):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
using 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());
来源示例 6(ruby):
## [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
require "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)
来源示例 7(bash):
## [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Responses版本,未实际调用。
curl 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"
}'
API请求超时
Flex处理较慢,更容易超时。来源说明官方SDK默认超时为10分钟,长提示词和复杂任务可能需要增加。
各SDK提供相应超时参数,Python与JavaScript使用示例中的timeout。对于返回408 Request Timeout的请求,官方SDK会自动重试两次再抛异常;实际行为仍应核对安装版本。
资源不可用错误
资源不足时可能返回429 Resource Unavailable,这种情况下不收费。
- 指数退避重试:适合容忍延迟且注重费用的任务,等待容量恢复。实现见退避示例。
- 使用标准处理重试:如果愿意偶尔增加费用来争取完成,可按来源将
service_tier设为auto或移除此参数。此时使用项目默认模式;项目默认层级仍需核对,不能无条件保证一定落到Standard。
同页Chat Completions版本:以下7个示例使用独立的消息与输出结构,不应直接混用于Responses。
来源示例 8(javascript):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
import 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);
来源示例 9(python):
## [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
from 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)
来源示例 10(go):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
package 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)
}
来源示例 11(java):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
import 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);
来源示例 12(csharp):
// [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
using 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);
来源示例 13(ruby):
## [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
require "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)
来源示例 14(bash):
## [中文] 设置service_tier为flex并按较慢处理调整超时;资源不可用时按策略退避或切回项目默认层级。本块为Chat Completions版本,字段结构不能直接套到Responses。
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": "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
TRAE-AI编程


更多建议: