From abe8552bed574325ac2fbf456c92b3301289c1ad Mon Sep 17 00:00:00 2001 From: Gwhyyy Date: Thu, 23 Feb 2023 17:46:58 +0100 Subject: [PATCH] [ Edit ] refactored completion models classes and invoked toString and equality for sub-models --- analysis_options.yaml | 81 ++++++++++++----- .../core/models/completion/completion.dart | 17 ++-- .../models/completion/stream/completion.dart | 27 +++++- .../completion/stream/sub_models/choices.dart | 33 ++++++- .../models/completion/sub_models/choice.dart | 21 +++-- .../models/completion/sub_models/usage.dart | 13 ++- pubspec.lock | 91 +++++++++++++++++++ pubspec.yaml | 1 + 8 files changed, 235 insertions(+), 49 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index dee8927a..4eeb65e5 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -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" \ No newline at end of file diff --git a/lib/src/core/models/completion/completion.dart b/lib/src/core/models/completion/completion.dart index d0a03c27..f37f49ed 100644 --- a/lib/src/core/models/completion/completion.dart +++ b/lib/src/core/models/completion/completion.dart @@ -1,4 +1,6 @@ import 'package:collection/collection.dart'; +import "package:meta/meta.dart"; + import 'sub_models/choice.dart'; import 'sub_models/usage.dart'; @@ -6,6 +8,7 @@ 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; @@ -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, @@ -37,7 +45,7 @@ class OpenAICompletionModel { id: json['id'], created: DateTime.fromMillisecondsSinceEpoch(json['created'] * 1000), model: json['model'], - choices: (json['choices'] as List) + choices: (json['choices'] as List) .map((i) => OpenAICompletionModelChoice.fromJson(i)) .toList(), usage: OpenAICompletionModelUsage.fromJson(json['usage']), @@ -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; - } } diff --git a/lib/src/core/models/completion/stream/completion.dart b/lib/src/core/models/completion/stream/completion.dart index 9d5019c8..eaa20485 100644 --- a/lib/src/core/models/completion/stream/completion.dart +++ b/lib/src/core/models/completion/stream/completion.dart @@ -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; @@ -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, @@ -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)'; + } } diff --git a/lib/src/core/models/completion/stream/sub_models/choices.dart b/lib/src/core/models/completion/stream/sub_models/choices.dart index 9bb359ff..5feb2028 100644 --- a/lib/src/core/models/completion/stream/sub_models/choices.dart +++ b/lib/src/core/models/completion/stream/sub_models/choices.dart @@ -1,3 +1,6 @@ +import "package:meta/meta.dart"; + +@immutable class OpenAIStreamCompletionModelChoice { /// The text generated by the completion. final String text; @@ -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, @@ -21,7 +32,8 @@ class OpenAIStreamCompletionModelChoice { /// This method is used to convert a [Map] object to a [OpenAIStreamCompletionModelChoice] object. factory OpenAIStreamCompletionModelChoice.fromJson( - Map json) { + Map json, + ) { return OpenAIStreamCompletionModelChoice( text: json['text'], index: json['index'], @@ -29,4 +41,19 @@ class OpenAIStreamCompletionModelChoice { 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; + } } diff --git a/lib/src/core/models/completion/sub_models/choice.dart b/lib/src/core/models/completion/sub_models/choice.dart index 4bd8d9be..31c7572c 100644 --- a/lib/src/core/models/completion/sub_models/choice.dart +++ b/lib/src/core/models/completion/sub_models/choice.dart @@ -1,3 +1,6 @@ +import 'package:meta/meta.dart'; + +@immutable class OpenAICompletionModelChoice { /// The text generated by the completion. final String text; @@ -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, @@ -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)'; diff --git a/lib/src/core/models/completion/sub_models/usage.dart b/lib/src/core/models/completion/sub_models/usage.dart index afe7a3e6..07be4efd 100644 --- a/lib/src/core/models/completion/sub_models/usage.dart +++ b/lib/src/core/models/completion/sub_models/usage.dart @@ -1,3 +1,6 @@ +import 'package:meta/meta.dart'; + +@immutable class OpenAICompletionModelUsage { /// The number of tokens in the prompt. final int? promptTokens; @@ -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, @@ -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)'; diff --git a/pubspec.lock b/pubspec.lock index c50dd227..b9fe662f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -15,6 +15,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "5.4.0" + analyzer_plugin: + dependency: transitive + description: + name: analyzer_plugin + url: "https://pub.dartlang.org" + source: hosted + version: "0.11.2" + ansicolor: + dependency: transitive + description: + name: ansicolor + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" args: dependency: transitive description: @@ -64,6 +78,34 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.2" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.17.2" + dart_code_metrics: + dependency: "direct dev" + description: + name: dart_code_metrics + url: "https://pub.dartlang.org" + source: hosted + version: "5.6.0" + dart_code_metrics_presets: + dependency: transitive + description: + name: dart_code_metrics_presets + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + dart_style: + dependency: transitive + description: + name: dart_style + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.4" file: dependency: transitive description: @@ -85,6 +127,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.15.1" http: dependency: "direct main" description: @@ -120,6 +169,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.6.5" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "4.8.0" lints: dependency: "direct dev" description: @@ -176,6 +232,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.3" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.0" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" pool: dependency: transitive description: @@ -183,6 +253,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.5.1" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.4" pub_semver: dependency: transitive description: @@ -190,6 +267,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.3" + pub_updater: + dependency: transitive + description: + name: pub_updater + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.4" shelf: dependency: transitive description: @@ -323,6 +407,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "6.2.2" yaml: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index eb6464df..7d58d2d7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,6 +9,7 @@ environment: sdk: ">=2.18.5 <3.0.0" dev_dependencies: + dart_code_metrics: ^5.6.0 lints: ^2.0.0 test: ^1.16.0