This commit is contained in:
爪哇程序猿
2023-10-14 01:19:21 +08:00
parent 637a54326f
commit aefef92fbe
31 changed files with 6843 additions and 96 deletions
Generated
+1 -1
View File
@@ -8,4 +8,4 @@
</list>
</option>
</component>
</project>
</project>
+1 -2
View File
@@ -24,7 +24,6 @@
<bytecodeTargetLevel>
<module name="AccountModule" target="11" />
<module name="BarrageModule" target="11" />
<module name="chopperbot-test" target="11" />
<module name="common" target="11" />
<module name="console" target="11" />
<module name="CreeperModule" target="11" />
@@ -39,7 +38,7 @@
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="ChopperBot" options="" />
<module name="ChopperBot" options="-parameters" />
<module name="chopperbot-account" options="-parameters" />
<module name="chopperbot-barrage" options="-parameters" />
<module name="chopperbot-common" options="-parameters" />
+12
View File
@@ -20,5 +20,17 @@
</library>
</libraries>
</data-source>
<data-source source="LOCAL" name="database" uuid="edab566b-7961-42be-a4d5-a0acd0bf13d1">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:E:\git项目\ChopperBot\database.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.43.0/sqlite-jdbc-3.43.0.0.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>
-3
View File
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$MAVEN_REPOSITORY$/org/springframework/boot/spring-boot-starter-parent/2.3.9.RELEASE/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-account/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-barrage/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-barrage/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-console/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-creeper/src/main/java" charset="UTF-8" />
@@ -17,7 +15,6 @@
<file url="file://$PROJECT_DIR$/chopperbot-section-work/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-section/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/chopperbot-test/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/doc/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
+5 -5
View File
@@ -26,15 +26,15 @@
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
-1
View File
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ASMPluginConfiguration">
<asm skipDebug="false" skipFrames="false" skipCode="false" expandFrames="false" />
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/chopperbot-test/chopperbot-test.iml" filepath="$PROJECT_DIR$/chopperbot-test/chopperbot-test.iml" />
</modules>
</component>
</project>
+23
View File
@@ -91,5 +91,28 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.squareup.okhttp3</groupId>-->
<!-- <artifactId>okhttp</artifactId>-->
<!-- <version>4.0.0-RC1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,38 @@
package org.example.api;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.core.gpt.ChatGPTPlugin;
import org.example.mapper.GPTKeyMapper;
import org.example.pojo.GPTKey;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @Date 2023/10/12
* @Author xiaochun
*/
@Component
public class GPTApi {
@Resource
private ChatGPTPlugin plugin;
@Resource
private GPTKeyMapper mapper;
public boolean changeKey(GPTKey newKey){
String oldKey = plugin.getKey().getKey();
if (mapper.update(newKey,new QueryWrapper<GPTKey>().eq("key",oldKey))==1) {
plugin.setKey(newKey);
return true;
}
return false;
}
}
@@ -0,0 +1,13 @@
package org.example.core.config;
/**
* @Date 2023/10/13
* @Author xiaochun
*/
public class GPTConfig {
public static final String GPT_API_URL = "https://api.misakanetwork.com.cn/v1/chat/completions";
public static final String GPT_MODEL = "gpt-3.5-turbo-16k-0613";
public static final String GPT_SYSTEM_BASE = "请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我合适的标签来形容这段内容";
}
@@ -0,0 +1,48 @@
package org.example.core.gpt;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.example.core.msgbuilder.AbstractMsgBuilder;
import java.util.List;
/**
* @Date 2023/10/13
* @Author xiaochun
*/
public class ChatGPTMsgBuilder extends AbstractMsgBuilder {
private Role system = new Role("system","");
private Role user = new Role("user","");
public ChatGPTMsgBuilder system(String msg){
system.setContent(msg);
return this;
}
public ChatGPTMsgBuilder user(String msg){
user.setContent(msg);
return this;
}
public ChatGPTMsgBuilder model(String msg){
build("model",msg);
return this;
}
public ChatGPTMsgBuilder stream(boolean b){
build("stream",b);
return this;
}
@Override
public String done() {
this.map.put("messages", List.of(system,user));
return super.done();
}
@Data
@AllArgsConstructor
static class Role{
private String role;
private String content;
}
}
@@ -0,0 +1,84 @@
package org.example.core.gpt;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import okhttp3.*;
import okhttp3.internal.http2.Header;
import org.example.core.config.GPTConfig;
import org.example.core.msgbuilder.MsgBuilder;
import org.example.mapper.GPTKeyMapper;
import org.example.plugin.SpringBootPlugin;
import org.example.pojo.GPTKey;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
/**
* @Date 2023/10/12
* @Author xiaochun
*/
@Data
@Component
public class ChatGPTPlugin extends SpringBootPlugin {
@Resource
GPTKeyMapper mapper;
private GPTKey key;
private Headers header;
@Override
public boolean init(){
choseKey();
buildHeader(key.getKey());
return super.init();
}
public JSONObject reqGPT(MsgBuilder builder){
return reqGPT(builder.done());
}
public JSONObject reqGPT(String msg){
OkHttpClient client = new OkHttpClient();
Request request = buildReq(buildBody(msg));
try (Response response = client.newCall(request).execute()){
if (response.body() != null) {
String content = response.body().string();
return JSONObject.parseObject(content);
}
} catch (IOException e) {
this.error(String.format("Error: gpt request fail,Cause:%s", e.getCause()));
}
return null;
}
private RequestBody buildBody(String msg){
return RequestBody.create(msg, MediaType.parse("application/json"));
}
private Request buildReq(RequestBody body){
return new Request.Builder()
.url(key.getUrl())
.post(body)
.headers(header)
.build();
}
private void choseKey(){
List<GPTKey> gptKeys = mapper.selectList(new QueryWrapper<>());
if(gptKeys==null||gptKeys.isEmpty())throw new RuntimeException("invalid gpt key!please set your key!");
key = gptKeys.get(0);
}
private void buildHeader(String key){
this.header = new Headers.Builder()
.add("content-type", "application/json")
.add("Authorization", "Bearer " + key)
.build();
}
}
@@ -0,0 +1,24 @@
package org.example.init;
import org.example.constpool.ModuleName;
import org.example.constpool.PluginName;
import org.example.core.gpt.ChatGPTPlugin;
import org.example.init.SpringPlugInitMachine;
import org.example.plugin.annotation.Plugin;
import org.springframework.stereotype.Component;
/**
* @Date 2023/10/11
* @Author xiaochun
*/
@Plugin(moduleName = ModuleName.ACCOUNT,
pluginName = PluginName.CHAT_GPT,
pluginName_CN = "ChatGPT插件",
needPlugin = {},
pluginClass= ChatGPTPlugin.class,
springBootPlugin = true
)
@Component
public class ChatGPTPluginMachine extends SpringPlugInitMachine {
}
@@ -1,4 +1,4 @@
package org.example.core.init.moudle;
package org.example.init.moudle;
import org.example.constpool.ConstPool;
import org.example.constpool.ModuleName;
@@ -0,0 +1,11 @@
package org.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.pojo.GPTKey;
/**
* @Date 2023/10/12
* @Author xiaochun
*/
public interface GPTKeyMapper extends BaseMapper<GPTKey> {
}
@@ -0,0 +1,51 @@
package org.example.utils;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import netscape.javascript.JSObject;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.example.core.config.GPTConfig;
import java.util.List;
import java.util.Map;
/**
* @Date 2023/10/13
* @Author xiaochun
*/
public class GPTUtil {
// public static RequestBody getRequestBodyJSON(String[] labels){
// StringBuilder label = new StringBuilder();
// label.append("标签:[").append(labels[0]);
// for (int i = 1;i < labels.length; i++){
// label.append(",").append(labels[i]);
// }
// label.append("]");
// String videoInfo = "弹幕:帅,这操作我要学一年,太6了";
// String systemInfo = "请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容标签:[搞笑,秀操作,破防,泪目]";
//
// Map<String, Object> message = Map.of(
// "messages", List.of(new Role("system", "???"), new Role("user", "hello")),
// "ai", "gpt-3.5-turbo-16k-0613",
// "stream", false
// );
// String requestBodyString = JSONObject.toJSONString(message);
// RequestBody requestBody = RequestBody.create(requestBodyString, MediaType.parse("application/json"));
// return requestBody;
// }
// public static Headers getHeaders(String key){
//
// }
//
// @Data
// @AllArgsConstructor
// static class Role{
// private String role;
// private String content;
// }
}
@@ -1,7 +1,7 @@
package org.example.core.creeper.protocl;
import com.qq.tars.protocol.tars.TarsOutputStream;
import org.example.util.HttpClientUtil;
//import org.example.util.HttpClientUtil;
import java.io.IOException;
import java.util.ArrayList;
@@ -31,7 +31,8 @@ public class HuyaProtocol {
Map<String, String> header = new HashMap<>();
header.put("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Mobile Safari/537.36");
header.put("Content-Type", "application/x-www-form-urlencoded");
String roomPage = HttpClientUtil.get(url, header);
//String roomPage = HttpClientUtil.get(url, header);
String roomPage = "HttpClientUtil.get(url, header)";
Pattern pattern = Pattern.compile("lYyid\":([0-9]+)");
Matcher matcher = pattern.matcher(roomPage);
String ayyuid = "";
@@ -42,6 +42,8 @@ public class PluginName {
//Account
public static final String VIDEO_PUSH_PLUGIN = "VideoPush";
public static final String CHAT_GPT = "ChatGPT";
public static final String LIVE_CONFIG_PLUGIN= "LiveConfig";
@@ -0,0 +1,32 @@
package org.example.core.msgbuilder;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* @Date 2023/10/13
* @Author xiaochun
*/
public abstract class AbstractMsgBuilder implements MsgBuilder{
protected Map<String,Object> map;
public AbstractMsgBuilder() {
this.map = new HashMap<>();
}
@Override
public AbstractMsgBuilder build(String key, Object data){
if(data==null){
return this;
}
map.put(key,data);
return this;
}
@Override
public String done(){
return JSONObject.toJSONString(map);
}
}
@@ -0,0 +1,11 @@
package org.example.core.msgbuilder;
/**
* @Date 2023/10/13
* @Author xiaochun
*/
public interface MsgBuilder {
MsgBuilder build(String key,Object data);
String done();
}
@@ -2,6 +2,8 @@ package org.example.service;
import org.example.api.GPTApi;
import org.example.core.gpt.ChatGPTPlugin;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
@@ -22,4 +24,5 @@ public interface AccountService {
void edit(Account account);
GPTApi chatGptPlugin();
}
@@ -2,6 +2,8 @@ package org.example.service.impl;
import org.example.api.AccountApi;
import org.example.api.GPTApi;
import org.example.core.gpt.ChatGPTPlugin;
import org.example.core.pojo.Account;
import org.example.core.pojo.AccountVO;
@@ -20,6 +22,9 @@ import java.util.List;
public class AccountServiceImpl implements AccountService {
@Resource
AccountApi accountApi;
@Resource
GPTApi gptApi;
@Override
public List<AccountVO> getAllUser(int id) {
return accountApi.getAllUsers(id);
@@ -39,4 +44,9 @@ public class AccountServiceImpl implements AccountService {
public void edit(Account account) {
accountApi.editUser(account);
}
@Override
public GPTApi chatGptPlugin() {
return gptApi;
}
}
@@ -42,7 +42,6 @@ public abstract class ConfigInitMachine extends CommonInitMachine{
}catch (Exception e){
return fail(e.getMessage());
}
}
@Override
@@ -57,6 +56,4 @@ public abstract class ConfigInitMachine extends CommonInitMachine{
);
logger.info("[\uD83C\uDD96] {} close success",path);
}
}
@@ -26,5 +26,4 @@ import java.util.List;
@Component
public class HotGuardInitMachine extends SpringPlugInitMachine{
}
Binary file not shown.
+17
View File
@@ -102,5 +102,22 @@
<version>portable-1.7.8</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,37 @@
package org.example.account;
import org.example.ConsoleApplication;
import org.example.constpool.PluginName;
import org.example.core.gpt.ChatGPTMsgBuilder;
import org.example.core.gpt.ChatGPTPlugin;
import org.example.init.InitPluginRegister;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @Date 2023/10/14
* @Author xiaochun
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConsoleApplication.class,webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class gptTest {
@Resource
ChatGPTPlugin plugin;
// String videoInfo = "弹幕:帅,这操作我要学一年,太6了";
// String systemInfo = "请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容标签:[搞笑,秀操作,破防,泪目]";
@Test
public void test(){
ChatGPTMsgBuilder builder = new ChatGPTMsgBuilder().model(plugin.getKey().getModel())
.system("请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容标签:[搞笑,秀操作,破防,泪目]")
.user("弹幕:帅,这操作我要学一年,太6了")
.stream(false);
ChatGPTPlugin plugin1 = InitPluginRegister.getPlugin(PluginName.CHAT_GPT, ChatGPTPlugin.class);
System.out.println(plugin.reqGPT(builder));
System.out.println(plugin1.reqGPT(builder));
}
}
+1 -1
View File
@@ -31,5 +31,5 @@
"CreeperConfig":true
}
},
"updateTime":"2023-10-13 16:27:29"
"updateTime":"2023-10-14 00:41:38"
}
+6414 -67
View File
File diff suppressed because it is too large Load Diff
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -17,7 +17,7 @@
<module>chopperbot-console</module>
<module>chopperbot-common</module>
<module>chopperbot-live</module>
<!-- <module>chopperbot-test</module>-->
<module>chopperbot-test</module>
</modules>
<packaging>pom</packaging>