new config

This commit is contained in:
slhaf
2024-09-28 20:15:29 +08:00
parent 34cb78adf9
commit 9f7629eb20
6 changed files with 283 additions and 48 deletions

View File

@@ -7,11 +7,11 @@ import net.mamoe.mirai.event.events.FriendMessageEvent;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.utils.MiraiLogger;
import plugin.listener.UserMessageListener;
import plugin.pojo.Config;
import plugin.utils.ConfigUtil;
import java.io.IOException;
import static plugin.utils.ConfigUtil.config;
public final class App extends JavaPlugin {
@@ -33,10 +33,12 @@ public final class App extends JavaPlugin {
try {
logger = getLogger();
ConfigUtil.load();
owner = config.get("owner").substring(1);
bot = config.get("bot").substring(1);
Thread.sleep(1500);
Config config = ConfigUtil.getConfig();
owner = config.getOwner().substring(1);
bot = config.getBot().substring(1);
} catch (IOException | ClassNotFoundException e) {
} catch (IOException | ClassNotFoundException | InterruptedException e) {
throw new RuntimeException(e);
}
getLogger().info("ChatAI-InGroup-v2 loaded!");

View File

@@ -8,19 +8,22 @@ import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.data.At;
import org.jetbrains.annotations.NotNull;
import plugin.constant.ChatConstant;
import plugin.pojo.Config;
import plugin.utils.AIUtil;
import plugin.utils.ConfigUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static plugin.App.logger;
import static plugin.utils.ConfigUtil.config;
/**
* @author SLHAF
*/
public class UserMessageListener extends SimpleListenerHost {
private static final Config config = ConfigUtil.getConfig();
public enum Methods {
/**
@@ -72,7 +75,7 @@ public class UserMessageListener extends SimpleListenerHost {
}
Methods method = Methods.NONE;
if (content.contains(ChatConstant.CHANGE_MODEL) && !id.equals(config.get(ChatConstant.OWNER).substring(1))) {
if (content.contains(ChatConstant.CHANGE_MODEL) && !id.equals(config.getOwner().substring(1))) {
event.getGroup().sendMessage(new At(Long.parseLong(id)).plus("没有权限!"));
return;
}
@@ -124,7 +127,7 @@ public class UserMessageListener extends SimpleListenerHost {
Methods method = Methods.NORMAL;
if (content.contains(ChatConstant.CHANGE_MODEL) && !id.equals(config.get(ChatConstant.OWNER).substring(1))) {
if (content.contains(ChatConstant.CHANGE_MODEL) && !id.equals(config.getOwner().substring(1))) {
event.getFriend().sendMessage("没有权限!");
return;
}

View File

@@ -0,0 +1,212 @@
package plugin.pojo;
import java.util.HashMap;
public class Config {
/**
* 智谱apikey
*/
private String apikey;
/**
* 阿里accessKey
*/
private String accessKeyId;
private String accessKeySecret;
/**
* 基础配置
*/
private String owner;
private String modelNormal;
private String modelCode;
private String bot;
private String timeout;
private String timeCheck;
/**
* 自定义预设
*/
private HashMap<String,String> customCommands;
public Config() {
}
public Config(String apikey, String accessKeyId, String accessKeySecret, String owner, String modelNormal, String modelCode, String bot, String timeout, String timeCheck, HashMap<String, String> customCommands) {
this.apikey = apikey;
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.owner = owner;
this.modelNormal = modelNormal;
this.modelCode = modelCode;
this.bot = bot;
this.timeout = timeout;
this.timeCheck = timeCheck;
this.customCommands = customCommands;
}
/**
* 获取
* @return apikey
*/
public String getApikey() {
return apikey;
}
/**
* 设置
* @param apikey
*/
public void setApikey(String apikey) {
this.apikey = apikey;
}
/**
* 获取
* @return accessKeyId
*/
public String getAccessKeyId() {
return accessKeyId;
}
/**
* 设置
* @param accessKeyId
*/
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
/**
* 获取
* @return accessKeySecret
*/
public String getAccessKeySecret() {
return accessKeySecret;
}
/**
* 设置
* @param accessKeySecret
*/
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
/**
* 获取
* @return owner
*/
public String getOwner() {
return owner;
}
/**
* 设置
* @param owner
*/
public void setOwner(String owner) {
this.owner = owner;
}
/**
* 获取
* @return modelNormal
*/
public String getModelNormal() {
return modelNormal;
}
/**
* 设置
* @param modelNormal
*/
public void setModelNormal(String modelNormal) {
this.modelNormal = modelNormal;
}
/**
* 获取
* @return modelCode
*/
public String getModelCode() {
return modelCode;
}
/**
* 设置
* @param modelCode
*/
public void setModelCode(String modelCode) {
this.modelCode = modelCode;
}
/**
* 获取
* @return bot
*/
public String getBot() {
return bot;
}
/**
* 设置
* @param bot
*/
public void setBot(String bot) {
this.bot = bot;
}
/**
* 获取
* @return timeout
*/
public String getTimeout() {
return timeout;
}
/**
* 设置
* @param timeout
*/
public void setTimeout(String timeout) {
this.timeout = timeout;
}
/**
* 获取
* @return timeCheck
*/
public String getTimeCheck() {
return timeCheck;
}
/**
* 设置
* @param timeCheck
*/
public void setTimeCheck(String timeCheck) {
this.timeCheck = timeCheck;
}
/**
* 获取
* @return customCommands
*/
public HashMap<String, String> getCustomCommands() {
return customCommands;
}
/**
* 设置
* @param customCommands
*/
public void setCustomCommands(HashMap<String, String> customCommands) {
this.customCommands = customCommands;
}
@Override
public String toString() {
return "Config{apikey = " + apikey + ", accessKeyId = " + accessKeyId + ", accessKeySecret = " + accessKeySecret + ", owner = " + owner + ", modelNormal = " + modelNormal + ", modelCode = " + modelCode + ", bot = " + bot + ", timeout = " + timeout + ", timeCheck = " + timeCheck + ", customCommands = " + customCommands + "}";
}
}

View File

@@ -8,21 +8,21 @@ import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import plugin.constant.AIConstant;
import plugin.constant.ChatConstant;
import plugin.pojo.Config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static plugin.App.logger;
import static plugin.utils.ConfigUtil.config;
/**
* @author SLHAF
*/
public class AIUtil {
private static final String apikey;
private static final ClientV4 client;
private static final String requestIdTemplate = "ChatAI_InGroup_v2";
private static final String APIKEY;
private static final ClientV4 CLIENT;
private static final String REQUEST_ID_TEMPLATE = "ChatAI_InGroup_v2";
private static final HashMap<Long, List<ChatMessage>> userMessagesNormal = new HashMap<>();
private static final HashMap<Long, Long> userLatestTimeNormal = new HashMap<>();
private static String modelNormal;
@@ -31,19 +31,20 @@ public class AIUtil {
private static final HashMap<Long, Long> userLatestTimeCode = new HashMap<>();
private static String modelCode;
private static final Long checkTime, timeout;
private static final Long CHECK_TIME, TIMEOUT;
static {
apikey = config.get("apikey");
client = new ClientV4.Builder(apikey).build();
modelNormal = config.get("model_normal");
modelCode = config.get("model_code");
checkTime = Long.valueOf(ConfigUtil.config.get("time_check").substring(1));
timeout = Long.valueOf(config.get("timeout").substring(1));
Config config = ConfigUtil.getConfig();
APIKEY = config.getApikey();
CLIENT = new ClientV4.Builder(APIKEY).build();
modelNormal = config.getModelNormal();
modelCode = config.getModelCode();
CHECK_TIME = Long.valueOf(config.getTimeCheck().substring(1));
TIMEOUT = Long.valueOf(config.getTimeout().substring(1));
new Thread(() -> {
while (true) {
try {
Thread.sleep(checkTime);
Thread.sleep(CHECK_TIME);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
@@ -53,7 +54,7 @@ public class AIUtil {
//查看user最近时间如果超过30min则清理对应记录
userLatestTimeNormal.forEach((id, latestTime) -> {
Long currentTime = System.currentTimeMillis();
if (currentTime - latestTime > timeout && userMessagesNormal.containsKey(id)) {
if (currentTime - latestTime > TIMEOUT && userMessagesNormal.containsKey(id)) {
userMessagesNormal.remove(id);
logger.info("Normal记录清理:" + id);
}
@@ -154,7 +155,7 @@ public class AIUtil {
}
}
}
String requestId = String.format(requestIdTemplate, System.currentTimeMillis());
String requestId = String.format(REQUEST_ID_TEMPLATE, System.currentTimeMillis());
List<ChatMessage> messages = new ArrayList<>();
messages.add(new ChatMessage(ChatMessageRole.USER.value(), content));
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
@@ -164,7 +165,7 @@ public class AIUtil {
.messages(messages)
.requestId(requestId)
.build();
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
ModelApiResponse invokeModelApiResp = CLIENT.invokeModelApi(chatCompletionRequest);
int code = invokeModelApiResp.getCode();
if (code == 200) {
printTokenInfo(invokeModelApiResp);
@@ -181,7 +182,7 @@ public class AIUtil {
private static String getChatResponse(Long id, String content, String url,String model, HashMap<Long, List<ChatMessage>> userMessages, HashMap<Long, Long> userLatestTime) {
userLatestTime.put(id, System.currentTimeMillis());
String requestId = String.format(requestIdTemplate, System.currentTimeMillis());
String requestId = String.format(REQUEST_ID_TEMPLATE, System.currentTimeMillis());
//处理url内容
String result = "";
if(url != null){
@@ -213,7 +214,7 @@ public class AIUtil {
.requestId(requestId)
.build();
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
ModelApiResponse invokeModelApiResp = CLIENT.invokeModelApi(chatCompletionRequest);
int code = invokeModelApiResp.getCode();
if (code == 200) {
printTokenInfo(invokeModelApiResp);

View File

@@ -1,7 +1,10 @@
package plugin.utils;
import cn.hutool.core.bean.BeanUtil;
import lombok.Getter;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import plugin.pojo.Config;
import java.io.*;
import java.util.HashMap;
@@ -12,9 +15,9 @@ import static plugin.App.logger;
* @author SLHAF
*/
public class ConfigUtil {
public static HashMap<String, String> config;
private static final String CONFIG_PATH = "./config/ChatAIinGroup/config.yaml";
private static final Yaml yaml;
private static Config config;
private ConfigUtil() {
}
@@ -23,10 +26,12 @@ public class ConfigUtil {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
yaml = new Yaml(options);
config = new Config();
}
/**
* 检查配置
*
* @throws IOException 配置文件写入出错
*/
public static void load() throws IOException, ClassNotFoundException {
@@ -36,24 +41,26 @@ public class ConfigUtil {
//创建配置文件
file.getParentFile().mkdirs();
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write("apikey: \r\n");
writer.write("accessKeyId: \r\n");
writer.write("accessKeySecret: \r\n");
writer.write("owner: \r\n");
writer.write("model_normal: \r\n");
writer.write("model_code: \r\n");
writer.write("bot: \r\n");
writer.write("timeout: M3600000\r\n");
writer.write("time_check: M60000");
writer.flush();
writer.close();
config.setApikey("your_zhipu_apikey");
config.setAccessKeyId("your_ali_access_key_id");
config.setAccessKeySecret("your_ali_access_key_secret");
config.setOwner("your_bot_owner_qq_number(e.g. Q1145141919810)");
config.setModelNormal("glm-4-flash");
config.setModelCode("glm-4-flash");
config.setBot("your_bot_qq_number(e.g. Q1145141919810)");
config.setTimeout("M3600000");
config.setTimeCheck("M60000");
HashMap<String, String> commands = new HashMap<>();
commands.put("/c ", "你是一位智能编程助手,你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。 请用中文回答。");
commands.put("/example", "预设内容");
config.setCustomCommands(commands);
dump();
logger.warning("配置文件创建成功,请关闭后进行配置");
System.exit(0);
} else {
//读取配置文件
InputStream inputStream = new FileInputStream(CONFIG_PATH);
config = yaml.load(inputStream);
config = BeanUtil.toBean(yaml.load(new FileInputStream(CONFIG_PATH)), Config.class);
inputStream.close();
logger.info(config.toString());
logger.info("读取配置文件完毕");
@@ -62,24 +69,34 @@ public class ConfigUtil {
}
}
public static Config getConfig() {
return config;
}
/**
* 配置改变(模型)
*
* @param modelName 模型名称
*/
public static void modelNormalChange(String modelName) {
try {
config.put("model_normal", modelName);
yaml.dump(config, new FileWriter(CONFIG_PATH));
config.setModelNormal(modelName);
dump();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void modelCodeChange(String modelName) {
try {
config.put("model_code", modelName);
yaml.dump(config, new FileWriter(CONFIG_PATH));
config.setModelCode(modelName);
dump();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void dump() throws IOException {
yaml.dump(BeanUtil.beanToMap(config), new FileWriter(CONFIG_PATH));
}
}

View File

@@ -5,12 +5,11 @@ import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest;
import com.aliyun.ocr_api20210707.models.RecognizeAdvancedResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import plugin.pojo.Config;
import plugin.pojo.OCRDataInfo;
import static plugin.App.logger;
import static plugin.utils.ConfigUtil.config;
public class OCRUtil {
private static Client client;
@@ -18,14 +17,15 @@ public class OCRUtil {
static {
//读取密钥
String accessKeyId = config.get("accessKeyId");
String accessKeySecret = config.get("accessKeySecret");
Config pluginConfig = ConfigUtil.getConfig();
String accessKeyId = pluginConfig.getAccessKeyId();
String accessKeySecret = pluginConfig.getAccessKeySecret();
if (accessKeySecret == null || accessKeyId == null) {
isSupported = false;
logger.warning("未检测到阿里云OCR配置信息图片文字识别将不可用。");
} else {
isSupported = true;
Config config = new Config()
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";