Skip to content

Commit

Permalink
Add a functionCalls getter (#118)
Browse files Browse the repository at this point in the history
This is similar to the `text` getter, except that it does not check for
blocked prompt or blocked response outputs. Instead returns an empty
list if there are no functions for any reason. This matches the behavior
of the SDKs implemented in other languages.
  • Loading branch information
natebosch authored Apr 10, 2024
1 parent cf0babc commit af723c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkgs/google_generative_ai/lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ final class GenerateContentResponse {
[Candidate(), ...] => null,
};
}

/// The function call parts of the first candidate in [candidates], if any.
///
/// Returns an empty list if there are no candidates, or if the first
/// candidate has no [FunctionCall] parts. There is no error thrown if the
/// prompt or response were blocked.
Iterable<FunctionCall> get functionCalls =>
candidates.firstOrNull?.content.parts.whereType<FunctionCall>() ??
const [];
}

final class EmbedContentResponse {
Expand Down
3 changes: 1 addition & 2 deletions samples/dart/bin/function_calling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ void main() async {
final content = [Content.text(prompt)];
final response = await model.generateContent(content);

final functionCalls =
response.candidates.first.content.parts.whereType<FunctionCall>();
final functionCalls = response.functionCalls.toList();
if (functionCalls.isEmpty) {
print('No function calls.');
print(response.text);
Expand Down

0 comments on commit af723c8

Please sign in to comment.