Skip to content

Commit

Permalink
Added support for ChatGPT 3.5 Turbo
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Dec 18, 2023
1 parent a10104d commit 356f629
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
1 change: 1 addition & 0 deletions properties/mobibot.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/main/kotlin/net/thauvin/erik/mobibot/ReleaseInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
29 changes: 14 additions & 15 deletions src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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.")
}
Expand Down Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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()
Expand All @@ -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",
Expand Down
14 changes: 5 additions & 9 deletions src/main/kotlin/net/thauvin/erik/mobibot/modules/Gemini.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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.")
}
}
}
Expand All @@ -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} <query>"))
add(Utils.helpFormat("%c $GEMINI_CMD <query>"))
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 356f629

Please sign in to comment.