From a594c67028eb463c557185694d43396fed970256 Mon Sep 17 00:00:00 2001 From: Fernando Luca de Tena Date: Fri, 26 Jul 2024 17:44:50 +0200 Subject: [PATCH 1/2] fix(generative-ai-dart) & fix(firebase_vertexai): empty content with null role added to the history of the ChatSession when content was blocked due to Safety or other reasons. closes issue #197 --- pkgs/google_generative_ai/lib/src/chat.dart | 23 ++++++++------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/pkgs/google_generative_ai/lib/src/chat.dart b/pkgs/google_generative_ai/lib/src/chat.dart index 8953fa2..8f78944 100644 --- a/pkgs/google_generative_ai/lib/src/chat.dart +++ b/pkgs/google_generative_ai/lib/src/chat.dart @@ -27,11 +27,9 @@ import 'utils/mutex.dart'; /// response. final class ChatSession { final Future Function(Iterable content, - {List? safetySettings, - GenerationConfig? generationConfig}) _generateContent; + {List? safetySettings, GenerationConfig? generationConfig}) _generateContent; final Stream Function(Iterable content, - {List? safetySettings, - GenerationConfig? generationConfig}) _generateContentStream; + {List? safetySettings, GenerationConfig? generationConfig}) _generateContentStream; final _mutex = Mutex(); @@ -39,8 +37,8 @@ final class ChatSession { final List? _safetySettings; final GenerationConfig? _generationConfig; - ChatSession._(this._generateContent, this._generateContentStream, - this._history, this._safetySettings, this._generationConfig); + ChatSession._( + this._generateContent, this._generateContentStream, this._history, this._safetySettings, this._generationConfig); /// The content that has been successfully sent to, or received from, the /// generative model. @@ -68,7 +66,8 @@ final class ChatSession { try { final response = await _generateContent(_history.followedBy([message]), safetySettings: _safetySettings, generationConfig: _generationConfig); - if (response.candidates case [final candidate, ...]) { + + if (response.candidates case [final candidate, ...] when candidate.content.role != null) { _history.add(message); // TODO: Append role? _history.add(candidate.content); @@ -101,8 +100,7 @@ final class ChatSession { _mutex.acquire().then((lock) async { try { final responses = _generateContentStream(_history.followedBy([message]), - safetySettings: _safetySettings, - generationConfig: _generationConfig); + safetySettings: _safetySettings, generationConfig: _generationConfig); final content = []; await for (final response in responses) { if (response.candidates case [final candidate, ...]) { @@ -176,9 +174,6 @@ extension StartChatExtension on GenerativeModel { /// print(response.text); /// ``` ChatSession startChat( - {List? history, - List? safetySettings, - GenerationConfig? generationConfig}) => - ChatSession._(generateContent, generateContentStream, history ?? [], - safetySettings, generationConfig); + {List? history, List? safetySettings, GenerationConfig? generationConfig}) => + ChatSession._(generateContent, generateContentStream, history ?? [], safetySettings, generationConfig); } From 8266b6e76d202f9bc80220c5b5de7ba9a15da037 Mon Sep 17 00:00:00 2001 From: Fernando Luca de Tena Date: Fri, 26 Jul 2024 18:07:13 +0200 Subject: [PATCH 2/2] reversed wrong formatting --- pkgs/google_generative_ai/lib/src/chat.dart | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pkgs/google_generative_ai/lib/src/chat.dart b/pkgs/google_generative_ai/lib/src/chat.dart index 8f78944..8a539c2 100644 --- a/pkgs/google_generative_ai/lib/src/chat.dart +++ b/pkgs/google_generative_ai/lib/src/chat.dart @@ -27,9 +27,11 @@ import 'utils/mutex.dart'; /// response. final class ChatSession { final Future Function(Iterable content, - {List? safetySettings, GenerationConfig? generationConfig}) _generateContent; + {List? safetySettings, + GenerationConfig? generationConfig}) _generateContent; final Stream Function(Iterable content, - {List? safetySettings, GenerationConfig? generationConfig}) _generateContentStream; + {List? safetySettings, + GenerationConfig? generationConfig}) _generateContentStream; final _mutex = Mutex(); @@ -37,8 +39,8 @@ final class ChatSession { final List? _safetySettings; final GenerationConfig? _generationConfig; - ChatSession._( - this._generateContent, this._generateContentStream, this._history, this._safetySettings, this._generationConfig); + ChatSession._(this._generateContent, this._generateContentStream, + this._history, this._safetySettings, this._generationConfig); /// The content that has been successfully sent to, or received from, the /// generative model. @@ -100,7 +102,8 @@ final class ChatSession { _mutex.acquire().then((lock) async { try { final responses = _generateContentStream(_history.followedBy([message]), - safetySettings: _safetySettings, generationConfig: _generationConfig); + safetySettings: _safetySettings, + generationConfig: _generationConfig); final content = []; await for (final response in responses) { if (response.candidates case [final candidate, ...]) { @@ -174,6 +177,9 @@ extension StartChatExtension on GenerativeModel { /// print(response.text); /// ``` ChatSession startChat( - {List? history, List? safetySettings, GenerationConfig? generationConfig}) => - ChatSession._(generateContent, generateContentStream, history ?? [], safetySettings, generationConfig); + {List? history, + List? safetySettings, + GenerationConfig? generationConfig}) => + ChatSession._(generateContent, generateContentStream, history ?? [], + safetySettings, generationConfig); }