diff --git a/.idea/misc.xml b/.idea/misc.xml index 26293d3..f63c128 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,6 +6,7 @@ + diff --git a/src/bld/java/net/thauvin/erik/MobibotBuild.java b/src/bld/java/net/thauvin/erik/MobibotBuild.java index 6f4e7cc..087affa 100644 --- a/src/bld/java/net/thauvin/erik/MobibotBuild.java +++ b/src/bld/java/net/thauvin/erik/MobibotBuild.java @@ -101,11 +101,11 @@ public class MobibotBuild extends Project { .include(dependency("com.squareup.okhttp3", "okhttp", "4.12.0")) .include(dependency("net.aksingh", "owm-japis", "2.5.3.0")) .include(dependency("net.objecthunter", "exp4j", "0.4.8")) - .include(dependency("org.json", "json", "20240205")) + .include(dependency("org.json", "json", "20240303")) .include(dependency("org.jsoup", "jsoup", "1.17.2")) // Thauvin .include(dependency("net.thauvin.erik", "cryptoprice", "1.0.3-SNAPSHOT")) - .include(dependency("net.thauvin.erik", "jokeapi", "0.9.1")) + .include(dependency("net.thauvin.erik", "jokeapi", "0.9.2-SNAPSHOT")) .include(dependency("net.thauvin.erik", "pinboard-poster", "1.1.2-SNAPSHOT")) .include(dependency("net.thauvin.erik.urlencoder", "urlencoder-lib-jvm", "1.4.0")); scope(test) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt index b94b5d7..c7ba4ba 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt @@ -34,6 +34,7 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils.sendMessage +import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import org.pircbotx.hooks.types.GenericMessageEvent @@ -98,31 +99,26 @@ class ChatGpt : AbstractModule() { // ChatGPT command private const val CHATGPT_CMD = "chatgpt" - @JvmStatic @Throws(ModuleException::class) fun chat(query: String, apiKey: String?, maxTokens: Int): String { if (!apiKey.isNullOrEmpty()) { - val content = query.replace("\"", "\\\"") + val jsonObject = JSONObject() + jsonObject.put("model", "gpt-3.5-turbo-1106") + jsonObject.put("max_tokens", maxTokens) + val message = JSONObject() + message.put("role", "user") + message.put("content", query) + val messages = JSONArray() + messages.put(message) + jsonObject.put("messages", messages) + val request = HttpRequest.newBuilder() .uri(URI.create(API_URL)) .header("Content-Type", "application/json") .header("Authorization", "Bearer $apiKey") .header("User-Agent", Constants.USER_AGENT) - .POST( - HttpRequest.BodyPublishers.ofString( - """{ - "model": "gpt-3.5-turbo-1106", - "max_tokens": $maxTokens, - "messages": [ - { - "role": "user", - "content": "$content" - } - ] - }""".trimIndent() - ) - ) + .POST(HttpRequest.BodyPublishers.ofString(jsonObject.toString())) .build() try { val response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())