diff --git a/properties/mobibot.properties b/properties/mobibot.properties index bc31e8a..a7526cc 100644 --- a/properties/mobibot.properties +++ b/properties/mobibot.properties @@ -87,6 +87,7 @@ disabled-modules=mastodon # gcloud config set project PROJECT_ID # gcloud auth login LOGIN # gcloud auth application-default login +# #gemini-project-id= #gemini-location=us-west1 #gemini-max-tokens=1024 diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/ReleaseInfo.kt b/src/main/kotlin/net/thauvin/erik/mobibot/ReleaseInfo.kt index f75b334..f805722 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/ReleaseInfo.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/ReleaseInfo.kt @@ -14,12 +14,12 @@ import java.time.ZoneId */ object ReleaseInfo { const val PROJECT = "mobibot" - const val VERSION = "0.8.0-rc+20231217213124" + const val VERSION = "0.8.0-rc+20231218140603" @JvmField @Suppress("MagicNumber") val BUILD_DATE: LocalDateTime = LocalDateTime.ofInstant( - Instant.ofEpochMilli(1702877484912L), ZoneId.systemDefault() + Instant.ofEpochMilli(1702937164085L), ZoneId.systemDefault() ) const val WEBSITE = "https://www.mobitopia.org/mobibot/" 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 bd92332..e2ad717 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt @@ -34,10 +34,8 @@ 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.apache.commons.text.WordUtils import org.json.JSONException import org.json.JSONObject -import org.json.JSONWriter import org.pircbotx.hooks.types.GenericMessageEvent import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -48,7 +46,7 @@ import java.net.http.HttpRequest import java.net.http.HttpResponse class ChatGpt : AbstractModule() { - private val logger: Logger = LoggerFactory.getLogger(ChatGpt::class.java) + val logger: Logger = LoggerFactory.getLogger(ChatGpt::class.java) override val name = CHATGPT_NAME @@ -60,7 +58,7 @@ class ChatGpt : AbstractModule() { properties.getOrDefault(MAX_TOKENS_PROP, "1024").toInt() ) if (answer.isNotBlank()) { - event.sendMessage(WordUtils.wrap(answer, 400)) + event.sendMessage(answer) } else { event.respond("$name is stumped.") } @@ -95,7 +93,7 @@ class ChatGpt : AbstractModule() { const val MAX_TOKENS_PROP = "chatgpt-max-tokens" // ChatGPT API URL - private const val API_URL = "https://api.openai.com/v1/completions" + private const val API_URL = "https://api.openai.com/v1/chat/completions" // ChatGPT command private const val CHATGPT_CMD = "chatgpt" @@ -105,7 +103,7 @@ class ChatGpt : AbstractModule() { @Throws(ModuleException::class) fun chat(query: String, apiKey: String?, maxTokens: Int): String { if (!apiKey.isNullOrEmpty()) { - val prompt = JSONWriter.valueToString("Q:$query\nA:") + val content = query.replace("\"", "\\\"") val request = HttpRequest.newBuilder() .uri(URI.create(API_URL)) .header("Content-Type", "application/json") @@ -114,14 +112,15 @@ class ChatGpt : AbstractModule() { .POST( HttpRequest.BodyPublishers.ofString( """{ - "model": "text-davinci-003", - "prompt": $prompt, - "temperature": 0, - "max_tokens": $maxTokens, - "top_p": 1, - "frequency_penalty": 0, - "presence_penalty": 0 - }""".trimIndent() + "model": "gpt-3.5-turbo-1106", + "max_tokens": $maxTokens, + "messages": [ + { + "role": "user", + "content": "$content" + } + ] + }""".trimIndent() ) ) .build() @@ -131,7 +130,7 @@ class ChatGpt : AbstractModule() { try { val jsonResponse = JSONObject(response.body()) val choices = jsonResponse.getJSONArray("choices") - return choices.getJSONObject(0).getString("text").trim() + return choices.getJSONObject(0).getJSONObject("message").getString("content").trim() } catch (e: JSONException) { throw ModuleException( "$CHATGPT_CMD($query): JSON", diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Gemini.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Gemini.kt index 2f7c842..6f9b5ff 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Gemini.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Gemini.kt @@ -1,16 +1,12 @@ package net.thauvin.erik.mobibot.modules -import com.google.auth.Credentials import com.google.cloud.vertexai.VertexAI -import com.google.cloud.vertexai.api.GenerateContentResponse import com.google.cloud.vertexai.api.GenerationConfig import com.google.cloud.vertexai.generativeai.preview.ChatSession import com.google.cloud.vertexai.generativeai.preview.GenerativeModel import com.google.cloud.vertexai.generativeai.preview.ResponseHandler import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils.sendMessage -import okio.IOException -import org.apache.commons.text.WordUtils import org.pircbotx.hooks.types.GenericMessageEvent import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -86,7 +82,7 @@ class Gemini : AbstractModule() { val session = ChatSession(model) val response = session.sendMessage(query) - return ResponseHandler.getText(response); + return ResponseHandler.getText(response) } } catch (e: Exception) { throw ModuleException( @@ -96,7 +92,7 @@ class Gemini : AbstractModule() { ) } } else { - throw ModuleException("${GEMINI_CMD}($query)", "No ${GEMINI_NAME} Project ID or Location specified.") + throw ModuleException("${GEMINI_CMD}($query)", "No $GEMINI_NAME Project ID or Location specified.") } } } @@ -105,10 +101,10 @@ class Gemini : AbstractModule() { commands.add(GEMINI_CMD) with(help) { add("To get answers from $name:") - add(Utils.helpFormat("%c ${GEMINI_CMD} ")) + add(Utils.helpFormat("%c $GEMINI_CMD ")) add("For example:") - add(Utils.helpFormat("%c ${GEMINI_CMD} explain quantum computing in simple terms")) - add(Utils.helpFormat("%c ${GEMINI_CMD} how do I make an HTTP request in Javascript?")) + add(Utils.helpFormat("%c $GEMINI_CMD explain quantum computing in simple terms")) + add(Utils.helpFormat("%c $GEMINI_CMD how do I make an HTTP request in Javascript?")) } initProperties(PROJECT_ID_PROP, LOCATION_PROPR, MAX_TOKENS_PROP) } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt index 603c353..7180c13 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ChatGptTest.kt @@ -54,11 +54,12 @@ class ChatGptTest : LocalProperties() { assertThat( ChatGpt.chat("how do I make an HTTP request in Javascript?", apiKey, 100) ).contains("XMLHttpRequest") + assertThat( ChatGpt.chat("how do I encode a URL in java?", apiKey, 60) ).contains("URLEncoder") - assertFailure { ChatGpt.chat("1 liter to gallon", apiKey, 0) } + assertFailure { ChatGpt.chat("1 liter to gallon", apiKey, -1) } .isInstanceOf(ModuleException::class.java) } }