mirror of
https://github.com/mii443/ChatGPTBot.git
synced 2025-08-22 15:05:26 +00:00
ChatGPT
This commit is contained in:
11
build.gradle
11
build.gradle
@ -1,9 +1,13 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
|
id 'com.github.johnrengelman.shadow' version '6.0.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'codes.mii'
|
group = 'codes.mii'
|
||||||
version = '1.0-SNAPSHOT'
|
version = '1.0-SNAPSHOT'
|
||||||
|
compileJava.options.encoding = 'UTF-8'
|
||||||
|
compileTestJava.options.encoding = 'UTF-8'
|
||||||
|
javadoc.options.encoding = 'UTF-8'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@ -19,6 +23,8 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly "org.spigotmc:spigot-api:1.19.3-R0.1-SNAPSHOT"
|
compileOnly "org.spigotmc:spigot-api:1.19.3-R0.1-SNAPSHOT"
|
||||||
|
implementation 'com.theokanning.openai-gpt3-java:service:0.11.0'
|
||||||
|
implementation 'com.google.code.gson:gson:2.10.1'
|
||||||
}
|
}
|
||||||
|
|
||||||
def targetJavaVersion = 17
|
def targetJavaVersion = 17
|
||||||
@ -45,3 +51,8 @@ processResources {
|
|||||||
expand props
|
expand props
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
project.configurations.implementation.canBeResolved = true
|
||||||
|
configurations = [project.configurations.implementation]
|
||||||
|
}
|
28
src/main/java/codes/mii/chatgptbot/ChatGPT.java
Normal file
28
src/main/java/codes/mii/chatgptbot/ChatGPT.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package codes.mii.chatgptbot;
|
||||||
|
|
||||||
|
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
|
||||||
|
import com.theokanning.openai.completion.chat.ChatMessage;
|
||||||
|
import com.theokanning.openai.service.OpenAiService;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatGPT {
|
||||||
|
public static ChatGPTResponse chat(ChatGPTInput input) {
|
||||||
|
FileConfiguration config = ChatGPTBot.getPlugin(ChatGPTBot.class).getConfig();
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String message = config.getString("base_input") + "\n" + gson.toJson(input);
|
||||||
|
|
||||||
|
OpenAiService service = new OpenAiService(config.getString("openai_api_key"));
|
||||||
|
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
|
||||||
|
.model(config.getString("model"))
|
||||||
|
.messages(List.of(new ChatMessage("user", message)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String response = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage().getContent();
|
||||||
|
|
||||||
|
return gson.fromJson(response, ChatGPTResponse.class);
|
||||||
|
}
|
||||||
|
}
|
@ -3,15 +3,9 @@ package codes.mii.chatgptbot;
|
|||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public final class ChatGPTBot extends JavaPlugin {
|
public final class ChatGPTBot extends JavaPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
// Plugin startup logic
|
saveDefaultConfig();
|
||||||
|
getServer().getPluginManager().registerEvents(new OnAsyncPlayerChatEvent(), this);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDisable() {
|
|
||||||
// Plugin shutdown logic
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
src/main/java/codes/mii/chatgptbot/ChatGPTInput.java
Normal file
15
src/main/java/codes/mii/chatgptbot/ChatGPTInput.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package codes.mii.chatgptbot;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatGPTInput {
|
||||||
|
public String username;
|
||||||
|
public String message;
|
||||||
|
public List<Double> position;
|
||||||
|
|
||||||
|
public ChatGPTInput(String username, String message, List<Double> position) {
|
||||||
|
this.username = username;
|
||||||
|
this.message = message;
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
}
|
15
src/main/java/codes/mii/chatgptbot/ChatGPTResponse.java
Normal file
15
src/main/java/codes/mii/chatgptbot/ChatGPTResponse.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package codes.mii.chatgptbot;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatGPTResponse {
|
||||||
|
public String response_message;
|
||||||
|
public String to;
|
||||||
|
public List<String> commands;
|
||||||
|
|
||||||
|
public ChatGPTResponse(String response_message, String to, List<String> commands) {
|
||||||
|
this.response_message = response_message;
|
||||||
|
this.to = to;
|
||||||
|
this.commands = commands;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package codes.mii.chatgptbot;
|
||||||
|
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.bukkit.Bukkit.getServer;
|
||||||
|
|
||||||
|
public class OnAsyncPlayerChatEvent implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent e) {
|
||||||
|
FileConfiguration config = ChatGPTBot.getPlugin(ChatGPTBot.class).getConfig();
|
||||||
|
|
||||||
|
// check prefix
|
||||||
|
String prefix = config.getString("chat_prefix");
|
||||||
|
if (!e.getMessage().startsWith(Objects.requireNonNull(prefix))) return;
|
||||||
|
|
||||||
|
String message = e.getMessage().replaceFirst(prefix, "");
|
||||||
|
String username = e.getPlayer().getName();
|
||||||
|
Location location = e.getPlayer().getLocation();
|
||||||
|
List<Double> position = List.of(location.getX(), location.getY(), location.getZ());
|
||||||
|
|
||||||
|
BukkitRunnable runnable = new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// generate response
|
||||||
|
ChatGPTInput request = new ChatGPTInput(username, message, position);
|
||||||
|
ChatGPTResponse response = ChatGPT.chat(request);
|
||||||
|
|
||||||
|
// format
|
||||||
|
String response_message = Objects.requireNonNull(config.getString("response_format"))
|
||||||
|
.replace("%TO%", response.to)
|
||||||
|
.replace("%MESSAGE%", response.response_message);
|
||||||
|
getServer().broadcastMessage(response_message);
|
||||||
|
|
||||||
|
// dispatch commands
|
||||||
|
getServer().getScheduler().runTask(ChatGPTBot.getPlugin(ChatGPTBot.class), () -> {
|
||||||
|
response.commands.forEach(raw_command -> {
|
||||||
|
String command = raw_command.replaceFirst("/", "");
|
||||||
|
getServer().getLogger().info("ChatGPT: /" + command);
|
||||||
|
|
||||||
|
if (config.getBoolean("dispatch_command_as_player")) {
|
||||||
|
getServer().dispatchCommand(e.getPlayer(), command);
|
||||||
|
} else {
|
||||||
|
getServer().dispatchCommand(ChatGPTBot.getPlugin(ChatGPTBot.class).getServer().getConsoleSender(), command);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Thread chatgptThread = new Thread(runnable);
|
||||||
|
chatgptThread.start();
|
||||||
|
}
|
||||||
|
}
|
16
src/main/resources/config.yml
Normal file
16
src/main/resources/config.yml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
openai_api_key: "YOUR OPENAI API KEY"
|
||||||
|
model: "gpt-3.5-turbo"
|
||||||
|
response_format: "<ChatGPT> %MESSAGE%"
|
||||||
|
chat_prefix: "chatgpt "
|
||||||
|
dispatch_command_as_player: true
|
||||||
|
|
||||||
|
base_input: |
|
||||||
|
あなたはMinecraftサーバー内で全権限を持っています。プレイヤーの指示に従ってください。
|
||||||
|
|
||||||
|
入力
|
||||||
|
{"username": 名,"message": メッセージ,"position": [x, y, z]}
|
||||||
|
|
||||||
|
出力
|
||||||
|
{"response_message": 返信,"to": 相手の名前,"commands": [Minecraftコマンドをこの配列へ記述。]}
|
||||||
|
|
||||||
|
会話がこれから始まります。
|
Reference in New Issue
Block a user