import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main extends JavaPlugin {
private static final String CHATGPT_API_ENDPOINT = "https://api.openai.com/v1/engine/davinci-codex/completions";
private static final String CHATGPT_API_KEY = "API KEY HERE";
@Override
public void onEnable() {
getLogger().info("MC-GPT has been enabled!");
}
@Override
public void onDisable() {
getLogger().info("MC-GPT has been disabled!");
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("chatgpt")) {
if (sender instanceof Player) {
Player player = (Player) sender;
String input = String.join(" ", args);
String response = getChatGPTResponse(input);
player.sendMessage(ChatColor.BLUE + ">MC-GPT: " + ChatColor.RESET + response);
} else {
sender.sendMessage(ChatColor.RED + "Cette commande ne peut être exécutée que par un joueur.");
}
return true;
}
return false;
}
private String getChatGPTResponse(String input) {
try {
URL url = new URL(CHATGPT_API_ENDPOINT);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Bearer " + CHATGPT_API_KEY);
con.setRequestProperty("Content-Type", "application/json");
String data = String.format("{\"prompt\": \"%s\",\"max_tokens\": 50,\"temperature\": 0.7}", input);
con.setDoOutput(true);
con.getOutputStream().write(data.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
JSONObject jsonObject = new JSONObject(response.toString());
return jsonObject.getJSONArray("choices").getJSONObject(0).getString("text");
} catch (IOException e) {
getLogger().warning("Erreur lors de l'appel de l'API ChatGPT: " + e.getMessage());
return "Une erreur s'est produite lors de l'appel de l'API ChatGPT.";
}
}
}```
I don't know why but my code doesn't work, for this code, I used **spigot** and **JSONObject**
#Problem with api in java
7 messages · Page 1 of 1 (latest)
What exactly doesn't work?
Also, I see that you're using https://api.openai.com/v1/engine/davinci-codex/completions", this isn't a valid endpoint
The correct endpoint is https://api.openai.com/v1/completions
and you also need a model parameter, likely text-davinci-003 in your case
also since you're doing this in minecraft, you probably don't want to make API requests like this in the main thread, you should wrap things in a BukkitRunnable or a Runnable and run it asynchronously, since these API requests can take a long time, especially if it runs into a rate limit
@harsh sonnet thank you very much, have a good day