Commit 1ea94d9245c33e4f21bcbbf8511375836df980df
1 parent
238ef898
注释 chat controller,现在没用了,需要修改
Showing
1 changed file
with
77 additions
and
77 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/ChatController.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.admin; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | ||
| 4 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 5 | -import cn.iocoder.yudao.module.ai.ErrorCodeConstants; | ||
| 6 | -import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO; | ||
| 7 | -import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; | ||
| 8 | -import io.swagger.v3.oas.annotations.Operation; | ||
| 9 | -import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 10 | -import jakarta.servlet.http.HttpServletResponse; | ||
| 11 | -import lombok.extern.slf4j.Slf4j; | ||
| 12 | -import org.springframework.ai.chat.ChatClient; | ||
| 13 | -import org.springframework.ai.chat.ChatResponse; | ||
| 14 | -import org.springframework.ai.chat.prompt.Prompt; | ||
| 15 | -import org.springframework.ai.openai.OpenAiChatClient; | ||
| 16 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 17 | -import org.springframework.context.ApplicationContext; | ||
| 18 | -import org.springframework.validation.annotation.Validated; | ||
| 19 | -import org.springframework.web.bind.annotation.PostMapping; | ||
| 20 | -import org.springframework.web.bind.annotation.RequestBody; | ||
| 21 | -import org.springframework.web.bind.annotation.RequestMapping; | ||
| 22 | -import org.springframework.web.bind.annotation.RestController; | ||
| 23 | -import reactor.core.publisher.Flux; | ||
| 24 | - | ||
| 25 | -import java.util.function.Consumer; | ||
| 26 | - | ||
| 27 | -// TODO done @fansili:有了 swagger 注释,就不用类注释了 | ||
| 28 | -@Tag(name = "AI模块") | ||
| 29 | -@RestController | ||
| 30 | -@RequestMapping("/ai-api") | ||
| 31 | -@Slf4j | ||
| 32 | -public class ChatController { | ||
| 33 | - | ||
| 34 | - @Autowired | ||
| 35 | - private ApplicationContext applicationContext; | ||
| 36 | - | ||
| 37 | - @PostMapping("/chat") | ||
| 38 | - @Operation(summary = "对话聊天", description = "简单的ai聊天") | ||
| 39 | - public CommonResult chat(@RequestBody @Validated AiChatReqVO reqVO) { | ||
| 40 | - ChatClient chatClient = getChatClient(reqVO.getAiModel()); | ||
| 41 | - String res; | ||
| 42 | - try { | ||
| 43 | - res = chatClient.call(reqVO.getPrompt()); | ||
| 44 | - } catch (Exception e) { | ||
| 45 | - res = e.getMessage(); | ||
| 46 | - } | ||
| 47 | - return CommonResult.success(res); | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - @PostMapping("/chatStream") | ||
| 51 | - @Operation(summary = "对话聊天chatStream", description = "简单的ai聊天") | ||
| 52 | - public CommonResult chatStream(HttpServletResponse response, @RequestBody @Validated AiChatReqVO reqVO) throws InterruptedException { | ||
| 53 | - OpenAiChatClient chatClient = applicationContext.getBean(OpenAiChatClient.class); | ||
| 54 | - Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getPrompt())); | ||
| 55 | - chatResponse.subscribe(new Consumer<ChatResponse>() { | ||
| 56 | - @Override | ||
| 57 | - public void accept(ChatResponse chatResponse) { | ||
| 58 | - System.err.println(chatResponse.getResults().get(0).getOutput().getContent()); | ||
| 59 | - } | ||
| 60 | - }); | ||
| 61 | - return CommonResult.success(null); | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - /** | ||
| 65 | - * 根据 ai模型 获取对于的 模型实现类 | ||
| 66 | - * | ||
| 67 | - * @param aiModelEnum | ||
| 68 | - * @return | ||
| 69 | - */ | ||
| 70 | - private ChatClient getChatClient(OpenAiModelEnum aiModelEnum) { | ||
| 71 | - if (OpenAiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) { | ||
| 72 | - return applicationContext.getBean(OpenAiChatClient.class); | ||
| 73 | - } | ||
| 74 | - // AI模型暂不支持 | ||
| 75 | - throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODULE_NOT_SUPPORTED); | ||
| 76 | - } | ||
| 77 | -} | 1 | +//package cn.iocoder.yudao.module.ai.controller.admin; |
| 2 | +// | ||
| 3 | +//import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | ||
| 4 | +//import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 5 | +//import cn.iocoder.yudao.module.ai.ErrorCodeConstants; | ||
| 6 | +//import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO; | ||
| 7 | +//import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; | ||
| 8 | +//import io.swagger.v3.oas.annotations.Operation; | ||
| 9 | +//import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 10 | +//import jakarta.servlet.http.HttpServletResponse; | ||
| 11 | +//import lombok.extern.slf4j.Slf4j; | ||
| 12 | +//import org.springframework.ai.chat.ChatClient; | ||
| 13 | +//import org.springframework.ai.chat.ChatResponse; | ||
| 14 | +//import org.springframework.ai.chat.prompt.Prompt; | ||
| 15 | +//import org.springframework.ai.openai.OpenAiChatClient; | ||
| 16 | +//import org.springframework.beans.factory.annotation.Autowired; | ||
| 17 | +//import org.springframework.context.ApplicationContext; | ||
| 18 | +//import org.springframework.validation.annotation.Validated; | ||
| 19 | +//import org.springframework.web.bind.annotation.PostMapping; | ||
| 20 | +//import org.springframework.web.bind.annotation.RequestBody; | ||
| 21 | +//import org.springframework.web.bind.annotation.RequestMapping; | ||
| 22 | +//import org.springframework.web.bind.annotation.RestController; | ||
| 23 | +//import reactor.core.publisher.Flux; | ||
| 24 | +// | ||
| 25 | +//import java.util.function.Consumer; | ||
| 26 | +// | ||
| 27 | +//// TODO done @fansili:有了 swagger 注释,就不用类注释了 | ||
| 28 | +//@Tag(name = "AI模块") | ||
| 29 | +//@RestController | ||
| 30 | +//@RequestMapping("/ai-api") | ||
| 31 | +//@Slf4j | ||
| 32 | +//public class ChatController { | ||
| 33 | +// | ||
| 34 | +// @Autowired | ||
| 35 | +// private ApplicationContext applicationContext; | ||
| 36 | +// | ||
| 37 | +// @PostMapping("/chat") | ||
| 38 | +// @Operation(summary = "对话聊天", description = "简单的ai聊天") | ||
| 39 | +// public CommonResult chat(@RequestBody @Validated AiChatReqVO reqVO) { | ||
| 40 | +// ChatClient chatClient = getChatClient(reqVO.getAiModel()); | ||
| 41 | +// String res; | ||
| 42 | +// try { | ||
| 43 | +// res = chatClient.call(reqVO.getPrompt()); | ||
| 44 | +// } catch (Exception e) { | ||
| 45 | +// res = e.getMessage(); | ||
| 46 | +// } | ||
| 47 | +// return CommonResult.success(res); | ||
| 48 | +// } | ||
| 49 | +// | ||
| 50 | +// @PostMapping("/chatStream") | ||
| 51 | +// @Operation(summary = "对话聊天chatStream", description = "简单的ai聊天") | ||
| 52 | +// public CommonResult chatStream(HttpServletResponse response, @RequestBody @Validated AiChatReqVO reqVO) throws InterruptedException { | ||
| 53 | +// OpenAiChatClient chatClient = applicationContext.getBean(OpenAiChatClient.class); | ||
| 54 | +// Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getPrompt())); | ||
| 55 | +// chatResponse.subscribe(new Consumer<ChatResponse>() { | ||
| 56 | +// @Override | ||
| 57 | +// public void accept(ChatResponse chatResponse) { | ||
| 58 | +// System.err.println(chatResponse.getResults().get(0).getOutput().getContent()); | ||
| 59 | +// } | ||
| 60 | +// }); | ||
| 61 | +// return CommonResult.success(null); | ||
| 62 | +// } | ||
| 63 | +// | ||
| 64 | +// /** | ||
| 65 | +// * 根据 ai模型 获取对于的 模型实现类 | ||
| 66 | +// * | ||
| 67 | +// * @param aiModelEnum | ||
| 68 | +// * @return | ||
| 69 | +// */ | ||
| 70 | +// private ChatClient getChatClient(OpenAiModelEnum aiModelEnum) { | ||
| 71 | +// if (OpenAiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) { | ||
| 72 | +// return applicationContext.getBean(OpenAiChatClient.class); | ||
| 73 | +// } | ||
| 74 | +// // AI模型暂不支持 | ||
| 75 | +// throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODULE_NOT_SUPPORTED); | ||
| 76 | +// } | ||
| 77 | +//} |