fix:缓存请求

上级 4fd0b164
......@@ -139,6 +139,28 @@
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/service -->
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/client -->
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>client</artifactId>
<version>0.11.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.12</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.23</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -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 {
......
//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
......@@ -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 {
......
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;
}
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;
}
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<Object> messages;
Integer maxTokens;
Double temperature;
Double topP;
Integer n;
Boolean stream;
Integer logprobs;
Boolean echo;
List<String> stop;
Double presencePenalty;
Double frequencyPenalty;
Integer bestOf;
Map<String, Integer> logitBias;
String user;
}
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> choices;
/**
* 使用情况
*/
Usage usage;
}
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<String> isNotExist = new ArrayList<>();
//获取picPath下面所有的文件名
final List<String> 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<String> strings = Files.readLines(file, Charsets.UTF_8);
for (String string : strings) {
//判断是否包含方法名称,即指定字符串
if (string.contains(word)) {
//文件存在
IS_EXIST = true;
}
}
}
/**
* 获取图片名称
*
* @param path
* @return
*/
public static List<String> getPicName(String path) {
List<String> 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
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<String, String> 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<Object> 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> 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<Object> 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<Object> getUserCacheMessages(){
// RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
// List<Object> cacheMessages = redisCache.getCacheObject(JwtTokenUtil.getUserId + ":messages");
// if (StringUtils.isEmpty(cacheMessages)){
// cacheMessages= new ArrayList<>();
// }
// return cacheMessages;
// }
//
// public static void setUserCacheMessages(List<Object> messages){
// while(messages.size()>20){
// messages.remove(0);
// }
// RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
// redisCache.setCacheObject(JwtTokenUtil.getUserId + ":messages", messages, 30, TimeUnit.MINUTES);
// }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kwan.springbootkwan.mapper.UserMapper">
<!-- <select id="getUserByName" resultType="com.kwan.springbootkwan.entity.User">-->
<!-- SELECT * from user-->
<!-- where 1=1-->
<!-- <if test="name != null and name != ''">-->
<!-- and name = #{name}-->
<!-- </if>-->
<!-- </select>-->
<select id="getUserByName" resultType="com.kwan.springbootkwan.entity.User">
SELECT *
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册