Skip to content

Commit

Permalink
fix issues from analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
cynthiajoan committed Sep 12, 2024
1 parent 5ad6063 commit 824583c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,36 @@ final class Candidate {

/// Message for finish reason.
final String? finishMessage;

/// The concatenation of the text parts of [content], if any.
///
/// If this candidate was finished for a reason of [FinishReason.recitation]
/// or [FinishReason.safety], accessing this text will throw a
/// [GenerativeAIException].
///
/// If [content] contains any text parts, this value is the concatenation of
/// the text.
///
/// If [content] does not contain any text parts, this value is `null`.
String? get text {
if (finishReason case FinishReason.recitation || FinishReason.safety) {
final String suffix;
if (finishMessage case final message? when message.isNotEmpty) {
suffix = ': $message';
} else {
suffix = '';
}
throw VertexAIException(
'Candidate was blocked due to $finishReason$suffix');
}
return switch (content.parts) {
// Special case for a single TextPart to avoid iterable chain.
[TextPart(:final text)] => text,
final parts when parts.any((p) => p is TextPart) =>
parts.whereType<TextPart>().map((p) => p.text).join(),
_ => null,
};
}
}

/// Safety rating for a piece of content.
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_vertexai/firebase_vertexai/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ dev_dependencies:
flutter_lints: ^4.0.0
flutter_test:
sdk: flutter
matcher: ^0.12.16
mockito: ^5.0.0
plugin_platform_interface: ^2.1.3
matcher: ^0.12.16
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Matcher matchesPart(Part part) => switch (part) {
isA<FunctionResponse>()
.having((p) => p.name, 'name', name)
.having((p) => p.response, 'args', response),
_ => throw StateError('Unhandled Part type.'),
};

Matcher matchesContent(Content content) => isA<Content>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ void main() {
),
],
CitationMetadata([
CitationSource(574, 705, Uri.https('example.com', ''), ''),
CitationSource(899, 1026, Uri.https('example.com', ''), ''),
CitationSource(574, 705, Uri.https('example.com'), ''),
CitationSource(899, 1026, Uri.https('example.com'), ''),
]),
FinishReason.stop,
null,
Expand Down Expand Up @@ -480,8 +480,8 @@ void main() {
),
],
CitationMetadata([
CitationSource(574, 705, Uri.https('example.com', ''), ''),
CitationSource(899, 1026, Uri.https('example.com', ''), ''),
CitationSource(574, 705, Uri.https('example.com'), ''),
CitationSource(899, 1026, Uri.https('example.com'), ''),
]),
FinishReason.stop,
null,
Expand Down Expand Up @@ -563,37 +563,37 @@ void main() {
);
});

// test('text getter joins content', () async {
// const response = '''
// {
// "candidates": [
// {
// "content": {
// "parts": [
// {
// "text": "Initial text"
// },
// {
// "functionCall": {"name": "someFunction", "args": {}}
// },
// {
// "text": " And more text"
// }
// ],
// "role": "model"
// },
// "finishReason": "STOP",
// "index": 0
// }
// ]
// }
// ''';
// final decoded = jsonDecode(response) as Object;
// final generateContentResponse = parseGenerateContentResponse(decoded);
// expect(generateContentResponse.text, 'Initial text And more text');
// expect(generateContentResponse.candidates.single.text,
// 'Initial text And more text');
// });
test('text getter joins content', () async {
const response = '''
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Initial text"
},
{
"functionCall": {"name": "someFunction", "args": {}}
},
{
"text": " And more text"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
]
}
''';
final decoded = jsonDecode(response) as Object;
final generateContentResponse = parseGenerateContentResponse(decoded);
expect(generateContentResponse.text, 'Initial text And more text');
expect(generateContentResponse.candidates.single.text,
'Initial text And more text');
});
});

group('parses and throws error responses', () {
Expand Down

0 comments on commit 824583c

Please sign in to comment.