Reworked the ChatGPT JSON request programmatically
This commit is contained in:
parent
7931e4adbe
commit
cf0bafff94
3 changed files with 15 additions and 18 deletions
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue