Compare commits

...

3 Commits

Author SHA1 Message Date
ronger
ba374888eb 🎨 增加模型选择功能 2024-08-06 10:50:42 +08:00
ronger
fc2bf19b44
🐛 修复日志问题
🐛 修复日志问题
2024-08-04 10:56:48 +08:00
ronger
e2dec70445 🐛 修复日志问题 2024-08-04 10:42:12 +08:00
4 changed files with 54 additions and 58 deletions

52
pom.xml
View File

@ -32,21 +32,13 @@
<groupId>io.netty</groupId>
<artifactId>netty-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
@ -59,12 +51,27 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--- log 日志依赖 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.3</version>
<version>1.2.13</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
@ -75,18 +82,9 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.19.0</version>
<version>1.2.13</version>
</dependency>
<!--- log 日志依赖 end -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec</artifactId>
@ -251,12 +249,6 @@
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -481,7 +473,7 @@
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
@ -490,7 +482,7 @@
<profileActive>prod</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>

View File

@ -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<ChatMessage> list = new ArrayList<>(4);
list.add(chatMessage);
return sendMessage(user, list);
}
@PostMapping("/new-chat")
public GlobalResult newChat(@RequestBody List<ChatMessageModel> messages) {
public GlobalResult newChat(@RequestBody ChatModel chatModel) {
List<ChatMessageModel> 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<ChatMessage> list) {
boolean isAdmin = UserUtils.isAdmin(user.getEmail());
private GlobalResult sendMessage(User user, List<ChatMessage> 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();

View File

@ -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<ChatMessageModel> messages;
}

View File

@ -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;
/**
* 文件上传控制器
*