Commit 37790d5fc13571316a7fbc8e2b0b95dce5966145

Authored by cherishsince
1 parent ebd5cbfc

临时提交 对话聊天chatStream 接口调用会超时!

yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/ChatController.java
... ... @@ -7,8 +7,13 @@ import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO;
7 7 import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
8 8 import io.swagger.v3.oas.annotations.Operation;
9 9 import io.swagger.v3.oas.annotations.tags.Tag;
  10 +import jakarta.servlet.http.HttpServletResponse;
  11 +import lombok.extern.slf4j.Slf4j;
10 12 import org.springframework.ai.chat.ChatClient;
  13 +import org.springframework.ai.chat.ChatResponse;
  14 +import org.springframework.ai.chat.prompt.Prompt;
11 15 import org.springframework.ai.openai.OpenAiChatClient;
  16 +import org.springframework.ai.openai.api.OpenAiApi;
12 17 import org.springframework.beans.factory.annotation.Autowired;
13 18 import org.springframework.context.ApplicationContext;
14 19 import org.springframework.validation.annotation.Validated;
... ... @@ -16,6 +21,10 @@ import org.springframework.web.bind.annotation.PostMapping;
16 21 import org.springframework.web.bind.annotation.RequestBody;
17 22 import org.springframework.web.bind.annotation.RequestMapping;
18 23 import org.springframework.web.bind.annotation.RestController;
  24 +import reactor.core.publisher.Flux;
  25 +
  26 +import java.util.Scanner;
  27 +import java.util.function.Consumer;
19 28  
20 29 /**
21 30 * AI模块
... ... @@ -26,6 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
26 35 @Tag(name = "AI模块")
27 36 @RestController
28 37 @RequestMapping("/ai-api")
  38 +@Slf4j
29 39 public class ChatController {
30 40  
31 41 @Autowired
... ... @@ -44,6 +54,39 @@ public class ChatController {
44 54 return CommonResult.success(res);
45 55 }
46 56  
  57 + @PostMapping("/chatStream")
  58 + @Operation(summary = "对话聊天chatStream", description = "简单的ai聊天")
  59 + public CommonResult chatStream(HttpServletResponse response, @RequestBody @Validated AiChatReqVO reqVO) throws InterruptedException {
  60 + OpenAiChatClient chatClient = applicationContext.getBean(OpenAiChatClient.class);
  61 + Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getInputText()));
  62 + chatResponse.subscribe(new Consumer<ChatResponse>() {
  63 + @Override
  64 + public void accept(ChatResponse chatResponse) {
  65 + System.err.println(chatResponse.getResults().get(0).getOutput().getContent());
  66 + }
  67 + });
  68 + return CommonResult.success("1");
  69 + }
  70 +
  71 + public static void main(String[] args) {
  72 + OpenAiChatClient openAiChatClient = new OpenAiChatClient(new OpenAiApi("openkey"));
  73 + Flux<ChatResponse> responseFlux = openAiChatClient.stream(new Prompt("最好的编程语言!"));
  74 + long now = System.currentTimeMillis();
  75 + responseFlux.subscribe(new Consumer<ChatResponse>() {
  76 + @Override
  77 + public void accept(ChatResponse chatResponse) {
  78 + if (chatResponse.getResults().get(0).getOutput() == null) {
  79 + return;
  80 + }
  81 + System.err.println(chatResponse.getResults().get(0).getOutput().getContent());
  82 + }
  83 + });
  84 +
  85 + // 阻止退出
  86 + Scanner scanner = new Scanner(System.in);
  87 + scanner.nextLine();
  88 + }
  89 +
47 90 /**
48 91 * 根据 ai模型 获取对于的 模型实现类
49 92 *
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties
... ... @@ -3,4 +3,9 @@
3 3 # openAI https://openai.com/
4 4 spring.ai.openai.api-key=${OPEN_AI_KEY}
5 5 spring.ai.openai.chat.options.model=gpt-3.5-turbo
6   -spring.ai.openai.chat.options.temperature=0.7
7 6 \ No newline at end of file
  7 +spring.ai.openai.chat.options.temperature=0.7
  8 +#spring.ai.vectorstore.milvus.client.connect-timeout-ms=50000
  9 +#spring.ai.vectorstore.milvus.client.keep-alive-timeout-ms=50000
  10 +#spring.ai.vectorstore.milvus.client.keep-alive-time-ms=80000
  11 +#spring.ai.vectorstore.pinecone.server-side-timeout=100s
  12 +
... ...