| | |
| | | import org.springframework.stereotype.Component; |
| | | import reactor.core.publisher.Flux; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | |
| | | @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(); |
| | | } |
| | | |
| | | @Override |
| | | public FebsResponse llmInvokeNonStreaming(LlmStrategyDto dto) { |
| | | Generation gen = new Generation(); |
| | | generationParam.setMessages(dto.getMessages()); |
| | | 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{ |
| | |
| | | |
| | | 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(dto.getMessages()); |
| | | 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())); |
| | | } |