-
Notifications
You must be signed in to change notification settings - Fork 225
fix: add provider checker #375
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
Conversation
|
@MonkeyCode-AI review |
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.
我是 MonkeyCode AI 编程助手,你可以在 GitHub 任意仓库的任意 Issue 或 PR 中 at @MonkeyCode-AI 来呼唤我。
任务执行细节请参考: 068fa9b0-b3c9-11f0-aed9-ee6ce69b85f1
Pull Request 概述
- 这个PR的主要目的是增强模型提供者的选择逻辑,通过引入一个新的函数
tools.CheckProvider来自动修正当用户未明确指定提供者时的情况。
Pull Request 变更详情
- 此PR增加了对模型提供商的智能识别和修正,提高了系统的健壮性和用户体验。
变更详情
| 文件路径 | 变更类型 | 变更内容 |
|---|---|---|
| backend/internal/model/repo/model.go | 修改 | 引入了 tools.CheckProvider 函数以尝试修正用户未指定 provider 时的情况 |
| backend/internal/proxy/proxy.go | 修改 | 使用 tools.CheckProvider 函数替代直接比较 m.Provider == consts.ModelProviderZhiPu |
backend/internal/model/repo/model.go
Outdated
| status = consts.ModelStatusDefault | ||
| } | ||
|
|
||
| // 尝试修正用户不清楚 provider 时的情况 |
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.
在 backend/internal/model/repo/model.go 中新增了对 tools 包的导入,并添加了注释 "尝试修正用户不清楚 provider 时的情况"。但是,tools.CheckProvider 函数并未在项目中找到。请确认该函数是否已实现或将在后续提交中添加。
| // 尝试修正用户不清楚 provider 时的情况 | |
| ```go | |
| // TODO: 实现 CheckProvider 函数或确保其存在于 tools 包中 | |
| // m.Provider = tools.CheckProvider(ctx, m.Provider, m.ModelName) |
|
|
||
| metadata := make(map[string]string) | ||
| if m.Provider == consts.ModelProviderZhiPu { | ||
| if tools.CheckProvider(r.In.Context(), m.Provider, m.ModelName) == consts.ModelProviderZhiPu { |
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.
在 backend/internal/proxy/proxy.go 中同样使用了 tools.CheckProvider 函数,但该项目中未找到此函数的实现。这可能导致编译错误或运行时错误。
| if tools.CheckProvider(r.In.Context(), m.Provider, m.ModelName) == consts.ModelProviderZhiPu { | |
| ```go | |
| // TODO: 实现 CheckProvider 函数或确保其存在于 tools 包中 | |
| // if tools.CheckProvider(r.In.Context(), m.Provider, m.ModelName) == consts.ModelProviderZhiPu { |
aa29e75 to
72a45af
Compare
|
@MonkeyCode-AI review again |
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.
我是 MonkeyCode AI 编程助手,你可以在 GitHub 任意仓库的任意 Issue 或 PR 中 at @MonkeyCode-AI 来呼唤我。
任务执行细节请参考: 79e80074-b3c9-11f0-b260-ee6ce69b85f1
Pull Request 概述
- 这个PR主要目的是增强模型提供商的自动识别功能,特别是针对智谱(ZhiPu)模型,通过模型名称前缀来自动设置提供商。
Pull Request 变更详情
- 此PR通过添加一个新的工具函数CheckProvider来实现模型提供商的自动推断,并在模型创建和代理转发两个关键位置应用该逻辑。
变更详情
| 文件路径 | 变更类型 | 变更内容 |
|---|---|---|
| backend/internal/model/repo/model.go | 修改 | 在创建模型时增加自动识别提供商逻辑 |
| backend/internal/proxy/proxy.go | 修改 | 在代理转发时使用新的提供商检查逻辑 |
| backend/pkg/tools/provider_checker.go | 添加 | 新增工具函数CheckProvider用于根据模型名前缀推断提供商 |
backend/internal/model/repo/model.go
Outdated
| } | ||
|
|
||
| // 尝试修正用户不清楚 provider 时的情况 | ||
| m.Provider = tools.CheckProvider(ctx, m.Provider, m.ModelName) |
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.
在创建模型时添加了自动识别提供商的逻辑,这是一个很好的增强功能。不过,直接修改传入的参数 m.Provider 可能会让人困惑,因为这改变了函数输入的行为。更好的做法可能是创建一个新的局部变量来保存修正后的值。
| m.Provider = tools.CheckProvider(ctx, m.Provider, m.ModelName) | |
| \t// 尝试修正用户不清楚 provider 时的情况\n\tprovider := tools.CheckProvider(ctx, m.Provider, m.ModelName)\n\tcreate := r.db.Model.Create().\n\t\tSetUserID(m.AdminID).\n\t\tSetShowName(m.ShowName).\n\t\tSetModelName(m.ModelName).\n\t\tSetProvider(provider).\n\t\tSetAPIBase(m.APIBase).\n\t\tSetAPIKey(m.APIKey).\n\t\tSetAPIVersion(m.APIVersion).\n\t\tSetAPIHeader(m.APIHeader).\n\t\tSetModelType(m.ModelType).\n\t\tSetStatus(status) |
|
|
||
| metadata := make(map[string]string) | ||
| if m.Provider == consts.ModelProviderZhiPu { | ||
| if tools.CheckProvider(r.In.Context(), m.Provider, m.ModelName) == consts.ModelProviderZhiPu { |
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.
在代理转发逻辑中使用了新的CheckProvider函数,这是合理的。但需要注意的是,这里的实现可能会导致重复的上下文传递(ctx),因为tools.CheckProvider的第一个参数是ctx,但函数内部并未使用它。可以考虑简化函数签名。
| if tools.CheckProvider(r.In.Context(), m.Provider, m.ModelName) == consts.ModelProviderZhiPu { | |
| \tmetadata := make(map[string]string)\n\tif tools.CheckProvider(context.Background(), m.Provider, m.ModelName) == consts.ModelProviderZhiPu { |
| "github.com/chaitin/MonkeyCode/backend/consts" | ||
| ) | ||
|
|
||
| // CheckProvider 根据 model 的前缀来判断 provider |
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.
新增的CheckProvider函数实现了基本的提供商识别逻辑,但目前只支持根据"glm"前缀识别智谱提供商。考虑到系统的可扩展性,建议采用更灵活的配置方式,比如通过配置文件或数据库来管理模型名称与提供商的映射关系,而不是硬编码在函数中。
| // CheckProvider 根据 model 的前缀来判断 provider | |
| // CheckProvider 根据 model 的前缀来判断 provider\nfunc CheckProvider(ctx context.Context, provider consts.ModelProvider, modelName string) consts.ModelProvider {\n\tif provider == consts.ModelProviderOther {\n\t\t// 可以考虑从配置或数据库加载映射关系\n\t\tproviderMappings := map[string]consts.ModelProvider{\n\t\t\t"glm": consts.ModelProviderZhiPu,\n\t\t\t// 可以添加更多映射\n\t\t}\n\t\t\n\t\tfor prefix, mappedProvider := range providerMappings {\n\t\t\tif strings.HasPrefix(modelName, prefix) {\n\t\t\t\treturn mappedProvider\n\t\t\t}\n\t\t}\n\t}\n\treturn provider\n} |
| ) | ||
|
|
||
| // CheckProvider 根据 model 的前缀来判断 provider | ||
| func CheckProvider(ctx context.Context, provider consts.ModelProvider, modelName string) consts.ModelProvider { |
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.
CheckProvider函数的第一个参数ctx在当前实现中并未被使用,可以考虑移除以简化函数签名,除非未来有计划使用它。
| func CheckProvider(ctx context.Context, provider consts.ModelProvider, modelName string) consts.ModelProvider { | |
| // CheckProvider 根据 model 的前缀来判断 provider\nfunc CheckProvider(provider consts.ModelProvider, modelName string) consts.ModelProvider { |
b304522 to
4fe15fa
Compare
4fe15fa to
8a727fa
Compare
变更描述
新增 provider 为 other 时的修正判断
变更类型
影响范围
创建模型
聊天请求
关闭 #