Skip to content

Commit

Permalink
feat: Support specifying tool choice in Assistant API in openai_dart (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Apr 16, 2024
1 parent a864dae commit 97d7977
Show file tree
Hide file tree
Showing 10 changed files with 4,697 additions and 2,204 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: invalid_annotation_target
part of open_a_i_schema;

// ==========================================
// CLASS: AssistantsFunctionCallOption
// ==========================================

/// No Description
@freezed
class AssistantsFunctionCallOption with _$AssistantsFunctionCallOption {
const AssistantsFunctionCallOption._();

/// Factory constructor for AssistantsFunctionCallOption
const factory AssistantsFunctionCallOption({
/// The name of the function to call.
required String name,
}) = _AssistantsFunctionCallOption;

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

/// List of all property names of schema
static const List<String> propertyNames = ['name'];

/// Perform validations on the schema property values
String? validateSchema() {
return null;
}

/// Map representation of object (not serialized)
Map<String, dynamic> toMap() {
return {
'name': name,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: invalid_annotation_target
part of open_a_i_schema;

// ==========================================
// CLASS: AssistantsNamedToolChoice
// ==========================================

/// Specifies a tool the model should use. Use to force the model to call a specific tool.
@freezed
class AssistantsNamedToolChoice with _$AssistantsNamedToolChoice {
const AssistantsNamedToolChoice._();

/// Factory constructor for AssistantsNamedToolChoice
const factory AssistantsNamedToolChoice({
/// The type of the tool. If type is `function`, the function name must be set
required AssistantsToolType type,

/// No Description
@JsonKey(includeIfNull: false) AssistantsFunctionCallOption? function,
}) = _AssistantsNamedToolChoice;

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

/// List of all property names of schema
static const List<String> propertyNames = ['type', 'function'];

/// Perform validations on the schema property values
String? validateSchema() {
return null;
}

/// Map representation of object (not serialized)
Map<String, dynamic> toMap() {
return {
'type': type,
'function': function,
};
}
}

// ==========================================
// ENUM: AssistantsToolType
// ==========================================

/// The type of the tool. If type is `function`, the function name must be set
enum AssistantsToolType {
@JsonValue('function')
function,
@JsonValue('code_interpreter')
codeInterpreter,
@JsonValue('retrieval')
retrieval,
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class CreateRunRequest with _$CreateRunRequest {
@JsonKey(name: 'truncation_strategy', includeIfNull: false)
TruncationObject? truncationStrategy,

/// Controls which (if any) tool is called by the model.
/// `none` means the model will not call any tools and instead generates a message.
/// `auto` is the default value and means the model can pick between generating a message or calling a tool.
/// Specifying a particular tool like `{"type": "TOOL_TYPE"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
@_CreateRunRequestToolChoiceConverter()
@JsonKey(name: 'tool_choice', includeIfNull: false)
CreateRunRequestToolChoice? toolChoice,

/// Specifies the format that the model must output. Compatible with [GPT-4 Turbo](/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON.
Expand Down Expand Up @@ -85,6 +93,7 @@ class CreateRunRequest with _$CreateRunRequest {
'max_prompt_tokens',
'max_completion_tokens',
'truncation_strategy',
'tool_choice',
'response_format',
'stream'
];
Expand Down Expand Up @@ -128,6 +137,7 @@ class CreateRunRequest with _$CreateRunRequest {
'max_prompt_tokens': maxPromptTokens,
'max_completion_tokens': maxCompletionTokens,
'truncation_strategy': truncationStrategy,
'tool_choice': toolChoice,
'response_format': responseFormat,
'stream': stream,
};
Expand Down Expand Up @@ -238,6 +248,87 @@ class _CreateRunRequestModelConverter
}
}

// ==========================================
// ENUM: CreateRunRequestToolChoiceMode
// ==========================================

/// `none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function.
enum CreateRunRequestToolChoiceMode {
@JsonValue('none')
none,
@JsonValue('auto')
auto,
}

// ==========================================
// CLASS: CreateRunRequestToolChoice
// ==========================================

/// Controls which (if any) tool is called by the model.
/// `none` means the model will not call any tools and instead generates a message.
/// `auto` is the default value and means the model can pick between generating a message or calling a tool.
/// Specifying a particular tool like `{"type": "TOOL_TYPE"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
@freezed
sealed class CreateRunRequestToolChoice with _$CreateRunRequestToolChoice {
const CreateRunRequestToolChoice._();

/// `none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function.
const factory CreateRunRequestToolChoice.mode(
CreateRunRequestToolChoiceMode value,
) = CreateRunRequestToolChoiceEnumeration;

/// No Description
const factory CreateRunRequestToolChoice.tool(
AssistantsNamedToolChoice value,
) = CreateRunRequestToolChoiceAssistantsNamedToolChoice;

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

/// Custom JSON converter for [CreateRunRequestToolChoice]
class _CreateRunRequestToolChoiceConverter
implements JsonConverter<CreateRunRequestToolChoice?, Object?> {
const _CreateRunRequestToolChoiceConverter();

@override
CreateRunRequestToolChoice? fromJson(Object? data) {
if (data == null) {
return null;
}
if (data is String &&
_$CreateRunRequestToolChoiceModeEnumMap.values.contains(data)) {
return CreateRunRequestToolChoiceEnumeration(
_$CreateRunRequestToolChoiceModeEnumMap.keys.elementAt(
_$CreateRunRequestToolChoiceModeEnumMap.values.toList().indexOf(data),
),
);
}
if (data is Map<String, dynamic>) {
try {
return CreateRunRequestToolChoiceAssistantsNamedToolChoice(
AssistantsNamedToolChoice.fromJson(data),
);
} catch (e) {}
}
throw Exception(
'Unexpected value for CreateRunRequestToolChoice: $data',
);
}

@override
Object? toJson(CreateRunRequestToolChoice? data) {
return switch (data) {
CreateRunRequestToolChoiceEnumeration(value: final v) =>
_$CreateRunRequestToolChoiceModeEnumMap[v]!,
CreateRunRequestToolChoiceAssistantsNamedToolChoice(value: final v) =>
v.toJson(),
null => null,
};
}
}

// ==========================================
// ENUM: CreateRunRequestResponseFormatMode
// ==========================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class CreateThreadAndRunRequest with _$CreateThreadAndRunRequest {
@JsonKey(name: 'truncation_strategy', includeIfNull: false)
TruncationObject? truncationStrategy,

/// Controls which (if any) tool is called by the model.
/// `none` means the model will not call any tools and instead generates a message.
/// `auto` is the default value and means the model can pick between generating a message or calling a tool.
/// Specifying a particular tool like `{"type": "TOOL_TYPE"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
@_CreateThreadAndRunRequestToolChoiceConverter()
@JsonKey(name: 'tool_choice', includeIfNull: false)
CreateThreadAndRunRequestToolChoice? toolChoice,

/// Specifies the format that the model must output. Compatible with [GPT-4 Turbo](/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`.
///
/// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON.
Expand Down Expand Up @@ -79,6 +87,7 @@ class CreateThreadAndRunRequest with _$CreateThreadAndRunRequest {
'max_prompt_tokens',
'max_completion_tokens',
'truncation_strategy',
'tool_choice',
'response_format',
'stream'
];
Expand Down Expand Up @@ -121,6 +130,7 @@ class CreateThreadAndRunRequest with _$CreateThreadAndRunRequest {
'max_prompt_tokens': maxPromptTokens,
'max_completion_tokens': maxCompletionTokens,
'truncation_strategy': truncationStrategy,
'tool_choice': toolChoice,
'response_format': responseFormat,
'stream': stream,
};
Expand Down Expand Up @@ -231,6 +241,94 @@ class _ThreadAndRunModelConverter
}
}

// ==========================================
// ENUM: CreateThreadAndRunRequestToolChoiceMode
// ==========================================

/// `none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function.
enum CreateThreadAndRunRequestToolChoiceMode {
@JsonValue('none')
none,
@JsonValue('auto')
auto,
}

// ==========================================
// CLASS: CreateThreadAndRunRequestToolChoice
// ==========================================

/// Controls which (if any) tool is called by the model.
/// `none` means the model will not call any tools and instead generates a message.
/// `auto` is the default value and means the model can pick between generating a message or calling a tool.
/// Specifying a particular tool like `{"type": "TOOL_TYPE"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
@freezed
sealed class CreateThreadAndRunRequestToolChoice
with _$CreateThreadAndRunRequestToolChoice {
const CreateThreadAndRunRequestToolChoice._();

/// `none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function.
const factory CreateThreadAndRunRequestToolChoice.mode(
CreateThreadAndRunRequestToolChoiceMode value,
) = CreateThreadAndRunRequestToolChoiceEnumeration;

/// No Description
const factory CreateThreadAndRunRequestToolChoice.tool(
AssistantsNamedToolChoice value,
) = CreateThreadAndRunRequestToolChoiceAssistantsNamedToolChoice;

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

/// Custom JSON converter for [CreateThreadAndRunRequestToolChoice]
class _CreateThreadAndRunRequestToolChoiceConverter
implements JsonConverter<CreateThreadAndRunRequestToolChoice?, Object?> {
const _CreateThreadAndRunRequestToolChoiceConverter();

@override
CreateThreadAndRunRequestToolChoice? fromJson(Object? data) {
if (data == null) {
return null;
}
if (data is String &&
_$CreateThreadAndRunRequestToolChoiceModeEnumMap.values
.contains(data)) {
return CreateThreadAndRunRequestToolChoiceEnumeration(
_$CreateThreadAndRunRequestToolChoiceModeEnumMap.keys.elementAt(
_$CreateThreadAndRunRequestToolChoiceModeEnumMap.values
.toList()
.indexOf(data),
),
);
}
if (data is Map<String, dynamic>) {
try {
return CreateThreadAndRunRequestToolChoiceAssistantsNamedToolChoice(
AssistantsNamedToolChoice.fromJson(data),
);
} catch (e) {}
}
throw Exception(
'Unexpected value for CreateThreadAndRunRequestToolChoice: $data',
);
}

@override
Object? toJson(CreateThreadAndRunRequestToolChoice? data) {
return switch (data) {
CreateThreadAndRunRequestToolChoiceEnumeration(value: final v) =>
_$CreateThreadAndRunRequestToolChoiceModeEnumMap[v]!,
CreateThreadAndRunRequestToolChoiceAssistantsNamedToolChoice(
value: final v
) =>
v.toJson(),
null => null,
};
}
}

// ==========================================
// ENUM: CreateThreadAndRunRequestResponseFormatMode
// ==========================================
Expand Down

0 comments on commit 97d7977

Please sign in to comment.