Skip to content

Commit

Permalink
feat: Support Predicted Outputs in openai_dart (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz authored Dec 6, 2024
1 parent 10c6688 commit 315fe0f
Show file tree
Hide file tree
Showing 21 changed files with 2,392 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AssistantObject with _$AssistantObject {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down Expand Up @@ -192,7 +192,7 @@ enum AssistantResponseFormatMode {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sealed class AssistantStreamEvent with _$AssistantStreamEvent {
// UNION: ErrorEvent
// ------------------------------------------

/// Occurs when an [error](https://platform.openai.com/docs/guides/error-codes/api-errors) occurs. This can happen due to an internal server error or a timeout.
/// Occurs when an [error](https://platform.openai.com/docs/guides/error-codes#api-errors) occurs. This can happen due to an internal server error or a timeout.
const factory AssistantStreamEvent.errorEvent({
/// The type of the event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,38 @@ class CompletionTokensDetails with _$CompletionTokensDetails {

/// Factory constructor for CompletionTokensDetails
const factory CompletionTokensDetails({
/// When using Predicted Outputs, the number of tokens in the
/// prediction that appeared in the completion.
@JsonKey(name: 'accepted_prediction_tokens', includeIfNull: false)
int? acceptedPredictionTokens,

/// Audio input tokens generated by the model.
@JsonKey(name: 'audio_tokens', includeIfNull: false) int? audioTokens,

/// Tokens generated by the model for reasoning.
@JsonKey(name: 'reasoning_tokens', includeIfNull: false)
int? reasoningTokens,

/// When using Predicted Outputs, the number of tokens in the
/// prediction that did not appear in the completion. However, like
/// reasoning tokens, these tokens are still counted in the total
/// completion tokens for purposes of billing, output, and context window
/// limits.
@JsonKey(name: 'rejected_prediction_tokens', includeIfNull: false)
int? rejectedPredictionTokens,
}) = _CompletionTokensDetails;

/// Object construction from a JSON representation
factory CompletionTokensDetails.fromJson(Map<String, dynamic> json) =>
_$CompletionTokensDetailsFromJson(json);

/// List of all property names of schema
static const List<String> propertyNames = ['reasoning_tokens'];
static const List<String> propertyNames = [
'accepted_prediction_tokens',
'audio_tokens',
'reasoning_tokens',
'rejected_prediction_tokens'
];

/// Perform validations on the schema property values
String? validateSchema() {
Expand All @@ -35,7 +56,10 @@ class CompletionTokensDetails with _$CompletionTokensDetails {
/// Map representation of object (not serialized)
Map<String, dynamic> toMap() {
return {
'accepted_prediction_tokens': acceptedPredictionTokens,
'audio_tokens': audioTokens,
'reasoning_tokens': reasoningTokens,
'rejected_prediction_tokens': rejectedPredictionTokens,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CreateAssistantRequest with _$CreateAssistantRequest {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down Expand Up @@ -290,7 +290,7 @@ enum CreateAssistantResponseFormatMode {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {

/// Factory constructor for CreateChatCompletionRequest
const factory CreateChatCompletionRequest({
/// ID of the model to use. See the [model endpoint compatibility](https://platform.openai.com/docs/models/model-endpoint-compatibility)
/// ID of the model to use. See the [model endpoint compatibility](https://platform.openai.com/docs/models#model-endpoint-compatibility)
/// table for details on which models work with the Chat API.
@_ChatCompletionModelConverter() required ChatCompletionModel model,

Expand All @@ -38,7 +38,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {
/// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the
/// text so far, decreasing the model's likelihood to repeat the same line verbatim.
///
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
@JsonKey(name: 'frequency_penalty', includeIfNull: false)
@Default(0.0)
double? frequencyPenalty,
Expand Down Expand Up @@ -81,13 +81,19 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {
/// A list of modalities that the model may use to generate the completion.
@JsonKey(includeIfNull: false) List<ChatCompletionModality>? modalities,

/// Configuration for a [Predicted Output](https://platform.openai.com/docs/guides/predicted-outputs),
/// which can greatly improve response times when large parts of the model
/// response are known ahead of time. This is most common when you are
/// regenerating a file with only minor changes to most of the content.
@JsonKey(includeIfNull: false) PredictionContent? prediction,

/// Parameters for audio output. Required when audio output is requested with `modalities: ["audio"]`.
/// [Learn more](https://platform.openai.com/docs/guides/audio).
@JsonKey(includeIfNull: false) ChatCompletionAudioOptions? audio,

/// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
///
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
@JsonKey(name: 'presence_penalty', includeIfNull: false)
@Default(0.0)
double? presencePenalty,
Expand Down Expand Up @@ -178,12 +184,12 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {
@JsonKey(name: 'tool_choice', includeIfNull: false)
ChatCompletionToolChoiceOption? toolChoice,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling)
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
/// during tool use.
@JsonKey(name: 'parallel_tool_calls', includeIfNull: false)
bool? parallelToolCalls,

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
@JsonKey(includeIfNull: false) String? user,

/// Deprecated in favor of `tool_choice`.
Expand Down Expand Up @@ -223,6 +229,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {
'max_completion_tokens',
'n',
'modalities',
'prediction',
'audio',
'presence_penalty',
'response_format',
Expand Down Expand Up @@ -318,6 +325,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {
'max_completion_tokens': maxCompletionTokens,
'n': n,
'modalities': modalities,
'prediction': prediction,
'audio': audio,
'presence_penalty': presencePenalty,
'response_format': responseFormat,
Expand Down Expand Up @@ -416,7 +424,7 @@ enum ChatCompletionModels {
// CLASS: ChatCompletionModel
// ==========================================

/// ID of the model to use. See the [model endpoint compatibility](https://platform.openai.com/docs/models/model-endpoint-compatibility)
/// ID of the model to use. See the [model endpoint compatibility](https://platform.openai.com/docs/models#model-endpoint-compatibility)
/// table for details on which models work with the Chat API.
@freezed
sealed class ChatCompletionModel with _$ChatCompletionModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CreateCompletionRequest with _$CreateCompletionRequest {
/// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the
/// text so far, decreasing the model's likelihood to repeat the same line verbatim.
///
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
@JsonKey(name: 'frequency_penalty', includeIfNull: false)
@Default(0.0)
double? frequencyPenalty,
Expand Down Expand Up @@ -70,7 +70,7 @@ class CreateCompletionRequest with _$CreateCompletionRequest {

/// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
///
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details)
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
@JsonKey(name: 'presence_penalty', includeIfNull: false)
@Default(0.0)
double? presencePenalty,
Expand Down Expand Up @@ -107,7 +107,7 @@ class CreateCompletionRequest with _$CreateCompletionRequest {
/// We generally recommend altering this or `temperature` but not both.
@JsonKey(name: 'top_p', includeIfNull: false) @Default(1.0) double? topP,

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
@JsonKey(includeIfNull: false) String? user,
}) = _CreateCompletionRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CreateEmbeddingRequest with _$CreateEmbeddingRequest {
/// The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
@JsonKey(includeIfNull: false) int? dimensions,

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
@JsonKey(includeIfNull: false) String? user,
}) = _CreateEmbeddingRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CreateFineTuningJobRequest with _$CreateFineTuningJobRequest {
/// Factory constructor for CreateFineTuningJobRequest
const factory CreateFineTuningJobRequest({
/// The name of the model to fine-tune. You can select one of the
/// [supported models](https://platform.openai.com/docs/guides/fine-tuning/what-models-can-be-fine-tuned).
/// [supported models](https://platform.openai.com/docs/guides/fine-tuning#what-models-can-be-fine-tuned).
@_FineTuningModelConverter() required FineTuningModel model,

/// The ID of an uploaded file that contains training data.
Expand Down Expand Up @@ -136,7 +136,7 @@ enum FineTuningModels {
// ==========================================

/// The name of the model to fine-tune. You can select one of the
/// [supported models](https://platform.openai.com/docs/guides/fine-tuning/what-models-can-be-fine-tuned).
/// [supported models](https://platform.openai.com/docs/guides/fine-tuning#what-models-can-be-fine-tuned).
@freezed
sealed class FineTuningModel with _$FineTuningModel {
const FineTuningModel._();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CreateImageRequest with _$CreateImageRequest {
@Default(ImageStyle.vivid)
ImageStyle? style,

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
@JsonKey(includeIfNull: false) String? user,
}) = _CreateImageRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ class CreateRunRequest with _$CreateRunRequest {
@JsonKey(name: 'tool_choice', includeIfNull: false)
CreateRunRequestToolChoice? toolChoice,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling)
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
/// during tool use.
@JsonKey(name: 'parallel_tool_calls', includeIfNull: false)
bool? parallelToolCalls,

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down Expand Up @@ -411,7 +411,7 @@ enum CreateRunRequestResponseFormatMode {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ class CreateThreadAndRunRequest with _$CreateThreadAndRunRequest {
@JsonKey(name: 'tool_choice', includeIfNull: false)
CreateThreadAndRunRequestToolChoice? toolChoice,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling)
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
/// during tool use.
@JsonKey(name: 'parallel_tool_calls', includeIfNull: false)
bool? parallelToolCalls,

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down Expand Up @@ -437,7 +437,7 @@ enum CreateThreadAndRunRequestResponseFormatMode {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ModifyAssistantRequest with _$ModifyAssistantRequest {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down Expand Up @@ -172,7 +172,7 @@ enum ModifyAssistantResponseFormatMode {

/// Specifies the format that the model must output. Compatible with
/// [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
/// [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models
/// since `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures
Expand Down
Loading

0 comments on commit 315fe0f

Please sign in to comment.