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 defaultModel undefined error #5071

Merged
merged 5 commits into from
Jul 25, 2024
Merged

Fix defaultModel undefined error #5071

merged 5 commits into from
Jul 25, 2024

Conversation

ZTH7
Copy link
Contributor

@ZTH7 ZTH7 commented Jul 21, 2024

💻 变更类型 | Change Type

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

🔀 变更说明 | Description of Change

Fixed the issue where the variable Default_Model would produce "undefined" in the model list, and requests would return an "invalid model id" error after specifying the model provider in Default_Model.

📝 补充信息 | Additional Information

#5039
#5064

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation for the default model selection to ensure it only corresponds to valid entries in the model table, preventing potential errors.
    • Enhanced logic for setting model properties to accurately reflect existing values or default to the provided model name, allowing for better handling of default models.
    • Refined logic to correctly identify and set the default model based on naming conventions, ensuring better model availability handling.

Copy link

vercel bot commented Jul 21, 2024

@ZTH7 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 Jul 21, 2024

Walkthrough

The recent updates to the collectModelTableWithDefaultModel function enhance both validation and assignment for the defaultModel parameter. Now, defaultModel must not only be a non-empty string but also a valid key in the modelTable. Additional improvements include more robust logic for setting the model's name and displayName, ensuring accurate representation and reducing potential errors in model management.

Changes

File Change Summary
app/utils/model.ts Enhanced validation for defaultModel to ensure it corresponds to an existing key in modelTable and improved logic for setting name and displayName properties based on model availability.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ModelManager
    participant ModelTable

    User->>ModelManager: Request to set default model
    ModelManager->>ModelTable: Check if default model is valid
    alt Valid default model
        ModelManager->>ModelTable: Retrieve name and displayName
        ModelManager->>User: Default model set successfully
    else Invalid default model
        ModelManager->>User: Error: Invalid default model
    end
Loading

Poem

🐇 Hopping through code, what a joyful sight,
With checks in place, our models take flight!
No more confusion, just clear paths ahead,
Stability reigns, let worries be shed.
A cheer for the changes, let’s all gather near,
A brighter tomorrow is finally here! 🎉


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 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 115f357 and 2fd68bc.

Files selected for processing (1)
  • app/utils/model.ts (1 hunks)
Additional comments not posted (1)
app/utils/model.ts (1)

101-105: Enhanced validation for defaultModel.

The additional check ensures that defaultModel exists in modelTable before setting it as available and default. This change improves robustness by preventing invalid model IDs.

Copy link
Contributor

Your build has completed!

Preview deployment

@@ -98,10 +98,9 @@ export function collectModelTableWithDefaultModel(
defaultModel: string,
) {
let modelTable = collectModelTable(models, customModels);
if (defaultModel && defaultModel !== "") {
if (defaultModel && defaultModel !== "" && defaultModel in modelTable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please see L82:

modelTable[`${customModelName}@${provider?.id}`] = {...}

Copy link
Contributor Author

@ZTH7 ZTH7 Jul 22, 2024

Choose a reason for hiding this comment

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

Pardon me, I don't understand your meaning. I know modelTable's key include the provider name, but the defaultModel should also specify the provider. Is there any problem here?

Copy link
Contributor

Choose a reason for hiding this comment

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

我的意思是,当前系统已经是一个在线上运行了挺长时间,并且有很多人自己部署的系统。
所以,牵涉到配置相关的东西,需要尽量做到旧的配置能平稳迁移到新系统。
所以,这里可能需要做一下兼容(当用户旧的defaultModel没有配置provider name的时候,期望也能正常生效)

Copy link
Contributor

Choose a reason for hiding this comment

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

translate:

I mean, the current system is one that has been running online for a long time and has been deployed by many people themselves.
Therefore, when it comes to configuration-related things, it is necessary to ensure that the old configuration can be smoothly migrated to the new system.
Therefore, some compatibility may be required here (when the user's old defaultModel does not have the provider name configured, it is expected to take effect normally).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

了解了,但鉴于用户旧的default_model没有指定provider,我选择创建一个独立的可选项,而不是帮他自动选择一个provider(如图)。
这同时也兼容了用户旧的default_model所指定的模型可能不在custom_models中的情况。
image

Copy link
Contributor

Choose a reason for hiding this comment

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

或许针对就配置,帮用户选中匹配到的第一个才是合理的?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

但我们选到的第一个provider不一定可用,同时也会让这个函数变得繁琐,因为如果没有遍历到对应的模型,我们依旧需要create一个新element。
事实上我认为由用户来指定一个provider是必要的,如果用户没有指定,我们或许不应该随机选择。
当然如果你坚持,我将进行修改,毕竟两种方案在我看来都是可行的。

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. 确实,选到第一个不一定可用

但是,选到第一个可用的概率其实比较大

  1. 没有遍历到的时候,还是需要create新的element

这个逻辑,感觉不是很合理。应该是在available=true的模型中选择modelName=defaultModelName的第一个,标记一下isDefault=true
如果创建新的,感觉不合理,而且可能导致这个新创建的模型并不能正确的进行对话

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. 确实,选到第一个不一定可用

但是,选到第一个可用的概率其实比较大

  1. 没有遍历到的时候,还是需要create新的element

这个逻辑,感觉不是很合理。应该是在available=true的模型中选择modelName=defaultModelName的第一个,标记一下isDefault=true
如果创建新的,感觉不合理,而且可能导致这个新创建的模型并不能正确的进行对话

Copy link
Contributor Author

@ZTH7 ZTH7 Jul 25, 2024

Choose a reason for hiding this comment

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

是的,我也感觉创建一个新的不合理,但由于在之前的版本并没有检查defaultModel是否在modelTable中,因此我担心有用户的配置是靠这个"bug"生效的。
如果你觉得可以,我也打算如果找不到便不再创建新的,以免这个新创建的模型并不能正确的进行对话。

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2fd68bc and aa4e855.

Files selected for processing (1)
  • app/utils/model.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/utils/model.ts

@ZTH7 ZTH7 requested a review from lloydzhou July 25, 2024 10:01
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aa4e855 and 5c04d3c.

Files selected for processing (1)
  • app/utils/model.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/utils/model.ts

isDefault: true,
};
const [modelName, providerName] = defaultModel.split("@");
if (providerName && providerName != "") {
Copy link
Contributor

Choose a reason for hiding this comment

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

感觉还是不太严谨:

  1. 首先应该判断defaultModel.includes('@')
  2. 如果是新的配置方式,就判断defaultModel in modelTable,如果在modelTable里面,就设置对应的这一项isDefault=true
  3. 如果是旧的配置方式(实际上这个时候defaultModel就是modelName),走后面的for循环
  4. 在循环中,不应该使用key.startsWith(modelName),应该使用key.split('@').shift() == modelName

Copy link
Contributor Author

Choose a reason for hiding this comment

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

感谢你的指点,关于

  1. 如果是新的配置方式,就判断defaultModel in modelTable,如果在modelTable里面,就设置对应的这一项isDefault=true

这一点,是否可能存在用户使用了不在modelTable中的provider的情况?我是考虑到可能存在这一点,因此没写defaultModel in modelTable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

感谢你的指点,关于

  1. 如果是新的配置方式,就判断defaultModel in modelTable,如果在modelTable里面,就设置对应的这一项isDefault=true

这一点,是否可能存在用户使用了不在modelTable中的provider的情况?我是考虑到可能存在这一点,因此没写defaultModel in modelTable

好像不会,我们不支持自定义provider,已更正。

Copy link
Contributor

Choose a reason for hiding this comment

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

以后可能会有计划自定义provider
可能是一种alias的方式,例如都是OpenAI的模式,但是可以同时存在两个openai1 + openai2

但是还是一个构思中的处理方式,还没进入开发阶段

@lloydzhou lloydzhou added help wanted Extra attention is needed planned planned feature, will support in the future labels Jul 25, 2024
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5c04d3c and c4334d4.

Files selected for processing (1)
  • app/utils/model.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/utils/model.ts

@lloydzhou lloydzhou merged commit f5499ff into ChatGPTNextWeb:main Jul 25, 2024
1 of 2 checks passed
@lloydzhou
Copy link
Contributor

fixed #5070 #5017 #5039 #5064

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed planned planned feature, will support in the future
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants