| | |
| | | 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 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.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private final AiProductPointService aiProductPointService; |
| | | private final AiProductDependencyService aiProductDependencyService; |
| | | private final AiMemberProductUnlockService aiMemberProductUnlockService; |
| | | |
| | | |
| | | @Override |
| | | public AiProduct getById(String id) { |
| | |
| | | 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); |
| | | } |
| | | |
| | | } |