Reworked the ChatGPT JSON request programmatically

This commit is contained in:
Erik C. Thauvin 2024-03-03 22:10:00 -08:00
parent 7931e4adbe
commit cf0bafff94
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 15 additions and 18 deletions

1
.idea/misc.xml generated
View file

@ -6,6 +6,7 @@
<pattern value="net.thauvin.erik.MobibotBuild" /> <pattern value="net.thauvin.erik.MobibotBuild" />
<pattern value="net.thauvin.erik.MobibotBuild" method="detekt" /> <pattern value="net.thauvin.erik.MobibotBuild" method="detekt" />
<pattern value="net.thauvin.erik.MobibotBuild" method="detektBaseline" /> <pattern value="net.thauvin.erik.MobibotBuild" method="detektBaseline" />
<pattern value="net.thauvin.erik.MobibotBuild" method="rootPom" />
</component> </component>
<component name="PDMPlugin"> <component name="PDMPlugin">
<option name="customRuleSets"> <option name="customRuleSets">

View file

@ -101,11 +101,11 @@ public class MobibotBuild extends Project {
.include(dependency("com.squareup.okhttp3", "okhttp", "4.12.0")) .include(dependency("com.squareup.okhttp3", "okhttp", "4.12.0"))
.include(dependency("net.aksingh", "owm-japis", "2.5.3.0")) .include(dependency("net.aksingh", "owm-japis", "2.5.3.0"))
.include(dependency("net.objecthunter", "exp4j", "0.4.8")) .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")) .include(dependency("org.jsoup", "jsoup", "1.17.2"))
// Thauvin // Thauvin
.include(dependency("net.thauvin.erik", "cryptoprice", "1.0.3-SNAPSHOT")) .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", "pinboard-poster", "1.1.2-SNAPSHOT"))
.include(dependency("net.thauvin.erik.urlencoder", "urlencoder-lib-jvm", "1.4.0")); .include(dependency("net.thauvin.erik.urlencoder", "urlencoder-lib-jvm", "1.4.0"));
scope(test) scope(test)

View file

@ -34,6 +34,7 @@ package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
@ -98,31 +99,26 @@ class ChatGpt : AbstractModule() {
// ChatGPT command // ChatGPT command
private const val CHATGPT_CMD = "chatgpt" private const val CHATGPT_CMD = "chatgpt"
@JvmStatic @JvmStatic
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun chat(query: String, apiKey: String?, maxTokens: Int): String { fun chat(query: String, apiKey: String?, maxTokens: Int): String {
if (!apiKey.isNullOrEmpty()) { 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() val request = HttpRequest.newBuilder()
.uri(URI.create(API_URL)) .uri(URI.create(API_URL))
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.header("Authorization", "Bearer $apiKey") .header("Authorization", "Bearer $apiKey")
.header("User-Agent", Constants.USER_AGENT) .header("User-Agent", Constants.USER_AGENT)
.POST( .POST(HttpRequest.BodyPublishers.ofString(jsonObject.toString()))
HttpRequest.BodyPublishers.ofString(
"""{
"model": "gpt-3.5-turbo-1106",
"max_tokens": $maxTokens,
"messages": [
{
"role": "user",
"content": "$content"
}
]
}""".trimIndent()
)
)
.build() .build()
try { try {
val response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()) val response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())