diff --git a/pkgs/google_generative_ai/analysis_options.yaml b/pkgs/google_generative_ai/analysis_options.yaml index 572dd23..a4fad6c 100644 --- a/pkgs/google_generative_ai/analysis_options.yaml +++ b/pkgs/google_generative_ai/analysis_options.yaml @@ -1 +1,30 @@ include: package:lints/recommended.yaml + +analyzer: + language: + strict-casts: true + strict-inference: true + +linter: + rules: + # consistency + - directives_ordering + - omit_local_variable_types + - prefer_relative_imports + - prefer_single_quotes + - sort_pub_dependencies + - unnecessary_library_directive + + # correctness + - always_declare_return_types + - avoid_catching_errors + - avoid_dynamic_calls + - only_throw_errors + - test_types_in_equals + - throw_in_finally + - type_annotate_public_apis + - unawaited_futures + + # documentation related + - comment_references + - library_private_types_in_public_api diff --git a/pkgs/google_generative_ai/lib/google_generative_ai.dart b/pkgs/google_generative_ai/lib/google_generative_ai.dart index 34ae671..8bb0eed 100644 --- a/pkgs/google_generative_ai/lib/google_generative_ai.dart +++ b/pkgs/google_generative_ai/lib/google_generative_ai.dart @@ -34,6 +34,8 @@ /// ``` library; +import 'src/model.dart'; + export 'src/api.dart' show BlockReason, diff --git a/pkgs/google_generative_ai/lib/src/api.dart b/pkgs/google_generative_ai/lib/src/api.dart index 01ec27f..dcfa2dc 100644 --- a/pkgs/google_generative_ai/lib/src/api.dart +++ b/pkgs/google_generative_ai/lib/src/api.dart @@ -14,6 +14,7 @@ import 'content.dart'; import 'error.dart'; +import 'model.dart'; final class CountTokensResponse { final int totalTokens; @@ -82,8 +83,8 @@ final class ContentEmbedding { ContentEmbedding(this.values); } -/// A set of the feedback metadata the prompt specified in -/// [GenerateContentRequest]. +/// A set of the feedback metadata the prompt specified in the [GenerativeModel] +/// request. final class PromptFeedback { final BlockReason? blockReason; final String? blockReasonMessage; @@ -263,12 +264,12 @@ enum TaskType { Object toJson() { return switch (this) { - unspecified => "TASK_TYPE_UNSPECIFIED", - retrievalQuery => "RETRIEVAL_QUERY", - retrievalDocument => "RETRIEVAL_DOCUMENT", - semanticSimilarity => "SEMANTIC_SIMILARITY", - classification => "CLASSIFICATION", - clustering => "CLUSTERING", + unspecified => 'TASK_TYPE_UNSPECIFIED', + retrievalQuery => 'RETRIEVAL_QUERY', + retrievalDocument => 'RETRIEVAL_DOCUMENT', + semanticSimilarity => 'SEMANTIC_SIMILARITY', + classification => 'CLASSIFICATION', + clustering => 'CLUSTERING', }; } } @@ -366,7 +367,9 @@ SafetyRating _parseSafetyRating(Object? jsonObject) { ContentEmbedding _parseContentEmbedding(Object? jsonObject) { return switch (jsonObject) { - {'values': List values} => ContentEmbedding([...values]), + {'values': List values} => ContentEmbedding([ + ...values.cast(), + ]), _ => throw FormatException('Unhandled ContentEmbedding format $jsonObject'), }; } diff --git a/pkgs/google_generative_ai/lib/src/error.dart b/pkgs/google_generative_ai/lib/src/error.dart index 34ba79c..f148b36 100644 --- a/pkgs/google_generative_ai/lib/src/error.dart +++ b/pkgs/google_generative_ai/lib/src/error.dart @@ -12,9 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -final class GenerativeAIException { +final class GenerativeAIException implements Exception { final String message; + GenerativeAIException(this.message); + @override String toString() => 'GenerateiveAIException: $message'; } diff --git a/pkgs/google_generative_ai/lib/src/model.dart b/pkgs/google_generative_ai/lib/src/model.dart index 6f47621..cbdcc5c 100644 --- a/pkgs/google_generative_ai/lib/src/model.dart +++ b/pkgs/google_generative_ai/lib/src/model.dart @@ -20,7 +20,7 @@ import 'client.dart'; import 'content.dart'; final _baseUrl = Uri.https('generativelanguage.googleapis.com'); -const _apiVersion = "v1"; +const _apiVersion = 'v1'; enum Task { generateContent('generateContent'), @@ -55,12 +55,13 @@ final class GenerativeModel { _generationConfig = generationConfig, _client = HttpApiClient(model: model, apiKey: apiKey); - Future _makeRequest(Task task, Map parameters) async { + Future _makeRequest( + Task task, Map parameters) async { final uri = _baseUrl.resolveUri( Uri(pathSegments: [_apiVersion, 'models', '$_model:${task._name}'])); final body = utf8.encode(jsonEncode(parameters)); final jsonString = await _client.makeRequest(uri, body); - return jsonDecode(jsonString); + return jsonDecode(jsonString) as Object; } Stream> _streamRequest(Task task, Object parameters) {