Skip to content

Commit

Permalink
Enable additional lints (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Jan 27, 2024
1 parent cfd4e87 commit 413000f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
29 changes: 29 additions & 0 deletions pkgs/google_generative_ai/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions pkgs/google_generative_ai/lib/google_generative_ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
/// ```
library;

import 'src/model.dart';

export 'src/api.dart'
show
BlockReason,
Expand Down
21 changes: 12 additions & 9 deletions pkgs/google_generative_ai/lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import 'content.dart';
import 'error.dart';
import 'model.dart';

final class CountTokensResponse {
final int totalTokens;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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',
};
}
}
Expand Down Expand Up @@ -366,7 +367,9 @@ SafetyRating _parseSafetyRating(Object? jsonObject) {

ContentEmbedding _parseContentEmbedding(Object? jsonObject) {
return switch (jsonObject) {
{'values': List values} => ContentEmbedding(<double>[...values]),
{'values': List values} => ContentEmbedding(<double>[
...values.cast<double>(),
]),
_ => throw FormatException('Unhandled ContentEmbedding format $jsonObject'),
};
}
Expand Down
4 changes: 3 additions & 1 deletion pkgs/google_generative_ai/lib/src/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
7 changes: 4 additions & 3 deletions pkgs/google_generative_ai/lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -55,12 +55,13 @@ final class GenerativeModel {
_generationConfig = generationConfig,
_client = HttpApiClient(model: model, apiKey: apiKey);

Future<Map> _makeRequest(Task task, Map<String, Object?> parameters) async {
Future<Object> _makeRequest(
Task task, Map<String, Object?> 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<Map<String, Object?>> _streamRequest(Task task, Object parameters) {
Expand Down

0 comments on commit 413000f

Please sign in to comment.