Skip to content

Commit

Permalink
[ Edit ] refactored completion models classes and invoked toString an…
Browse files Browse the repository at this point in the history
…d equality for sub-models
  • Loading branch information
anasfik committed Feb 23, 2023
1 parent 07fc2c5 commit abe8552
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 49 deletions.
81 changes: 57 additions & 24 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml
linter:
rules:

# Uncomment the following section to specify additional rules.
analyzer:
plugins:
- dart_code_metrics

# linter:
# rules:
# - camel_case_types
dart_code_metrics:
extends:
- ... # configures the list of preset configurations
metrics:
cyclomatic-complexity: 20
lines-of-code: 100
maximum-nesting-level: 4
number-of-parameters: 4

# analyzer:
# exclude:
# - path/to/excluded/files/**
rules:
- avoid-dynamic
- avoid-redundant-async
- avoid-passing-async-when-sync-expected
- avoid-redundant-async
- avoid-unnecessary-type-assertions
- avoid-unnecessary-type-casts
- avoid-unrelated-type-assertions
- avoid-unused-parameters
- avoid-nested-conditional-expressions
- newline-before-return
- no-boolean-literal-compare
- no-empty-block
- prefer-trailing-comma
- prefer-conditional-expressions
- no-equal-then-else
- prefer-moving-to-variable
- avoid-duplicate-exports
- avoid-dynamic
- avoid-late-keyword
- avoid-nested-conditional-expressions
- avoid-unnecessary-type-assertions
- avoid-unnecessary-type-casts
- member-ordering
- no-magic-number
- prefer-trailing-comma
- always-remove-listener
- avoid-border-all
- avoid-expanded-as-spacer
- avoid-wrapping-in-padding
- prefer-const-border-radius
- prefer-correct-edge-insets-constructor
- prefer-single-widget-per-file

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
# - arguments-ordering:
# child-last: true
# - prefer-match-file-name rules-exclude:
# - ... # configures the list of files that should be ignored by rules
# anti-patterns:
# - ... # configures the list of anti-patterns

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
exclude:
- "example/**"
- "build/**"
- "**/*.g.dart"
- "**/*.freezed.dart"
17 changes: 10 additions & 7 deletions lib/src/core/models/completion/completion.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:collection/collection.dart';
import "package:meta/meta.dart";

import 'sub_models/choice.dart';
import 'sub_models/usage.dart';

export 'sub_models/choice.dart';
export 'sub_models/usage.dart';
export 'stream/completion.dart';

@immutable
class OpenAICompletionModel {
/// The ID of the completion.
final String id;
Expand All @@ -22,8 +25,13 @@ class OpenAICompletionModel {
/// The usage of the completion, if any.
final OpenAICompletionModelUsage? usage;

@override
int get hashCode {
return id.hashCode ^ created.hashCode ^ model.hashCode ^ choices.hashCode;
}

/// This class is used to represent an OpenAI completion.
OpenAICompletionModel({
const OpenAICompletionModel({
required this.id,
required this.created,
required this.model,
Expand All @@ -37,7 +45,7 @@ class OpenAICompletionModel {
id: json['id'],
created: DateTime.fromMillisecondsSinceEpoch(json['created'] * 1000),
model: json['model'],
choices: (json['choices'] as List<dynamic>)
choices: (json['choices'] as List)
.map((i) => OpenAICompletionModelChoice.fromJson(i))
.toList(),
usage: OpenAICompletionModelUsage.fromJson(json['usage']),
Expand All @@ -59,9 +67,4 @@ class OpenAICompletionModel {
other.model == model &&
listEquals(other.choices, choices);
}

@override
int get hashCode {
return id.hashCode ^ created.hashCode ^ model.hashCode ^ choices.hashCode;
}
}
27 changes: 26 additions & 1 deletion lib/src/core/models/completion/stream/completion.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';

import 'sub_models/choices.dart';

export 'sub_models/choices.dart';

@immutable
class OpenAIStreamCompletionModel {
/// The ID of the completion.
final String id;
Expand All @@ -15,8 +19,13 @@ class OpenAIStreamCompletionModel {
/// The model used to generate the completion.
final String model;

@override
int get hashCode {
return id.hashCode ^ created.hashCode ^ choices.hashCode ^ model.hashCode;
}

/// This class is used to represent an OpenAI stream completion.
OpenAIStreamCompletionModel({
const OpenAIStreamCompletionModel({
required this.id,
required this.created,
required this.choices,
Expand All @@ -34,4 +43,20 @@ class OpenAIStreamCompletionModel {
model: json['model'],
);
}

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

return other.id == id &&
other.created == created &&
listEquals(other.choices, choices) &&
other.model == model;
}

@override
String toString() {
return 'OpenAIStreamCompletionModel(id: $id, created: $created, choices: $choices, model: $model)';
}
}
33 changes: 30 additions & 3 deletions lib/src/core/models/completion/stream/sub_models/choices.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import "package:meta/meta.dart";

@immutable
class OpenAIStreamCompletionModelChoice {
/// The text generated by the completion.
final String text;
Expand All @@ -9,10 +12,18 @@ class OpenAIStreamCompletionModelChoice {
final int? logprobs;

/// The reason the completion finished.
final dynamic finishReason;
final finishReason;

@override
int get hashCode {
return text.hashCode ^
index.hashCode ^
logprobs.hashCode ^
finishReason.hashCode;
}

/// This class is used to represent a choice generated by an OpenAI stream completion.
OpenAIStreamCompletionModelChoice({
const OpenAIStreamCompletionModelChoice({
required this.text,
required this.index,
required this.logprobs,
Expand All @@ -21,12 +32,28 @@ class OpenAIStreamCompletionModelChoice {

/// This method is used to convert a [Map<String, dynamic>] object to a [OpenAIStreamCompletionModelChoice] object.
factory OpenAIStreamCompletionModelChoice.fromJson(
Map<String, dynamic> json) {
Map<String, dynamic> json,
) {
return OpenAIStreamCompletionModelChoice(
text: json['text'],
index: json['index'],
logprobs: json['logprobs'],
finishReason: json['finishReason'],
);
}

@override
String toString() {
return 'OpenAIStreamCompletionModelChoice(text: $text, index: $index, logprobs: $logprobs, finishReason: $finishReason)';
}

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

return other.text == text &&
other.index == index &&
other.logprobs == logprobs &&
other.finishReason == finishReason;
}
}
21 changes: 12 additions & 9 deletions lib/src/core/models/completion/sub_models/choice.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:meta/meta.dart';

@immutable
class OpenAICompletionModelChoice {
/// The text generated by the completion.
final String text;
Expand All @@ -11,8 +14,16 @@ class OpenAICompletionModelChoice {
/// The reason the completion finished.
final String? finishReason;

@override
int get hashCode {
return text.hashCode ^
index.hashCode ^
logprobs.hashCode ^
finishReason.hashCode;
}

/// This class is used to represent a choice generated by an OpenAI completion.
OpenAICompletionModelChoice({
const OpenAICompletionModelChoice({
required this.text,
required this.index,
required this.logprobs,
Expand All @@ -39,14 +50,6 @@ class OpenAICompletionModelChoice {
other.finishReason == finishReason;
}

@override
int get hashCode {
return text.hashCode ^
index.hashCode ^
logprobs.hashCode ^
finishReason.hashCode;
}

@override
String toString() {
return 'OpenAICompletionModelChoice(text: $text, index: $index, logprobs: $logprobs, finishReason: $finishReason)';
Expand Down
13 changes: 8 additions & 5 deletions lib/src/core/models/completion/sub_models/usage.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:meta/meta.dart';

@immutable
class OpenAICompletionModelUsage {
/// The number of tokens in the prompt.
final int? promptTokens;
Expand All @@ -8,8 +11,12 @@ class OpenAICompletionModelUsage {
/// The total number of tokens in the prompt and completion.
final int? totalTokens;

@override
int get hashCode =>
promptTokens.hashCode ^ completionTokens.hashCode ^ totalTokens.hashCode;

/// This class is used to represent the usage of an OpenAI completion.
OpenAICompletionModelUsage({
const OpenAICompletionModelUsage({
required this.promptTokens,
required this.completionTokens,
required this.totalTokens,
Expand All @@ -33,10 +40,6 @@ class OpenAICompletionModelUsage {
other.totalTokens == totalTokens;
}

@override
int get hashCode =>
promptTokens.hashCode ^ completionTokens.hashCode ^ totalTokens.hashCode;

@override
String toString() =>
'OpenAICompletionModelUsage(promptTokens: $promptTokens, completionTokens: $completionTokens, totalTokens: $totalTokens)';
Expand Down
Loading

0 comments on commit abe8552

Please sign in to comment.