新增百度文言一心GPT接口模块
This commit is contained in:
parent
00cdf7b96e
commit
9fdf670c26
@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class QQBotApplication {
|
||||
public static final String version="QQBot v.1.4b.3";
|
||||
public static final String version="QQBot v.1.5";
|
||||
public static void main(String[] args) {
|
||||
System.out.println("version = " + version);
|
||||
SpringApplication.run(QQBotApplication.class, args);
|
||||
|
30
src/main/java/com/yutou/qqbot/data/baidu/Message.java
Normal file
30
src/main/java/com/yutou/qqbot/data/baidu/Message.java
Normal file
@ -0,0 +1,30 @@
|
||||
package com.yutou.qqbot.data.baidu;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Message {
|
||||
private String role = "user";
|
||||
private String content;
|
||||
|
||||
public Message() {
|
||||
}
|
||||
|
||||
|
||||
public boolean checkIsUser() {
|
||||
return "user".equals(role);
|
||||
}
|
||||
|
||||
public static Message create(String message) {
|
||||
return create(message, false);
|
||||
}
|
||||
|
||||
public static Message create(String message, boolean isGTP) {
|
||||
Message msg = new Message();
|
||||
msg.content = message;
|
||||
if (isGTP) {
|
||||
msg.role = "assistant";
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.yutou.qqbot.data.baidu;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ResponseMessage {
|
||||
private String id;
|
||||
private String object;
|
||||
private long created;
|
||||
private String result;
|
||||
private boolean isTruncated;
|
||||
private boolean needClearHistory;
|
||||
private Message usage;
|
||||
|
||||
public ResponseMessage() {
|
||||
}
|
||||
}
|
42
src/main/java/com/yutou/qqbot/models/Commands/BaiduGPT.java
Normal file
42
src/main/java/com/yutou/qqbot/models/Commands/BaiduGPT.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.yutou.qqbot.models.Commands;
|
||||
|
||||
import com.yutou.qqbot.Annotations.UseModel;
|
||||
import com.yutou.qqbot.QQBotManager;
|
||||
import com.yutou.qqbot.data.baidu.ResponseMessage;
|
||||
import com.yutou.qqbot.models.Model;
|
||||
import com.yutou.qqbot.utlis.BaiduGPTManager;
|
||||
import net.mamoe.mirai.event.events.MessageEvent;
|
||||
|
||||
@UseModel
|
||||
public class BaiduGPT extends Model {
|
||||
|
||||
@Override
|
||||
public boolean isUserPublic() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getUsePowers() {
|
||||
return new String[]{
|
||||
QQGroupCommands.GPT,
|
||||
QQGroupCommands.GPT_CLEAR
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getModelName() {
|
||||
return "百度文言一心GPT";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
|
||||
super.onMessage(qq, event, isGroup);
|
||||
if (msg.equals(QQGroupCommands.GPT_CLEAR)) {
|
||||
BaiduGPTManager.getManager().clear();
|
||||
QQBotManager.getInstance().sendMessage(qq, "已经失忆捏");
|
||||
} else if (isAt()) {
|
||||
ResponseMessage message = BaiduGPTManager.getManager().sendMessage(String.valueOf(qq), msg.replace("@2476945931", "").trim());
|
||||
QQBotManager.getInstance().sendMessage(qq, message.getResult());
|
||||
}
|
||||
}
|
||||
}
|
@ -44,6 +44,9 @@ public abstract class Model implements ModelInterface {
|
||||
public final static String QQ_WOODEN = "!电子木鱼";
|
||||
public final static String QQ_TIMEOUT = "!timer";
|
||||
|
||||
public final static String GPT="!百度gpt";
|
||||
public final static String GPT_CLEAR="!百度失忆";
|
||||
|
||||
}
|
||||
|
||||
public static class QQFromCommands {
|
||||
@ -117,4 +120,7 @@ public abstract class Model implements ModelInterface {
|
||||
chain.add(text);
|
||||
return chain;
|
||||
}
|
||||
public boolean isAt(){
|
||||
return msg.contains("@2476945931");
|
||||
}
|
||||
}
|
||||
|
82
src/main/java/com/yutou/qqbot/utlis/BaiduGPTManager.java
Normal file
82
src/main/java/com/yutou/qqbot/utlis/BaiduGPTManager.java
Normal file
@ -0,0 +1,82 @@
|
||||
package com.yutou.qqbot.utlis;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.qqbot.data.baidu.Message;
|
||||
import com.yutou.qqbot.data.baidu.ResponseMessage;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BaiduGPTManager {
|
||||
private static int MAX_MESSAGE = 5;
|
||||
private static BaiduGPTManager manager;
|
||||
private static final String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions";
|
||||
private static final String AppID = "36668599";
|
||||
private static final String ApiKey = "eyHo6K2ILBm7i378701Az1eT";
|
||||
private static final String SecretKey = "U4vXt8AOTM9FgB0Omft5IOh6vwhzoDgZ";
|
||||
private final Map<String, List<Message>> msgMap;
|
||||
|
||||
private BaiduGPTManager() {
|
||||
msgMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public static BaiduGPTManager getManager() {
|
||||
if (manager == null) {
|
||||
manager = new BaiduGPTManager();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
|
||||
public int setMaxMessageCount(int count) {
|
||||
MAX_MESSAGE = count;
|
||||
return MAX_MESSAGE;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
msgMap.clear();
|
||||
}
|
||||
|
||||
private String getToken() {
|
||||
String _url = String.format("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
|
||||
, ApiKey
|
||||
, SecretKey
|
||||
);
|
||||
String get = HttpTools.get(_url);
|
||||
JSONObject response = JSONObject.parseObject(get);
|
||||
return response.getString("access_token");
|
||||
}
|
||||
|
||||
public ResponseMessage sendMessage(String user, String message) {
|
||||
List<Message> messages = msgMap.getOrDefault(user, new ArrayList<>());
|
||||
if (messages.size() > MAX_MESSAGE * 2) {
|
||||
messages.remove(0);
|
||||
messages.remove(1);
|
||||
}
|
||||
messages.add(Message.create(message));
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("messages", messages);
|
||||
System.out.println("json = " + json);
|
||||
String post = HttpTools.post(url + "?access_token=" + getToken()
|
||||
, json.toJSONString().getBytes(StandardCharsets.UTF_8));
|
||||
System.out.println("post = " + post);
|
||||
ResponseMessage response = JSONObject.parseObject(post, ResponseMessage.class);
|
||||
messages.add(Message.create(response.getResult(), true));
|
||||
msgMap.put(user, messages);
|
||||
System.out.println("\n\n");
|
||||
return response;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
BaiduGPTManager.getManager().sendMessage("test", "定义个变量西瓜,它的值是5");
|
||||
Thread.sleep(1000);
|
||||
BaiduGPTManager.getManager().sendMessage("test", "西瓜的值的多少");
|
||||
Thread.sleep(1000);
|
||||
BaiduGPTManager.getManager().clear();
|
||||
BaiduGPTManager.getManager().sendMessage("test2", "西瓜的值的多少");
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user