🎨 调整 openai 接口

This commit is contained in:
ronger 2024-05-28 17:47:00 +08:00
parent 92cd83e116
commit ba3dffb3c4
3 changed files with 12 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package com.rymcu.forest.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/** /**
@ -21,4 +22,9 @@ public class RedisListenerConfig {
container.setConnectionFactory(connectionFactory); container.setConnectionFactory(connectionFactory);
return container; return container;
} }
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
return new StringRedisTemplate(redisConnectionFactory);
}
} }

View File

@ -60,7 +60,6 @@ public class OpenAiController {
throw new IllegalArgumentException("参数异常!"); throw new IllegalArgumentException("参数异常!");
} }
User user = UserUtils.getCurrentUserByToken(); User user = UserUtils.getCurrentUserByToken();
Collections.reverse(messages);
List<ChatMessage> list = new ArrayList<>(messages.size()); List<ChatMessage> list = new ArrayList<>(messages.size());
if (messages.size() > 4) { if (messages.size() > 4) {
messages = messages.subList(messages.size() - 4, messages.size()); messages = messages.subList(messages.size() - 4, messages.size());
@ -78,9 +77,10 @@ public class OpenAiController {
@NotNull @NotNull
private GlobalResult sendMessage(User user, List<ChatMessage> list) { private GlobalResult sendMessage(User user, List<ChatMessage> list) {
boolean isAdmin = UserUtils.isAdmin(user.getEmail());
OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180)); OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180));
ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() ChatCompletionRequest completionRequest = ChatCompletionRequest.builder()
.model("gpt-3.5-turbo-16k-0613") .model(isAdmin ? "gpt-4o" : "gpt-3.5-turbo-16k-0613")
.stream(true) .stream(true)
.messages(list) .messages(list)
.build(); .build();

View File

@ -78,4 +78,8 @@ public class UserUtils {
} }
throw new UnauthenticatedException(); throw new UnauthenticatedException();
} }
public static boolean isAdmin(String email) {
return userMapper.hasAdminPermission(email);
}
} }