| | |
| | | package cc.mrbird.febs.ai.strategy.Impl; |
| | | |
| | | import cc.mrbird.febs.ai.strategy.LlmStrategyService; |
| | | import cc.mrbird.febs.ai.strategy.enumerates.LlmStrategyContextEnum; |
| | | 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 cn.hutool.json.JSONUtil; |
| | | import com.alibaba.dashscope.aigc.generation.Generation; |
| | | import com.alibaba.dashscope.aigc.generation.GenerationParam; |
| | | import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| | |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @Component("AliLlmStrategyService") |
| | |
| | | private GenerationParam generationParam; |
| | | |
| | | private static final String apiKey = "sk-babdcf8799144134915cee2683794b2f"; |
| | | private static final String model = "qwen-plus"; |
| | | // private static final String model = "qwen-plus"; |
| | | private static final String model = "qwen3-14b-ft-202509031002-7446"; |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | |
| | | List<Message> messages = getMessages(dto); |
| | | Generation gen = new Generation(); |
| | | generationParam.setMessages(messages); |
| | | generationParam.setEnableThinking( false); |
| | | FebsResponse febsResponse = new FebsResponse(); |
| | | try { |
| | | GenerationResult result = gen.call(generationParam); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Flux<FebsResponse> llmInvokeStreaming(List<LlmStrategyDto> dto) { |
| | | public Flux<FebsResponse> llmInvokeStreamingWithThink(List<LlmStrategyDto> dto) { |
| | | if (CollUtil.isEmpty(dto)){ |
| | | throw new FebsException("百炼大模型初始化异常"); |
| | | } |
| | |
| | | Generation gen = new Generation(); |
| | | generationParam.setMessages(messages); |
| | | generationParam.setResultFormat(GenerationParam.ResultFormat.MESSAGE); |
| | | generationParam.setIncrementalOutput(true); |
| | | generationParam.setEnableThinking( true); |
| | | generationParam.setIncrementalOutput( true); |
| | | Flowable<GenerationResult> result; |
| | | try { |
| | | result = gen.streamCall(generationParam); |
| | |
| | | |
| | | return Flux.from(result) |
| | | .map(message -> { |
| | | String content = message.getOutput().getChoices().get(0).getMessage().getContent(); |
| | | return new FebsResponse().success().data(content); |
| | | HashMap<String, String> stringStringHashMap = new HashMap<>(); |
| | | if (StrUtil.isNotEmpty(message.getOutput().getChoices().get(0).getMessage().getReasoningContent())){ |
| | | stringStringHashMap.put(LlmStrategyContextEnum.THINK.name(),message.getOutput().getChoices().get(0).getMessage().getReasoningContent()); |
| | | |
| | | System.out.print(message.getOutput().getChoices().get(0).getMessage().getReasoningContent()); |
| | | } |
| | | if (StrUtil.isNotEmpty(message.getOutput().getChoices().get(0).getMessage().getContent())){ |
| | | stringStringHashMap.put(LlmStrategyContextEnum.CONTENT.name(),message.getOutput().getChoices().get(0).getMessage().getContent()); |
| | | System.out.print(message.getOutput().getChoices().get(0).getMessage().getContent()); |
| | | } |
| | | return new FebsResponse().success().data(stringStringHashMap); |
| | | }) |
| | | .doOnComplete(() -> { |
| | | long endTime = System.currentTimeMillis(); |
| | | System.out.println("百炼大模型输出:" + (endTime - startTime) + "毫秒"); |
| | | }) |
| | | .doOnError(error -> { |
| | | throw new FebsException(StrUtil.format("百炼大模型输出失败:{}",error)); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public Flux<FebsResponse> llmInvokeStreamingNoThink(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(messages); |
| | | generationParam.setResultFormat(GenerationParam.ResultFormat.MESSAGE); |
| | | generationParam.setIncrementalOutput( true); |
| | | generationParam.setEnableThinking( false); |
| | | Flowable<GenerationResult> result; |
| | | try { |
| | | result = gen.streamCall(generationParam); |
| | | } catch (NoApiKeyException | InputRequiredException e) { |
| | | throw new FebsException(StrUtil.format("百炼大模型输出失败:{}",e.getMessage())); |
| | | } |
| | | |
| | | return Flux.from(result) |
| | | .map(message -> { |
| | | HashMap<String, String> stringStringHashMap = new HashMap<>(); |
| | | if (StrUtil.isNotEmpty(message.getOutput().getChoices().get(0).getMessage().getContent())){ |
| | | String content = message.getOutput().getChoices().get(0).getMessage().getContent(); |
| | | System.out.print( content); |
| | | stringStringHashMap.put(LlmStrategyContextEnum.CONTENT.name(),content); |
| | | } |
| | | return new FebsResponse().success().data(stringStringHashMap); |
| | | }) |
| | | .doOnComplete(() -> { |
| | | long endTime = System.currentTimeMillis(); |