| | |
| | | package cn.iocoder.yudao.module.ai.service.model; |
| | | |
| | | import cn.iocoder.yudao.module.ai.enums.model.AiPlatformEnum; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.core.model.AiModelFactory; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.core.model.midjourney.api.MidjourneyApi; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.core.model.suno.api.SunoApi; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.ai.controller.admin.model.vo.model.AiModelPageReqVO; |
| | | import cn.iocoder.yudao.module.ai.controller.admin.model.vo.model.AiModelSaveReqVO; |
| | | import cn.iocoder.yudao.module.ai.controller.admin.model.vo.model.AiModelUpdateStatusReqVO; |
| | | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiApiKeyDO; |
| | | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiModelDO; |
| | | import cn.iocoder.yudao.module.ai.dal.mysql.model.AiChatMapper; |
| | | import com.agentsflex.llm.ollama.OllamaLlm; |
| | | import com.agentsflex.llm.ollama.OllamaLlmConfig; |
| | | import com.agentsflex.llm.qwen.QwenLlm; |
| | | import com.agentsflex.llm.qwen.QwenLlmConfig; |
| | | import dev.tinyflow.core.Tinyflow; |
| | | import cn.iocoder.yudao.module.ai.dal.mysql.model.AiModelMapper; |
| | | import cn.iocoder.yudao.module.ai.enums.model.AiModelCapabilityEnum; |
| | | import cn.iocoder.yudao.module.ai.enums.model.AiModelTypeEnum; |
| | | import cn.iocoder.yudao.module.ai.enums.model.AiPlatformEnum; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.config.YudaoAiProperties; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.core.model.AiModelFactory; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.ai.chat.model.ChatModel; |
| | | import org.springframework.ai.embedding.EmbeddingModel; |
| | | import org.springframework.ai.image.ImageModel; |
| | | import org.springframework.ai.vectorstore.SimpleVectorStore; |
| | | import org.springframework.ai.vectorstore.VectorStore; |
| | | import org.springframework.ai.vectorstore.milvus.MilvusVectorStore; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * AI 模型 Service 实现类 |
| | | * |
| | | * @author fansili |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class AiModelServiceImpl implements AiModelService { |
| | | |
| | | @Resource |
| | | private AiApiKeyService apiKeyService; |
| | | |
| | | @Resource |
| | | private AiChatMapper modelMapper; |
| | | |
| | | private AiModelMapper modelMapper; |
| | | @Resource |
| | | private AiModelFactory modelFactory; |
| | | @Resource |
| | | private YudaoAiProperties aiProperties; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createModel(AiModelSaveReqVO createReqVO) { |
| | | // 1. 校验 |
| | | AiPlatformEnum.validatePlatform(createReqVO.getPlatform()); |
| | | apiKeyService.validateApiKey(createReqVO.getKeyId()); |
| | | |
| | | // 2. 插入 |
| | | AiModelDO model = BeanUtils.toBean(createReqVO, AiModelDO.class); |
| | | model.setCapability(resolveCapability(createReqVO.getCapability())); |
| | | if (CommonStatusEnum.isEnable(model.getStatus())) { |
| | | disableOtherEnabledModels(model); |
| | | } |
| | | modelMapper.insert(model); |
| | | return model.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateModel(AiModelSaveReqVO updateReqVO) { |
| | | // 1. 校验 |
| | | validateModelExists(updateReqVO.getId()); |
| | | AiModelDO existing = validateModelExists(updateReqVO.getId()); |
| | | AiPlatformEnum.validatePlatform(updateReqVO.getPlatform()); |
| | | apiKeyService.validateApiKey(updateReqVO.getKeyId()); |
| | | |
| | | // 2. 更新 |
| | | AiModelDO updateObj = BeanUtils.toBean(updateReqVO, AiModelDO.class); |
| | | modelMapper.updateById(updateObj); |
| | | AiModelDO model = BeanUtils.toBean(updateReqVO, AiModelDO.class); |
| | | // 未传能力时沿用原值,避免老前端提交把视觉模型降级成文本能力 |
| | | model.setCapability(resolveCapability( |
| | | updateReqVO.getCapability() == null || updateReqVO.getCapability().isBlank() |
| | | ? existing.getCapability() : updateReqVO.getCapability())); |
| | | if (CommonStatusEnum.isEnable(model.getStatus())) { |
| | | disableOtherEnabledModels(model); |
| | | } |
| | | modelMapper.updateById(model); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteModel(Long id) { |
| | | // 校验存在 |
| | | validateModelExists(id); |
| | | // 删除 |
| | | modelMapper.deleteById(id); |
| | | } |
| | | |
| | | private AiModelDO validateModelExists(Long id) { |
| | | AiModelDO model = modelMapper.selectById(id); |
| | | if (modelMapper.selectById(id) == null) { |
| | | throw exception(MODEL_NOT_EXISTS); |
| | | } |
| | | return model; |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public AiModelDO getRequiredDefaultModel(Integer type) { |
| | | AiModelDO model = modelMapper.selectFirstByStatus(type, CommonStatusEnum.ENABLE.getStatus()); |
| | | return getRequiredDefaultModel(type, AiModelCapabilityEnum.defaultCapability()); |
| | | } |
| | | |
| | | @Override |
| | | public AiModelDO getRequiredDefaultModel(Integer type, String capability) { |
| | | AiModelDO model = modelMapper.selectFirstByTypeAndCapabilityAndStatus( |
| | | type, resolveCapability(capability), CommonStatusEnum.ENABLE.getStatus()); |
| | | if (model == null) { |
| | | throw exception(MODEL_DEFAULT_NOT_EXISTS); |
| | | throw exception(MODEL_NOT_ENABLED, |
| | | AiModelTypeEnum.getName(type), AiModelCapabilityEnum.getName(resolveCapability(capability))); |
| | | } |
| | | return model; |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateModelStatus(AiModelUpdateStatusReqVO updateReqVO) { |
| | | AiModelDO model = validateModelExists(updateReqVO.getId()); |
| | | model.setStatus(updateReqVO.getStatus()); |
| | | if (CommonStatusEnum.isEnable(updateReqVO.getStatus())) { |
| | | // 启用:自动关闭同「类型 + 能力」下的其他模型,保证同组只有一个启用 |
| | | disableOtherEnabledModels(model); |
| | | } |
| | | modelMapper.updateById(model); |
| | | } |
| | | |
| | | @Override |
| | | public AiModelDO validateModel(Long id) { |
| | | AiModelDO model = validateModelExists(id); |
| | | if (CommonStatusEnum.isDisable(model.getStatus())) { |
| | | throw exception(MODEL_DISABLE); |
| | | } |
| | | if (CommonStatusEnum.isDisable(model.getStatus())) throw exception(MODEL_DISABLE, model.getName()); |
| | | return model; |
| | | } |
| | | |
| | | @Override |
| | | public List<AiModelDO> getModelListByStatusAndType(Integer status, Integer type, String platform) { |
| | | return modelMapper.selectListByStatusAndType(status, type, platform); |
| | | } |
| | | |
| | | /** |
| | | * 关闭同「类型 + 能力」下已启用的其他模型(不含自身) |
| | | */ |
| | | private void disableOtherEnabledModels(AiModelDO model) { |
| | | List<AiModelDO> sameGroup = modelMapper.selectListByTypeAndCapabilityAndStatus( |
| | | model.getType(), resolveCapability(model.getCapability()), CommonStatusEnum.ENABLE.getStatus()); |
| | | for (AiModelDO other : sameGroup) { |
| | | if (other.getId().equals(model.getId())) { |
| | | continue; |
| | | } |
| | | AiModelDO update = new AiModelDO(); |
| | | update.setId(other.getId()); |
| | | update.setStatus(CommonStatusEnum.DISABLE.getStatus()); |
| | | modelMapper.updateById(update); |
| | | } |
| | | } |
| | | |
| | | private static String resolveCapability(String capability) { |
| | | return capability == null || capability.isBlank() |
| | | ? AiModelCapabilityEnum.defaultCapability() : capability; |
| | | } |
| | | |
| | | private AiModelDO validateModelExists(Long id) { |
| | | AiModelDO model = modelMapper.selectById(id); |
| | | if (model == null) throw exception(MODEL_NOT_EXISTS); |
| | | return model; |
| | | } |
| | | |
| | | // ========== 与 Spring AI 集成 ========== |
| | |
| | | AiModelDO model = validateModel(id); |
| | | AiApiKeyDO apiKey = apiKeyService.validateApiKey(model.getKeyId()); |
| | | AiPlatformEnum platform = AiPlatformEnum.validatePlatform(apiKey.getPlatform()); |
| | | return modelFactory.getOrCreateChatModel(platform, apiKey.getApiKey(), apiKey.getUrl()); |
| | | } |
| | | |
| | | @Override |
| | | public ImageModel getImageModel(Long id) { |
| | | AiModelDO model = validateModel(id); |
| | | AiApiKeyDO apiKey = apiKeyService.validateApiKey(model.getKeyId()); |
| | | AiPlatformEnum platform = AiPlatformEnum.validatePlatform(apiKey.getPlatform()); |
| | | return modelFactory.getOrCreateImageModel(platform, apiKey.getApiKey(), apiKey.getUrl()); |
| | | } |
| | | |
| | | @Override |
| | | public MidjourneyApi getMidjourneyApi(Long id) { |
| | | AiModelDO model = validateModel(id); |
| | | AiApiKeyDO apiKey = apiKeyService.validateApiKey(model.getKeyId()); |
| | | return modelFactory.getOrCreateMidjourneyApi(apiKey.getApiKey(), apiKey.getUrl()); |
| | | } |
| | | |
| | | @Override |
| | | public SunoApi getSunoApi() { |
| | | AiApiKeyDO apiKey = apiKeyService.getRequiredDefaultApiKey( |
| | | AiPlatformEnum.SUNO.getPlatform(), CommonStatusEnum.ENABLE.getStatus()); |
| | | return modelFactory.getOrCreateSunoApi(apiKey.getApiKey(), apiKey.getUrl()); |
| | | // temperature / maxTokens 是 AI 模型表上一直存在、却从没人读过的两列: |
| | | // 不读 temperature,库里配的值对调用方是假的;不读 maxTokens,长输出会被静默截断 |
| | | return modelFactory.getOrCreateChatModel(platform, apiKey.getApiKey(), apiKey.getUrl(), model.getModel(), |
| | | model.getTemperature(), model.getMaxTokens(), aiProperties.getTimeout()); |
| | | } |
| | | |
| | | @Override |
| | | public VectorStore getOrCreateVectorStore(Long id, Map<String, Class<?>> metadataFields) { |
| | | // 获取模型 + 密钥 |
| | | AiModelDO model = validateModel(id); |
| | | AiApiKeyDO apiKey = apiKeyService.validateApiKey(model.getKeyId()); |
| | | AiPlatformEnum platform = AiPlatformEnum.validatePlatform(apiKey.getPlatform()); |
| | | |
| | | // 创建或获取 EmbeddingModel 对象 |
| | | EmbeddingModel embeddingModel = modelFactory.getOrCreateEmbeddingModel( |
| | | platform, apiKey.getApiKey(), apiKey.getUrl(), model.getModel()); |
| | | |
| | | // 创建或获取 VectorStore 对象 |
| | | return modelFactory.getOrCreateVectorStore(SimpleVectorStore.class, embeddingModel, metadataFields); |
| | | // return modelFactory.getOrCreateVectorStore(QdrantVectorStore.class, embeddingModel, metadataFields); |
| | | // return modelFactory.getOrCreateVectorStore(RedisVectorStore.class, embeddingModel, metadataFields); |
| | | // return modelFactory.getOrCreateVectorStore(MilvusVectorStore.class, embeddingModel, metadataFields); |
| | | } |
| | | |
| | | // TODO @lesan:是不是返回 Llm 对象会好点哈? |
| | | @Override |
| | | public void getLLmProvider4Tinyflow(Tinyflow tinyflow, Long modelId) { |
| | | AiModelDO model = validateModel(modelId); |
| | | AiApiKeyDO apiKey = apiKeyService.validateApiKey(model.getKeyId()); |
| | | AiPlatformEnum platform = AiPlatformEnum.validatePlatform(apiKey.getPlatform()); |
| | | switch (platform) { |
| | | // TODO @lesan 考虑到未来不需要使用agents-flex 现在仅测试通义千问 |
| | | // TODO @lesan:【重要】是不是可以实现一个 SpringAiLlm,这样的话,内部全部用它就好了。只实现 chat 部分;这样,就把 flex 作为一个 agent 框架,内部调用,还是 spring ai 相关的。成本可能低一点?! |
| | | case TONG_YI: |
| | | QwenLlmConfig qwenLlmConfig = new QwenLlmConfig(); |
| | | qwenLlmConfig.setApiKey(apiKey.getApiKey()); |
| | | qwenLlmConfig.setModel(model.getModel()); |
| | | // TODO @lesan:这个有点奇怪。。。如果一个链式里,有多个模型,咋整呀。。。 |
| | | tinyflow.setLlmProvider(id -> new QwenLlm(qwenLlmConfig)); |
| | | break; |
| | | case OLLAMA: |
| | | OllamaLlmConfig ollamaLlmConfig = new OllamaLlmConfig(); |
| | | ollamaLlmConfig.setEndpoint(apiKey.getUrl()); |
| | | ollamaLlmConfig.setModel(model.getModel()); |
| | | tinyflow.setLlmProvider(id -> new OllamaLlm(ollamaLlmConfig)); |
| | | break; |
| | | } |
| | | return modelFactory.getOrCreateVectorStore(MilvusVectorStore.class, embeddingModel, metadataFields); |
| | | } |
| | | |
| | | } |