Skip to content

Commit f63803d

Browse files
committed
[ Edit, Add ] edited and updated docs of tool model directory files code
1 parent 313b07c commit f63803d

File tree

6 files changed

+166
-6
lines changed

6 files changed

+166
-6
lines changed

lib/src/core/models/tool/function/function.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
// ignore_for_file: public_member_api_docs, sort_constructors_first
12
import 'dart:convert';
23

4+
import 'package:collection/collection.dart';
5+
36
import 'property.dart';
7+
48
export 'property.dart';
59

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

25+
/// Weither the function have a description.
26+
bool get haveDescription => description != null;
27+
28+
@override
29+
int get hashCode =>
30+
name.hashCode ^ description.hashCode ^ parametersSchema.hashCode;
31+
32+
/// {@macro openai_function_model}
1833
const OpenAIFunctionModel({
1934
required this.name,
2035
required this.parametersSchema,
2136
this.description,
2237
});
2338

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

56+
/// This method is used to convert a [Map<String, dynamic>] object to a [OpenAIFunctionModel] object.
3957
factory OpenAIFunctionModel.fromMap(Map<String, dynamic> map) {
4058
return OpenAIFunctionModel(
4159
name: map['name'],
4260
parametersSchema: jsonDecode(map['arguments']) as Map<String, dynamic>,
4361
);
4462
}
4563

64+
/// This method is used to convert a [OpenAIFunctionModel] object to a [Map<String, dynamic>] object.
4665
Map<String, dynamic> toMap() {
4766
return {
4867
'name': name,
4968
if (description != null) 'description': description,
5069
'parameters': parametersSchema,
5170
};
5271
}
72+
73+
@override
74+
String toString() =>
75+
'OpenAIFunctionModel(name: $name, description: $description, parametersSchema: $parametersSchema)';
76+
77+
@override
78+
bool operator ==(covariant OpenAIFunctionModel other) {
79+
if (identical(this, other)) return true;
80+
final mapEquals = const DeepCollectionEquality().equals;
81+
82+
return other.name == name &&
83+
other.description == description &&
84+
mapEquals(other.parametersSchema, parametersSchema);
85+
}
5386
}

lib/src/core/models/tool/function/function_call.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:meta/meta.dart';
22

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

14+
/// The value of the function call.
1215
final value;
1316

17+
@override
18+
int get hashCode => value.hashCode;
19+
20+
/// {@macro function_call}
1421
const FunctionCall._({required this.value});
1522

1623
/// Specifying a particular function forces the model to call that function.
@@ -19,4 +26,14 @@ class FunctionCall {
1926
'name': functionName,
2027
});
2128
}
29+
30+
@override
31+
String toString() => value.toString();
32+
33+
@override
34+
bool operator ==(covariant FunctionCall other) {
35+
if (identical(this, other)) return true;
36+
37+
return other.value == value;
38+
}
2239
}

lib/src/core/models/tool/function/function_call_response.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// ignore_for_file: public_member_api_docs, sort_constructors_first
12
import 'dart:convert';
23

4+
import 'package:collection/collection.dart';
35
import 'package:meta/meta.dart';
46

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

18+
/// Weither the response have a name.
19+
bool get haveName => name != null;
20+
21+
/// Weither the response have arguments.
22+
bool get haveArguments => arguments != null;
23+
24+
@override
25+
int get hashCode => name.hashCode ^ arguments.hashCode;
26+
27+
/// {@macro function_call_response}
1328
const FunctionCallResponse({
1429
required this.name,
1530
required this.arguments,
1631
});
1732

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

@@ -32,6 +48,7 @@ class FunctionCallResponse {
3248
);
3349
}
3450

51+
/// This method is used to convert a [FunctionCallResponse] object to a [Map<String, dynamic>] object.
3552
Map<String, dynamic> toMap() {
3653
return {
3754
'name': name,
@@ -42,4 +59,12 @@ class FunctionCallResponse {
4259
@override
4360
String toString() =>
4461
'FunctionCallResponse(name: $name, arguments: $arguments)';
62+
63+
@override
64+
bool operator ==(covariant FunctionCallResponse other) {
65+
if (identical(this, other)) return true;
66+
final mapEquals = const DeepCollectionEquality().equals;
67+
68+
return other.name == name && mapEquals(other.arguments, arguments);
69+
}
4570
}

lib/src/core/models/tool/function/property.dart

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
1+
/// {@template openai_function_property}
2+
/// This class is used to represent an OpenAI function property.
3+
/// {@endtemplate}
14
class OpenAIFunctionProperty {
5+
/// an integer type.
26
static const functionTypeInteger = 'integer';
7+
8+
/// A string type.
39
static const functionTypeString = 'string';
10+
11+
/// A boolean type.
412
static const functionTypeBoolean = 'boolean';
13+
14+
/// A number type.
515
static const functionTypeNumber = 'number';
16+
17+
/// An array (List) type.
618
static const functionTypeArray = 'array';
19+
20+
/// An object (Map) type.
721
static const functionTypeObject = 'object';
822

23+
/// The name of the property.
924
final String name;
25+
26+
/// Weither the property is required.
1027
final bool isRequired;
28+
29+
/// The type of the property.
1130
final Map<String, dynamic> _typeMap;
1231

32+
@override
33+
int get hashCode {
34+
return name.hashCode ^ _typeMap.hashCode ^ isRequired.hashCode;
35+
}
36+
37+
/// {@macro openai_function_property}
1338
const OpenAIFunctionProperty({
1439
required this.name,
1540
required Map<String, dynamic> typeMap,
1641
this.isRequired = false,
1742
List<String>? requiredProperties,
1843
}) : _typeMap = typeMap;
1944

45+
/// {@macro openai_function_property}
46+
/// This a factory constructor that allows you to create a new function property with a primitive type.
2047
factory OpenAIFunctionProperty.primitive({
2148
required String name,
2249
String? description,
2350
bool isRequired = false,
2451
required String type,
25-
List<dynamic>? enumValues,
52+
List? enumValues,
2653
}) {
2754
return OpenAIFunctionProperty(
2855
name: name,
@@ -35,6 +62,8 @@ class OpenAIFunctionProperty {
3562
);
3663
}
3764

65+
/// {@macro openai_function_property}
66+
/// This a factory constructor that allows you to create a new function property with a string type.
3867
factory OpenAIFunctionProperty.string({
3968
required String name,
4069
String? description,
@@ -50,6 +79,8 @@ class OpenAIFunctionProperty {
5079
);
5180
}
5281

82+
/// {@macro openai_function_property}
83+
/// This a factory constructor that allows you to create a new function property with a boolean type.
5384
factory OpenAIFunctionProperty.boolean({
5485
required String name,
5586
String? description,
@@ -63,6 +94,8 @@ class OpenAIFunctionProperty {
6394
);
6495
}
6596

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

112+
/// {@macro openai_function_property}
113+
/// This a factory constructor that allows you to create a new function property with a number type.
79114
factory OpenAIFunctionProperty.number({
80115
required String name,
81116
String? description,
@@ -89,6 +124,8 @@ class OpenAIFunctionProperty {
89124
);
90125
}
91126

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

146+
/// {@macro openai_function_property}
147+
/// This a factory constructor that allows you to create a new function property with an object (Map) type.
109148
factory OpenAIFunctionProperty.object({
110149
required String name,
111150
String? description,
@@ -133,19 +172,17 @@ class OpenAIFunctionProperty {
133172
);
134173
}
135174

136-
@override
137-
int get hashCode {
138-
return name.hashCode ^ _typeMap.hashCode;
139-
}
140-
175+
/// The type entry of the property.
141176
MapEntry<String, Map<String, dynamic>> typeEntry() {
142177
return MapEntry(name, _typeMap);
143178
}
144179

180+
/// The type map of the property.
145181
Map<String, dynamic> typeMap() {
146182
return _typeMap;
147183
}
148184

185+
/// This method is used to convert a [OpenAIFunctionProperty] object to a [Map<String, dynamic>] object.
149186
Map<String, dynamic> toMap() {
150187
return {
151188
'name': name,

lib/src/core/models/tool/function/stream_function_call_response.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// ignore_for_file: public_member_api_docs, sort_constructors_first
12
import 'package:meta/meta.dart';
23

4+
/// {@template stream_function_call_response_model}
5+
/// This class is used to represent a stream function call response.
6+
/// {@endtemplate}
37
@immutable
48
class StreamFunctionCallResponse {
59
/// The name of the function that the model wants to call.
@@ -8,18 +12,30 @@ class StreamFunctionCallResponse {
812
/// The arguments that the model wants to pass to the function.
913
final String? arguments;
1014

15+
// Weither the function have a name or not.
16+
bool get hasName => name != null;
17+
18+
// Weither the function have arguments or not.
19+
bool get hasArguments => arguments != null;
20+
21+
@override
22+
int get hashCode => name.hashCode ^ arguments.hashCode;
23+
24+
/// {@macro stream_function_call_response_model}
1125
const StreamFunctionCallResponse({
1226
required this.name,
1327
required this.arguments,
1428
});
1529

30+
/// This method is used to convert a [Map<String, dynamic>] object to a [StreamFunctionCallResponse] object.
1631
factory StreamFunctionCallResponse.fromMap(Map<String, dynamic> map) {
1732
return StreamFunctionCallResponse(
1833
name: map['name'],
1934
arguments: map['arguments'],
2035
);
2136
}
2237

38+
/// This method is used to convert a [StreamFunctionCallResponse] object to a [Map<String, dynamic>] object.
2339
Map<String, dynamic> toMap() {
2440
return {
2541
'name': name,
@@ -30,4 +46,11 @@ class StreamFunctionCallResponse {
3046
@override
3147
String toString() =>
3248
'StreamFunctionCallResponse(name: $name, arguments: $arguments)';
49+
50+
@override
51+
bool operator ==(covariant StreamFunctionCallResponse other) {
52+
if (identical(this, other)) return true;
53+
54+
return other.name == name && other.arguments == arguments;
55+
}
3356
}

0 commit comments

Comments
 (0)