Aktualizovat src/main/java/it/ohalee/minecraftgpt/Main.java

This commit is contained in:
Radim Lipovčan 2024-03-29 18:46:41 +01:00
parent 8878ddf5e5
commit 73a96ccf59

View File

@ -24,7 +24,7 @@ public class Main extends JavaPlugin {
public static Cache<Player, List<ChatMessage>> CACHE;
public static Cache<Player, Type> USER_TYPE = CacheBuilder.newBuilder().build();
@Override
public void onEnable() {
saveDefaultConfig();
@ -44,7 +44,7 @@ public class Main extends JavaPlugin {
notification.getKey().sendMessage(Messages.format(getConfig().getString("command.toggle.disabled")));
}
}).build();
OpenAiService service = new CustomOpenAiService(openAiKey.getKey(), Duration.ofSeconds(150));
String priority = getConfig().getString("chat-priority", "HIGH").toUpperCase();
Class<AsyncPlayerChatEvent> eventClass = AsyncPlayerChatEvent.class;
getServer().getPluginManager().registerEvent(eventClass, new ChatHandler(this), EventPriority.valueOf(priority), (listener, event) -> {
@ -66,6 +66,37 @@ public class Main extends JavaPlugin {
getLogger().info("Plugin enabled!");
}
public class CustomOpenAiService extends OpenAiService {
private final ExecutorService executorService;
private final String openaiBaseUrl = "http://frigate.lipovcan.cz:3001";
public CustomOpenAiService(String token, Duration timeout) {
super(token, timeout);
ObjectMapper mapper = OpenAiService.defaultObjectMapper();
OkHttpClient client = OpenAiService.defaultClient(token, timeout);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(openaiBaseUrl)
.client(client)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
OpenAiApi customApi = retrofit.create(OpenAiApi.class);
// 使用反射设置 OpenAiService 类的 api 字段
try {
Field apiField = OpenAiService.class.getDeclaredField("api");
apiField.setAccessible(true);
apiField.set(this, customApi);
} catch (NoSuchFieldException | IllegalAccessException e) {
// 处理异常
e.printStackTrace();
}
this.executorService = client.dispatcher().executorService();
}
}
@Override
public void onDisable() {
getLogger().info("Plugin disabled!");