mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
处理todo
This commit is contained in:
@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO;
|
||||
import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
|
||||
import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@ -13,7 +13,6 @@ import org.springframework.ai.chat.ChatClient;
|
||||
import org.springframework.ai.chat.ChatResponse;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.openai.OpenAiChatClient;
|
||||
import org.springframework.ai.openai.api.OpenAiApi;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@ -23,16 +22,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
// TODO @fansili:有了 swagger 注释,就不用类注释了
|
||||
/**
|
||||
* AI模块
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/3/3 20:28
|
||||
*/
|
||||
// TODO done @fansili:有了 swagger 注释,就不用类注释了
|
||||
@Tag(name = "AI模块")
|
||||
@RestController
|
||||
@RequestMapping("/ai-api")
|
||||
@ -48,7 +40,7 @@ public class ChatController {
|
||||
ChatClient chatClient = getChatClient(reqVO.getAiModel());
|
||||
String res;
|
||||
try {
|
||||
res = chatClient.call(reqVO.getInputText());
|
||||
res = chatClient.call(reqVO.getPrompt());
|
||||
} catch (Exception e) {
|
||||
res = e.getMessage();
|
||||
}
|
||||
@ -59,33 +51,14 @@ public class ChatController {
|
||||
@Operation(summary = "对话聊天chatStream", description = "简单的ai聊天")
|
||||
public CommonResult chatStream(HttpServletResponse response, @RequestBody @Validated AiChatReqVO reqVO) throws InterruptedException {
|
||||
OpenAiChatClient chatClient = applicationContext.getBean(OpenAiChatClient.class);
|
||||
Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getInputText()));
|
||||
Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getPrompt()));
|
||||
chatResponse.subscribe(new Consumer<ChatResponse>() {
|
||||
@Override
|
||||
public void accept(ChatResponse chatResponse) {
|
||||
System.err.println(chatResponse.getResults().get(0).getOutput().getContent());
|
||||
}
|
||||
});
|
||||
return CommonResult.success("1");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
OpenAiChatClient openAiChatClient = new OpenAiChatClient(new OpenAiApi("openkey"));
|
||||
Flux<ChatResponse> responseFlux = openAiChatClient.stream(new Prompt("最好的编程语言!"));
|
||||
long now = System.currentTimeMillis();
|
||||
responseFlux.subscribe(new Consumer<ChatResponse>() {
|
||||
@Override
|
||||
public void accept(ChatResponse chatResponse) {
|
||||
if (chatResponse.getResults().get(0).getOutput() == null) {
|
||||
return;
|
||||
}
|
||||
System.err.println(chatResponse.getResults().get(0).getOutput().getContent());
|
||||
}
|
||||
});
|
||||
|
||||
// 阻止退出
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
scanner.nextLine();
|
||||
return CommonResult.success(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,8 +67,8 @@ public class ChatController {
|
||||
* @param aiModelEnum
|
||||
* @return
|
||||
*/
|
||||
private ChatClient getChatClient(AiModelEnum aiModelEnum) {
|
||||
if (AiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) {
|
||||
private ChatClient getChatClient(OpenAiModelEnum aiModelEnum) {
|
||||
if (OpenAiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) {
|
||||
return applicationContext.getBean(OpenAiChatClient.class);
|
||||
}
|
||||
// AI模型暂不支持
|
||||
|
@ -1,27 +1,21 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.vo;
|
||||
|
||||
import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
|
||||
import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
// TODO @fansili 1)swagger 注释不太对;2)有了 swagger 注释,就不用类注释了
|
||||
/**
|
||||
* ai 聊天 req
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/3/4 12:33
|
||||
*/
|
||||
@Schema(description = "用户 App - 上传文件 Request VO")
|
||||
// TODO done @fansili 1)swagger 注释不太对;2)有了 swagger 注释,就不用类注释了
|
||||
@Data
|
||||
@Schema(description = "用户 App - 上传文件 Request VO")
|
||||
public class AiChatReqVO {
|
||||
|
||||
@Schema(description = "输入内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "输入内容不能为空")
|
||||
private String inputText;
|
||||
@Schema(description = "提示词", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "提示词不能为空!")
|
||||
private String prompt;
|
||||
|
||||
@Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "AI模型不能为空")
|
||||
private AiModelEnum aiModel;
|
||||
private OpenAiModelEnum aiModel;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user