Skip to content
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

gemini using real sse format response #3677 #3688 #4906

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

lloydzhou
Copy link
Contributor

@lloydzhou lloydzhou commented Jul 1, 2024

https://github.com/google-gemini/cookbook/blob/main/quickstarts/rest/Streaming_REST.ipynb

image

image

Summary by CodeRabbit

  • New Features

    • Improved chat functionality to support streaming responses, enhancing real-time interaction capabilities.
  • Refactor

    • Updated internal response handling logic for better performance and maintainability.

Copy link

vercel bot commented Jul 1, 2024

@lloydzhou is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

coderabbitai bot commented Jul 1, 2024

Walkthrough

The changes primarily introduce conditional URL modifications and enhanced response handling for streaming data through the refactoring of the chat method in the GeminiProApi class. These updates involve importing certain modules and adjusting how requests and responses are processed, improving the functionality and behavior of the Google API's interaction within the application.

Changes

File Change Summary
app/api/google/[...path]/route.ts Updated the fetchUrl construction to conditionally append &alt=sse based on req?.nextUrl?.searchParams?.get("alt").
app/client/platforms/google.ts Refactored the GeminiProApi class by:
  • Importing necessary modules.
  • Adjusting chat method for streaming responses using fetchEventSource.
  • Updating baseUrl construction.
  • Revising response handling for streaming and non-streaming requests.

Poem

In the code, where changes bloom,
URLs shift, and streams resume.
APIs talk, with data flow,
Enabling chats in a streaming show.
A rabbit hops, with glee and pride,
For code refined, on this joyride.


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@lloydzhou lloydzhou requested a review from Dogtiti July 1, 2024 13:25
@lloydzhou lloydzhou added the planned planned feature, will support in the future label Jul 1, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
app/client/platforms/google.ts (1)

Line range hint 150-262: Streaming response handling looks good. Consider optimizing animateResponseText.

The updates to handle streaming responses using fetchEventSource are necessary and well-implemented. However, the animateResponseText function could be optimized to reduce the number of calls to requestAnimationFrame.

-          const fetchCount = Math.max(1, Math.round(remainText.length / 60));
-          const fetchText = remainText.slice(0, fetchCount);
-          responseText += fetchText;
-          remainText = remainText.slice(fetchCount);
-          options.onUpdate?.(responseText, fetchText);
+          const fetchText = remainText.slice(0, 60);
+          responseText += fetchText;
+          remainText = remainText.slice(60);
+          options.onUpdate?.(responseText, fetchText);
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ce3b6a0 and 69974d5.

Files selected for processing (2)
  • app/api/google/[...path]/route.ts (1 hunks)
  • app/client/platforms/google.ts (6 hunks)
Additional comments not posted (4)
app/api/google/[...path]/route.ts (1)

66-68: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to handle match the new logic.

Verification successful

LGTM! The function usage is correctly implemented.

The fetchUrl construction logic matches the new logic, and the function handles the request and response appropriately. No further issues are found.

  • app/api/google/[...path]/route.ts: Verified the handle function implementation.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `handle` match the new logic.

# Test: Search for the function usage. Expect: Only occurrences of the new logic.
rg --type js --type ts $'handle'

Length of output: 1863



Script:

#!/bin/bash
# Extract the implementation of the `handle` function from `app/api/google/[...path]/route.ts`
ast-grep --lang typescript --pattern $'async function handle($_) {
  $$$
}' app/api/google/[...path]/route.ts

Length of output: 112



Script:

#!/bin/bash
# Search for the `handle` function definition within `app/api/google/[...path]/route.ts`
rg 'async function handle' app/api/google/[...path]/route.ts -A 20

Length of output: 576



Script:

#!/bin/bash
# Extend the search to capture more lines of the `handle` function implementation
rg 'async function handle' app/api/google/[...path]/route.ts -A 50

Length of output: 1260



Script:

#!/bin/bash
# Extend the search to capture the complete `handle` function implementation
rg 'async function handle' app/api/google/[...path]/route.ts -A 100

Length of output: 2518

app/client/platforms/google.ts (3)

6-11: Imports look good.

The newly added imports are necessary for the updated functionality.


129-131: Base URL construction looks good.

The updates to the baseUrl construction handle custom configurations and app-specific URLs appropriately.


Line range hint 263-276: Non-streaming response handling looks good.

The updates to differentiate between streaming and non-streaming responses are necessary and well-implemented.

Copy link
Contributor

github-actions bot commented Jul 1, 2024

Your build has completed!

Preview deployment

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 69974d5 and c4ad66f.

Files selected for processing (1)
  • app/client/platforms/google.ts (6 hunks)
Additional comments not posted (7)
app/client/platforms/google.ts (7)

150-150: Timeout Handling: Ensure proper cleanup

Ensure that the request timeout is properly handled and cleaned up to avoid potential memory leaks or dangling requests.

Consider adding a comment or additional logic to ensure proper cleanup.


6-10: New Imports: Ensure correct usage and compatibility

New imports have been added for Locale, EventStreamContentType, fetchEventSource, and prettyObject. Ensure these imports are correctly used and compatible with the rest of the codebase.

Verification successful

New Imports: Verified for correct usage and compatibility

The newly added imports Locale, EventStreamContentType, fetchEventSource, and prettyObject are correctly used and integrated across the codebase.

  • Locale is used in various components and utility files.
  • EventStreamContentType and fetchEventSource are used in multiple platform files.
  • prettyObject is utilized in different utility and platform files.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the usage and compatibility of newly added imports.

# Test: Search for the usage of `Locale`, `EventStreamContentType`, `fetchEventSource`, and `prettyObject`. Expect: Correct usage in the codebase.
rg --type python $'Locale|EventStreamContentType|fetchEventSource|prettyObject'

Length of output: 111



Script:

#!/bin/bash
# Description: Verify the usage and compatibility of newly added imports.

# Test: Search for the usage of `Locale`, `EventStreamContentType`, `fetchEventSource`, and `prettyObject`. Expect: Correct usage in the codebase.
rg --type ts 'Locale|EventStreamContentType|fetchEventSource|prettyObject'

Length of output: 30208


129-131: Conditional URL Construction: Verify correctness

The construction of baseUrl has been updated for streaming requests. Ensure the URL is correctly constructed based on the environment and configuration.

Verification successful

Conditional URL Construction: Verification of Correctness

The construction of baseUrl in app/client/platforms/google.ts appears to be correctly handled based on the environment and configuration. The URL is conditionally assigned based on whether custom configurations are used, and further modified based on whether the application is running in an app environment.

  • Lines of Interest:
    • let baseUrl = "";
    • if (accessStore.useCustomConfig) { baseUrl = accessStore.googleUrl; }
    • if (!baseUrl) { baseUrl = isApp ? DEFAULT_API_HOST + "/api/proxy/google/" + Google.ChatPath(modelConfig.model) : this.path(Google.ChatPath(modelConfig.model)); }
    • if (isApp) { baseUrl += ?key=${accessStore.googleApiKey}; }
    • const chatPath = baseUrl.replace("generateContent", "streamGenerateContent") + (baseUrl.indexOf("?") > -1 ? "&alt=sse" : "?alt=sse");

The URL construction logic ensures that baseUrl is correctly set based on the environment (isApp) and configuration (useCustomConfig). Additionally, the URL is modified appropriately for streaming requests by replacing generateContent with streamGenerateContent and appending query parameters as needed.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correctness of URL construction for streaming requests.

# Test: Search for the construction of `baseUrl`. Expect: Correct URL construction logic.
rg --type python $'baseUrl'

Length of output: 57



Script:

#!/bin/bash
# Description: Verify the correctness of URL construction for streaming requests.

# Test: Search for the construction of `baseUrl`. Expect: Correct URL construction logic.
rg --type typescript 'baseUrl'

Length of output: 65



Script:

#!/bin/bash
# Description: Verify the correctness of URL construction for streaming requests.

# Test: Search for the construction of `baseUrl`. Expect: Correct URL construction logic.
rg 'baseUrl' --glob '*.ts'

Length of output: 3914



Script:

#!/bin/bash
# Description: Extract and review the relevant lines from app/client/platforms/google.ts to verify URL construction.

# Extract lines around the baseUrl construction in app/client/platforms/google.ts
rg -A 10 -B 10 'baseUrl' app/client/platforms/google.ts

Length of output: 2529


189-261: Streaming Response Handling: Ensure proper handling and error management

The logic for handling streaming responses using fetchEventSource has been added. Ensure that the response handling, error management, and resource cleanup are properly implemented.

Verification successful

Streaming Response Handling: Verified

The implementation of fetchEventSource in the files google.ts, openai.ts, and anthropic.ts correctly handles streaming responses, manages errors, and ensures resource cleanup. The logic is consistent and includes all necessary components.

  • app/client/platforms/google.ts
  • app/client/platforms/openai.ts
  • app/client/platforms/anthropic.ts
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the handling of streaming responses using `fetchEventSource`.

# Test: Search for the usage of `fetchEventSource`. Expect: Proper handling of streaming responses, error management, and resource cleanup.
rg --type python $'fetchEventSource'

Length of output: 66



Script:

#!/bin/bash
# Description: Verify the handling of streaming responses using `fetchEventSource`.

# Test: Search for the usage of `fetchEventSource` in TypeScript files. Expect: Proper handling of streaming responses, error management, and resource cleanup.
rg --type typescript 'fetchEventSource'

Length of output: 74



Script:

#!/bin/bash
# Description: Verify the handling of streaming responses using `fetchEventSource`.

# Test: Search for the usage of `fetchEventSource` in TypeScript files. Expect: Proper handling of streaming responses, error management, and resource cleanup.
rg --type ts 'fetchEventSource'

Length of output: 390



Script:

#!/bin/bash
# Description: Extract the usage context of `fetchEventSource` in relevant TypeScript files.

# Extract and display the context around `fetchEventSource` in the identified files.
rg --type ts -A 20 -B 10 'fetchEventSource' app/client/platforms/anthropic.ts app/client/platforms/openai.ts app/client/platforms/google.ts

Length of output: 11342


185-188: Conditional SSE URL: Ensure correct URL construction

The construction of the SSE URL is conditional based on the presence of query parameters. Ensure the URL is correctly constructed for both cases.

Verification successful

Conditional SSE URL: Ensure correct URL construction

The SSE URL construction logic correctly handles both cases of the presence and absence of query parameters.

  • The URL is correctly constructed by replacing "generateContent" with "streamGenerateContent".
  • It appends &alt=sse if there are existing query parameters, or ?alt=sse if there are none.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correctness of SSE URL construction.

# Test: Search for the construction of the SSE URL. Expect: Correct URL construction logic for both cases.
rg --type python $'streamGenerateContent'

Length of output: 71



Script:

#!/bin/bash
# Description: Verify the correctness of SSE URL construction.

# Test: Search for the construction of the SSE URL. Expect: Correct URL construction logic for both cases.
rg 'streamGenerateContent' --type typescript

Length of output: 79



Script:

#!/bin/bash
# Description: Verify the correctness of SSE URL construction.

# Test: Search for the construction of the SSE URL. Expect: Correct URL construction logic for both cases.
rg 'streamGenerateContent' --type ts

Length of output: 139



Script:

#!/bin/bash
# Description: Retrieve the relevant lines of code around the SSE URL construction.

# Test: Display the relevant lines of code around the SSE URL construction.
rg 'baseUrl.replace("generateContent", "streamGenerateContent")' --context 5 --type ts

Length of output: 88



Script:

#!/bin/bash
# Description: Retrieve the relevant lines of code around the SSE URL construction.

# Test: Display the relevant lines of code around the SSE URL construction.
rg 'baseUrl.replace("generateContent", "streamGenerateContent")' app/client/platforms/google.ts -A 5 -B 5

Length of output: 107



Script:

#!/bin/bash
# Description: Retrieve the relevant lines of code around the SSE URL construction.

# Test: Display the relevant lines of code around the SSE URL construction.
sed -n '180,190p' app/client/platforms/google.ts

Length of output: 477


275-275: Extracted Message: Verify correctness

The line const message = apiClient.extractMessage(resJson); has been updated. Ensure that the extracted message is correctly handled and used.

Verification successful

Verified: Extracted Message Handling

The extracted message from apiClient.extractMessage(resJson) is correctly handled and used in the code:

  • The extracted message is appended to remainText.

  • The extracted message is passed to the options.onFinish callback function.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correctness of the extracted message handling.

# Test: Search for the usage of `apiClient.extractMessage`. Expect: Correct handling and usage of the extracted message.
rg --type python $'extractMessage'

Length of output: 64



Script:

#!/bin/bash
# Description: Verify the correctness of the extracted message handling.

# Test: Search for the usage of `apiClient.extractMessage`. Expect: Correct handling and usage of the extracted message.
rg --type ts 'extractMessage'

Length of output: 566



Script:

#!/bin/bash
# Extract surrounding lines of the occurrences in google.ts to analyze the context and usage of the extracted message.
rg --type ts -A 5 -B 5 'extractMessage' app/client/platforms/google.ts

Length of output: 1025


183-184: Abort Handling: Ensure proper response completion

Ensure that the controller.signal.onabort properly handles the response completion and cleans up resources.

app/client/platforms/google.ts Show resolved Hide resolved
@lloydzhou lloydzhou merged commit 78e2b41 into ChatGPTNextWeb:main Jul 3, 2024
1 of 2 checks passed
@lloydzhou lloydzhou deleted the feature-gemini-streaming branch August 1, 2024 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
planned planned feature, will support in the future
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants