From ba374888eb0f6412c41303375f4c2c21c9026391 Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 6 Aug 2024 10:50:42 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=A2=9E=E5=8A=A0=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/openai/OpenAiController.java | 30 +++++++------------ .../rymcu/forest/openai/entity/ChatModel.java | 21 +++++++++++++ .../web/api/common/UploadController.java | 9 +----- 3 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/rymcu/forest/openai/entity/ChatModel.java diff --git a/src/main/java/com/rymcu/forest/openai/OpenAiController.java b/src/main/java/com/rymcu/forest/openai/OpenAiController.java index d12be57..5cf3032 100644 --- a/src/main/java/com/rymcu/forest/openai/OpenAiController.java +++ b/src/main/java/com/rymcu/forest/openai/OpenAiController.java @@ -1,10 +1,10 @@ package com.rymcu.forest.openai; -import com.alibaba.fastjson.JSONObject; import com.rymcu.forest.core.result.GlobalResult; import com.rymcu.forest.core.result.GlobalResultGenerator; import com.rymcu.forest.entity.User; import com.rymcu.forest.openai.entity.ChatMessageModel; +import com.rymcu.forest.openai.entity.ChatModel; import com.rymcu.forest.openai.service.OpenAiService; import com.rymcu.forest.openai.service.SseService; import com.rymcu.forest.util.Html2TextUtil; @@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.time.Duration; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -42,20 +41,9 @@ public class OpenAiController { private String token; @PostMapping("/chat") - public GlobalResult chat(@RequestBody JSONObject jsonObject) { - String message = jsonObject.getString("message"); - if (StringUtils.isBlank(message)) { - throw new IllegalArgumentException("参数异常!"); - } - User user = UserUtils.getCurrentUserByToken(); - ChatMessage chatMessage = new ChatMessage("user", message); - List list = new ArrayList<>(4); - list.add(chatMessage); - return sendMessage(user, list); - } - - @PostMapping("/new-chat") - public GlobalResult newChat(@RequestBody List messages) { + public GlobalResult newChat(@RequestBody ChatModel chatModel) { + List messages = chatModel.getMessages(); + String model = chatModel.getModel(); if (messages.isEmpty()) { throw new IllegalArgumentException("参数异常!"); } @@ -72,15 +60,17 @@ public class OpenAiController { ChatMessage message = new ChatMessage(chatMessageModel.getRole(), Html2TextUtil.getContent(chatMessageModel.getContent())); list.add(message); }); - return sendMessage(user, list); + return sendMessage(user, list, model); } @NotNull - private GlobalResult sendMessage(User user, List list) { - boolean isAdmin = UserUtils.isAdmin(user.getEmail()); + private GlobalResult sendMessage(User user, List list, String model) { + if (StringUtils.isBlank(model)) { + model = "gpt-3.5-turbo-16k-0613"; + } OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() - .model("gpt-3.5-turbo-16k-0613") + .model(model) .stream(true) .messages(list) .build(); diff --git a/src/main/java/com/rymcu/forest/openai/entity/ChatModel.java b/src/main/java/com/rymcu/forest/openai/entity/ChatModel.java new file mode 100644 index 0000000..d08a963 --- /dev/null +++ b/src/main/java/com/rymcu/forest/openai/entity/ChatModel.java @@ -0,0 +1,21 @@ +package com.rymcu.forest.openai.entity; + +import lombok.Data; + +import java.util.List; + +/** + * Created on 2024/8/6 10:24. + * + * @author ronger + * @email ronger-x@outlook.com + * @desc : com.rymcu.forest.openai.entity + */ +@Data +public class ChatModel { + + String model; + + List messages; + +} diff --git a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java index 54d7ec2..30519ae 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java @@ -8,16 +8,11 @@ import com.rymcu.forest.dto.LinkToImageUrlDTO; import com.rymcu.forest.dto.TokenUser; import com.rymcu.forest.enumerate.FilePath; import com.rymcu.forest.service.ForestFileService; -import com.rymcu.forest.util.FileUtils; -import com.rymcu.forest.util.SpringContextHolder; -import com.rymcu.forest.util.UserUtils; -import com.rymcu.forest.util.Utils; +import com.rymcu.forest.util.*; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.apache.shiro.authz.UnauthorizedException; -import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; -import org.apache.shiro.authz.annotation.RequiresRoles; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.springframework.transaction.annotation.Transactional; @@ -34,8 +29,6 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.*; -import com.rymcu.forest.util.SSRFUtil; - /** * 文件上传控制器 *