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

fix: allow isVisionModel function read runtime env var VISION_MODELS #5983

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JiangYingjin
Copy link

@JiangYingjin JiangYingjin commented Dec 25, 2024

πŸ’» ε˜ζ›΄η±»εž‹ | Change Type

  • feat
  • fix
  • refactor
  • perf
  • style
  • test
  • docs
  • ci
  • chore
  • build

πŸ”€ ε˜ζ›΄θ―΄ζ˜Ž | Description of Change

Problem: Before the fix, the isVisionModel function read VISION_MODELS from the getClientConfig function. However, getClientConfig was unable to read VISION_MODELS from the program's runtime environment variables. This issue caused visual models outside the predefined and build-time environment variables to be evaluated as false by the isVisionModel function, resulting in them losing their visual capabilities. Specifically, the VISION_MODELS environment variable passed by users in Docker was not taking effect.

Fix Logic: The VISION_MODELS variable was added to ProcessEnv in app/config/server.ts. This allows the client to read it via /api/config and store it in accessStore along with other variables like customModels. Since the isVisionModel function in app/utils.ts cannot directly access accessStore, a new parameter, customVisionModels, was added to the isVisionModel function. When calling isVisionModel, accessStore.visionModels is passed to the isVisionModel function via the new customVisionModels parameter. This enables the isVisionModel function to read the VISION_MODELS runtime environment variable.

Fix Result: After the fix, the isVisionModel function can read VISION_MODELS not only from getClientConfig but also from the program's runtime environment variables, resolving the problem described above.

πŸ“ θ‘₯充俑息 | Additional Information

The test unit test/vision-model-checker.test.ts was adjusted. Custom visual models were passed in through the new customVisionModels parameter, and the tests passed.

Summary by CodeRabbit

  • New Features

    • Introduced a new property for vision models in the configuration options.
    • Enhanced logic to determine vision models across various API classes.
    • Added support for custom vision models in the chat component and related functionalities.
  • Bug Fixes

    • Improved handling of image uploads and pastes based on model type.
  • Documentation

    • Updated comments and variable names for clarity.
  • Tests

    • Enhanced test cases to include custom vision models in the identification logic.

Copy link

vercel bot commented Dec 25, 2024

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

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Dec 25, 2024

Walkthrough

This pull request introduces a comprehensive enhancement to vision model handling across multiple components of the application. The changes primarily focus on expanding the configuration options for vision models by adding a new visionModels property to various configuration objects and updating the isVisionModel function to accept custom vision models. The modifications span server-side configurations, client-side platform APIs, and utility functions, providing a more flexible approach to identifying and managing vision models.

Changes

File Change Summary
app/api/config/route.ts Added visionModels to DANGER_CONFIG object
app/client/platforms/* (Anthropic, Google, OpenAI, Tencent) Updated isVisionModel function calls to include accessStore.visionModels
app/components/chat.tsx Added customVisionModels variable and updated related functions
app/config/server.ts Introduced new VISION_MODELS environment variable
app/store/access.ts Added visionModels to DEFAULT_ACCESS_STATE
app/utils.ts Updated isVisionModel function signature and logic
test/vision-model-checker.test.ts Added customVisionModels for testing

Sequence Diagram

sequenceDiagram
    participant Client as Client Application
    participant AccessStore as Access Store
    participant ConfigService as Config Service
    participant ModelUtils as Model Utilities

    Client->>AccessStore: Retrieve vision models
    AccessStore-->>Client: Return vision models
    Client->>ModelUtils: Check if model is vision model
    ModelUtils->>ModelUtils: Combine default and custom vision models
    ModelUtils-->>Client: Return vision model status
Loading

Possibly related PRs

Suggested Labels

help wanted, planned

Suggested Reviewers

  • lloydzhou

Poem

🐰 Hop, hop, through code so bright,
Vision models now take flight!
Configs dance, models shine clear,
Flexibility brings us cheer! 🌈
A rabbit's code, both smart and light! πŸš€

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

Copy link
Contributor

@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

🧹 Nitpick comments (2)
app/client/platforms/openai.ts (1)

197-198: Minor placement nitpick
Declaring accessStore a few lines above its usage might reduce clarity slightly. Consider moving these lines closer to where isVisionModel is called for enhanced readability.

app/components/chat.tsx (1)

1461-1462: Avoid duplicate store lookups
You're declaring another customVisionModels = useAccessStore().visionModels; here, similar to earlier references. Consider consolidating the logic to a single place or passing the variable as a prop to reduce potential confusion and keep the code cleaner.

πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 0c3d446 and fb5e9e5.

πŸ“’ Files selected for processing (10)
  • app/api/config/route.ts (1 hunks)
  • app/client/platforms/anthropic.ts (1 hunks)
  • app/client/platforms/google.ts (1 hunks)
  • app/client/platforms/openai.ts (2 hunks)
  • app/client/platforms/tencent.ts (1 hunks)
  • app/components/chat.tsx (5 hunks)
  • app/config/server.ts (3 hunks)
  • app/store/access.ts (1 hunks)
  • app/utils.ts (2 hunks)
  • test/vision-model-checker.test.ts (3 hunks)
🧰 Additional context used
πŸͺ› Biome (1.9.4)
test/vision-model-checker.test.ts

[error] 66-66: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

πŸ”‡ Additional comments (25)
app/api/config/route.ts (1)

16-16: New property added for vision models.

The addition of visionModels: serverConfig.visionModels ensures that the vision model configuration is exposed through the DANGER_CONFIG. This is consistent with the newly introduced environment variable in server.ts. No issues detected.

test/vision-model-checker.test.ts (8)

5-5: Good approach to centralize custom vision models in test.

Defining customVisionModels in one place improves clarity and maintainability. Make sure to keep it in sync with any new additions.


31-31: Approved usage of the additional parameter.

Using customVisionModels as the second argument to isVisionModel properly tests custom models. This test is clear and robust.


36-36: Clear negative test case.

Verifying a non-vision model ensures the function correctly excludes them. This is good coverage.


48-48: Non-vision models tested thoroughly.

Similar to line 36, this test ensures correct exclusion handling for each model in the array.


54-54: Usage of runtime environment for vision models.

It's good that you're relying on process.env.VISION_MODELS dynamically, ensuring coverage for real-world scenarios.


55-59: Comprehensive checks for both environment-defined and custom models.

These lines verify that models from both sources are recognized or excluded correctly. Excellent test coverage.


64-64: Correct handling when VISION_MODELS is empty.

This covers a realistic edge case, ensuring the function safely defaults to no environment-defined vision models or relies on custom ones.


67-68: Verifying behavior after removing VISION_MODELS.

These checks confirm that the function behaves correctly when VISION_MODELS is no longer set. Good addition for realism.

app/store/access.ts (1)

126-126: New visionModels property introduced.

Adding visionModels to the default access state keeps the store consistent with the environment-driven configuration. No issues found.

app/config/server.ts (4)

24-24: VISION_MODELS environment variable declared.

This allows the runtime to define or override vision models dynamically, ensuring better configurability of vision features.


127-127: Safe fallback to empty string.

Using ?? "" as a fallback ensures that visionModels remains a string type. This straightforward approach prevents runtime errors if the env variable is undefined.


134-136: Applying logic within the GPT-4 disable block.

Filtering out 4-series models here is consistent. Ensure any future updates to these model prefixes also account for visionModels if needed.


254-254: Exposing visionModels in the config object.

This ensures that downstream consumers (e.g., DANGER_CONFIG) can access the vision model information. Good integration.

app/client/platforms/tencent.ts (1)

97-101: Looks good: usage of the new parameter
The newly introduced retrieval of accessStore.visionModels and passing it to isVisionModel correctly ensures runtime environment compatibility for vision models. No issues found with concurrency, error handling, or security.

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

86-86: Verify accessStore scope
Ensure that accessStore is declared in scope prior to its usage here. As long as your existing code properly defines it, this logic correctly utilizes isVisionModel with the new runtime vision models.

app/client/platforms/anthropic.ts (1)

89-93: Runtime environment reading is consistent
Accessing accessStore.visionModels to determine visionModel aligns with the new parameter approach. The code readability is good, and there are no evident logical or syntax issues.

app/client/platforms/openai.ts (1)

216-219: Correct integration of vision models
Passing accessStore.visionModels to isVisionModel ensures that externally set runtime environment variables are respected. This is a robust approach for supporting custom or user-provided vision models.

app/components/chat.tsx (5)

493-493: Variable type clarification
The customVisionModels variable references useAccessStore().visionModels. Ensure that this store value is consistently typed (e.g., string[] or string) throughout the codebase to avoid type mismatches when passing to other functions.


533-533: Check usage of isVisionModel arguments
Double-check that customVisionModels is a suitable type (string or string array) that matches the updated signature of isVisionModel. If visionModels is actually an array, consider converting it into a string or updating isVisionModel to accept an array parameter.


1466-1466: Check null/undefined safeguards
Before calling isVisionModel with customVisionModels, confirm that customVisionModels is defined to avoid any unexpected errors when joining strings inside isVisionModel.


1503-1503: Dependency array update
Adding customVisionModels to the dependency array ensures the callback re-runs when visionModels changes, which looks correct.


1551-1551: Minor comment addition
This line merely adds or updates a comment for shortcut keys without functional changes.

app/utils.ts (2)

10-10: Import usage confirmed
Importing getModelProvider here makes sense for checking model types. No issues found.


257-265: Validate string-manipulation logic for isVisionModel
The function signature now accepts customVisionModels as a string, then merges it with clientConfig?.visionModels using join, split, etc. This approach may lead to unexpected behavior if either variable is an array or if clientConfig?.visionModels is undefined. Consider:

  1. Double-check that both values are indeed strings.
  2. Implement extra checks to handle edge cases (e.g. empty strings, null, arrays).
  3. Potentially change the function parameter to [string] | string for better clarity.

});

test("should handle empty or missing VISION_MODELS", () => {
process.env.VISION_MODELS = "";
expect(isVisionModel("unrelated-model")).toBe(false);
expect(isVisionModel("unrelated-model", customVisionModels)).toBe(false);

delete process.env.VISION_MODELS;
Copy link
Contributor

Choose a reason for hiding this comment

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

πŸ› οΈ Refactor suggestion

Avoid using the delete operator.

Deleting environment variables can have performance overhead and may lead to unpredictable effects. Consider assigning process.env.VISION_MODELS = undefined instead of using delete.

-    delete process.env.VISION_MODELS;
+    process.env.VISION_MODELS = undefined;
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
delete process.env.VISION_MODELS;
process.env.VISION_MODELS = undefined;
🧰 Tools
πŸͺ› Biome (1.9.4)

[error] 66-66: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant