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