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

feat: Use LabyMod's Request/Response system #23

Closed
wants to merge 6 commits into from
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
24 changes: 4 additions & 20 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
version = "0.1.0"

plugins {
id("java-library")
}
import net.labymod.labygradle.common.extension.LabyModAnnotationProcessorExtension.ReferenceType

dependencies {
labyProcessor()
labyApi("api")

// If you want to use external libraries, you can do that here.
// The dependencies that are specified here are loaded into your project but will also
// automatically be downloaded by labymod, but only if the repository is public.
// If it is private, you have to add and compile the dependency manually.
// You have to specify the repository, there are getters for maven central and sonatype, every
// other repository has to be specified with their url. Example:
// maven(mavenCentral(), "org.apache.httpcomponents:httpclient:4.5.13")
}

labyModProcessor {
referenceType = net.labymod.gradle.core.processor.ReferenceType.INTERFACE
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
labyModAnnotationProcessor {
referenceType = ReferenceType.INTERFACE
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rappytv.labygpt.api;

import com.google.gson.annotations.SerializedName;

public class GPTMessage {
public String content;
public GPTRole role;
Expand All @@ -10,4 +12,13 @@ public GPTMessage(String content, GPTRole role, String name) {
this.role = role;
this.name = name;
}

public enum GPTRole {
@SerializedName("system")
System,
@SerializedName("user")
User,
@SerializedName("assistant")
Assistant
}
}
69 changes: 69 additions & 0 deletions api/src/main/java/com/rappytv/labygpt/api/GPTRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.rappytv.labygpt.api;

import com.google.gson.Gson;
import com.rappytv.labygpt.api.GPTMessage.GPTRole;
import net.labymod.api.util.I18n;
import net.labymod.api.util.io.web.request.Request;
import net.labymod.api.util.io.web.request.Request.Method;
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Consumer;

public class GPTRequest {

private final static Gson gson = new Gson();
public static final ArrayList<GPTMessage> queryHistory = new ArrayList<>();

public static void sendRequestAsync(String query, String key, String username,
String model, String behavior, Consumer<ApiResponse> responseConsumer) {

if(queryHistory.isEmpty()) {
queryHistory.add(new GPTMessage(behavior, GPTRole.System, "System"));
}
queryHistory.add(new GPTMessage(query, GPTRole.User, username));

Map<String, String> body = Map.of(
"model", model,
"messages", gson.toJson(queryHistory),
"user", username
);

Request.ofGson(ResponseBody.class)
.url("https://api.openai.com/v1/chat/completions")
.method(Method.POST)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + key)
.json(body)
.handleErrorStream()
.async()
.execute(response -> {
boolean successful;
String output = null;
String error = null;
ResponseBody responseBody = response.get();

if(response.hasException()) {
successful = false;
error = response.exception().getLocalizedMessage();
} else if(responseBody.error != null) {
error = responseBody.error.message;
if (error.isEmpty() && responseBody.error.code.equals("invalid_api_key")) {
error = I18n.translate("labygpt.messages.invalidBearer");
}
successful = false;
} else if(responseBody.choices.isEmpty()) {
successful = false;
} else {
GPTMessage message = responseBody.choices.getFirst().message;
output = message.content.replace("\n\n", "");
queryHistory.add(new GPTMessage(output, GPTRole.Assistant, username));
successful = true;
}
responseConsumer.accept(new ApiResponse(successful, output, error));
});
}

public record ApiResponse(boolean successful, String output, String error) {

}
}
94 changes: 17 additions & 77 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,99 +1,39 @@
plugins {
id("java-library")
id("net.labymod.gradle")
id("net.labymod.gradle.addon")
id("net.labymod.labygradle")
id("net.labymod.labygradle.addon")
}

group = "org.example"
version = "1.0.0"

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))
val versions = providers.gradleProperty("net.labymod.minecraft-versions").get().split(";")

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}
group = "org.example"
version = providers.environmentVariable("VERSION").getOrElse("1.1.3")

labyMod {
defaultPackageName = "com.rappytv.labygpt" //change this to your main package name (used by all modules)
defaultPackageName = "com.rappytv.labygpt.core"
addonInfo {
namespace = "labygpt"
displayName = "LabyGPT"
author = "RappyTV"
description = "Communicate with ChatGPT right in your Chat. Powered by OpenAI."
minecraftVersion = "*"
version = System.getenv().getOrDefault("VERSION", "1.1.3")
version = rootProject.version.toString()
}

minecraft {
registerVersions(
"1.8.9",
"1.12.2",
"1.16.5",
"1.17.1",
"1.18.2",
"1.19.2",
"1.19.3",
"1.19.4",
"1.20.1",
"1.20.2",
"1.20.4"
) { version, provider ->
configureRun(provider, version)
}

subprojects.forEach {
if (it.name != "game-runner") {
filter(it.name)
registerVersion(versions.toTypedArray()) {
runs {
getByName("client") {
devLogin = true
}
}
}
}

addonDev {
productionRelease()
}
}

subprojects {
plugins.apply("java-library")
plugins.apply("net.labymod.gradle")
plugins.apply("net.labymod.gradle.addon")

repositories {
maven("https://libraries.minecraft.net/")
maven("https://repo.spongepowered.org/repository/maven-public/")
}
}

fun configureRun(provider: net.labymod.gradle.core.minecraft.provider.VersionProvider, gameVersion: String) {
provider.runConfiguration {
mainClass = "net.minecraft.launchwrapper.Launch"
jvmArgs("-Dnet.labymod.running-version=${gameVersion}")
jvmArgs("-Dmixin.debug=true")
jvmArgs("-Dnet.labymod.debugging.all=true")
jvmArgs("-Dmixin.env.disableRefMap=true")
plugins.apply("net.labymod.labygradle")
plugins.apply("net.labymod.labygradle.addon")

args("--tweakClass", "net.labymod.core.loader.vanilla.launchwrapper.LabyModLaunchWrapperTweaker")
args("--labymod-dev-environment", "true")
args("--addon-dev-environment", "true")
}

provider.javaVersion = when (gameVersion) {
else -> {
JavaVersion.VERSION_17
}
}

provider.mixin {
val mixinMinVersion = when (gameVersion) {
"1.8.9", "1.12.2", "1.16.5" -> {
"0.6.6"
}

else -> {
"0.8.2"
}
}

minVersion = mixinMinVersion
}
}
group = rootProject.group
version = rootProject.version
}
29 changes: 6 additions & 23 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
version = "0.1.0"

plugins {
id("java-library")
}

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}
import net.labymod.labygradle.common.extension.LabyModAnnotationProcessorExtension.ReferenceType

dependencies {
labyProcessor()
api(project(":api"))

// If you want to use external libraries, you can do that here.
// The dependencies that are specified here are loaded into your project but will also
// automatically be downloaded by labymod, but only if the repository is public.
// If it is private, you have to add and compile the dependency manually.
// You have to specify the repository, there are getters for maven central and sonatype, every
// other repository has to be specified with their url. Example:
// maven(mavenCentral(), "org.apache.httpcomponents:httpclient:4.5.13")
}

labyModProcessor {
referenceType = net.labymod.gradle.core.processor.ReferenceType.DEFAULT
// An example of how to add an external dependency that is used by the addon.
// addonMavenDependency("org.jeasy:easy-random:5.0.0")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
labyModAnnotationProcessor {
referenceType = ReferenceType.DEFAULT
}
85 changes: 0 additions & 85 deletions core/src/main/java/com/rappytv/labygpt/api/GPTRequest.java

This file was deleted.

12 changes: 0 additions & 12 deletions core/src/main/java/com/rappytv/labygpt/api/GPTRole.java

This file was deleted.

16 changes: 0 additions & 16 deletions core/src/main/java/com/rappytv/labygpt/api/RequestBody.java

This file was deleted.

Loading
Loading