| | |
| | | import cc.mrbird.febs.ai.strategy.param.LlmStrategyDto; |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.yinhe.req.AiRequestDto; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | |
| | | @Component("AliApplicationLlmStrategyServiceImpl") |
| | | public class AliApplicationLlmStrategyServiceImpl implements LlmStrategyService { |
| | | |
| | | private static final String apiKey = "sk-babdcf8799144134915cee2683794b2f"; |
| | | private static final String apiKey = "sk-4b97b556ba7c4350a41f2856f75b9377"; |
| | | |
| | | private final String bizParam_1 = "prompt_ai_system"; |
| | | private final String bizParam_2 = "question"; |
| | |
| | | return bizParamsMap; |
| | | } |
| | | |
| | | private List<Message> getMessages(List<LlmStrategyDto> dto) { |
| | | List<Message> messages = new ArrayList<>(); |
| | | for (LlmStrategyDto item : dto){ |
| | | if (StrUtil.equals(item.getRole(),AiTypeEnum.MESSAGES.getName())){ |
| | | List<LlmStrategyDto> messagesList = item.getMessages(); |
| | | for (LlmStrategyDto dtoItem : messagesList){ |
| | | 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; |
| | | } |
| | | |
| | | private String getQuery(List<LlmStrategyDto> dto) { |
| | | String query = null; |
| | | for (LlmStrategyDto dtoItem : dto){ |
| | |
| | | if (StrUtil.equals(dtoItem.getRole(),Role.TOOL.getValue())){ |
| | | int code = Integer.parseInt(dtoItem.getContent()); |
| | | appId = LlmApplicationAppIdEnum.HIGH_LIGHT.getAppIdByCode(code); |
| | | break; |
| | | } |
| | | } |
| | | return appId; |
| | | } |
| | | |
| | | private String getAppIdV2(List<LlmStrategyDto> dto) { |
| | | String appId = null; |
| | | for (LlmStrategyDto dtoItem : dto){ |
| | | if (StrUtil.equals(dtoItem.getRole(),Role.TOOL.getValue())){ |
| | | appId = dtoItem.getContent(); |
| | | break; |
| | | } |
| | | } |
| | |
| | | } |
| | | HashMap prompt = getPrompt(dto); |
| | | String query = getQuery(dto); |
| | | String appId = getAppId(dto); |
| | | // String appId = getAppId(dto); |
| | | String appId = getAppIdV2(dto); |
| | | if (prompt == null || prompt.size() == 0){ |
| | | throw new FebsException("百炼工作流初始化异常"); |
| | | } |
| | |
| | | if (appId == null){ |
| | | throw new FebsException("百炼工作流初始化异常"); |
| | | } |
| | | |
| | | List<Message> messages = getMessages(dto); |
| | | long startTime = System.currentTimeMillis(); |
| | | ApplicationParam param = ApplicationParam.builder() |
| | | // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 |
| | |
| | | .appId(appId) //替换为实际的应用 ID |
| | | .flowStreamMode(FlowStreamMode.MESSAGE_FORMAT) |
| | | .prompt(query) |
| | | .messages( messages) |
| | | .bizParams(JsonUtils.toJsonObject( prompt)) |
| | | .build(); |
| | | |
| | |
| | | throw new FebsException(StrUtil.format("百炼工作流输出失败:{}",error)); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public Flux<FebsResponse> llmInvokeStreamingNoThink(AiRequestDto aiRequestDto) { |
| | | return null; |
| | | } |
| | | } |