-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在Minecraft Java服务器中,回答的时候服务器会卡一下 #318
Comments
请使用异步 |
不太懂,有其他解决方案吗 |
请贴出代码 |
package top.peacefuly.minecraftChatGPT.command;
import com.plexpt.chatgpt.ChatGPT;
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;
import com.plexpt.chatgpt.entity.chat.Message;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import top.peacefuly.minecraftChatGPT.MinecraftChatGPT;
import java.util.Arrays;
public class ChatCommand implements CommandExecutor {
private final MinecraftChatGPT plugin;
public ChatCommand(MinecraftChatGPT plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
String apikey = this.plugin.getConfig().getString("apikey");
String apihost = this.plugin.getConfig().getString("apihost");
String gptModel = this.plugin.getConfig().getString("gpt-model");
int maxtokens = this.plugin.getConfig().getInt("max-tokens");
if (apikey == null || apikey.isEmpty() || apikey.equals("sk-xxxxxx")) {
commandSender.sendMessage("Please set apikey in config.yml");
return true;
}
ChatGPT chatGPT = ChatGPT.builder()
.apiKey(apikey)
.apiHost(apihost)
.timeout(15000)
.build()
.init();
if (commandSender instanceof Player p){
String question = String.join(" ", strings);
Message message = Message.of(question);
ChatCompletion chatCompletion = ChatCompletion.builder()
.model(gptModel)
.messages(Arrays.asList(message))
.maxTokens(maxtokens)
.stream(false)
.build();
ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
p.sendMessage(response.getChoices().get(0).getMessage().getContent());
}
return true;
}
} |
使用 stream 模式 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
recording.mp4
The text was updated successfully, but these errors were encountered: