From cc58c4cf6a699556f446ae0d5980eedb4bbfc774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Thu, 23 Mar 2023 15:06:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=BC=93=E5=AD=98=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 22 +++ .../controller/FileUploadController.java | 2 +- .../controller/PersonController.java | 59 ------- .../controller/RedisController.java | 2 +- .../entity/chat/ChoiceMessage.java | 30 ++++ .../springbootkwan/entity/chat/Choices.java | 33 ++++ .../entity/chat/CompletionChatRequest.java | 41 +++++ .../entity/chat/CompletionChatResponse.java | 49 ++++++ .../utils/FileContainsUtil.java | 150 ++++++++++++++++++ .../utils/OpenAIChatGptUtil.java | 99 ++++++++++++ src/main/resources/mapper/UserMapper.xml | 7 - 11 files changed, 426 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/com/kwan/springbootkwan/controller/PersonController.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/chat/Choices.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatRequest.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatResponse.java create mode 100644 src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java create mode 100644 src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java diff --git a/pom.xml b/pom.xml index ab3dc48..5c8dd73 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,28 @@ dynamic-datasource-spring-boot-starter 3.5.1 + + + com.theokanning.openai-gpt3-java + service + 0.11.0 + + + + com.theokanning.openai-gpt3-java + client + 0.11.1 + + + cn.hutool + hutool-all + 5.8.12 + + + com.alibaba.fastjson2 + fastjson2 + 2.0.23 + diff --git a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java index df2b520..c9b4e95 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java @@ -21,7 +21,7 @@ import java.util.UUID; * @version : 2.2.0 * @date : 2022/12/19 16:07 */ -@Api(description = "文件上传信息", tags = "FileUploadController") +@Api(value = "文件上传信息", tags = "FileUploadController") @RestController public class FileUploadController { diff --git a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java deleted file mode 100644 index bf567fb..0000000 --- a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java +++ /dev/null @@ -1,59 +0,0 @@ -//package com.kwan.springbootkwan.controller; -// -//import com.kwan.springbootkwan.entity.Person; -//import com.kwan.springbootkwan.entity.User; -//import com.kwan.springbootkwan.service.IUserService; -//import io.swagger.annotations.Api; -//import io.swagger.annotations.ApiOperation; -//import org.springframework.batch.core.Job; -//import org.springframework.batch.core.JobParameters; -//import org.springframework.batch.core.launch.JobLauncher; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.web.bind.annotation.*; -// -//import java.util.Date; -//import java.util.List; -// -///** -// * Person相关 -// * -// * @author : qinyingjie -// * @version : 2.2.0 -// * @date : 2022/12/19 16:08 -// */ -//@Api(description = "person用户信息", tags = "PersonController") -//@RestController -//@RequestMapping("/person") -//public class PersonController { -// -// @Autowired -// private JobLauncher jobLauncher; -// @Autowired -// private Job job; -// -// /** -// * { -// * "name": "zhang san", -// * "age": 24, -// * "birthday": "2022-12-19" -// * } -// */ -// @ApiOperation(value = "json返回", notes = "json返回") -// @GetMapping("/person") -// public Person person() { -// Person person = new Person(); -// person.setUsername("zhang san"); -// person.setId(24); -// person.setAddress("湖北"); -// return person; -// } -// -// @GetMapping("/hello") -// public void hello() { -// try { -// jobLauncher.run(job, new JobParameters()); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } -//} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java index c28a5c5..d9b5ea8 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; -@Api(description = "redis测试信息", tags = "RedisController") +@Api(value = "redis测试信息", tags = "RedisController") @RestController @RequestMapping("/test") public class RedisController { diff --git a/src/main/java/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java b/src/main/java/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java new file mode 100644 index 0000000..3d34808 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java @@ -0,0 +1,30 @@ +package com.kwan.springbootkwan.entity.chat; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +/** + * 结果 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/3/22 21:12 + */ +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class ChoiceMessage { + /** + * 角色 + */ + String role; + /** + * 具体内容 + */ + String content; +} + diff --git a/src/main/java/com/kwan/springbootkwan/entity/chat/Choices.java b/src/main/java/com/kwan/springbootkwan/entity/chat/Choices.java new file mode 100644 index 0000000..62fff46 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/chat/Choices.java @@ -0,0 +1,33 @@ +package com.kwan.springbootkwan.entity.chat; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +/** + * 结果集 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/3/22 21:12 + */ +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Choices { + /** + * 内容对象 + */ + private ChoiceMessage message; + /** + * 结束原因 + */ + private String finish_reason; + /** + * 索引位置 + */ + private Integer index; +} diff --git a/src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatRequest.java b/src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatRequest.java new file mode 100644 index 0000000..d60967b --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatRequest.java @@ -0,0 +1,41 @@ +package com.kwan.springbootkwan.entity.chat; + + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + + +/** + * 请求参数 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/3/22 21:00 + */ +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class CompletionChatRequest { + String model; + List messages; + Integer maxTokens; + Double temperature; + Double topP; + Integer n; + Boolean stream; + Integer logprobs; + Boolean echo; + List stop; + Double presencePenalty; + Double frequencyPenalty; + Integer bestOf; + Map logitBias; + String user; +} + diff --git a/src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatResponse.java b/src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatResponse.java new file mode 100644 index 0000000..cb22d6a --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatResponse.java @@ -0,0 +1,49 @@ +package com.kwan.springbootkwan.entity.chat; + +import com.theokanning.openai.Usage; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + + +/** + * 响应结果 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/3/22 21:13 + */ +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class CompletionChatResponse { + /** + * id + */ + String id; + /** + * chat.completion + */ + String object; + /** + * 创建时间 + */ + long created; + /** + * 模型 + */ + String model; + /** + * 结果集 + */ + List choices; + /** + * 使用情况 + */ + Usage usage; +} + diff --git a/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java b/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java new file mode 100644 index 0000000..3d152f9 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java @@ -0,0 +1,150 @@ +package com.kwan.springbootkwan.utils; + + +import cn.hutool.core.date.StopWatch; +import com.alibaba.fastjson2.JSON; +import com.google.common.base.Charsets; +import com.google.common.io.Files; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * 字符串是否存在文件中 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/2/8 10:45 + */ +public class FileContainsUtil { + + /** + * 默认不存在 + */ + private static boolean IS_EXIST = false; + /** + * 图片路径 + */ + private static final String PIC_PATH = "/Users/qinyingjie/Documents/idea-workspace/blogimg/"; + /** + * 博客路径 + */ + private static final String BLOG_FOLDER = "/Users/qinyingjie/Documents/idea-workspace/blog/"; + + public static void main(String[] args) throws IOException { + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + final List isNotExist = new ArrayList<>(); + //获取picPath下面所有的文件名 + final List picNames = getPicName(PIC_PATH); + System.out.println("图片总数为" + picNames.size()); + for (String word : picNames) { + IS_EXIST = false; + //指定类型的文件 + String suffix = ".md"; + //包含某个字符串 + traverseFolder(BLOG_FOLDER, suffix, word); + //文件不存在 + if (!IS_EXIST) { + isNotExist.add(word); + deletePic(PIC_PATH + word); + } + } + System.out.println("不存在图片总数为" + isNotExist.size()); + stopWatch.stop(); + //毫秒输出 + System.out.println(JSON.toJSONString(stopWatch.getTaskInfo())); + } + + /** + * 获取文件 + * + * @param path + * @param suffix + * @param word + * @throws IOException + */ + public static void traverseFolder(String path, String suffix, String word) throws IOException { + File file = new File(path); + if (file.exists()) { + //获取文件夹下的文件 + File[] files = file.listFiles(); + if (null != files && files.length != 0) { + for (File file2 : files) { + //是否是文件夹 + if (file2.isDirectory()) { + traverseFolder(file2.getAbsolutePath(), suffix, word); + } else { + //包含md结尾的文件 + if (file2.getAbsolutePath().contains(suffix)) { + getParams(file2.getAbsolutePath(), word); + } + } + } + } + } + } + + /** + * 判断文件是否存在 + * + * @param classPath + * @param word + * @throws IOException + */ + public static void getParams(String classPath, String word) throws IOException { + File file = new File(classPath); + //每行作为一个字符串,存为列表元素 + List strings = Files.readLines(file, Charsets.UTF_8); + for (String string : strings) { + //判断是否包含方法名称,即指定字符串 + if (string.contains(word)) { + //文件存在 + IS_EXIST = true; + } + } + } + + /** + * 获取图片名称 + * + * @param path + * @return + */ + public static List getPicName(String path) { + List picNames = new ArrayList<>(); + File file = new File(path); + if (file.exists()) { + //获取文件夹下的文件 + File[] files = file.listFiles(); + if (null != files && files.length != 0) { + for (File file2 : files) { + //是否是文件夹 + if (!file2.isDirectory()) { + //包含md结尾的文件 + final String name = file2.getName(); + picNames.add(name); + } + } + } + } + return picNames; + } + + /** + * 删除文件 + * + * @param picPath + */ + public static void deletePic(String picPath) { + File file = new File(picPath); + try { + file.delete(); + System.out.printf("删除文件成功:%s%n", picPath); + } catch (Exception e) { + System.err.printf("无法删除的路径 %s%n%s", picPath, e); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java b/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java new file mode 100644 index 0000000..ce33195 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java @@ -0,0 +1,99 @@ +package com.kwan.springbootkwan.utils; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpUtil; +import com.alibaba.fastjson2.JSON; +import com.kwan.springbootkwan.entity.chat.ChoiceMessage; +import com.kwan.springbootkwan.entity.chat.Choices; +import com.kwan.springbootkwan.entity.chat.CompletionChatRequest; +import com.kwan.springbootkwan.entity.chat.CompletionChatResponse; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 人工智能chatgpt-api + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/3/22 20:40 + */ +public class OpenAIChatGptUtil { + /** + * 请求地址 + */ + final static private String chatCompletionUrl = "https://api.openai.com/v1/chat/completions"; + /** + * 模型 + */ + final static private String model = "gpt-3.5-turbo"; + /** + * 这里使用你自己的认证信息 + */ + final static private String Authorization = "Bearer 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"; + + + public static void main(String[] args) { + String question = "默写静夜思"; + //这里是我根据模型请求封装的模型实体 + CompletionChatRequest completionChatRequest = new CompletionChatRequest(); + //封装http请求 + HttpRequest post = HttpUtil.createPost(chatCompletionUrl).timeout(300000); + Map headers = new HashMap<>(); + headers.put("Authorization", Authorization); + post.addHeaders(headers); + post.contentType("application/json;charset=UTF-8"); + completionChatRequest.setModel(model); + ChoiceMessage message = new ChoiceMessage("user", question); + List messages = new ArrayList<>(); + messages.add(message); + completionChatRequest.setMessages(messages); + post.body(JSON.toJSONString(completionChatRequest)); + String body = post.execute().body(); + CompletionChatResponse completionChatResponse = JSON.parseObject(body, CompletionChatResponse.class); + final List choices = completionChatResponse.getChoices(); + for (Choices choice : choices) { + final ChoiceMessage message1 = choice.getMessage(); + final String content = message1.getContent(); + System.out.println(content); + } + System.out.println("成功"); + } + +// public static CompletionChatResponse chatCompletion(String prompt) {//prompt即要将要发送至gtp的内容 +// //这里我把用户发送和机器人返回的20条内容内容存在了redis中,用于关联对话上下文场景,如果不需要,注释掉即可,直接new一个ArrayList +// List messages = getUserCacheMessages(); +// int size = messages.size(); +// +// if (size > 19) { +// messages.remove(0); +// } +// +// messages.add(message); +// setUserCacheMessages(messages);//把新的放到redis +// completionChatRequest.setMessages(messages); +// post.body(JSON.toJSONString(completionChatRequest)); +// String body = post.execute().body(); +// CompletionChatResponse completionChatResponse = JSON.parseObject(body, CompletionChatResponse.class); +// return completionChatResponse; +// } +// +// public static List getUserCacheMessages(){ +// RedisCache redisCache = SpringUtils.getBean(RedisCache.class); +// List cacheMessages = redisCache.getCacheObject(JwtTokenUtil.getUserId + ":messages"); +// if (StringUtils.isEmpty(cacheMessages)){ +// cacheMessages= new ArrayList<>(); +// } +// return cacheMessages; +// } +// +// public static void setUserCacheMessages(List messages){ +// while(messages.size()>20){ +// messages.remove(0); +// } +// RedisCache redisCache = SpringUtils.getBean(RedisCache.class); +// redisCache.setCacheObject(JwtTokenUtil.getUserId + ":messages", messages, 30, TimeUnit.MINUTES); +// } +} diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index 8feac8e..c567196 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -1,13 +1,6 @@ - - - - - - -