Skip to content
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

Open
MCheping8108 opened this issue Sep 4, 2024 · 5 comments
Open

Comments

@MCheping8108
Copy link

recording.mp4
@PlexPt
Copy link
Owner

PlexPt commented Sep 4, 2024

请使用异步

@MCheping8108
Copy link
Author

请使用异步

不太懂,有其他解决方案吗

@PlexPt
Copy link
Owner

PlexPt commented Sep 4, 2024

请贴出代码

@MCheping8108
Copy link
Author

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;
    }
}

@PlexPt
Copy link
Owner

PlexPt commented Sep 4, 2024

使用 stream 模式

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants