fix(vertexai): update history getter to reflect google_generative_ai updates#13040
Merged
Merged
Conversation
…_ai updates - Removed the private _history member from the Flutterfire ChatSession class - Updated the constructor to accept initialHistory parameter - Modified the startChat method to use initialHistory - Updated the history getter to return history from google_generative_ai package This ensures that the history returned by the Flutterfire ChatSession class is always up-to-date and consistent with the state of the chat session.
- Clarified that the history is maintained by the google_generative_ai package - Modified history getter comment to highlight that it returns the most current state of the chat session
… fix/update-chat-history
cynthiajoan
approved these changes
Aug 15, 2024
natebosch
approved these changes
Aug 15, 2024
Collaborator
|
@Toglefritz Thanks for the PR, can you fix the format so we can go ahead and merge it in? https://github.com/firebase/flutterfire/actions/runs/9799303587/job/27950531837?pr=13040 |
Contributor
Author
|
Hello @cynthiajoan. Thank you for taking the time to review my PR. I fixed the formatting using Melos. Additionally, it looks like this PR was originally set to merge into the master branch. I updated the base to the main branch. Please let me know if this is contrary to your contribution process, or just change the base back. Thanks again! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem Description
There are two
ChatSessionclasses: one in the Flutterfire Vertex AI package and one in the google_generative_ai package. Each of these classes maintains a private_historylist to store the content that has been sent to or received from the generative model. Both classes also provide ahistorygetter that returns this list to the clients. However, an issue arose because thesendMessagemethod in the FlutterfireChatSessionclass relies on thesendMessagemethod from the google_generative_ai.ChatSession class to update the chat history. Consequently, only the_historylist in the google_generative_ai.ChatSession class was being updated, while the_historylist in the FlutterfireChatSessionclass remained outdated. This discrepancy caused thehistorygetter in the FlutterfireChatSessionclass to always return an empty list as the chat session history, leading to inconsistencies and potential errors in the application. Exactly the same issue can be observed with thesendMessageStreammethods defined in each class.The video below demonstrates this issue. In the example app, a
Textwidget has been added to the bottom of the screen to display the total length of the chat session history using thehistorygetter from the FlutterfireChatSessionclass. As shown in the video, the count remains at zero, even when messages are present and visible in the chat session. This indicates that thehistorygetter in the FlutterfireChatSessionclass is not being updated correctly with the latest messages, highlighting the need for the proposed fix.Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-07-04.at.12.57.58.mp4
Solution Description
To resolve this issue, the
ChatSessionclass in the Flutterfire plugin was updated so that thehistorygetter returns the history from the google_generative_ai package, which is the one being actively updated when new messages are added to the chat. This change ensures that thehistoryreturned to clients accurately reflects the current state of the chat session.Here are the specific changes made to the
ChatSessionclass:_historymember and its initialization in the constructor were removed, as it was no longer necessary to maintain a separate history list in the FlutterfireChatSessionclass.initialHistoryparameter, which is used to initialize the chat session in thestartChatmethod call.historygetter was updated to fetch the history from the google_ai.ChatSession instance. This ensures that the history returned by the FlutterfireChatSessionclass is always up-to-date and consistent with the state of the chat session.The video below demonstrates the proposed solution for the
ChatSessionclass in the Flutterfire package. In the example app, aTextwidget at the bottom of the screen displays the total length of the chat session history using the updatedhistorygetter from the Flutterfire ChatSession class. As new messages are added to the chat, theTextwidget correctly updates to reflect the current length of the chat history. This confirms that thehistorygetter is now accurately reporting the chat history, validating the effectiveness of the proposed fix.Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-07-04.at.14.12.43.mp4
Alternate Solution Description
An alternate solution to the issue would involve updating the
sendMessageandsendMessageStreammethods in the FlutterfireChatSessionclass to ensure that the_historylist within the Flutterfire class is updated whenever these methods are called. This approach would maintain the original functionality of thehistorygetter in the FlutterfireChatSessionclass and keep the class constructor unchanged. However, it would result in maintaining two versions of the same chat history: one in the FlutterfireChatSessionclass and one in the google_generative_ai.ChatSession class.Related Issues
none found
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]).This will ensure a smooth and quick review process. Updating the
pubspec.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?