| | |
| | | import cc.mrbird.febs.ai.strategy.param.LlmStrategyDto; |
| | | 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.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.common.Message; |
| | | import com.alibaba.dashscope.common.Role; |
| | | import com.alibaba.dashscope.exception.InputRequiredException; |
| | | import com.alibaba.dashscope.exception.NoApiKeyException; |
| | | import io.reactivex.Flowable; |
| | |
| | | import reactor.core.publisher.Flux; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | @Component("AliLlmStrategyService") |
| | | public class AliLlmStrategyServiceImpl implements LlmStrategyService { |
| | |
| | | .build(); |
| | | } |
| | | |
| | | private List<Message> getMessages(List<LlmStrategyDto> dto) { |
| | | List<Message> messages = new ArrayList<>(); |
| | | for (LlmStrategyDto dtoItem : dto){ |
| | | if (StrUtil.equals(dtoItem.getRole(),Role.SYSTEM.getValue())){ |
| | | messages.add(Message.builder() |
| | | .role(Role.SYSTEM.getValue()) |
| | | .content(dtoItem.getContent()) |
| | | .build()); |
| | | } |
| | | if (StrUtil.equals(dtoItem.getRole(),Role.USER.getValue())){ |
| | | messages.add(Message.builder() |
| | | .role(Role.USER.getValue()) |
| | | .content(dtoItem.getContent()) |
| | | .build()); |
| | | } |
| | | if (StrUtil.equals(dtoItem.getRole(),Role.ASSISTANT.getValue())){ |
| | | messages.add(Message.builder() |
| | | .role(Role.ASSISTANT.getValue()) |
| | | .content(dtoItem.getContent()) |
| | | .build()); |
| | | } |
| | | } |
| | | return messages; |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse llmInvokeNonStreaming(LlmStrategyDto dto) { |
| | | public FebsResponse llmInvokeNonStreaming(List<LlmStrategyDto> dto) { |
| | | if (CollUtil.isEmpty(dto)){ |
| | | throw new FebsException("百炼大模型初始化异常"); |
| | | } |
| | | List<Message> messages = getMessages(dto); |
| | | Generation gen = new Generation(); |
| | | generationParam.setMessages(dto.getMessages()); |
| | | generationParam.setMessages(messages); |
| | | FebsResponse febsResponse = new FebsResponse(); |
| | | try { |
| | | GenerationResult result = gen.call(generationParam); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Flux<FebsResponse> llmInvokeStreaming(LlmStrategyDto dto) { |
| | | public Flux<FebsResponse> llmInvokeStreaming(List<LlmStrategyDto> dto) { |
| | | if (CollUtil.isEmpty(dto)){ |
| | | throw new FebsException("百炼大模型初始化异常"); |
| | | } |
| | | List<Message> messages = getMessages(dto); |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | Generation gen = new Generation(); |
| | | generationParam.setMessages(dto.getMessages()); |
| | | generationParam.setMessages(messages); |
| | | generationParam.setResultFormat(GenerationParam.ResultFormat.MESSAGE); |
| | | generationParam.setIncrementalOutput(true); |
| | | Flowable<GenerationResult> result; |