Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
cc58c4cf
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
cc58c4cf
编写于
3月 23, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:缓存请求
上级
4fd0b164
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
426 addition
and
68 deletion
+426
-68
pom.xml
pom.xml
+22
-0
src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java
.../kwan/springbootkwan/controller/FileUploadController.java
+1
-1
src/main/java/com/kwan/springbootkwan/controller/PersonController.java
.../com/kwan/springbootkwan/controller/PersonController.java
+0
-59
src/main/java/com/kwan/springbootkwan/controller/RedisController.java
...a/com/kwan/springbootkwan/controller/RedisController.java
+1
-1
src/main/java/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java
...va/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java
+30
-0
src/main/java/com/kwan/springbootkwan/entity/chat/Choices.java
...ain/java/com/kwan/springbootkwan/entity/chat/Choices.java
+33
-0
src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatRequest.java
...wan/springbootkwan/entity/chat/CompletionChatRequest.java
+41
-0
src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatResponse.java
...an/springbootkwan/entity/chat/CompletionChatResponse.java
+49
-0
src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java
.../java/com/kwan/springbootkwan/utils/FileContainsUtil.java
+150
-0
src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java
...java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java
+99
-0
src/main/resources/mapper/UserMapper.xml
src/main/resources/mapper/UserMapper.xml
+0
-7
未找到文件。
pom.xml
浏览文件 @
cc58c4cf
...
@@ -139,6 +139,28 @@
...
@@ -139,6 +139,28 @@
<artifactId>
dynamic-datasource-spring-boot-starter
</artifactId>
<artifactId>
dynamic-datasource-spring-boot-starter
</artifactId>
<version>
3.5.1
</version>
<version>
3.5.1
</version>
</dependency>
</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>
</dependencies>
<build>
<build>
<plugins>
<plugins>
...
...
src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java
浏览文件 @
cc58c4cf
...
@@ -21,7 +21,7 @@ import java.util.UUID;
...
@@ -21,7 +21,7 @@ import java.util.UUID;
* @version : 2.2.0
* @version : 2.2.0
* @date : 2022/12/19 16:07
* @date : 2022/12/19 16:07
*/
*/
@Api
(
description
=
"文件上传信息"
,
tags
=
"FileUploadController"
)
@Api
(
value
=
"文件上传信息"
,
tags
=
"FileUploadController"
)
@RestController
@RestController
public
class
FileUploadController
{
public
class
FileUploadController
{
...
...
src/main/java/com/kwan/springbootkwan/controller/PersonController.java
已删除
100644 → 0
浏览文件 @
4fd0b164
//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
src/main/java/com/kwan/springbootkwan/controller/RedisController.java
浏览文件 @
cc58c4cf
...
@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
...
@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
@Api
(
description
=
"redis测试信息"
,
tags
=
"RedisController"
)
@Api
(
value
=
"redis测试信息"
,
tags
=
"RedisController"
)
@RestController
@RestController
@RequestMapping
(
"/test"
)
@RequestMapping
(
"/test"
)
public
class
RedisController
{
public
class
RedisController
{
...
...
src/main/java/com/kwan/springbootkwan/entity/chat/ChoiceMessage.java
0 → 100644
浏览文件 @
cc58c4cf
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
;
}
src/main/java/com/kwan/springbootkwan/entity/chat/Choices.java
0 → 100644
浏览文件 @
cc58c4cf
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
;
}
src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatRequest.java
0 → 100644
浏览文件 @
cc58c4cf
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
;
}
src/main/java/com/kwan/springbootkwan/entity/chat/CompletionChatResponse.java
0 → 100644
浏览文件 @
cc58c4cf
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
;
}
src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java
0 → 100644
浏览文件 @
cc58c4cf
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
src/main/java/com/kwan/springbootkwan/utils/OpenAIChatGptUtil.java
0 → 100644
浏览文件 @
cc58c4cf
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);
// }
}
src/main/resources/mapper/UserMapper.xml
浏览文件 @
cc58c4cf
<?xml version="1.0" encoding="UTF-8"?>
<?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">
<!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"
>
<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
id=
"getUserByName"
resultType=
"com.kwan.springbootkwan.entity.User"
>
SELECT *
SELECT *
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录