O processamento Flex oferece custos menores para requisições às APIs Responses ou Chat Completions em troca de tempos de resposta mais longos e indisponibilidade ocasional de recursos. É ideal para tarefas fora de produção ou de menor prioridade, como avaliações de modelos, enriquecimento de dados e cargas de trabalho assíncronas.
Os tokens são cobrados pelas tarifas da API Batch, com descontos adicionais pelo uso de cache de prompts.
O processamento Flex está em beta e disponível para um conjunto limitado de modelos. Os modelos compatíveis
estão listados na página de preços.
Para usar o processamento Flex, defina o parâmetro service_tier como flex na sua requisição à API:
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
Como o processamento Flex é mais lento, as requisições têm maior probabilidade de exceder o tempo limite. Veja alguns pontos a considerar ao lidar com essas situações:
- Tempo limite padrão: O tempo limite padrão é de 10 minutos para requisições à API feitas com um SDK oficial da OpenAI. Pode ser necessário aumentar esse tempo limite para prompts longos ou tarefas complexas.
- Configuração dos tempos limite: Cada SDK oferece um parâmetro para aumentar esse tempo limite. Nos SDKs de Python e JavaScript, esse parâmetro é
timeout, como mostrado nos exemplos de código acima.
- Novas tentativas automáticas: Os SDKs da OpenAI repetem automaticamente duas vezes as requisições que retornam o código de erro
408 Request Timeout antes de lançar uma exceção.
Às vezes, o processamento Flex pode não ter recursos suficientes para atender às suas requisições, resultando no código de erro 429 Resource Unavailable. Não haverá cobrança quando isso ocorrer.
Considere implementar estas estratégias para lidar com erros de indisponibilidade de recursos:
-
Repita as requisições com espera exponencial: Implementar uma espera exponencial é adequado para cargas de trabalho que toleram atrasos e tem como objetivo minimizar os custos, pois sua requisição poderá ser concluída quando houver mais capacidade disponível. Para detalhes de implementação, consulte este exemplo do Cookbook.
-
Repita as requisições com processamento padrão: Ao receber um erro de indisponibilidade de recursos, implemente uma estratégia de novas tentativas com processamento padrão se, no seu caso de uso, valer a pena aceitar custos ocasionalmente mais altos para garantir a conclusão bem-sucedida. Para isso, defina service_tier como auto na nova tentativa ou remova o parâmetro service_tier para usar o modo padrão do projeto.