-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[Vertex AI] Refactor generateContentStream to fix Swift 6 warning #14504
Conversation
The function in was refactored to use instead of returning an iterator. The file was also modified to directly return the and use it with a loop. This change allows for more idiomatic use of the stream, improving the code's readability and maintainability.
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.
Hello @paulb777, 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!
Summary of Changes
This pull request addresses a Swift 6 warning and aims to improve the readability and maintainability of the code. The core change involves refactoring the generateContentStream
function in GenerativeModel.swift
to use an AsyncThrowingStream
with a for try await
loop instead of returning an iterator. This allows for more idiomatic stream handling. Additionally, a similar change was made to GenerativeAIService.swift
to handle the stream using stream.lines
and a for try await
loop.
Highlights
- Refactor: The
generateContentStream
function inGenerativeModel.swift
was refactored to useAsyncThrowingStream
and afor try await
loop for better stream handling. - Swift 6 Warning: This pull request fixes a Swift 6 warning, as discussed in issue [VertexAI] Add Swift 6 testing to CI #14503.
- Idiomatic Stream Usage: The changes promote more idiomatic use of streams, enhancing code readability and maintainability.
Changelog
Click here to see the changelog
- FirebaseVertexAI/Sources/GenerativeAIService.swift
- Refactored the stream processing logic to use
stream.lines
and afor try await
loop. - The code now iterates through the lines of the stream, parses JSON data, and yields content to the continuation.
- Error handling is included within the loop to finish the continuation with an error if parsing fails.
- The
continuation.finish(throwing: nil)
is now called inside the loop and after the loop.
- Refactored the stream processing logic to use
- FirebaseVertexAI/Sources/GenerativeModel.swift
- Refactored
generateContentStream
to return anAsyncThrowingStream
directly. - The stream is now processed within a
Task
using afor try await
loop. - Error handling and prompt feedback checks are performed within the loop.
- The
continuation.yield(response)
is called inside the loop to provide valid content. - The
continuation.finish(throwing: nil)
is now called after the loop.
- Refactored
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
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 .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and 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 to provide feedback.
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.
Did you know?
The concept of streams in programming dates back to the early days of Unix, where they were used to connect different programs together.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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
The pull request refactors the generateContentStream
function to address a Swift 6 warning and improves code readability. The changes are generally well-structured and address the issue described in the pull request description. The use of AsyncThrowingStream
and await
makes the code more modern and easier to follow.
Summary of Findings
- Error Handling in Streaming: The error handling within the
stream.lines
loop inGenerativeAIService.swift
could be improved to ensure that all potential errors are properly handled and propagated. - Redundant
finish(throwing: nil)
calls: Thefinish(throwing: nil)
calls inGenerativeAIService.swift
andGenerativeModel.swift
appear to be redundant and could be simplified. - Task Management in
GenerativeModel.swift
: Consider adding error handling within theTask
block inGenerativeModel.swift
to ensure that any unexpected errors are caught and handled appropriately.
Assessment
The pull request refactors the generateContentStream
function to address a Swift 6 warning and improves code readability. The changes appear to be well-structured and address the issue described in the pull request description. However, there are a few areas where the code could be improved for clarity and error handling. I recommend addressing these comments before merging, and users should have others review and approve this code before merging.
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.
LGTM to me now. (I just pushed a minor change to rename responseIterator
to responseStream
for clarity and updated the PR name/description)
One thing I'm not certain about is whether this affects the ability for the Task
to get cancelled if the caller stops reading with for try await
but I think getting the Swift 6 build in good shape is more important.
Not relevant after Nick's changes
Refactored
GenerativeModel.generateContentStream(...)
to useinit(_:bufferingPolicy:_:)
(AsyncStream.Continuation
approach) instead ofinit(unfolding:)
to resolve the Swift 6 warning "Capture of responseIterator with non-Sendable type AsyncThrowingStream<GenerateContentResponse, Error>
" discussed in #14503.#no-changelog