Administrator
2025-09-01 1f7c85d6c632f50fd145bd8517933bd55202aef1
src/main/java/cc/mrbird/febs/ai/strategy/Impl/AliLlmStrategyServiceImpl.java
@@ -4,33 +4,75 @@
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 org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Component("AliLlmStrategyService")
public class AliLlmStrategyServiceImpl implements LlmStrategyService {
    @Override
    public FebsResponse llmInvokeNonStreaming(LlmStrategyDto dto) {
        Generation gen = new Generation();
        GenerationParam param = GenerationParam.builder()
    private GenerationParam generationParam;
    @PostConstruct
    public void init() {
        this.generationParam = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey("sk-babdcf8799144134915cee2683794b2f")
                // 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(dto.getMessages())
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .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(List<LlmStrategyDto> dto) {
        if (CollUtil.isEmpty(dto)){
            throw new FebsException("百炼大模型初始化异常");
        }
        List<Message> messages = getMessages(dto);
        Generation gen = new Generation();
        generationParam.setMessages(messages);
        FebsResponse febsResponse = new FebsResponse();
        try {
            GenerationResult result = gen.call(param);
            GenerationResult result = gen.call(generationParam);
            if (result != null && result.getOutput() != null && result.getOutput().getChoices().size() > 0){
                febsResponse.success().data(result.getOutput().getChoices().get(0).getMessage().getContent());
            }else{
@@ -45,25 +87,20 @@
    }
    @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 param = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey("sk-babdcf8799144134915cee2683794b2f")
                // 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
//                .model("deepseek-r1")
//                .model("qwen-turbo-0624-ft-202508281725-c2dc")
                .messages(dto.getMessages())
//                .resultFormat(GenerationParam.ResultFormat.TEXT)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true)
                .build();
        generationParam.setMessages(messages);
        generationParam.setResultFormat(GenerationParam.ResultFormat.MESSAGE);
        generationParam.setIncrementalOutput(true);
        Flowable<GenerationResult> result;
        try {
            result = gen.streamCall(param);
            result = gen.streamCall(generationParam);
        } catch (NoApiKeyException | InputRequiredException e) {
            throw new FebsException(StrUtil.format("百炼大模型输出失败:{}",e.getMessage()));
        }