| | |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.dashscope.app.Application; |
| | | import com.alibaba.dashscope.app.ApplicationParam; |
| | | import com.alibaba.dashscope.app.ApplicationResult; |
| | | import com.alibaba.dashscope.app.FlowStreamMode; |
| | | import com.alibaba.dashscope.exception.InputRequiredException; |
| | | import com.alibaba.dashscope.exception.NoApiKeyException; |
| | | import com.alibaba.dashscope.utils.JsonUtils; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | bizParams.put(bizParam_3,dto.getJsonFormat()); |
| | | String query = dto.getQuery(); |
| | | long startTime = System.currentTimeMillis(); |
| | | // ApplicationParam param = ApplicationParam.builder() |
| | | // // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 |
| | | // .apiKey(apiKey) |
| | | // .appId(appId) //替换为实际的应用 ID |
| | | // .flowStreamMode(FlowStreamMode.MESSAGE_FORMAT) |
| | | // .prompt(query) |
| | | // .bizParams(JsonUtils.toJsonObject( bizParams)) |
| | | // .build(); |
| | | // |
| | | // Application application = new Application(); |
| | | // Flowable<ApplicationResult> result; |
| | | // try { |
| | | // result = application.streamCall(param); |
| | | // } catch (NoApiKeyException | InputRequiredException e) { |
| | | // throw new FebsException(StrUtil.format("百炼工作流输出失败:{}",e.getMessage())); |
| | | // } |
| | | // |
| | | // return Flux.from(result) |
| | | // .map(message -> { |
| | | // HashMap<String, String> stringStringHashMap = new HashMap<>(); |
| | | // if (!message.getOutput().getFinishReason().equals("stop")){ |
| | | // stringStringHashMap.put(LlmStrategyContextEnum.CONTENT.name(),message.getOutput().getWorkflowMessage().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)); |
| | | // }); |
| | | return null; |
| | | ApplicationParam param = ApplicationParam.builder() |
| | | // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 |
| | | .apiKey(apiKey) |
| | | .appId(appId) //替换为实际的应用 ID |
| | | .flowStreamMode(FlowStreamMode.MESSAGE_FORMAT) |
| | | .prompt(query) |
| | | .bizParams(JsonUtils.toJsonObject( bizParams)) |
| | | .build(); |
| | | |
| | | Application application = new Application(); |
| | | ApplicationResult result = null; |
| | | try { |
| | | result = application.call(param); |
| | | } catch (NoApiKeyException e) { |
| | | throw new FebsException("百炼工作流调用异常"); |
| | | } catch (InputRequiredException e) { |
| | | throw new FebsException("百炼工作流调用异常"); |
| | | } |
| | | return result.getOutput().getText(); |
| | | } |
| | | } |