-
Notifications
You must be signed in to change notification settings - Fork 232
[app-builder] 新增用户对于模型列表增删改查接口 #38
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
Merged
CodeCasterX
merged 8 commits into
ModelEngine-Group:main
from
lizhichao51:develop_model
Apr 17, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
581ca3a
[app-platform] 新增用户对于模型列表增删改查接口
lizhichao51 9bc768b
[app-platform] 按照团队标准刷新注释信息, UserModelPo增加createdAt参数。
lizhichao51 140e07c
[app-builder] 修改检视意见,重构了模型列表接口的代码,提高了可读性和维护性。
lizhichao51 10f9128
[app-builder] 修改检视意见,为模型列表的增删改查功能添加了单元测试,确保接口的稳定性。
lizhichao51 dfbbf37
[app-builder] 修改检视意见,修复单侧内容无实际意义。
lizhichao51 71b516e
[app-builder] 修改检视意见,修改注释意义语病。
lizhichao51 8713e76
[app-builder] 修改检视意见,修改接口各字段为更有意义的名字。
lizhichao51 e553e44
[app-builder] 修改检视意见,优化 repo 层与 service 层的职责边界。
lizhichao51 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...-center/src/main/java/modelengine/fit/jade/aipp/model/repository/UserModelPluginRepo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package modelengine.fit.jade.aipp.model.repository; | ||
|
CodeCasterX marked this conversation as resolved.
Outdated
|
||
|
|
||
| import modelengine.fit.jade.aipp.model.po.UserModelDetailPo; | ||
| import modelengine.fitframework.annotation.Genericable; | ||
| import modelengine.fitframework.annotation.Property; | ||
| import modelengine.jade.carver.tool.annotation.Group; | ||
| import modelengine.jade.carver.tool.annotation.ToolMethod; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 表示用户模型信息用于插件的持久化层的接口。 | ||
| * | ||
| * @author lizhichao | ||
| * @since 2025/4/9 | ||
| */ | ||
| @Group(name = "User_Model_Tool") | ||
| public interface UserModelPluginRepo { | ||
| /** | ||
| * 根据用户标识来查询该用户可用的用戶模型列表。 | ||
| * | ||
| * @param userId 表示用户标识。 | ||
| * @return 表示该用户可用的用戶模型列表 {@link List}{@code <}{@link UserModelDetailPo}{@code >}。 | ||
| */ | ||
| @ToolMethod(name = "get_user_model_list", description = "根据用户标识来查询该用户可用的用戶模型列表") | ||
| @Genericable(id = "modelengine.fit.jade.aipp.model.getUserModelList") | ||
| List<UserModelDetailPo> getUserModelList(@Property(description = "用户id", required = true) String userId); | ||
|
|
||
| /** | ||
| * 为用户添加模型。 | ||
| * | ||
| * @param userId 表示用户标识。 | ||
| * @param apiKey 表示该用户访问模型所需的 API Key。 | ||
| * @param modelName 表示模型名称。 | ||
| * @param baseUrl 表示模型访问的地址。 | ||
| * @return 添加结果提示信息。 | ||
| */ | ||
| @ToolMethod(name = "add_user_model", description = "为用户添加可用的模型信息") | ||
| @Genericable(id = "modelengine.fit.jade.userModel.addUserModel") | ||
| String addUserModel( | ||
| @Property(description = "用户id", required = true) String userId, | ||
| @Property(description = "模型访问的 API Key", required = true) String apiKey, | ||
| @Property(description = "模型名称", required = true) String modelName, | ||
| @Property(description = "模型访问地址", required = true) String baseUrl | ||
| ); | ||
|
|
||
| /** | ||
| * 删除用户绑定的模型信息。 | ||
| * | ||
| * @param userId 表示用户标识。 | ||
| * @param modelId 表示待删除的模型标识。 | ||
| * @return 删除结果提示信息。 | ||
| */ | ||
| @ToolMethod(name = "delete_user_model", description = "删除用户绑定的模型信息") | ||
| @Genericable(id = "modelengine.fit.jade.userModel.deleteUserModel") | ||
| String deleteUserModel( | ||
| @Property(description = "用户id", required = true) String userId, | ||
| @Property(description = "模型id", required = true) String modelId | ||
| ); | ||
|
|
||
| /** | ||
| * 将指定模型设置为该用户的默认模型。 | ||
| * | ||
| * @param userId 表示用户标识。 | ||
| * @param modelId 表示要设为默认的模型标识。 | ||
| * @return 切换默认模型的提示信息。 | ||
| */ | ||
| @ToolMethod(name = "switch_default_model", description = "将指定模型设置为用户的默认模型") | ||
| @Genericable(id = "modelengine.fit.jade.userModel.switchDefaultModel") | ||
| String switchDefaultModel( | ||
| @Property(description = "用户id", required = true) String userId, | ||
| @Property(description = "默认模型id", required = true) String modelId | ||
| ); | ||
| } | ||
163 changes: 163 additions & 0 deletions
163
...rc/main/java/modelengine/fit/jade/aipp/model/repository/impl/UserModelPluginRepoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package modelengine.fit.jade.aipp.model.repository.impl; | ||
|
lizhichao51 marked this conversation as resolved.
Outdated
|
||
|
|
||
| import modelengine.fit.jade.aipp.model.mapper.ModelMapper; | ||
| import modelengine.fit.jade.aipp.model.mapper.UserModelMapper; | ||
| import modelengine.fit.jade.aipp.model.po.ModelPo; | ||
| import modelengine.fit.jade.aipp.model.po.UserModelDetailPo; | ||
| import modelengine.fit.jade.aipp.model.po.UserModelPo; | ||
| import modelengine.fit.jade.aipp.model.repository.UserModelPluginRepo; | ||
| import modelengine.fit.jade.aipp.model.repository.UserModelRepo; | ||
| import modelengine.fitframework.annotation.Component; | ||
| import modelengine.fitframework.annotation.Fitable; | ||
| import modelengine.fitframework.annotation.Property; | ||
| import modelengine.fitframework.log.Logger; | ||
| import modelengine.fitframework.util.CollectionUtils; | ||
| import modelengine.jade.carver.tool.annotation.Attribute; | ||
| import modelengine.jade.carver.tool.annotation.Group; | ||
| import modelengine.jade.carver.tool.annotation.ToolMethod; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * 表示用户模型信息用于插件的持久化层的接口 {@link UserModelRepo} 的实现。 | ||
| * | ||
| * @author lizhichao | ||
| * @since 2025/4/9 | ||
| */ | ||
| @Component | ||
| @Group(name = "User_Model_Tool_Impl") | ||
| public class UserModelPluginRepoImpl implements UserModelPluginRepo { | ||
|
lizhichao51 marked this conversation as resolved.
Outdated
|
||
| private static final Logger log = Logger.get(UserModelRepoImpl.class); | ||
| private static final String FITABLE_ID = "aipp.model.repository"; | ||
| public static final String DEFAULT_MODEL_TYPE = "chat_completions"; | ||
| private final ModelMapper modelMapper; | ||
| private final UserModelMapper userModelMapper; | ||
|
|
||
| /** | ||
| * 构造方法。 | ||
| * | ||
| * @param modelMapper 模型信息表的 MyBatis 映射接口,用于处理模型增删查改。 | ||
| * @param userModelMapper 用户与模型绑定关系的 MyBatis 映射接口,用于管理用户模型映射数据。 | ||
| */ | ||
| public UserModelPluginRepoImpl(ModelMapper modelMapper, UserModelMapper userModelMapper) { | ||
| this.modelMapper = modelMapper; | ||
| this.userModelMapper = userModelMapper; | ||
| } | ||
|
|
||
| @Override | ||
| @Fitable(id = FITABLE_ID) | ||
| @ToolMethod(name = "获取用户模型列表", description = "根据用户标识来查询该用户可用的模型列表", extensions = { | ||
| @Attribute(key = "tags", value = "FIT"), @Attribute(key = "tags", value = "MODEL") | ||
| }) | ||
| @Property(description = "返回该用户可用的模型列表") | ||
| public List<UserModelDetailPo> getUserModelList(String userId) { | ||
| log.info("start get model list for {}.", userId); | ||
| List<UserModelPo> userModelPos = this.userModelMapper.listUserModels(userId); | ||
| if (CollectionUtils.isEmpty(userModelPos)) { | ||
| log.warn("No user model records found for userId={}.", userId); | ||
| return Collections.emptyList(); | ||
| } | ||
| List<String> modelIds = userModelPos.stream() | ||
| .map(UserModelPo::getModelId) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
| List<ModelPo> modelPos = this.modelMapper.listModels(modelIds); | ||
| // 构建 modelId → ModelPo 映射 | ||
| Map<String, ModelPo> modelMap = modelPos.stream() | ||
| .map(model -> Map.entry(model.getModelId(), model)) | ||
| .collect(Collectors.toMap( | ||
| Map.Entry::getKey, | ||
| Map.Entry::getValue, | ||
| (a, b) -> a | ||
| )); | ||
| return userModelPos.stream().map(userModel -> { | ||
| ModelPo model = modelMap.get(userModel.getModelId()); | ||
| return new UserModelDetailPo( | ||
| userModel.getCreatedAt(), | ||
| userModel.getModelId(), | ||
| userModel.getUserId(), | ||
| model != null ? model.getName() : null, | ||
| model != null ? model.getBaseUrl() : null, | ||
| userModel.getIsDefault() | ||
| ); | ||
| }).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| @Fitable(id = FITABLE_ID) | ||
| @ToolMethod(name = "添加模型", description = "为用户添加可用的模型信息", extensions = { | ||
| @Attribute(key = "tags", value = "FIT"), @Attribute(key = "tags", value = "MODEL") | ||
| }) | ||
| @Property(description = "为用户添加可用的模型信息") | ||
| public String addUserModel(String userId, String apiKey, | ||
| String modelName, String baseUrl) { | ||
| log.info("start add user model for {}.", userId); | ||
| String modelId = UUID.randomUUID().toString().replace("-", ""); | ||
| int isDefault = this.userModelMapper.userHasDefaultModel(userId) ? 0 : 1; | ||
|
|
||
| ModelPo modelPo = new ModelPo(modelId, modelName, modelId, baseUrl, DEFAULT_MODEL_TYPE); | ||
|
lizhichao51 marked this conversation as resolved.
Outdated
|
||
| modelPo.setCreatedBy(userId); | ||
| modelPo.setUpdatedBy(userId); | ||
| this.modelMapper.insertModel(modelPo); | ||
|
|
||
| UserModelPo userModelPo = new UserModelPo(userId, modelId, apiKey, isDefault); | ||
| userModelPo.setCreatedBy(userId); | ||
| userModelPo.setUpdatedBy(userId); | ||
| this.userModelMapper.addUserModel(userModelPo); | ||
| return "添加模型成功。"; | ||
|
lizhichao51 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| @Fitable(id = FITABLE_ID) | ||
| @ToolMethod(name = "删除模型", description = "删除用户绑定的模型信息", extensions = { | ||
| @Attribute(key = "tags", value = "FIT"), @Attribute(key = "tags", value = "MODEL") | ||
| }) | ||
| @Property(description = "删除用户绑定的模型信息") | ||
| public String deleteUserModel(String userId, String modelId) { | ||
|
lizhichao51 marked this conversation as resolved.
|
||
| log.info("start delete user model for {}.", userId); | ||
| List<UserModelPo> userModels = this.userModelMapper.listUserModels(userId); | ||
| if (userModels == null || userModels.isEmpty()) { | ||
|
lizhichao51 marked this conversation as resolved.
Outdated
|
||
| return "删除模型失败,当前用户没有任何模型记录。"; | ||
| } | ||
|
|
||
| UserModelPo target = userModels.stream() | ||
| .filter(m -> Objects.equals(m.getModelId(), modelId)) | ||
| .findFirst() | ||
| .orElse(null); | ||
| if (target == null) { | ||
| return "删除模型失败,该模型不属于当前用户。"; | ||
| } | ||
| this.userModelMapper.deleteByModelId(modelId); | ||
| this.modelMapper.deleteByModelId(modelId); | ||
| // 如果删除的不是默认模型,直接返回 | ||
| if (target.getIsDefault() != 1) { | ||
| return "删除模型成功。"; | ||
| } | ||
| userModels.remove(target); | ||
| // 如果没有默认模型,但还有其他记录,则设置最新创建的为默认 | ||
| if (!userModels.isEmpty()) { | ||
|
lizhichao51 marked this conversation as resolved.
Outdated
|
||
| UserModelPo latest = userModels.stream() | ||
|
lizhichao51 marked this conversation as resolved.
Outdated
|
||
| .max(Comparator.comparing(UserModelPo::getCreatedAt)) | ||
| .orElse(null); | ||
| this.userModelMapper.switchDefaultForUser(userId, latest.getModelId()); | ||
| return String.format("删除默认模型成功,添加%s为默认模型。", this.modelMapper.get(latest.getModelId()).getName()); | ||
| } | ||
| return "删除模型成功,当前无默认模型。"; | ||
| } | ||
|
|
||
| @Override | ||
| @Fitable(id = FITABLE_ID) | ||
| @ToolMethod(name = "切换默认模型", description = "将指定模型设置为用户的默认模型", extensions = { | ||
| @Attribute(key = "tags", value = "FIT"), @Attribute(key = "tags", value = "MODEL") | ||
| }) | ||
| @Property(description = "将指定模型设置为用户的默认模型") | ||
| public String switchDefaultModel(String userId, String modelId) { | ||
| log.info("start switch default model for {}.", userId); | ||
| int rows = this.userModelMapper.switchDefaultForUser(userId, modelId); | ||
| if (rows == 0) { | ||
| return "未查到对应模型。"; | ||
| } | ||
| return String.format("已切换%s为默认模型。", this.modelMapper.get(modelId).getName()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.