Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
target
pom.xml
CraftGPT.iml
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.12.0</version>
<version>0.14.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -106,5 +106,10 @@
<version>4.3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.zawn.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.10.6</version>
</dependency>
</dependencies>
</project>
23 changes: 21 additions & 2 deletions src/main/java/com/theaiguy_/craftgpt/gpt.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.theaiguy_.craftgpt;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.theokanning.openai.client.OpenAiApi;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.service.OpenAiService;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
Expand All @@ -22,6 +29,7 @@ public class gpt implements CommandExecutor
public static final HashMap<String, List<ChatMessage>> messages = new HashMap<>();
private static final HashMap<String, Long> cooldowns = new HashMap<>();
static Long cooldownMs = config.getLong("cooldown");
static String baseUrl = config.getString("chatgpt.base-url");
static String token = config.getString("chatgpt.token");


Expand Down Expand Up @@ -63,7 +71,18 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
{
adventure.sender(sender).sendMessage(getFormattedString("messages.generating"));

OpenAiService service = new OpenAiService(Objects.requireNonNull(token), Duration.ofMinutes(3));
ObjectMapper mapper = OpenAiService.defaultObjectMapper();
OkHttpClient client = OpenAiService.defaultClient(Objects.requireNonNull(token), Duration.ofMinutes(3));

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Objects.requireNonNull(baseUrl))
.client(client)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();

OpenAiService service = new OpenAiService(retrofit.create(OpenAiApi.class), client.dispatcher().executorService());

ChatCompletionRequest.ChatCompletionRequestBuilder completionRequestBuilder = ChatCompletionRequest.builder()
.messages(messages.get(sender.getName()))
.model(config.getString("chatgpt.model"))
Expand Down Expand Up @@ -94,4 +113,4 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
});
return true;
}
}
}
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ chatgpt:
# NOTE: This is only stored locally and is not used for anything other than making requests.
token: ""

# API base url.
# Change this if you wish to use alternative API endpoints (e.g. https://api.openai-proxy.com/).
# Make sure that it is compatible with OpenAI API calls.
base-url: https://api.openai.com/

# ID of the model to use.
# See https://platform.openai.com/docs/models/model-endpoint-compatibility
# for details on which models work with the Chat API.
Expand Down