添加了设置采样、重载配置两个功能(默认采样值temperature、top_p均为1)
修改了帮助命令相关内容 function_call相关功能待开发(重载配置->反射加载)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package work.slhaf.chatai;
|
package work.slhaf.chatai;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
|
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
|
||||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
|
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
|
||||||
import net.mamoe.mirai.event.GlobalEventChannel;
|
import net.mamoe.mirai.event.GlobalEventChannel;
|
||||||
@@ -13,6 +14,7 @@ import work.slhaf.chatai.config.ModelConfigTemplate;
|
|||||||
import work.slhaf.chatai.listener.FriendMessageListener;
|
import work.slhaf.chatai.listener.FriendMessageListener;
|
||||||
import work.slhaf.chatai.listener.GroupMessageListener;
|
import work.slhaf.chatai.listener.GroupMessageListener;
|
||||||
import work.slhaf.chatai.listener.OwnerMessageListener;
|
import work.slhaf.chatai.listener.OwnerMessageListener;
|
||||||
|
import work.slhaf.chatai.ocr.util.OCRUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -22,6 +24,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* @author SLHAF
|
* @author SLHAF
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public final class App extends JavaPlugin {
|
public final class App extends JavaPlugin {
|
||||||
public static final App INSTANCE = new App();
|
public static final App INSTANCE = new App();
|
||||||
|
|
||||||
@@ -41,17 +44,16 @@ public final class App extends JavaPlugin {
|
|||||||
//加载配置
|
//加载配置
|
||||||
try {
|
try {
|
||||||
Config config = Config.ConfigLoader.load();
|
Config config = Config.ConfigLoader.load();
|
||||||
App.class.getClassLoader().loadClass("work.slhaf.chatai.utils.OCRUtil");
|
OCRUtil.load();
|
||||||
App.class.getClassLoader().loadClass("work.slhaf.chatai.utils.ChatUtil");
|
|
||||||
|
|
||||||
owner = config.getOwner().substring(1);
|
owner = config.getOwner().substring(1);
|
||||||
bot = config.getBot().substring(1);
|
bot = config.getBot().substring(1);
|
||||||
customCommands = config.getCustomCommandTemplates();
|
customCommands = config.getCustomCommandTemplates();
|
||||||
blacklist = config.getBlacklist();
|
blacklist = config.getBlacklist();
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
getLogger().info("ChatAI loaded!");
|
log.info("ChatAI loaded!");
|
||||||
|
|
||||||
//群聊监听器
|
//群聊监听器
|
||||||
GlobalEventChannel.INSTANCE.filterIsInstance(GroupMessageEvent.class)
|
GlobalEventChannel.INSTANCE.filterIsInstance(GroupMessageEvent.class)
|
||||||
@@ -59,7 +61,7 @@ public final class App extends JavaPlugin {
|
|||||||
String msg = event.getMessage().contentToString();
|
String msg = event.getMessage().contentToString();
|
||||||
long groupId = event.getGroup().getId();
|
long groupId = event.getGroup().getId();
|
||||||
String prefix = msg.split(Constant.Order.SPLIT_BLANK)[0];
|
String prefix = msg.split(Constant.Order.SPLIT_BLANK)[0];
|
||||||
return (msg.startsWith(Constant.Order.PREFIX_DEFAULT + bot) || checkCommandExist(prefix,customCommands)) && !blacklist.contains(groupId);
|
return (msg.startsWith(Constant.Order.PREFIX_DEFAULT + bot) || checkCommandExist(prefix, customCommands)) && !blacklist.contains(groupId);
|
||||||
}).registerListenerHost(new GroupMessageListener());
|
}).registerListenerHost(new GroupMessageListener());
|
||||||
|
|
||||||
//私聊监听器
|
//私聊监听器
|
||||||
@@ -69,7 +71,7 @@ public final class App extends JavaPlugin {
|
|||||||
String sender = String.valueOf(event.getFriend().getId());
|
String sender = String.valueOf(event.getFriend().getId());
|
||||||
String prefix = msg.split(Constant.Order.SPLIT_BLANK)[0];
|
String prefix = msg.split(Constant.Order.SPLIT_BLANK)[0];
|
||||||
// return !(msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) && !msg.equals(Constant.Order.MSG_HELP);
|
// return !(msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) && !msg.equals(Constant.Order.MSG_HELP);
|
||||||
return checkCommandExist(prefix,customCommands) //包含指令前缀
|
return checkCommandExist(prefix, customCommands) //包含指令前缀
|
||||||
|| !((msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) || msg.startsWith(Constant.Order.PREFIX_CUSTOM)); //如果不包含指令前缀,则不能以设置指令、"/"开头
|
|| !((msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) || msg.startsWith(Constant.Order.PREFIX_CUSTOM)); //如果不包含指令前缀,则不能以设置指令、"/"开头
|
||||||
}).registerListenerHost(new FriendMessageListener());
|
}).registerListenerHost(new FriendMessageListener());
|
||||||
|
|
||||||
@@ -84,46 +86,88 @@ public final class App extends JavaPlugin {
|
|||||||
//帮助监听
|
//帮助监听
|
||||||
GlobalEventChannel.INSTANCE
|
GlobalEventChannel.INSTANCE
|
||||||
.filterIsInstance(MessageEvent.class)
|
.filterIsInstance(MessageEvent.class)
|
||||||
.filter(event -> event.getMessage().contentToString().equals(Constant.Order.MSG_HELP))
|
.filter(event -> event.getMessage().contentToString().startsWith(Constant.Order.MSG_HELP))
|
||||||
.subscribeAlways(MessageEvent.class, event -> {
|
.subscribeAlways(MessageEvent.class, event -> {
|
||||||
synchronized (customCommands) {
|
synchronized (customCommands) {
|
||||||
final String[] helpMsg = {"""
|
String msg = event.getMessage().contentToString();
|
||||||
————<群聊命令>————
|
String result;
|
||||||
|
if (msg.equals(Constant.Order.MSG_HELP)) {
|
||||||
@<bot> <content> (仅限群聊)
|
result = """
|
||||||
/<command> <content>
|
————<ChatAI>————
|
||||||
|
>> 群聊命令
|
||||||
例:
|
>> 预设列表
|
||||||
@机器人 你好
|
>> 预设查询|<预设指令>
|
||||||
/c 你好
|
>> 模型列表""";
|
||||||
|
} else {
|
||||||
————<控制命令>————
|
try {
|
||||||
|
String helpCommand = msg.split(Constant.Order.SPLIT_BLANK)[1];
|
||||||
$ clearAll (仅限群聊)
|
String[] split = helpCommand.split(Constant.Order.SPLIT_CUSTOM);
|
||||||
$ shutUp (仅限群聊)
|
helpCommand = split[0];
|
||||||
$ speak (仅限群聊)
|
Config config = Config.ConfigLoader.getConfig();
|
||||||
$ 添加预设|<预设指令>|<模型模板ID>|<模型名称>|<预设内容>
|
result = switch (helpCommand) {
|
||||||
$ 切换模型|<预设指令>|<模型模板ID>|<模型名称>
|
case "群聊命令" -> """
|
||||||
$ 更改预设|<预设指令>|<预设内容>
|
————<群聊命令>————
|
||||||
$ 删除预设|<预设指令>
|
@<bot> <message> (仅限群聊)
|
||||||
|
/<command> <message>
|
||||||
例:
|
|
||||||
$ 添加预设|/c|<模型模板ID>|glm-4-flash|你是一只猫娘...
|
例:
|
||||||
|
@机器人 你好
|
||||||
"""};
|
/c 你好""";
|
||||||
helpMsg[0] += "————<预设列表>————";
|
case "控制命令" -> """
|
||||||
for (CustomCommandTemplate customCommand : customCommands) {
|
————<控制命令>————
|
||||||
helpMsg[0] += "\r\n\r\n"+customCommand.toString();
|
$ clearAll (仅限群聊)
|
||||||
|
$ shutUp (仅限群聊)
|
||||||
|
$ speak (仅限群聊)
|
||||||
|
$ 添加预设|<预设指令>|<模型模板ID>|<模型名称>|<预设内容>
|
||||||
|
$ 切换模型|<预设指令>|<模型模板ID>|<模型名称>
|
||||||
|
$ 更改预设|<预设指令>|<预设内容>
|
||||||
|
$ 删除预设|<预设指令>
|
||||||
|
|
||||||
|
例:
|
||||||
|
$ 添加预设|/c|<模型模板ID>|glm-4-flash|你是一只猫娘...""";
|
||||||
|
case "预设列表" -> {
|
||||||
|
StringBuilder customListStr = new StringBuilder("————<预设列表>————");
|
||||||
|
for (CustomCommandTemplate customCommandTemplate : config.getCustomCommandTemplates()) {
|
||||||
|
customListStr.append("\r\n\r\n")
|
||||||
|
.append("command: ").append(customCommandTemplate.getCommand()).append("\r\n")
|
||||||
|
.append("model: ").append(customCommandTemplate.getCustomModel());
|
||||||
|
}
|
||||||
|
yield customListStr.toString();
|
||||||
|
}
|
||||||
|
case "预设查询" -> {
|
||||||
|
StringBuilder customDetail = new StringBuilder("————<预设查询>————");
|
||||||
|
if (split.length == 1) {
|
||||||
|
yield """
|
||||||
|
格式不正确
|
||||||
|
/chatHelp 预设查询|<command>""";
|
||||||
|
}
|
||||||
|
String customCommand = split[1];
|
||||||
|
|
||||||
|
for (CustomCommandTemplate customCommandTemplate : config.getCustomCommandTemplates()) {
|
||||||
|
if (customCommand.equals(customCommandTemplate.getCommand())) {
|
||||||
|
customDetail.append("\r\n")
|
||||||
|
.append(customCommandTemplate);
|
||||||
|
yield customDetail.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yield "未找到指令" + customCommand;
|
||||||
|
}
|
||||||
|
case "模型列表" -> {
|
||||||
|
StringBuilder modelListStr = new StringBuilder("————<模型列表>————");
|
||||||
|
for (ModelConfigTemplate modelConfigTemplate : config.getModelConfigTemplates()) {
|
||||||
|
modelListStr.append("\r\n\r\n")
|
||||||
|
.append("name: ").append(modelConfigTemplate.getTemplate_name()).append("\r\n")
|
||||||
|
.append("base_url: ").append(modelConfigTemplate.getBase_url());
|
||||||
|
}
|
||||||
|
yield modelListStr.toString();
|
||||||
|
}
|
||||||
|
default -> "未知指令,输入`/chatHelp`查看";
|
||||||
|
};
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = "出现错误: \r\n"+e.getMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
helpMsg[0] += "\r\n";
|
event.getSubject().sendMessage(result);
|
||||||
helpMsg[0] += "————<模型列表>————";
|
|
||||||
for (int i = 0; i < Config.ConfigLoader.getConfig().getModelConfigTemplates().size(); i++) {
|
|
||||||
ModelConfigTemplate modelConfigTemplate = Config.ConfigLoader.getConfig().getModelConfigTemplates().get(i);
|
|
||||||
helpMsg[0] += "\r\n\r\n"+"TemplateIndex: "+i+"\r\n" +
|
|
||||||
"name: "+modelConfigTemplate.getTemplate_name() +"\r\n"+
|
|
||||||
"base_url: "+modelConfigTemplate.getBase_url()+"\r\n";
|
|
||||||
}
|
|
||||||
event.getSubject().sendMessage(helpMsg[0]);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -140,6 +184,6 @@ public final class App extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
getLogger().info("ChatAI-InGroup disabled!");
|
log.info("ChatAI-InGroup disabled!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,10 +26,9 @@ public class ChatClient {
|
|||||||
private String url;
|
private String url;
|
||||||
private String apikey;
|
private String apikey;
|
||||||
private String model;
|
private String model;
|
||||||
private String prompt = "12333";
|
|
||||||
|
|
||||||
private int top_p;
|
private double top_p;
|
||||||
private int temperature;
|
private double temperature;
|
||||||
private int max_tokens;
|
private int max_tokens;
|
||||||
|
|
||||||
private List<Message> messages = new ArrayList<>();
|
private List<Message> messages = new ArrayList<>();
|
||||||
@@ -39,12 +38,12 @@ public class ChatClient {
|
|||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatClient(String clientId, String url, String apikey, String model, int top_p, int temperature, int max_tokens) {
|
public ChatClient(String clientId, String url, String apikey, String model, double top_p, double temperature, int max_tokens) {
|
||||||
this(url, apikey, model, top_p, temperature, max_tokens);
|
this(url, apikey, model, top_p, temperature, max_tokens);
|
||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatClient(String url, String apikey, String model, int top_p, int temperature, int max_tokens) {
|
public ChatClient(String url, String apikey, String model, double top_p, double temperature, int max_tokens) {
|
||||||
this(url, apikey, model);
|
this(url, apikey, model);
|
||||||
this.top_p = top_p;
|
this.top_p = top_p;
|
||||||
this.temperature = temperature;
|
this.temperature = temperature;
|
||||||
@@ -58,36 +57,30 @@ public class ChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPromotion(String promotion) {
|
public void setPromotion(String promotion) {
|
||||||
Message message = Message.builder()
|
if (!promotion.equals("null")) {
|
||||||
.role(Constant.Character.SYSTEM)
|
Message message = Message.builder()
|
||||||
.content(promotion)
|
.role(Constant.Character.SYSTEM)
|
||||||
.build();
|
.content(promotion)
|
||||||
messages.add(0, message);
|
.build();
|
||||||
|
messages.add(0, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatResponse runChat(String content) {
|
public ChatResponse runChat(String content) {
|
||||||
HttpRequest request = HttpRequest.post(url + "/completions");
|
HttpRequest request = HttpRequest.post(url + "/completions");
|
||||||
log.info("URL: {}",request.getUrl());
|
log.info("URL: {}", request.getUrl());
|
||||||
request.header("Content-Type", "application/json");
|
request.header("Content-Type", "application/json");
|
||||||
request.header("Authorization", "Bearer " + apikey);
|
request.header("Authorization", "Bearer " + apikey);
|
||||||
|
|
||||||
Message message = new Message(Constant.Character.USER, content);
|
Message message = new Message(Constant.Character.USER, content);
|
||||||
messages.add(message);
|
messages.add(message);
|
||||||
ChatBody body;
|
ChatBody body = ChatBody.builder()
|
||||||
if (top_p > 0) {
|
.model(model)
|
||||||
body = ChatBody.builder()
|
.messages(messages)
|
||||||
.model(model)
|
.top_p(top_p)
|
||||||
.messages(messages)
|
.temperature(temperature)
|
||||||
.top_p(top_p)
|
.max_tokens(max_tokens)
|
||||||
.temperature(temperature)
|
.build();
|
||||||
.max_tokens(max_tokens)
|
|
||||||
.build();
|
|
||||||
} else {
|
|
||||||
body = ChatBody.builder()
|
|
||||||
.model(model)
|
|
||||||
.messages(messages)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponse response = request.body(JSONUtil.toJsonStr(body)).execute();
|
HttpResponse response = request.body(JSONUtil.toJsonStr(body)).execute();
|
||||||
ChatResponse finalResponse;
|
ChatResponse finalResponse;
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ public class Constant {
|
|||||||
public static final String MSG_STATUS = "/status";
|
public static final String MSG_STATUS = "/status";
|
||||||
public static final String PREFIX_CUSTOM = "/";
|
public static final String PREFIX_CUSTOM = "/";
|
||||||
public static final String MODEL_CHANGE = ".*\\|.*";
|
public static final String MODEL_CHANGE = ".*\\|.*";
|
||||||
|
public static final String SET_TEMP = ".*\\|.*\\|.*";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class StatusCode {
|
public static class StatusCode {
|
||||||
|
|||||||
@@ -13,13 +13,10 @@ public class ChatBody {
|
|||||||
private String model;
|
private String model;
|
||||||
@NonNull
|
@NonNull
|
||||||
private List<Message> messages;
|
private List<Message> messages;
|
||||||
@Builder.Default
|
private double temperature;
|
||||||
private int temperature = 1;
|
private double top_p;
|
||||||
@Builder.Default
|
|
||||||
private int top_p = 1;
|
|
||||||
private boolean stream;
|
private boolean stream;
|
||||||
@Builder.Default
|
private int max_tokens;
|
||||||
private int max_tokens = 1024;
|
|
||||||
private int presence_penalty;
|
private int presence_penalty;
|
||||||
private int frequency_penalty;
|
private int frequency_penalty;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.chatai.utils;
|
package work.slhaf.chatai.chat.util;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.chatai.chat.ChatClient;
|
import work.slhaf.chatai.chat.ChatClient;
|
||||||
@@ -63,18 +63,27 @@ public class ChatUtil {
|
|||||||
int modelTemplateIndex;
|
int modelTemplateIndex;
|
||||||
String customModel;
|
String customModel;
|
||||||
String customPromotion;
|
String customPromotion;
|
||||||
|
double top_p;
|
||||||
|
double temperature;
|
||||||
|
int max_tokens;
|
||||||
|
|
||||||
Config config = Config.ConfigLoader.getConfig();
|
Config config = Config.ConfigLoader.getConfig();
|
||||||
for (CustomCommandTemplate customCommandTemplate : config.getCustomCommandTemplates()) {
|
for (CustomCommandTemplate customCommandTemplate : config.getCustomCommandTemplates()) {
|
||||||
if (customCommandTemplate.getCommand().equals(command)) {
|
if (customCommandTemplate.getCommand().equals(command)) {
|
||||||
|
//读取预设配置
|
||||||
modelTemplateIndex = customCommandTemplate.getModelTemplateIndex();
|
modelTemplateIndex = customCommandTemplate.getModelTemplateIndex();
|
||||||
customModel = customCommandTemplate.getCustomModel();
|
customModel = customCommandTemplate.getCustomModel();
|
||||||
customPromotion = customCommandTemplate.getCustomPromotion();
|
customPromotion = customCommandTemplate.getCustomPromotion();
|
||||||
|
top_p = customCommandTemplate.getTop_p();
|
||||||
|
temperature = customCommandTemplate.getTemperature();
|
||||||
|
max_tokens = customCommandTemplate.getMax_tokens();
|
||||||
|
|
||||||
|
//读取模型配置
|
||||||
ModelConfigTemplate modelConfigTemplate = config.getModelConfigTemplates().get(modelTemplateIndex);
|
ModelConfigTemplate modelConfigTemplate = config.getModelConfigTemplates().get(modelTemplateIndex);
|
||||||
String url = modelConfigTemplate.getBase_url();
|
String url = modelConfigTemplate.getBase_url();
|
||||||
String apikey = modelConfigTemplate.getApikey();
|
String apikey = modelConfigTemplate.getApikey();
|
||||||
|
|
||||||
ChatClient chatClient = new ChatClient(chatId, url, apikey, customModel);
|
ChatClient chatClient = new ChatClient(chatId, url, apikey, customModel,top_p,temperature,max_tokens);
|
||||||
chatClient.setPromotion(customPromotion);
|
chatClient.setPromotion(customPromotion);
|
||||||
chatClients.put(chatId, chatClient);
|
chatClients.put(chatId, chatClient);
|
||||||
log.info("final content: {}", content);
|
log.info("final content: {}", content);
|
||||||
@@ -69,7 +69,7 @@ public class Config {
|
|||||||
public static class ConfigLoader {
|
public static class ConfigLoader {
|
||||||
private static final Yaml yaml;
|
private static final Yaml yaml;
|
||||||
@Getter
|
@Getter
|
||||||
private static Config config = new Config();
|
private static Config config;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
DumperOptions options = new DumperOptions();
|
DumperOptions options = new DumperOptions();
|
||||||
@@ -78,6 +78,7 @@ public class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Config load() throws IOException {
|
public static Config load() throws IOException {
|
||||||
|
config = new Config();
|
||||||
LoaderOptions loaderOptions = new LoaderOptions();
|
LoaderOptions loaderOptions = new LoaderOptions();
|
||||||
TagInspector tagInspector = tag -> true;
|
TagInspector tagInspector = tag -> true;
|
||||||
loaderOptions.setTagInspector(tagInspector);
|
loaderOptions.setTagInspector(tagInspector);
|
||||||
@@ -158,8 +159,8 @@ public class Config {
|
|||||||
|
|
||||||
//自定义预设
|
//自定义预设
|
||||||
ArrayList<CustomCommandTemplate> customCommands = new ArrayList<>();
|
ArrayList<CustomCommandTemplate> customCommands = new ArrayList<>();
|
||||||
customCommands.add(new CustomCommandTemplate("default", 0, "glm-4-flash", "null"));
|
customCommands.add(new CustomCommandTemplate("default", 0, "glm-4-flash", "null",1,1,1024));
|
||||||
customCommands.add(new CustomCommandTemplate("/c", 0, "glm-4-flash", "你是一位智能编程助手,你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。 请用中文回答。"));
|
customCommands.add(new CustomCommandTemplate("/c", 0, "glm-4-flash", "你是一位智能编程助手,你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。 请用中文回答。",1,1,1024));
|
||||||
config.setCustomCommandTemplates(customCommands);
|
config.setCustomCommandTemplates(customCommands);
|
||||||
dump();
|
dump();
|
||||||
log.warn("配置文件创建成功,请关闭后进行配置");
|
log.warn("配置文件创建成功,请关闭后进行配置");
|
||||||
@@ -228,7 +229,7 @@ public class Config {
|
|||||||
return "当前指令已存在!\r\n" + customCommand;
|
return "当前指令已存在!\r\n" + customCommand;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CustomCommandTemplate customCommand = new CustomCommandTemplate(command, modelTemplateIndex, customModel, customPromotion);
|
CustomCommandTemplate customCommand = new CustomCommandTemplate(command, modelTemplateIndex, customModel, customPromotion,1,1,1024);
|
||||||
customCommands.add(customCommand);
|
customCommands.add(customCommand);
|
||||||
return "预设添加完毕!\r\n" + customCommand;
|
return "预设添加完毕!\r\n" + customCommand;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,19 @@ public class CustomCommandTemplate {
|
|||||||
private int modelTemplateIndex;
|
private int modelTemplateIndex;
|
||||||
private String customModel;
|
private String customModel;
|
||||||
private String customPromotion;
|
private String customPromotion;
|
||||||
|
private double top_p = 1;
|
||||||
|
private double temperature = 1;
|
||||||
|
private int max_tokens = 1024;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "command: " + command + "\r\n" +
|
return "command: " + command + "\r\n" +
|
||||||
"modelTemplateIndex: " + modelTemplateIndex + "\r\n" +
|
"modelTemplateIndex: " + modelTemplateIndex + "\r\n" +
|
||||||
"customModel: " + customModel + "\r\n" +
|
"customModel: " + customModel + "\r\n" +
|
||||||
"customPromotion: " + customPromotion;
|
"customPromotion: " + customPromotion + "\r\n" +
|
||||||
|
"top_p: " + top_p + "\r\n" +
|
||||||
|
"temperature: " + temperature +"\r\n" +
|
||||||
|
"max_tokens: " + max_tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkedHashMap<String,String> toMap(){
|
public LinkedHashMap<String,String> toMap(){
|
||||||
@@ -32,6 +38,9 @@ public class CustomCommandTemplate {
|
|||||||
map.put("modelTemplateIndex", String.valueOf(modelTemplateIndex));
|
map.put("modelTemplateIndex", String.valueOf(modelTemplateIndex));
|
||||||
map.put("customModel",customModel);
|
map.put("customModel",customModel);
|
||||||
map.put("customPromotion",customPromotion);
|
map.put("customPromotion",customPromotion);
|
||||||
|
map.put("top_p",String.valueOf(top_p));
|
||||||
|
map.put("temperature",String.valueOf(temperature));
|
||||||
|
map.put("max_tokens",String.valueOf(max_tokens));
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,5 +49,8 @@ public class CustomCommandTemplate {
|
|||||||
this.modelTemplateIndex = Integer.parseInt(map.get("modelTemplateIndex"));
|
this.modelTemplateIndex = Integer.parseInt(map.get("modelTemplateIndex"));
|
||||||
this.customModel = map.get("customModel");
|
this.customModel = map.get("customModel");
|
||||||
this.customPromotion = map.get("customPromotion");
|
this.customPromotion = map.get("customPromotion");
|
||||||
|
this.top_p = Double.parseDouble(map.get("top_p"));
|
||||||
|
this.temperature = Double.parseDouble(map.get("temperature"));
|
||||||
|
this.max_tokens = Integer.parseInt(map.get("max_tokens"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package work.slhaf.chatai.constant;
|
|
||||||
|
|
||||||
public enum MethodsConstant {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 正常对话
|
|
||||||
*/
|
|
||||||
NORMAL,
|
|
||||||
/**
|
|
||||||
* 预设对话
|
|
||||||
*/
|
|
||||||
CUSTOM,
|
|
||||||
/**
|
|
||||||
* 未匹配
|
|
||||||
*/
|
|
||||||
NONE
|
|
||||||
}
|
|
||||||
@@ -4,8 +4,8 @@ import net.mamoe.mirai.event.EventHandler;
|
|||||||
import net.mamoe.mirai.event.SimpleListenerHost;
|
import net.mamoe.mirai.event.SimpleListenerHost;
|
||||||
import net.mamoe.mirai.event.events.FriendMessageEvent;
|
import net.mamoe.mirai.event.events.FriendMessageEvent;
|
||||||
import work.slhaf.chatai.chat.constant.Constant;
|
import work.slhaf.chatai.chat.constant.Constant;
|
||||||
import work.slhaf.chatai.utils.ChatUtil;
|
import work.slhaf.chatai.chat.util.ChatUtil;
|
||||||
import work.slhaf.chatai.utils.OCRUtil;
|
import work.slhaf.chatai.ocr.util.OCRUtil;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import net.mamoe.mirai.event.SimpleListenerHost;
|
|||||||
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
||||||
import net.mamoe.mirai.message.data.At;
|
import net.mamoe.mirai.message.data.At;
|
||||||
import work.slhaf.chatai.chat.constant.Constant;
|
import work.slhaf.chatai.chat.constant.Constant;
|
||||||
import work.slhaf.chatai.utils.ChatUtil;
|
import work.slhaf.chatai.chat.util.ChatUtil;
|
||||||
import work.slhaf.chatai.utils.OCRUtil;
|
import work.slhaf.chatai.ocr.util.OCRUtil;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import net.mamoe.mirai.event.events.GroupMessageEvent;
|
|||||||
import net.mamoe.mirai.event.events.MessageEvent;
|
import net.mamoe.mirai.event.events.MessageEvent;
|
||||||
import net.mamoe.mirai.message.data.At;
|
import net.mamoe.mirai.message.data.At;
|
||||||
import work.slhaf.chatai.chat.constant.Constant;
|
import work.slhaf.chatai.chat.constant.Constant;
|
||||||
|
import work.slhaf.chatai.chat.util.ChatUtil;
|
||||||
import work.slhaf.chatai.config.Config;
|
import work.slhaf.chatai.config.Config;
|
||||||
import work.slhaf.chatai.config.CustomCommandTemplate;
|
import work.slhaf.chatai.config.CustomCommandTemplate;
|
||||||
import work.slhaf.chatai.utils.ChatUtil;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -61,10 +61,12 @@ public class OwnerMessageListener extends SimpleListenerHost {
|
|||||||
private void onFriendMessageEvent(FriendMessageEvent event) throws IOException {
|
private void onFriendMessageEvent(FriendMessageEvent event) throws IOException {
|
||||||
String primaryContent = event.getMessage().contentToString().split(Constant.Order.SPLIT_BLANK)[1];
|
String primaryContent = event.getMessage().contentToString().split(Constant.Order.SPLIT_BLANK)[1];
|
||||||
String instruction;
|
String instruction;
|
||||||
String arguments;
|
String arguments = null;
|
||||||
try {
|
try {
|
||||||
instruction = primaryContent.split(Constant.Order.SPLIT_CUSTOM)[0];
|
instruction = primaryContent.split(Constant.Order.SPLIT_CUSTOM)[0];
|
||||||
arguments = primaryContent.substring(instruction.length()+1);
|
if (!instruction.equals(primaryContent)){
|
||||||
|
arguments = primaryContent.substring(instruction.length() + 1);
|
||||||
|
}
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
event.getFriend().sendMessage("操作失败,缺少参数");
|
event.getFriend().sendMessage("操作失败,缺少参数");
|
||||||
return;
|
return;
|
||||||
@@ -76,8 +78,8 @@ public class OwnerMessageListener extends SimpleListenerHost {
|
|||||||
/**
|
/**
|
||||||
* 对命令及其参数进行处理
|
* 对命令及其参数进行处理
|
||||||
*
|
*
|
||||||
* @param instruction 命令
|
* @param instruction 命令
|
||||||
* @param arguments 参数
|
* @param arguments 参数
|
||||||
* @return 处理结果
|
* @return 处理结果
|
||||||
*/
|
*/
|
||||||
private String handleCommand(String instruction, String arguments) throws IOException {
|
private String handleCommand(String instruction, String arguments) throws IOException {
|
||||||
@@ -89,10 +91,10 @@ public class OwnerMessageListener extends SimpleListenerHost {
|
|||||||
int modelTemplateIndex = Integer.parseInt(argumentsArray[1]);
|
int modelTemplateIndex = Integer.parseInt(argumentsArray[1]);
|
||||||
String customModel = argumentsArray[2];
|
String customModel = argumentsArray[2];
|
||||||
String customPromotion = argumentsArray[3];
|
String customPromotion = argumentsArray[3];
|
||||||
if (modelTemplateIndex >= Config.ConfigLoader.getConfig().getModelConfigTemplates().size()){
|
if (modelTemplateIndex >= Config.ConfigLoader.getConfig().getModelConfigTemplates().size()) {
|
||||||
yield "不存在模型模板["+modelTemplateIndex+"]";
|
yield "不存在模型模板[" + modelTemplateIndex + "]";
|
||||||
}
|
}
|
||||||
yield Config.ConfigLoader.addCustom(command,modelTemplateIndex,customModel,customPromotion);
|
yield Config.ConfigLoader.addCustom(command, modelTemplateIndex, customModel, customPromotion);
|
||||||
} else {
|
} else {
|
||||||
yield "格式不正确!";
|
yield "格式不正确!";
|
||||||
}
|
}
|
||||||
@@ -103,19 +105,19 @@ public class OwnerMessageListener extends SimpleListenerHost {
|
|||||||
String[] argumentsArray = arguments.split(Constant.Order.SPLIT_CUSTOM);
|
String[] argumentsArray = arguments.split(Constant.Order.SPLIT_CUSTOM);
|
||||||
String command = argumentsArray[0];
|
String command = argumentsArray[0];
|
||||||
int modelTemplateIndex = Integer.parseInt(argumentsArray[1]);
|
int modelTemplateIndex = Integer.parseInt(argumentsArray[1]);
|
||||||
if (modelTemplateIndex >= Config.ConfigLoader.getConfig().getModelConfigTemplates().size()){
|
if (modelTemplateIndex >= Config.ConfigLoader.getConfig().getModelConfigTemplates().size()) {
|
||||||
yield "不存在模型模板["+modelTemplateIndex+"]";
|
yield "不存在模型模板[" + modelTemplateIndex + "]";
|
||||||
}
|
}
|
||||||
String customModel = argumentsArray[2];
|
String customModel = argumentsArray[2];
|
||||||
//迭代现有预设,更改对应信息
|
//迭代现有预设,更改对应信息
|
||||||
for (CustomCommandTemplate customCommandTemplate : Config.ConfigLoader.getConfig().getCustomCommandTemplates()) {
|
for (CustomCommandTemplate customCommandTemplate : Config.ConfigLoader.getConfig().getCustomCommandTemplates()) {
|
||||||
if (customCommandTemplate.getCommand().equals(command)){
|
if (customCommandTemplate.getCommand().equals(command)) {
|
||||||
customCommandTemplate.setModelTemplateIndex(modelTemplateIndex);
|
customCommandTemplate.setModelTemplateIndex(modelTemplateIndex);
|
||||||
customCommandTemplate.setCustomModel(customModel);
|
customCommandTemplate.setCustomModel(customModel);
|
||||||
yield "指令: "+command+"模型切换成功!\r\n"+customCommandTemplate;
|
yield "指令: " + command + "模型切换成功!\r\n" + customCommandTemplate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yield "未找到指令: "+command;
|
yield "未找到指令: " + command;
|
||||||
} else {
|
} else {
|
||||||
yield "格式不正确!";
|
yield "格式不正确!";
|
||||||
}
|
}
|
||||||
@@ -127,17 +129,68 @@ public class OwnerMessageListener extends SimpleListenerHost {
|
|||||||
String customPromotion = argumentsArray[1];
|
String customPromotion = argumentsArray[1];
|
||||||
//迭代现有预设,更改对应信息
|
//迭代现有预设,更改对应信息
|
||||||
for (CustomCommandTemplate customCommandTemplate : Config.ConfigLoader.getConfig().getCustomCommandTemplates()) {
|
for (CustomCommandTemplate customCommandTemplate : Config.ConfigLoader.getConfig().getCustomCommandTemplates()) {
|
||||||
if (customCommandTemplate.getCommand().equals(command)){
|
if (customCommandTemplate.getCommand().equals(command)) {
|
||||||
customCommandTemplate.setCustomPromotion(customPromotion);
|
customCommandTemplate.setCustomPromotion(customPromotion);
|
||||||
yield "指令: "+command+"更改预设成功!\r\n"+customCommandTemplate;
|
yield "指令: " + command + "更改预设成功!\r\n" + customCommandTemplate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yield "未找到指令: "+command;
|
yield "未找到指令: " + command;
|
||||||
} else {
|
} else {
|
||||||
yield "格式不正确!";
|
yield "格式不正确!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case "设置采样" -> {
|
||||||
|
if (!arguments.matches(Constant.Order.SET_TEMP)) {
|
||||||
|
yield "格式不正确";
|
||||||
|
}
|
||||||
|
String[] split = arguments.split(Constant.Order.SPLIT_CUSTOM);
|
||||||
|
String command = split[0];
|
||||||
|
int type;
|
||||||
|
double value;
|
||||||
|
try {
|
||||||
|
type = Integer.parseInt(split[1]);
|
||||||
|
value = Double.parseDouble(split[2]);
|
||||||
|
if (type != 1 && type != 2) {
|
||||||
|
yield """
|
||||||
|
格式不正确
|
||||||
|
type = 1 -> temperature
|
||||||
|
type = 2 -> top_p""";
|
||||||
|
}
|
||||||
|
if (type == 1 && (value > 2 || value < 0) || (type == 2 && (value > 1 || value < 0))) {
|
||||||
|
yield """
|
||||||
|
采样设置错误:
|
||||||
|
采样类型:
|
||||||
|
1 -> temperature
|
||||||
|
2 -> top_p
|
||||||
|
采样数值:
|
||||||
|
1 -> 0-2
|
||||||
|
2 -> 0-1
|
||||||
|
默认为1,值越高,输出越随机""";
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
yield """
|
||||||
|
格式不正确:
|
||||||
|
$ 设置采样|<指令>|<采样类型>|<采样数值>""";
|
||||||
|
}
|
||||||
|
//检查是否存在对应指令
|
||||||
|
for (CustomCommandTemplate customCommandTemplate : Config.ConfigLoader.getConfig().getCustomCommandTemplates()) {
|
||||||
|
if (customCommandTemplate.getCommand().equals(split[0])) {
|
||||||
|
if (type == 1){
|
||||||
|
customCommandTemplate.setTemperature(value);
|
||||||
|
}else {
|
||||||
|
customCommandTemplate.setTop_p(value);
|
||||||
|
}
|
||||||
|
yield "采样已设置";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yield "未找到指令: " + command;
|
||||||
|
|
||||||
|
}
|
||||||
case "删除预设" -> Config.ConfigLoader.removeCustom(arguments);
|
case "删除预设" -> Config.ConfigLoader.removeCustom(arguments);
|
||||||
|
case "重载配置" -> {
|
||||||
|
Config.ConfigLoader.load();
|
||||||
|
yield "配置已重新加载";
|
||||||
|
}
|
||||||
default -> "该指令不存在!";
|
default -> "该指令不存在!";
|
||||||
};
|
};
|
||||||
Config.ConfigLoader.dump();
|
Config.ConfigLoader.dump();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.chatai.pojo;
|
package work.slhaf.chatai.ocr.pojo;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.chatai.utils;
|
package work.slhaf.chatai.ocr.util;
|
||||||
|
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.aliyun.ocr_api20210707.Client;
|
import com.aliyun.ocr_api20210707.Client;
|
||||||
@@ -8,14 +8,14 @@ import com.aliyun.tea.TeaException;
|
|||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.chatai.config.Config;
|
import work.slhaf.chatai.config.Config;
|
||||||
import work.slhaf.chatai.pojo.OCRDataInfo;
|
import work.slhaf.chatai.ocr.pojo.OCRDataInfo;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class OCRUtil {
|
public class OCRUtil {
|
||||||
private static Client client;
|
private static Client client;
|
||||||
public static boolean isSupported;
|
public static boolean isSupported;
|
||||||
|
|
||||||
static {
|
public static void load() {
|
||||||
//读取密钥
|
//读取密钥
|
||||||
Config pluginConfig = Config.ConfigLoader.getConfig();
|
Config pluginConfig = Config.ConfigLoader.getConfig();
|
||||||
String accessKeyId = pluginConfig.getOcrConfig().getAccessKeyId();
|
String accessKeyId = pluginConfig.getOcrConfig().getAccessKeyId();
|
||||||
Reference in New Issue
Block a user