From ba3dffb3c49104215edd9cf5ff1de292601f1859 Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 28 May 2024 17:47:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:art:=20=E8=B0=83=E6=95=B4=20openai=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/rymcu/forest/config/RedisListenerConfig.java | 6 ++++++ src/main/java/com/rymcu/forest/openai/OpenAiController.java | 4 ++-- src/main/java/com/rymcu/forest/util/UserUtils.java | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java b/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java index 676ee27..ec7ee36 100644 --- a/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java +++ b/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java @@ -3,6 +3,7 @@ package com.rymcu.forest.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** @@ -21,4 +22,9 @@ public class RedisListenerConfig { container.setConnectionFactory(connectionFactory); return container; } + + @Bean + public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { + return new StringRedisTemplate(redisConnectionFactory); + } } diff --git a/src/main/java/com/rymcu/forest/openai/OpenAiController.java b/src/main/java/com/rymcu/forest/openai/OpenAiController.java index 4375c81..19644e5 100644 --- a/src/main/java/com/rymcu/forest/openai/OpenAiController.java +++ b/src/main/java/com/rymcu/forest/openai/OpenAiController.java @@ -60,7 +60,6 @@ public class OpenAiController { throw new IllegalArgumentException("参数异常!"); } User user = UserUtils.getCurrentUserByToken(); - Collections.reverse(messages); List list = new ArrayList<>(messages.size()); if (messages.size() > 4) { messages = messages.subList(messages.size() - 4, messages.size()); @@ -78,9 +77,10 @@ public class OpenAiController { @NotNull private GlobalResult sendMessage(User user, List list) { + boolean isAdmin = UserUtils.isAdmin(user.getEmail()); OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() - .model("gpt-3.5-turbo-16k-0613") + .model(isAdmin ? "gpt-4o" : "gpt-3.5-turbo-16k-0613") .stream(true) .messages(list) .build(); diff --git a/src/main/java/com/rymcu/forest/util/UserUtils.java b/src/main/java/com/rymcu/forest/util/UserUtils.java index 78fe61d..277dccf 100644 --- a/src/main/java/com/rymcu/forest/util/UserUtils.java +++ b/src/main/java/com/rymcu/forest/util/UserUtils.java @@ -78,4 +78,8 @@ public class UserUtils { } throw new UnauthenticatedException(); } + + public static boolean isAdmin(String email) { + return userMapper.hasAdminPermission(email); + } } From 31dd3ea516e64f7f156a1550b790152a0232b8c6 Mon Sep 17 00:00:00 2001 From: ronger Date: Fri, 5 Jul 2024 18:49:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:bug:=20=E4=BD=BF=E7=94=A8=E5=A4=B1?= =?UTF-8?q?=E6=95=88=20token=20=E8=AF=B7=E6=B1=82=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20401=20=E6=95=B0=E6=8D=AE=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/rymcu/forest/auth/JwtFilter.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/rymcu/forest/auth/JwtFilter.java b/src/main/java/com/rymcu/forest/auth/JwtFilter.java index b2fb341..e7c2835 100644 --- a/src/main/java/com/rymcu/forest/auth/JwtFilter.java +++ b/src/main/java/com/rymcu/forest/auth/JwtFilter.java @@ -1,7 +1,10 @@ package com.rymcu.forest.auth; import com.alibaba.fastjson2.JSONObject; +import com.rymcu.forest.core.result.GlobalResult; import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.core.result.ResultCode; +import com.rymcu.forest.util.ErrorCode; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureException; @@ -126,7 +129,8 @@ public class JwtFilter extends BasicHttpAuthenticationFilter { HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setContentType("application/json"); httpServletResponse.setCharacterEncoding("UTF-8"); - httpServletResponse.getOutputStream().write(JSONObject.toJSONString(GlobalResultGenerator.genErrorResult("未登录或已登录超时,请重新登录")).getBytes()); + httpServletResponse.getOutputStream().write(JSONObject.toJSONString(new GlobalResult<>(ResultCode.UNAUTHENTICATED)).getBytes()); + httpServletResponse.getOutputStream().flush(); } catch (IOException e) { // 错误日志 log.error(e.getMessage()); From bb19d5e6a8852700a34d79566f9d64a0bab68626 Mon Sep 17 00:00:00 2001 From: ronger Date: Fri, 5 Jul 2024 19:04:17 +0800 Subject: [PATCH 3/3] Update OpenAiController.java --- src/main/java/com/rymcu/forest/openai/OpenAiController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/rymcu/forest/openai/OpenAiController.java b/src/main/java/com/rymcu/forest/openai/OpenAiController.java index 19644e5..d12be57 100644 --- a/src/main/java/com/rymcu/forest/openai/OpenAiController.java +++ b/src/main/java/com/rymcu/forest/openai/OpenAiController.java @@ -80,7 +80,7 @@ public class OpenAiController { boolean isAdmin = UserUtils.isAdmin(user.getEmail()); OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() - .model(isAdmin ? "gpt-4o" : "gpt-3.5-turbo-16k-0613") + .model("gpt-3.5-turbo-16k-0613") .stream(true) .messages(list) .build();