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

feat: runtime configuration of vision-capable models #5919

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

Conversation

Yiming3
Copy link

@Yiming3 Yiming3 commented Dec 10, 2024

💻 变更类型 | Change Type

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

🔀 变更说明 | Description of Change

This PR provides runtime configuration of vision-capable models suggested from #5843

  1. Replacing hardcoded keyword checks with regex patterns for better flexibility
  2. Adding support for environment variable NEXT_PUBLIC_VISION_MODELS to allow runtime configuration of vision-capable models
  3. Moving model patterns to constants for better maintainability
Key Changes:
  • Added VISION_MODEL_REGEXES and EXCLUDE_VISION_MODEL_REGEXES in constant.ts
  • Updated isVisionModel() to use regex patterns instead of string includes
  • Added support for NEXT_PUBLIC_VISION_MODELS environment variable

📝 补充信息 | Additional Information

  • The regex patterns provide more precise matching (e.g., exact match for "dall-e-3")
  • Maintains backward compatibility with existing vision model detection
    1

Summary by CodeRabbit

  • New Features

    • Introduced new constants for identifying and excluding specific vision models.
    • Enhanced model identification with a dynamic approach using environment variables.
  • Bug Fixes

    • Improved the logic for determining vision models, making it more robust and flexible.

…ies to models through env var NEXT_PUBLIC_VISION_MODELS.
Copy link

vercel bot commented Dec 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextchat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 10, 2024 5:53am

Copy link

vercel bot commented Dec 10, 2024

@Yiming3 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 10, 2024

Walkthrough

The changes in this pull request introduce two new constants in app/constant.ts for identifying vision models using regular expressions. The VISION_MODEL_REGEXES constant contains patterns for various vision models, while EXCLUDE_VISION_MODEL_REGEXES specifies a regex to exclude a particular model. Additionally, the isVisionModel function in app/utils.ts has been updated to utilize these new constants and to retrieve model identifiers from an environment variable, enhancing the flexibility and robustness of model identification.

Changes

File Change Summary
app/constant.ts Added constants: VISION_MODEL_REGEXES (array of regex patterns for vision models) and EXCLUDE_VISION_MODEL_REGEXES (regex to exclude a specific model).
app/utils.ts Updated isVisionModel function to use environment variable NEXT_PUBLIC_VISION_MODELS for model identifiers and incorporated new regex constants for model identification.

Possibly related PRs

Suggested labels

planned

Suggested reviewers

  • lloydzhou

🐰 In the land of code where rabbits play,
New constants hop in, brightening the day.
With regex patterns, they dance and they twirl,
Excluding the models that make our heads whirl.
In utils, we fetch from the environment's chest,
Flexibility blooms, and we code with zest! 🌟


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. (Experiment)
  • @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: 2

🧹 Outside diff range and nitpick comments (1)
app/utils.ts (1)

263-264: Add Unit Tests for isVisionModel Function

To ensure that the isVisionModel function correctly identifies vision models, consider adding unit tests that cover various model names, including edge cases and potential false positives or negatives.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 83cea3a and a433d16.

📒 Files selected for processing (2)
  • app/constant.ts (1 hunks)
  • app/utils.ts (2 hunks)
🔇 Additional comments (1)
app/constant.ts (1)

307-308: Ensure EXCLUDE_VISION_MODEL_REGEXES Accurately Excludes Intended Models

Verify that /claude-3-5-haiku-20241022/ precisely matches and excludes only the specific model "claude-3-5-haiku-20241022" without affecting other models named similarly.

Apply this diff to clarify the intent:

 export const EXCLUDE_VISION_MODEL_REGEXES = [
-  /claude-3-5-haiku-20241022/
+  /^claude-3-5-haiku-20241022$/
 ];

This change ensures that only the exact model name is excluded.

Comment on lines +256 to +261
const envVisionModels = process.env.NEXT_PUBLIC_VISION_MODELS?.split(",").map(
(m) => m.trim(),
);
if (envVisionModels?.includes(model)) {
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle Potential Empty or Malformed Environment Variable

If NEXT_PUBLIC_VISION_MODELS is undefined, empty, or improperly formatted, envVisionModels might be undefined or contain unintended values. Consider adding a default value or additional validation to ensure robust handling.

Apply this diff to improve robustness:

 const envVisionModels = process.env.NEXT_PUBLIC_VISION_MODELS?.split(",").map(
   (m) => m.trim(),
 );
+const visionModelsSet = new Set(envVisionModels ?? []);
 
-if (envVisionModels?.includes(model)) {
+if (visionModelsSet.has(model)) {
   return true;
 }
📝 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
const envVisionModels = process.env.NEXT_PUBLIC_VISION_MODELS?.split(",").map(
(m) => m.trim(),
);
if (envVisionModels?.includes(model)) {
return true;
}
const envVisionModels = process.env.NEXT_PUBLIC_VISION_MODELS?.split(",").map(
(m) => m.trim(),
);
const visionModelsSet = new Set(envVisionModels ?? []);
if (visionModelsSet.has(model)) {
return true;
}

Comment on lines +294 to +305
export const VISION_MODEL_REGEXES = [
/vision/,
/gpt-4o/,
/claude-3/,
/gemini-1\.5/,
/gemini-exp/,
/learnlm/,
/qwen-vl/,
/qwen2-vl/,
/gpt-4-turbo(?!.*preview)/, // Matches "gpt-4-turbo" but not "gpt-4-turbo-preview"
/^dall-e-3$/, // Matches exactly "dall-e-3"
];
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Let me generate the final response based on the test results.

Regex Patterns in VISION_MODEL_REGEXES Need Adjustments

The test results reveal some issues with the regex patterns:

  • The /gpt-4-turbo(?!.*preview)/ pattern is not working as intended in bash regex - it's not matching "gpt-4-turbo" as expected
  • The /gemini-1\.5/ pattern works correctly, matching "gemini-1.5-pro"
  • The /^dall-e-3$/ pattern works as expected, matching exactly "dall-e-3" but not "dall-e-3-beta"

Consider:

  • Using a simpler negative pattern for gpt-4-turbo, like ^gpt-4-turbo$ to match exactly "gpt-4-turbo"
  • Adding test cases for other patterns (vision, gpt-4o, claude-3, etc.) to ensure they match their intended targets
🔗 Analysis chain

Verify Correctness of VISION_MODEL_REGEXES Patterns

Ensure that each regex in VISION_MODEL_REGEXES accurately matches the intended vision models without unintended matches. Pay special attention to:

  • /gemini-1\.5/: Confirm that the backslash is correctly escaping the dot to match gemini-1.5.
  • /gpt-4-turbo(?!.*preview)/: Ensure that this pattern matches "gpt-4-turbo" but not "gpt-4-turbo-preview".

Run the following script to test the regex patterns against a list of model names:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Test VISION_MODEL_REGEXES against existing model names

# Define model names for testing
model_names=(
  "gpt-4-turbo"
  "gpt-4-turbo-preview"
  "gemini-1.5-pro"
  "gemini-1x5-pro"
  "dall-e-3"
  "dall-e-3-beta"
)

# Define regex patterns
regex_patterns=(
  "vision"
  "gpt-4o"
  "claude-3"
  "gemini-1\\.5"
  "gemini-exp"
  "learnlm"
  "qwen-vl"
  "qwen2-vl"
  "gpt-4-turbo(?!.*preview)"
  "^dall-e-3$"
)

# Test each model name against each regex
for model in "${model_names[@]}"; do
  for pattern in "${regex_patterns[@]}"; do
    if [[ $model =~ $pattern ]]; then
      echo "Model '$model' matches regex '$pattern'"
    fi
  done
done

Length of output: 5253

@Yiming3 Yiming3 changed the title feat: use regex patterns for vision models and allow adding capabilit… feat: runtime configuration of vision-capable models Dec 10, 2024
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