Skip to content

Commit

Permalink
[ Edit, Add ] edited and updated docs of tool model directory files code
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfik committed Nov 22, 2023
1 parent 313b07c commit f63803d
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 6 deletions.
33 changes: 33 additions & 0 deletions lib/src/core/models/tool/function/function.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

import 'package:collection/collection.dart';

import 'property.dart';

export 'property.dart';

/// {@template openai_function_model}
/// This class is used to represent an OpenAI function.
/// {@endtemplate}
class OpenAIFunctionModel {
/// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
/// underscores and dashes, with a maximum length of 64.
Expand All @@ -15,12 +22,22 @@ class OpenAIFunctionModel {
/// [JSON Schema](https://json-schema.org/understanding-json-schema) object.
final Map<String, dynamic> parametersSchema;

/// Weither the function have a description.
bool get haveDescription => description != null;

@override
int get hashCode =>
name.hashCode ^ description.hashCode ^ parametersSchema.hashCode;

/// {@macro openai_function_model}
const OpenAIFunctionModel({
required this.name,
required this.parametersSchema,
this.description,
});

/// {@macro openai_function_model}
/// This a factory constructor that allows you to create a new function with valid parameters schema.
factory OpenAIFunctionModel.withParameters({
required String name,
String? description,
Expand All @@ -36,18 +53,34 @@ class OpenAIFunctionModel {
);
}

/// This method is used to convert a [Map<String, dynamic>] object to a [OpenAIFunctionModel] object.
factory OpenAIFunctionModel.fromMap(Map<String, dynamic> map) {
return OpenAIFunctionModel(
name: map['name'],
parametersSchema: jsonDecode(map['arguments']) as Map<String, dynamic>,
);
}

/// This method is used to convert a [OpenAIFunctionModel] object to a [Map<String, dynamic>] object.
Map<String, dynamic> toMap() {
return {
'name': name,
if (description != null) 'description': description,
'parameters': parametersSchema,
};
}

@override
String toString() =>
'OpenAIFunctionModel(name: $name, description: $description, parametersSchema: $parametersSchema)';

@override
bool operator ==(covariant OpenAIFunctionModel other) {
if (identical(this, other)) return true;
final mapEquals = const DeepCollectionEquality().equals;

return other.name == name &&
other.description == description &&
mapEquals(other.parametersSchema, parametersSchema);
}
}
17 changes: 17 additions & 0 deletions lib/src/core/models/tool/function/function_call.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:meta/meta.dart';

/// {@template function_call}
/// Controls how the model responds to function calls.
/// {@endtemplate}
@immutable
class FunctionCall {
/// Force the model to respond to the end-user instead of calling a function.
Expand All @@ -9,8 +11,13 @@ class FunctionCall {
/// The model can pick between an end-user or calling a function.
static const auto = FunctionCall._(value: 'auto');

/// The value of the function call.
final value;

@override
int get hashCode => value.hashCode;

/// {@macro function_call}
const FunctionCall._({required this.value});

/// Specifying a particular function forces the model to call that function.
Expand All @@ -19,4 +26,14 @@ class FunctionCall {
'name': functionName,
});
}

@override
String toString() => value.toString();

@override
bool operator ==(covariant FunctionCall other) {
if (identical(this, other)) return true;

return other.value == value;
}
}
25 changes: 25 additions & 0 deletions lib/src/core/models/tool/function/function_call_response.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:meta/meta.dart';

/// {@template function_call_response}
/// This class is used to represent an OpenAI function call response.
/// {@endtemplate}
@immutable
class FunctionCallResponse {
/// The name of the function that the model wants to call.
Expand All @@ -10,11 +15,22 @@ class FunctionCallResponse {
/// The arguments that the model wants to pass to the function.
final Map<String, dynamic>? arguments;

/// Weither the response have a name.
bool get haveName => name != null;

/// Weither the response have arguments.
bool get haveArguments => arguments != null;

@override
int get hashCode => name.hashCode ^ arguments.hashCode;

/// {@macro function_call_response}
const FunctionCallResponse({
required this.name,
required this.arguments,
});

/// This method is used to convert a [Map<String, dynamic>] object to a [FunctionCallResponse] object.
factory FunctionCallResponse.fromMap(Map<String, dynamic> map) {
final argsField = map['arguments'];

Expand All @@ -32,6 +48,7 @@ class FunctionCallResponse {
);
}

/// This method is used to convert a [FunctionCallResponse] object to a [Map<String, dynamic>] object.
Map<String, dynamic> toMap() {
return {
'name': name,
Expand All @@ -42,4 +59,12 @@ class FunctionCallResponse {
@override
String toString() =>
'FunctionCallResponse(name: $name, arguments: $arguments)';

@override
bool operator ==(covariant FunctionCallResponse other) {
if (identical(this, other)) return true;
final mapEquals = const DeepCollectionEquality().equals;

return other.name == name && mapEquals(other.arguments, arguments);
}
}
49 changes: 43 additions & 6 deletions lib/src/core/models/tool/function/property.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
/// {@template openai_function_property}
/// This class is used to represent an OpenAI function property.
/// {@endtemplate}
class OpenAIFunctionProperty {
/// an integer type.
static const functionTypeInteger = 'integer';

/// A string type.
static const functionTypeString = 'string';

/// A boolean type.
static const functionTypeBoolean = 'boolean';

/// A number type.
static const functionTypeNumber = 'number';

/// An array (List) type.
static const functionTypeArray = 'array';

/// An object (Map) type.
static const functionTypeObject = 'object';

/// The name of the property.
final String name;

/// Weither the property is required.
final bool isRequired;

/// The type of the property.
final Map<String, dynamic> _typeMap;

@override
int get hashCode {
return name.hashCode ^ _typeMap.hashCode ^ isRequired.hashCode;
}

/// {@macro openai_function_property}
const OpenAIFunctionProperty({
required this.name,
required Map<String, dynamic> typeMap,
this.isRequired = false,
List<String>? requiredProperties,
}) : _typeMap = typeMap;

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with a primitive type.
factory OpenAIFunctionProperty.primitive({
required String name,
String? description,
bool isRequired = false,
required String type,
List<dynamic>? enumValues,
List? enumValues,
}) {
return OpenAIFunctionProperty(
name: name,
Expand All @@ -35,6 +62,8 @@ class OpenAIFunctionProperty {
);
}

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with a string type.
factory OpenAIFunctionProperty.string({
required String name,
String? description,
Expand All @@ -50,6 +79,8 @@ class OpenAIFunctionProperty {
);
}

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with a boolean type.
factory OpenAIFunctionProperty.boolean({
required String name,
String? description,
Expand All @@ -63,6 +94,8 @@ class OpenAIFunctionProperty {
);
}

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with an integer type.
factory OpenAIFunctionProperty.integer({
required String name,
String? description,
Expand All @@ -76,6 +109,8 @@ class OpenAIFunctionProperty {
);
}

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with a number type.
factory OpenAIFunctionProperty.number({
required String name,
String? description,
Expand All @@ -89,6 +124,8 @@ class OpenAIFunctionProperty {
);
}

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with an array (List) type.
factory OpenAIFunctionProperty.array({
required String name,
String? description,
Expand All @@ -106,6 +143,8 @@ class OpenAIFunctionProperty {
);
}

/// {@macro openai_function_property}
/// This a factory constructor that allows you to create a new function property with an object (Map) type.
factory OpenAIFunctionProperty.object({
required String name,
String? description,
Expand Down Expand Up @@ -133,19 +172,17 @@ class OpenAIFunctionProperty {
);
}

@override
int get hashCode {
return name.hashCode ^ _typeMap.hashCode;
}

/// The type entry of the property.
MapEntry<String, Map<String, dynamic>> typeEntry() {
return MapEntry(name, _typeMap);
}

/// The type map of the property.
Map<String, dynamic> typeMap() {
return _typeMap;
}

/// This method is used to convert a [OpenAIFunctionProperty] object to a [Map<String, dynamic>] object.
Map<String, dynamic> toMap() {
return {
'name': name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:meta/meta.dart';

/// {@template stream_function_call_response_model}
/// This class is used to represent a stream function call response.
/// {@endtemplate}
@immutable
class StreamFunctionCallResponse {
/// The name of the function that the model wants to call.
Expand All @@ -8,18 +12,30 @@ class StreamFunctionCallResponse {
/// The arguments that the model wants to pass to the function.
final String? arguments;

// Weither the function have a name or not.
bool get hasName => name != null;

// Weither the function have arguments or not.
bool get hasArguments => arguments != null;

@override
int get hashCode => name.hashCode ^ arguments.hashCode;

/// {@macro stream_function_call_response_model}
const StreamFunctionCallResponse({
required this.name,
required this.arguments,
});

/// This method is used to convert a [Map<String, dynamic>] object to a [StreamFunctionCallResponse] object.
factory StreamFunctionCallResponse.fromMap(Map<String, dynamic> map) {
return StreamFunctionCallResponse(
name: map['name'],
arguments: map['arguments'],
);
}

/// This method is used to convert a [StreamFunctionCallResponse] object to a [Map<String, dynamic>] object.
Map<String, dynamic> toMap() {
return {
'name': name,
Expand All @@ -30,4 +46,11 @@ class StreamFunctionCallResponse {
@override
String toString() =>
'StreamFunctionCallResponse(name: $name, arguments: $arguments)';

@override
bool operator ==(covariant StreamFunctionCallResponse other) {
if (identical(this, other)) return true;

return other.name == name && other.arguments == arguments;
}
}
Loading

0 comments on commit f63803d

Please sign in to comment.