Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/src/providers/implementations/gemini_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GeminiProvider extends LlmProvider with ChangeNotifier {
/// model's generation behavior.
GeminiProvider({
required GenerativeModel model,
this.onFunctionCalls,
Iterable<ChatMessage>? history,
List<SafetySetting>? chatSafetySettings,
GenerationConfig? chatGenerationConfig,
Expand All @@ -38,7 +39,7 @@ class GeminiProvider extends LlmProvider with ChangeNotifier {
_chatGenerationConfig = chatGenerationConfig {
_chat = _startChat(history);
}

final void Function(Iterable<FunctionCall>)? onFunctionCalls;
final GenerativeModel _model;
final List<SafetySetting>? _chatSafetySettings;
final GenerationConfig? _chatGenerationConfig;
Expand All @@ -53,6 +54,7 @@ class GeminiProvider extends LlmProvider with ChangeNotifier {
prompt: prompt,
attachments: attachments,
contentStreamGenerator: (c) => _model.generateContentStream([c]),
onFunctionCalls: onFunctionCalls,
);

@override
Expand All @@ -68,6 +70,7 @@ class GeminiProvider extends LlmProvider with ChangeNotifier {
prompt: prompt,
attachments: attachments,
contentStreamGenerator: _chat!.sendMessageStream,
onFunctionCalls: onFunctionCalls,
);

// don't write this code if you're targeting the web until this is fixed:
Expand All @@ -90,6 +93,7 @@ class GeminiProvider extends LlmProvider with ChangeNotifier {
required Iterable<Attachment> attachments,
required Stream<GenerateContentResponse> Function(Content)
contentStreamGenerator,
required void Function(Iterable<FunctionCall>)? onFunctionCalls,
}) async* {
final content = Content('user', [
TextPart(prompt),
Expand All @@ -104,7 +108,13 @@ class GeminiProvider extends LlmProvider with ChangeNotifier {
// if (text != null) yield text;
// }
yield* response
.map((chunk) => chunk.text)
.map((chunk) {
if (chunk.candidates.any((e) => e.finishReason != null) &&
chunk.functionCalls.isNotEmpty) {
onFunctionCalls?.call(chunk.functionCalls);
}
return chunk.text;
})
.where((text) => text != null)
.cast<String>();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/providers/implementations/vertex_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class VertexProvider extends LlmProvider with ChangeNotifier {
/// model's generation behavior.
VertexProvider({
required GenerativeModel model,
this.onFunctionCalls,
Iterable<ChatMessage>? history,
List<SafetySetting>? chatSafetySettings,
GenerationConfig? chatGenerationConfig,
Expand All @@ -38,7 +39,7 @@ class VertexProvider extends LlmProvider with ChangeNotifier {
_chatGenerationConfig = chatGenerationConfig {
_chat = _startChat(history);
}

final void Function(Iterable<FunctionCall>)? onFunctionCalls;
final GenerativeModel _model;
final List<SafetySetting>? _chatSafetySettings;
final GenerationConfig? _chatGenerationConfig;
Expand Down