| | |
| | | package cc.mrbird.febs.ai.service.impl; |
| | | |
| | | import cc.mrbird.febs.ai.entity.AiMemberProductUnlock; |
| | | import cc.mrbird.febs.ai.entity.AiProduct; |
| | | import cc.mrbird.febs.ai.entity.AiProductDependency; |
| | | import cc.mrbird.febs.ai.entity.AiProductPoint; |
| | | import cc.mrbird.febs.ai.mapper.AiProductMapper; |
| | | import cc.mrbird.febs.ai.req.product.ApiProductInfoDto; |
| | | import cc.mrbird.febs.ai.req.product.ApiProductPageDto; |
| | | import cc.mrbird.febs.ai.req.product.ApiTranslateDto; |
| | | import cc.mrbird.febs.ai.res.product.ApiProductInfoVo; |
| | | import cc.mrbird.febs.ai.res.product.ApiProductVo; |
| | | import cc.mrbird.febs.ai.res.productPoint.ApiProductPointVo; |
| | | import cc.mrbird.febs.ai.service.*; |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.dashscope.aigc.generation.Generation; |
| | | import com.alibaba.dashscope.aigc.generation.GenerationParam; |
| | | import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| | | import com.alibaba.dashscope.aigc.generation.TranslationOptions; |
| | | import com.alibaba.dashscope.common.Message; |
| | | import com.alibaba.dashscope.common.Role; |
| | | import com.alibaba.dashscope.exception.InputRequiredException; |
| | | import com.alibaba.dashscope.exception.NoApiKeyException; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * AI产品表 Service实现类 |
| | |
| | | private final AiMemberRoleService aiMemberRoleService; |
| | | private final AiProductCategoryService aiProductCategoryService; |
| | | private final AiProductPointService aiProductPointService; |
| | | |
| | | private final AiProductDependencyService aiProductDependencyService; |
| | | private final AiMemberProductUnlockService aiMemberProductUnlockService; |
| | | |
| | | @Override |
| | | public AiProduct getById(String id) { |
| | |
| | | // 创建分页对象,传入当前页和每页大小 |
| | | Page<ApiProductVo> page = new Page<>(dto.getPageNow(), dto.getPageSize()); |
| | | Page<ApiProductVo> pageListByQuery = this.getPageListByQuery(page, dto); |
| | | List<ApiProductVo> records = pageListByQuery.getRecords(); |
| | | if (CollUtil.isNotEmpty( records)){ |
| | | |
| | | //stream流操作records,返回一个id的集合 |
| | | Set<String> productIds = records.stream().map(ApiProductVo::getId).collect(Collectors.toSet()); |
| | | |
| | | List<AiProductDependency> aiProductDependencies = aiProductDependencyService.selectListByProductIds(productIds); |
| | | if (CollUtil.isNotEmpty( aiProductDependencies)){ |
| | | //stream流操作aiProductDependencies,返回一个Map<targetProductId,AiProductDependency> |
| | | Map<String, AiProductDependency> targetProductIdAiProductDependencyMap = aiProductDependencies.stream().collect(Collectors.toMap(AiProductDependency::getTargetProductId, aiProductDependency -> aiProductDependency)); |
| | | for (ApiProductVo record : records){ |
| | | if (targetProductIdAiProductDependencyMap.containsKey(record.getId())){ |
| | | record.setState(0); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (StrUtil.isNotEmpty(dto.getMemberId())){ |
| | | List<AiMemberProductUnlock> aiMemberProductUnlocks = aiMemberProductUnlockService.selectListByMemberId(dto.getMemberId()); |
| | | if (CollUtil.isNotEmpty( aiMemberProductUnlocks)){ |
| | | //stream流操作aiMemberProductUnlocks,返回一个Map<targetProductId,AiMemberProductUnlock> |
| | | Map<String, AiMemberProductUnlock> aiMemberProductUnlocksMap = aiMemberProductUnlocks.stream().collect(Collectors.toMap(AiMemberProductUnlock::getProductId, aiMemberProductUnlock -> aiMemberProductUnlock)); |
| | | for (ApiProductVo record : records){ |
| | | if (aiMemberProductUnlocksMap.containsKey(record.getId())){ |
| | | record.setState(1); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return new FebsResponse().success().data(pageListByQuery); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse productInfo(ApiProductInfoDto dto) { |
| | | |
| | | ApiProductInfoVo vo = new ApiProductInfoVo(); |
| | | String id = dto.getId(); |
| | | AiProduct entity = this.getById(id); |
| | | ApiProductInfoVo vo = new ApiProductInfoVo(); |
| | | if (ObjectUtil.isNull( entity)){ |
| | | throw new FebsException("产品不存在"); |
| | | } |
| | | vo.setId(entity.getId()); |
| | | vo.setProductCategoryId(entity.getProductCategoryId()); |
| | | vo.setName(entity.getName()); |
| | |
| | | List<ApiProductPointVo> apiProductPointVos = aiProductPointService.listByProductId(id); |
| | | return new FebsResponse().success().data(apiProductPointVos); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiProduct> getProductListByQuery(LambdaQueryWrapper<AiProduct> productQuery) { |
| | | return aiProductMapper.selectList(productQuery); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse translate(ApiTranslateDto dto) { |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | HashMap<Object, Object> map = new HashMap<>(); |
| | | Generation gen = new Generation(); |
| | | Message userMsg = Message.builder() |
| | | .role(Role.USER.getValue()) |
| | | .content(dto.getContent()) |
| | | .build(); |
| | | TranslationOptions options = TranslationOptions.builder() |
| | | .sourceLang(dto.getSourceLang()) |
| | | .targetLang(dto.getTargetLang()) |
| | | .build(); |
| | | GenerationParam param = GenerationParam.builder() |
| | | // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx") |
| | | .apiKey("sk-babdcf8799144134915cee2683794b2f") |
| | | .model("qwen-mt-plus") |
| | | .messages(Collections.singletonList(userMsg)) |
| | | .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| | | .translationOptions(options) |
| | | .build(); |
| | | try { |
| | | GenerationResult result = gen.call(param); |
| | | String content = result.getOutput().getChoices().get(0).getMessage().getContent(); |
| | | map.put("content", dto.getContent()); |
| | | map.put("translate", content); |
| | | // 计算翻译耗时 |
| | | long endTime = System.currentTimeMillis(); |
| | | long duration = endTime - startTime; |
| | | map.put("durationMs", duration + "ms"); |
| | | } catch (NoApiKeyException e) { |
| | | e.printStackTrace(); |
| | | } catch (InputRequiredException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return new FebsResponse().success().data(map); |
| | | } |
| | | |
| | | } |