[ Edit ] refactored completion models classes and invoked toString and equality for sub-models

This commit is contained in:
Gwhyyy
2023-02-23 17:46:58 +01:00
parent 07fc2c5841
commit abe8552bed
8 changed files with 235 additions and 49 deletions

View File

@ -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"

View File

@ -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<dynamic>)
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;
}
}

View File

@ -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)';
}
}

View File

@ -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<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'],
@ -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;
}
}

View File

@ -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)';

View File

@ -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)';

View File

@ -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:

View File

@ -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