-
Notifications
You must be signed in to change notification settings - Fork 0
Stream Gemini Summary Response #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @trett, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a fundamental shift in how AI summaries are delivered to the user, moving from a batch-processing model to a real-time streaming approach. By leveraging Gemini's streaming capabilities and Server-Sent Events, the system can now progressively render summary content, metadata, and fun facts, providing a more dynamic and responsive user interface. This enhancement aims to make the summary generation process feel faster and more interactive for the end-user. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request is a significant improvement, refactoring the summary generation to use a streaming approach with Gemini's API and Server-Sent Events. The changes on the backend, API, and frontend are well-executed and will greatly enhance the user experience by providing incremental updates. I've identified a couple of areas for improvement in the backend service to increase robustness, particularly around error handling and response parsing in the new streaming logic.
| geminiResp.candidates.headOption | ||
| .flatMap(_.content.parts.flatMap(_.headOption)) | ||
| .map(_.text) | ||
| .map { text => | ||
| if text.startsWith("```html") then | ||
| text.stripPrefix("```html").stripSuffix("```").trim | ||
| else text.trim | ||
| } match | ||
| case Some(html) if html.nonEmpty => SummarySuccess(html) | ||
| case _ => | ||
| SummaryError("Could not extract summary from response.") | ||
| text.stripPrefix("```html").stripSuffix("```") | ||
| else text | ||
| } | ||
| .getOrElse("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic for stripping html` and from the response is not robust for a streaming context. It incorrectly assumes that the start and end markers will be contained within the same text chunk from the stream. This is unlikely to be true for a streaming response and can lead to markdown artifacts (like a dangling ````) being sent to the client.
A simpler and more robust approach that works per-chunk is to replace these markers wherever they appear.
geminiResp.candidates.headOption
.flatMap(_.content.parts.flatMap(_.headOption))
.map(_.text.replace("```html", "").replace("```", ""))
.getOrElse("")| .drain ++ Stream.emit( | ||
| SummaryEvent.Error(s"API error: ${response.status.reason}") | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this error path for a non-successful API response, a SummaryEvent.Error is emitted, but not a SummaryEvent.Done. While the caller streamSummary currently appends a Done event, this makes summarizeStream less self-contained and reliant on its call site for correct termination signaling. To improve robustness and encapsulation, summarizeStream should be responsible for emitting a Done event after an Error in all its error paths.
.drain ++ Stream.emits(Seq(SummaryEvent.Error(s"API error: ${response.status.reason}"), SummaryEvent.Done))| Stream | ||
| .eval(logger.error(error)(s"Error summarizing text: ${error.getMessage}")) | ||
| .drain ++ | ||
| Stream.emit(SummaryEvent.Error(errorMessage)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the error handling for non-2xx responses, this handleErrorWith block should also emit a SummaryEvent.Done after the SummaryEvent.Error. This makes the function more self-contained and ensures that clients always receive a termination event, even when errors occur within this stream.
Stream.emits(Seq(SummaryEvent.Error(errorMessage), SummaryEvent.Done))
This PR introduces streaming summary generation using Gemini's API.
Key Changes:
This change significantly improves the user experience by displaying the summary as it is generated, reducing the perceived waiting time.