| | |
| | | import cc.mrbird.febs.ai.mapper.AiMemberMapper; |
| | | import cc.mrbird.febs.ai.req.talk.AiTalkAnswerStream; |
| | | import cc.mrbird.febs.ai.res.memberTalk.ApiMemberTalkStreamVo; |
| | | import cc.mrbird.febs.ai.strategy.LlmStrategyEnum; |
| | | import cc.mrbird.febs.ai.strategy.LlmStrategyFactory; |
| | | import cc.mrbird.febs.ai.strategy.param.LlmStrategyDto; |
| | | import cc.mrbird.febs.ai.utils.UUID; |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | |
| | | import com.alibaba.dashscope.exception.NoApiKeyException; |
| | | import reactor.core.publisher.Flux; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author Administrator |
| | |
| | | private final MallMemberMapper mallMemberMapper; |
| | | private final AiMemberMapper aiMemberMapper; |
| | | private final RedisUtils redisUtils; |
| | | private final LlmStrategyFactory llmStrategyFactory; |
| | | @ApiOperation(value = "登录测试", notes = "登录测试") |
| | | @GetMapping(value = "/login") |
| | | public FebsResponse info() { |
| | |
| | | if (StrUtil.isEmpty(dto.getQuestion())){ |
| | | return Flux.just(new FebsResponse().fail().message("请输入问题")); |
| | | } |
| | | String question = dto.getQuestion(); |
| | | String prompt = dto.getPrompt() ; |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | Flowable<GenerationResult> result; |
| | | try { |
| | | result = callWithMessage(); |
| | | result = callWithMessageStream(question,prompt); |
| | | } catch (NoApiKeyException | InputRequiredException e) { |
| | | e.printStackTrace(); |
| | | return Flux.just(new FebsResponse().fail().message("调用AI服务失败: " + e.getMessage())); |
| | |
| | | .map(message -> { |
| | | String content = message.getOutput().getChoices().get(0).getMessage().getContent(); |
| | | System.out.print(content); |
| | | return new FebsResponse().success().message(content); |
| | | return new FebsResponse().success().data(content); |
| | | }) |
| | | .doOnComplete(() -> { |
| | | long endTime = System.currentTimeMillis(); |
| | |
| | | } |
| | | |
| | | |
| | | public static Flowable<GenerationResult> callWithMessage() throws NoApiKeyException, InputRequiredException { |
| | | |
| | | @ApiOperation("提问AI(流式)V3") |
| | | @ApiResponses({ |
| | | @ApiResponse(code = 200, message = "流式响应", response = ApiMemberTalkStreamVo.class), |
| | | }) |
| | | @PostMapping("/answerStreamV3") |
| | | public Flux<FebsResponse> answerStreamV3(@RequestBody @Validated AiTalkAnswerStream dto) { |
| | | if (StrUtil.isEmpty(dto.getQuestion())){ |
| | | return Flux.just(new FebsResponse().fail().message("请输入问题")); |
| | | } |
| | | |
| | | ArrayList<LlmStrategyDto> llmStrategyDtoList = new ArrayList<>(); |
| | | if (dto.getPrompt() != null){ |
| | | LlmStrategyDto llmStrategyDto = new LlmStrategyDto(); |
| | | llmStrategyDto.setRole(Role.SYSTEM.getValue()); |
| | | llmStrategyDto.setContent(dto.getPrompt()); |
| | | llmStrategyDtoList.add(llmStrategyDto); |
| | | } |
| | | if (dto.getQuestion() != null){ |
| | | LlmStrategyDto llmStrategyDto = new LlmStrategyDto(); |
| | | llmStrategyDto.setRole(Role.USER.getValue()); |
| | | llmStrategyDto.setContent(dto.getQuestion()); |
| | | llmStrategyDtoList.add(llmStrategyDto); |
| | | } |
| | | String modelName = LlmStrategyEnum.getName(dto.getType()); |
| | | |
| | | return llmStrategyFactory.getCalculationStrategyMap().get(modelName).llmInvokeStreaming(llmStrategyDtoList); |
| | | } |
| | | |
| | | |
| | | |
| | | @ApiOperation("提问AI(非流式响应)V4") |
| | | @ApiResponses({ |
| | | @ApiResponse(code = 200, message = "非流式响应", response = ApiMemberTalkStreamVo.class), |
| | | }) |
| | | @PostMapping("/answerStreamV4") |
| | | public FebsResponse answerStreamV4(@RequestBody @Validated AiTalkAnswerStream dto) { |
| | | if (StrUtil.isEmpty(dto.getQuestion())){ |
| | | return new FebsResponse().fail().message("请输入问题"); |
| | | } |
| | | ArrayList<LlmStrategyDto> llmStrategyDtoList = new ArrayList<>(); |
| | | if (dto.getPrompt() != null){ |
| | | LlmStrategyDto llmStrategyDto = new LlmStrategyDto(); |
| | | llmStrategyDto.setRole(Role.SYSTEM.getValue()); |
| | | llmStrategyDto.setContent(dto.getPrompt()); |
| | | llmStrategyDtoList.add(llmStrategyDto); |
| | | } |
| | | if (dto.getQuestion() != null){ |
| | | LlmStrategyDto llmStrategyDto = new LlmStrategyDto(); |
| | | llmStrategyDto.setRole(Role.USER.getValue()); |
| | | llmStrategyDto.setContent(dto.getQuestion()); |
| | | llmStrategyDtoList.add(llmStrategyDto); |
| | | } |
| | | String modelName = LlmStrategyEnum.getName(dto.getType()); |
| | | |
| | | return llmStrategyFactory.getCalculationStrategyMap().get(modelName).llmInvokeNonStreaming(llmStrategyDtoList); |
| | | } |
| | | |
| | | |
| | | public static Flowable<GenerationResult> callWithMessageStream(String question,String prompt) throws NoApiKeyException, InputRequiredException { |
| | | Generation gen = new Generation(); |
| | | Message systemMsg = Message.builder() |
| | | .role(Role.SYSTEM.getValue()) |
| | | .content("You are a helpful assistant.") |
| | | .content(prompt) |
| | | .build(); |
| | | Message userMsg = Message.builder() |
| | | .role(Role.USER.getValue()) |
| | | .content("你是谁?") |
| | | .content(question) |
| | | .build(); |
| | | GenerationParam param = GenerationParam.builder() |
| | | // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx") |
| | |
| | | .build(); |
| | | return gen.streamCall(param); |
| | | } |
| | | |
| | | |
| | | public static GenerationResult callWithMessage(String question,String prompt) throws ApiException, NoApiKeyException, InputRequiredException { |
| | | Generation gen = new Generation(); |
| | | Message systemMsg = Message.builder() |
| | | .role(Role.SYSTEM.getValue()) |
| | | .content(prompt) |
| | | .build(); |
| | | Message userMsg = Message.builder() |
| | | .role(Role.USER.getValue()) |
| | | .content(question) |
| | | .build(); |
| | | 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") |
| | | .messages(Arrays.asList(systemMsg, userMsg)) |
| | | .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| | | .build(); |
| | | return gen.call(param); |
| | | } |
| | | public static void main(String[] args) { |
| | | //定义一个开始时间为启动这个main方法的开始时间,用于计算运行时间 |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | Flowable<GenerationResult> result = null; |
| | | String question = "干的漂亮"; |
| | | String prompt = "{\"task\": \"作为专业的表扬能力评估分析师,分析question和用户输入内容,对用户输入内容进行评估与指导。\",\"question\":\"当资深骨干员工在突发场景中迅速响应并解决了持续一周的客户投诉达成客户满意度成就时,作为上级的你应如何表达赞赏?\",\"highlight\": \"识别符合表扬原则的积极策略,关注角色视角适配性、员工类型特点、具体行为描述、情感表达真诚度与个性化认可方式\"}"; |
| | | String prompt1 = "{\"task\": \"作为专业的表扬能力评估分析师,分析question和用户输入内容,对用户输入内容进行评估与指导。\",\"question\":\"当资深骨干员工在突发场景中迅速响应并解决了持续一周的客户投诉达成客户满意度成就时,作为上级的你应如何表达赞赏?\",\"suggestion\": \"提供针对性优化方案,涵盖场景要素匹配度提升(如正式场合需增加公开认可环节)、成就类型适配改进(如创新贡献应强调思维突破而非仅结果)、具体维度强化方向(如增加 STAR 法则中的 Result 部分描述)、跨文化 / 远程场景特殊策略(如虚拟团队可采用异步表扬 + 公开认可组合)\"}"; |
| | | String prompt2 = "{\"task\": \"作为专业的表扬能力评估分析师,分析question和用户输入内容,对用户输入内容进行评估与指导。\",\"question\":\"当资深骨干员工在突发场景中迅速响应并解决了持续一周的客户投诉达成客户满意度成就时,作为上级的你应如何表达赞赏?\",\"reference_answer\": \"根据问题用户的回答,生成参考示例\"}"; |
| | | String prompt3 = "{\"task\": \"作为专业的表扬能力评估分析师,分析question和用户输入内容,对用户输入内容进行评估与指导。\",\"question\":\"当资深骨干员工在突发场景中迅速响应并解决了持续一周的客户投诉达成客户满意度成就时,作为上级的你应如何表达赞赏?\",\"key_knowledge\": \"结合理论与实践深度解析\"}"; |
| | | GenerationResult result = null; |
| | | try { |
| | | result = callWithMessage(); |
| | | result = callWithMessage(question, prompt); |
| | | } catch (NoApiKeyException e) { |
| | | e.printStackTrace(); |
| | | } catch (InputRequiredException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Flux.from(result) |
| | | System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent()); |
| | | long endTime = System.currentTimeMillis(); |
| | | System.out.println("运行时间:" + (endTime - startTime) + "毫秒"); |
| | | long startTimeStream = System.currentTimeMillis(); |
| | | Flowable<GenerationResult> resultStream = null; |
| | | try { |
| | | resultStream = callWithMessageStream(question, prompt); |
| | | } catch (NoApiKeyException e) { |
| | | e.printStackTrace(); |
| | | } catch (InputRequiredException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Flux.from(resultStream) |
| | | .doOnNext(message -> { |
| | | String content = message.getOutput().getChoices().get(0).getMessage().getContent(); |
| | | System.out.print(content); |
| | | }) |
| | | .subscribe(); |
| | | long endTime = System.currentTimeMillis(); |
| | | System.out.println("运行时间:" + (endTime - startTime) + "毫秒"); |
| | | // result.blockingForEach(item -> { |
| | | //// log.info("item: {}", JSONUtil.toJsonStr(item)); |
| | | // // 提取文本内容 |
| | | // if (item.getOutput() != null && item.getOutput().getChoices() != null) { |
| | | // item.getOutput().getChoices().forEach(choice -> { |
| | | // if (choice.getMessage() != null) { |
| | | // // 根据实际 API 文档确定使用哪个方法 |
| | | //// if (choice.getMessage().getContents() != null) { |
| | | //// try { |
| | | //// choice.getMessage().getContents().forEach(content -> { |
| | | //// if (content != null) { |
| | | //// System.out.print(content.getType()); |
| | | //// } |
| | | //// }); |
| | | //// } catch (NullPointerException e) { |
| | | //// log.error("Error processing contents", e); |
| | | //// } |
| | | //// } |
| | | // if (choice.getMessage().getContent() != null) { |
| | | // System.out.print(choice.getMessage().getContent()); |
| | | // } |
| | | // } |
| | | // }); |
| | | // } |
| | | // }); |
| | | long endTimeStream = System.currentTimeMillis(); |
| | | System.out.println("运行时间:" + (endTimeStream - startTimeStream) + "毫秒"); |
| | | } |
| | | } |