Commit a3530555ca82d259fc4cde2f84ece4467a39553f
1 parent
4b5910f9
1、增加 chat 和 chatStream
Showing
17 changed files
with
97 additions
and
70 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/chat/AiChatRoleController.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/AiChatRoleController.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.app.chat; | 1 | +package cn.iocoder.yudao.module.ai.controller; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.module.ai.controller.app.chat.vo.*; | ||
| 5 | import cn.iocoder.yudao.module.ai.service.ChatRoleService; | 4 | import cn.iocoder.yudao.module.ai.service.ChatRoleService; |
| 5 | +import cn.iocoder.yudao.module.ai.vo.*; | ||
| 6 | import io.swagger.v3.oas.annotations.Operation; | 6 | import io.swagger.v3.oas.annotations.Operation; |
| 7 | import lombok.AllArgsConstructor; | 7 | import lombok.AllArgsConstructor; |
| 8 | import org.springframework.validation.annotation.Validated; | 8 | import org.springframework.validation.annotation.Validated; |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/ChatController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.controller; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.exceptions.ExceptionUtil; | ||
| 4 | +import cn.iocoder.yudao.framework.ai.chat.ChatResponse; | ||
| 5 | +import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; | ||
| 6 | +import cn.iocoder.yudao.framework.ai.config.AiClient; | ||
| 7 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 8 | +import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 9 | +import lombok.AllArgsConstructor; | ||
| 10 | +import lombok.extern.slf4j.Slf4j; | ||
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 12 | +import org.springframework.http.MediaType; | ||
| 13 | +import org.springframework.web.bind.annotation.GetMapping; | ||
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 15 | +import org.springframework.web.bind.annotation.RequestParam; | ||
| 16 | +import org.springframework.web.bind.annotation.RestController; | ||
| 17 | +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
| 18 | +import reactor.core.publisher.Flux; | ||
| 19 | + | ||
| 20 | +import java.io.IOException; | ||
| 21 | +import java.util.function.Consumer; | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * @author fansili | ||
| 25 | + * @time 2024/4/13 17:44 | ||
| 26 | + * @since 1.0 | ||
| 27 | + */ | ||
| 28 | +@Tag(name = "AI模块") | ||
| 29 | +@RestController | ||
| 30 | +@RequestMapping("/ai") | ||
| 31 | +@Slf4j | ||
| 32 | +@AllArgsConstructor | ||
| 33 | +public class ChatController { | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + | ||
| 37 | + @Autowired | ||
| 38 | + private AiClient aiClient; | ||
| 39 | + | ||
| 40 | + @GetMapping("/chat") | ||
| 41 | + public CommonResult<String> chat(@RequestParam("prompt") String prompt) { | ||
| 42 | + ChatResponse callRes = aiClient.call(new Prompt(prompt), "qianWen"); | ||
| 43 | + return CommonResult.success(callRes.getResult().getOutput().getContent()); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + // TODO @芋艿:调用这个方法异常,Unable to handle the Spring Security Exception because the response is already committed. | ||
| 47 | + @GetMapping(value = "/chatStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) | ||
| 48 | + public SseEmitter chatStream(@RequestParam("prompt") String prompt) { | ||
| 49 | + Utf8SseEmitter sseEmitter = new Utf8SseEmitter(); | ||
| 50 | + Flux<ChatResponse> streamResponse = aiClient.stream(new Prompt(prompt), "qianWen"); | ||
| 51 | + streamResponse.subscribe( | ||
| 52 | + new Consumer<ChatResponse>() { | ||
| 53 | + @Override | ||
| 54 | + public void accept(ChatResponse chatResponse) { | ||
| 55 | + String content = chatResponse.getResults().get(0).getOutput().getContent(); | ||
| 56 | + try { | ||
| 57 | + sseEmitter.send(content, MediaType.APPLICATION_JSON); | ||
| 58 | + } catch (IOException e) { | ||
| 59 | + log.error("发送异常{}", ExceptionUtil.getMessage(e)); | ||
| 60 | + // 如果不是因为关闭而抛出异常,则重新连接 | ||
| 61 | + sseEmitter.completeWithError(e); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + }, | ||
| 65 | + error -> { | ||
| 66 | + // | ||
| 67 | + log.error("subscribe错误 {}", ExceptionUtil.getMessage(error)); | ||
| 68 | + }, | ||
| 69 | + () -> { | ||
| 70 | + log.info("发送完成!"); | ||
| 71 | + sseEmitter.complete(); | ||
| 72 | + } | ||
| 73 | + ); | ||
| 74 | + return sseEmitter; | ||
| 75 | + } | ||
| 76 | +} |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/ChatController.java deleted
| 1 | -package cn.iocoder.yudao.module.ai.controller.admin.chat; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.ai.chat.ChatResponse; | ||
| 4 | -import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; | ||
| 5 | -import cn.iocoder.yudao.framework.ai.config.AiClient; | ||
| 6 | -import cn.iocoder.yudao.framework.ai.config.YudaoAiClient; | ||
| 7 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 8 | -import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 9 | -import lombok.AllArgsConstructor; | ||
| 10 | -import lombok.extern.slf4j.Slf4j; | ||
| 11 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 12 | -import org.springframework.web.bind.annotation.*; | ||
| 13 | - | ||
| 14 | -/** | ||
| 15 | - * @author fansili | ||
| 16 | - * @since 1.0 | ||
| 17 | - * @time 2024/4/13 17:44 | ||
| 18 | - */ | ||
| 19 | -@Tag(name = "AI模块") | ||
| 20 | -@RestController | ||
| 21 | -@RequestMapping("/admin-api/ai") | ||
| 22 | -@Slf4j | ||
| 23 | -@AllArgsConstructor | ||
| 24 | -public class ChatController { | ||
| 25 | - | ||
| 26 | - private final AiClient aiClient; | ||
| 27 | - | ||
| 28 | - @PostMapping("/chat") | ||
| 29 | - public CommonResult<String> chat(@RequestParam("prompt") String prompt) { | ||
| 30 | - ChatResponse callRes = aiClient.call(new Prompt(prompt), "qianWen"); | ||
| 31 | - return CommonResult.success(callRes.getResult().getOutput().getContent()); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | -} |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/package-info.java deleted
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/package-info.java deleted
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatRoleService.java
| 1 | package cn.iocoder.yudao.module.ai.service; | 1 | package cn.iocoder.yudao.module.ai.service; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.module.ai.controller.app.chat.vo.*; | 4 | +import cn.iocoder.yudao.module.ai.vo.*; |
| 5 | 5 | ||
| 6 | /** | 6 | /** |
| 7 | * chat 角色 | 7 | * chat 角色 |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatRoleServiceImpl.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatRoleServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.ai.service; | 1 | +package cn.iocoder.yudao.module.ai.service.impl; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.module.ai.controller.app.chat.vo.*; | 4 | +import cn.iocoder.yudao.module.ai.service.ChatRoleService; |
| 5 | +import cn.iocoder.yudao.module.ai.vo.*; | ||
| 5 | import lombok.AllArgsConstructor; | 6 | import lombok.AllArgsConstructor; |
| 6 | import lombok.extern.slf4j.Slf4j; | 7 | import lombok.extern.slf4j.Slf4j; |
| 7 | import org.springframework.stereotype.Service; | 8 | import org.springframework.stereotype.Service; |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/package-info.java deleted
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/AiChatReqVO.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/AiChatReqVO.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.admin.chat.vo; | 1 | +package cn.iocoder.yudao.module.ai.vo; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; | 3 | import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/chat/vo/ChatRoleAddReq.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatRoleAddReq.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.app.chat.vo; | 1 | +package cn.iocoder.yudao.module.ai.vo; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/chat/vo/ChatRoleListReq.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatRoleListReq.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.app.chat.vo; | 1 | +package cn.iocoder.yudao.module.ai.vo; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/chat/vo/ChatRoleListRes.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatRoleListRes.java
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/chat/vo/ChatRoleUpdateReq.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatRoleUpdateReq.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.app.chat.vo; | 1 | +package cn.iocoder.yudao.module.ai.vo; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/app/chat/vo/ChatRoleUpdateVisibilityReq.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatRoleUpdateVisibilityReq.java
| 1 | -package cn.iocoder.yudao.module.ai.controller.app.chat.vo; | 1 | +package cn.iocoder.yudao.module.ai.vo; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/chat.http
| @@ -16,24 +16,17 @@ tenant-id: 1 | @@ -16,24 +16,17 @@ tenant-id: 1 | ||
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | ### chat call | 18 | ### chat call |
| 19 | -POST {{baseUrl}}/admin-api/ai/chat?prompt=中国怎么样 | 19 | +GET {{baseUrl}}/ai/chat?prompt=中国怎么样 |
| 20 | tenant-id: 1 | 20 | tenant-id: 1 |
| 21 | Authorization: {{token}} | 21 | Authorization: {{token}} |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | -### chat call | ||
| 25 | -POST {{baseUrl}}/ai-api/chat | ||
| 26 | -tenant-id: 1 | ||
| 27 | -Authorization: {{token}} | ||
| 28 | - | ||
| 29 | 24 | ||
| 30 | ### chat call | 25 | ### chat call |
| 31 | -GET {{baseUrl}}/chat-role/list | 26 | +GET {{baseUrl}}/ai/chatStream?prompt=苹果是什么颜色? |
| 32 | tenant-id: 1 | 27 | tenant-id: 1 |
| 33 | Authorization: {{token}} | 28 | Authorization: {{token}} |
| 34 | 29 | ||
| 35 | 30 | ||
| 36 | 31 | ||
| 37 | 32 | ||
| 38 | - | ||
| 39 | - |
yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/http-client.env.json
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatyiyan/YiYanChatClient.java
| @@ -82,6 +82,12 @@ public class YiYanChatClient implements ChatClient, StreamingChatClient { | @@ -82,6 +82,12 @@ public class YiYanChatClient implements ChatClient, StreamingChatClient { | ||
| 82 | YiYanChatCompletionRequest request = this.createRequest(prompt, true); | 82 | YiYanChatCompletionRequest request = this.createRequest(prompt, true); |
| 83 | // 调用 callWithFunctionSupport 发送请求 | 83 | // 调用 callWithFunctionSupport 发送请求 |
| 84 | Flux<YiYanChatCompletion> response = this.yiYanApi.chatCompletionStream(request); | 84 | Flux<YiYanChatCompletion> response = this.yiYanApi.chatCompletionStream(request); |
| 85 | + response.doOnComplete(new Runnable() { | ||
| 86 | + @Override | ||
| 87 | + public void run() { | ||
| 88 | + String a = ";"; | ||
| 89 | + } | ||
| 90 | + }); | ||
| 85 | return response.map(res -> new ChatResponse(List.of(new Generation(res.getResult())))); | 91 | return response.map(res -> new ChatResponse(List.of(new Generation(res.getResult())))); |
| 86 | } | 92 | } |
| 87 | 93 |