new config
This commit is contained in:
@@ -7,11 +7,11 @@ import net.mamoe.mirai.event.events.FriendMessageEvent;
|
|||||||
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
||||||
import net.mamoe.mirai.utils.MiraiLogger;
|
import net.mamoe.mirai.utils.MiraiLogger;
|
||||||
import plugin.listener.UserMessageListener;
|
import plugin.listener.UserMessageListener;
|
||||||
|
import plugin.pojo.Config;
|
||||||
import plugin.utils.ConfigUtil;
|
import plugin.utils.ConfigUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static plugin.utils.ConfigUtil.config;
|
|
||||||
|
|
||||||
|
|
||||||
public final class App extends JavaPlugin {
|
public final class App extends JavaPlugin {
|
||||||
@@ -33,10 +33,12 @@ public final class App extends JavaPlugin {
|
|||||||
try {
|
try {
|
||||||
logger = getLogger();
|
logger = getLogger();
|
||||||
ConfigUtil.load();
|
ConfigUtil.load();
|
||||||
owner = config.get("owner").substring(1);
|
Thread.sleep(1500);
|
||||||
bot = config.get("bot").substring(1);
|
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);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
getLogger().info("ChatAI-InGroup-v2 loaded!");
|
getLogger().info("ChatAI-InGroup-v2 loaded!");
|
||||||
|
|||||||
@@ -8,19 +8,22 @@ import net.mamoe.mirai.event.events.GroupMessageEvent;
|
|||||||
import net.mamoe.mirai.message.data.At;
|
import net.mamoe.mirai.message.data.At;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import plugin.constant.ChatConstant;
|
import plugin.constant.ChatConstant;
|
||||||
|
import plugin.pojo.Config;
|
||||||
import plugin.utils.AIUtil;
|
import plugin.utils.AIUtil;
|
||||||
|
import plugin.utils.ConfigUtil;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static plugin.App.logger;
|
import static plugin.App.logger;
|
||||||
import static plugin.utils.ConfigUtil.config;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author SLHAF
|
* @author SLHAF
|
||||||
*/
|
*/
|
||||||
public class UserMessageListener extends SimpleListenerHost {
|
public class UserMessageListener extends SimpleListenerHost {
|
||||||
|
|
||||||
|
private static final Config config = ConfigUtil.getConfig();
|
||||||
|
|
||||||
public enum Methods {
|
public enum Methods {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -72,7 +75,7 @@ public class UserMessageListener extends SimpleListenerHost {
|
|||||||
}
|
}
|
||||||
Methods method = Methods.NONE;
|
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("没有权限!"));
|
event.getGroup().sendMessage(new At(Long.parseLong(id)).plus("没有权限!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -124,7 +127,7 @@ public class UserMessageListener extends SimpleListenerHost {
|
|||||||
Methods method = Methods.NORMAL;
|
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("没有权限!");
|
event.getFriend().sendMessage("没有权限!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
212
src/main/java/plugin/pojo/Config.java
Normal file
212
src/main/java/plugin/pojo/Config.java
Normal 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 + "}";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,21 +8,21 @@ import com.zhipu.oapi.service.v4.model.ChatMessageRole;
|
|||||||
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
|
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
|
||||||
import plugin.constant.AIConstant;
|
import plugin.constant.AIConstant;
|
||||||
import plugin.constant.ChatConstant;
|
import plugin.constant.ChatConstant;
|
||||||
|
import plugin.pojo.Config;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static plugin.App.logger;
|
import static plugin.App.logger;
|
||||||
import static plugin.utils.ConfigUtil.config;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author SLHAF
|
* @author SLHAF
|
||||||
*/
|
*/
|
||||||
public class AIUtil {
|
public class AIUtil {
|
||||||
private static final String apikey;
|
private static final String APIKEY;
|
||||||
private static final ClientV4 client;
|
private static final ClientV4 CLIENT;
|
||||||
private static final String requestIdTemplate = "ChatAI_InGroup_v2";
|
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, List<ChatMessage>> userMessagesNormal = new HashMap<>();
|
||||||
private static final HashMap<Long, Long> userLatestTimeNormal = new HashMap<>();
|
private static final HashMap<Long, Long> userLatestTimeNormal = new HashMap<>();
|
||||||
private static String modelNormal;
|
private static String modelNormal;
|
||||||
@@ -31,19 +31,20 @@ public class AIUtil {
|
|||||||
private static final HashMap<Long, Long> userLatestTimeCode = new HashMap<>();
|
private static final HashMap<Long, Long> userLatestTimeCode = new HashMap<>();
|
||||||
private static String modelCode;
|
private static String modelCode;
|
||||||
|
|
||||||
private static final Long checkTime, timeout;
|
private static final Long CHECK_TIME, TIMEOUT;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
apikey = config.get("apikey");
|
Config config = ConfigUtil.getConfig();
|
||||||
client = new ClientV4.Builder(apikey).build();
|
APIKEY = config.getApikey();
|
||||||
modelNormal = config.get("model_normal");
|
CLIENT = new ClientV4.Builder(APIKEY).build();
|
||||||
modelCode = config.get("model_code");
|
modelNormal = config.getModelNormal();
|
||||||
checkTime = Long.valueOf(ConfigUtil.config.get("time_check").substring(1));
|
modelCode = config.getModelCode();
|
||||||
timeout = Long.valueOf(config.get("timeout").substring(1));
|
CHECK_TIME = Long.valueOf(config.getTimeCheck().substring(1));
|
||||||
|
TIMEOUT = Long.valueOf(config.getTimeout().substring(1));
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(checkTime);
|
Thread.sleep(CHECK_TIME);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -53,7 +54,7 @@ public class AIUtil {
|
|||||||
//查看user最近时间,如果超过30min,则清理对应记录
|
//查看user最近时间,如果超过30min,则清理对应记录
|
||||||
userLatestTimeNormal.forEach((id, latestTime) -> {
|
userLatestTimeNormal.forEach((id, latestTime) -> {
|
||||||
Long currentTime = System.currentTimeMillis();
|
Long currentTime = System.currentTimeMillis();
|
||||||
if (currentTime - latestTime > timeout && userMessagesNormal.containsKey(id)) {
|
if (currentTime - latestTime > TIMEOUT && userMessagesNormal.containsKey(id)) {
|
||||||
userMessagesNormal.remove(id);
|
userMessagesNormal.remove(id);
|
||||||
logger.info("Normal记录清理:" + 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<>();
|
List<ChatMessage> messages = new ArrayList<>();
|
||||||
messages.add(new ChatMessage(ChatMessageRole.USER.value(), content));
|
messages.add(new ChatMessage(ChatMessageRole.USER.value(), content));
|
||||||
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
|
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
|
||||||
@@ -164,7 +165,7 @@ public class AIUtil {
|
|||||||
.messages(messages)
|
.messages(messages)
|
||||||
.requestId(requestId)
|
.requestId(requestId)
|
||||||
.build();
|
.build();
|
||||||
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
|
ModelApiResponse invokeModelApiResp = CLIENT.invokeModelApi(chatCompletionRequest);
|
||||||
int code = invokeModelApiResp.getCode();
|
int code = invokeModelApiResp.getCode();
|
||||||
if (code == 200) {
|
if (code == 200) {
|
||||||
printTokenInfo(invokeModelApiResp);
|
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) {
|
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());
|
userLatestTime.put(id, System.currentTimeMillis());
|
||||||
|
|
||||||
String requestId = String.format(requestIdTemplate, System.currentTimeMillis());
|
String requestId = String.format(REQUEST_ID_TEMPLATE, System.currentTimeMillis());
|
||||||
//处理url内容
|
//处理url内容
|
||||||
String result = "";
|
String result = "";
|
||||||
if(url != null){
|
if(url != null){
|
||||||
@@ -213,7 +214,7 @@ public class AIUtil {
|
|||||||
.requestId(requestId)
|
.requestId(requestId)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
|
ModelApiResponse invokeModelApiResp = CLIENT.invokeModelApi(chatCompletionRequest);
|
||||||
int code = invokeModelApiResp.getCode();
|
int code = invokeModelApiResp.getCode();
|
||||||
if (code == 200) {
|
if (code == 200) {
|
||||||
printTokenInfo(invokeModelApiResp);
|
printTokenInfo(invokeModelApiResp);
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package plugin.utils;
|
package plugin.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import lombok.Getter;
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
import plugin.pojo.Config;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -12,9 +15,9 @@ import static plugin.App.logger;
|
|||||||
* @author SLHAF
|
* @author SLHAF
|
||||||
*/
|
*/
|
||||||
public class ConfigUtil {
|
public class ConfigUtil {
|
||||||
public static HashMap<String, String> config;
|
|
||||||
private static final String CONFIG_PATH = "./config/ChatAIinGroup/config.yaml";
|
private static final String CONFIG_PATH = "./config/ChatAIinGroup/config.yaml";
|
||||||
private static final Yaml yaml;
|
private static final Yaml yaml;
|
||||||
|
private static Config config;
|
||||||
|
|
||||||
private ConfigUtil() {
|
private ConfigUtil() {
|
||||||
}
|
}
|
||||||
@@ -23,10 +26,12 @@ public class ConfigUtil {
|
|||||||
DumperOptions options = new DumperOptions();
|
DumperOptions options = new DumperOptions();
|
||||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
yaml = new Yaml(options);
|
yaml = new Yaml(options);
|
||||||
|
config = new Config();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查配置
|
* 检查配置
|
||||||
|
*
|
||||||
* @throws IOException 配置文件写入出错
|
* @throws IOException 配置文件写入出错
|
||||||
*/
|
*/
|
||||||
public static void load() throws IOException, ClassNotFoundException {
|
public static void load() throws IOException, ClassNotFoundException {
|
||||||
@@ -36,24 +41,26 @@ public class ConfigUtil {
|
|||||||
//创建配置文件
|
//创建配置文件
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
FileWriter writer = new FileWriter(file);
|
config.setApikey("your_zhipu_apikey");
|
||||||
writer.write("apikey: \r\n");
|
config.setAccessKeyId("your_ali_access_key_id");
|
||||||
writer.write("accessKeyId: \r\n");
|
config.setAccessKeySecret("your_ali_access_key_secret");
|
||||||
writer.write("accessKeySecret: \r\n");
|
config.setOwner("your_bot_owner_qq_number(e.g. Q1145141919810)");
|
||||||
writer.write("owner: \r\n");
|
config.setModelNormal("glm-4-flash");
|
||||||
writer.write("model_normal: \r\n");
|
config.setModelCode("glm-4-flash");
|
||||||
writer.write("model_code: \r\n");
|
config.setBot("your_bot_qq_number(e.g. Q1145141919810)");
|
||||||
writer.write("bot: \r\n");
|
config.setTimeout("M3600000");
|
||||||
writer.write("timeout: M3600000\r\n");
|
config.setTimeCheck("M60000");
|
||||||
writer.write("time_check: M60000");
|
HashMap<String, String> commands = new HashMap<>();
|
||||||
writer.flush();
|
commands.put("/c ", "你是一位智能编程助手,你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。 请用中文回答。");
|
||||||
writer.close();
|
commands.put("/example", "预设内容");
|
||||||
|
config.setCustomCommands(commands);
|
||||||
|
dump();
|
||||||
logger.warning("配置文件创建成功,请关闭后进行配置");
|
logger.warning("配置文件创建成功,请关闭后进行配置");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
} else {
|
} else {
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
InputStream inputStream = new FileInputStream(CONFIG_PATH);
|
InputStream inputStream = new FileInputStream(CONFIG_PATH);
|
||||||
config = yaml.load(inputStream);
|
config = BeanUtil.toBean(yaml.load(new FileInputStream(CONFIG_PATH)), Config.class);
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
logger.info(config.toString());
|
logger.info(config.toString());
|
||||||
logger.info("读取配置文件完毕");
|
logger.info("读取配置文件完毕");
|
||||||
@@ -62,24 +69,34 @@ public class ConfigUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Config getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置改变(模型)
|
* 配置改变(模型)
|
||||||
|
*
|
||||||
* @param modelName 模型名称
|
* @param modelName 模型名称
|
||||||
*/
|
*/
|
||||||
public static void modelNormalChange(String modelName) {
|
public static void modelNormalChange(String modelName) {
|
||||||
try {
|
try {
|
||||||
config.put("model_normal", modelName);
|
config.setModelNormal(modelName);
|
||||||
yaml.dump(config, new FileWriter(CONFIG_PATH));
|
dump();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void modelCodeChange(String modelName) {
|
public static void modelCodeChange(String modelName) {
|
||||||
try {
|
try {
|
||||||
config.put("model_code", modelName);
|
config.setModelCode(modelName);
|
||||||
yaml.dump(config, new FileWriter(CONFIG_PATH));
|
dump();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void dump() throws IOException {
|
||||||
|
yaml.dump(BeanUtil.beanToMap(config), new FileWriter(CONFIG_PATH));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,11 @@ import com.aliyun.ocr_api20210707.Client;
|
|||||||
import com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest;
|
import com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest;
|
||||||
import com.aliyun.ocr_api20210707.models.RecognizeAdvancedResponse;
|
import com.aliyun.ocr_api20210707.models.RecognizeAdvancedResponse;
|
||||||
import com.aliyun.tea.TeaException;
|
import com.aliyun.tea.TeaException;
|
||||||
import com.aliyun.teaopenapi.models.Config;
|
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
|
import plugin.pojo.Config;
|
||||||
import plugin.pojo.OCRDataInfo;
|
import plugin.pojo.OCRDataInfo;
|
||||||
|
|
||||||
import static plugin.App.logger;
|
import static plugin.App.logger;
|
||||||
import static plugin.utils.ConfigUtil.config;
|
|
||||||
|
|
||||||
public class OCRUtil {
|
public class OCRUtil {
|
||||||
private static Client client;
|
private static Client client;
|
||||||
@@ -18,14 +17,15 @@ public class OCRUtil {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
//读取密钥
|
//读取密钥
|
||||||
String accessKeyId = config.get("accessKeyId");
|
Config pluginConfig = ConfigUtil.getConfig();
|
||||||
String accessKeySecret = config.get("accessKeySecret");
|
String accessKeyId = pluginConfig.getAccessKeyId();
|
||||||
|
String accessKeySecret = pluginConfig.getAccessKeySecret();
|
||||||
if (accessKeySecret == null || accessKeyId == null) {
|
if (accessKeySecret == null || accessKeyId == null) {
|
||||||
isSupported = false;
|
isSupported = false;
|
||||||
logger.warning("未检测到阿里云OCR配置信息,图片文字识别将不可用。");
|
logger.warning("未检测到阿里云OCR配置信息,图片文字识别将不可用。");
|
||||||
} else {
|
} else {
|
||||||
isSupported = true;
|
isSupported = true;
|
||||||
Config config = new Config()
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
.setAccessKeyId(accessKeyId)
|
.setAccessKeyId(accessKeyId)
|
||||||
.setAccessKeySecret(accessKeySecret);
|
.setAccessKeySecret(accessKeySecret);
|
||||||
config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
|
config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
|
||||||
|
|||||||
Reference in New Issue
Block a user