Added max-token property

This commit is contained in:
Erik C. Thauvin 2023-01-12 00:57:12 -08:00
parent 8c13276556
commit 6894e05610
2 changed files with 25 additions and 7 deletions

View file

@ -34,6 +34,7 @@ package net.thauvin.erik.mobibot.modules
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.apache.commons.text.WordUtils
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import org.json.JSONWriter import org.json.JSONWriter
@ -54,9 +55,9 @@ class ChatGpt : AbstractModule() {
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
if (args.isNotBlank()) { if (args.isNotBlank()) {
try { try {
val answer = chat(args.trim(), properties[CHATGPT_API_KEY]) val answer = chat(args.trim(), properties[CHATGPT_API_KEY], properties[CHATGPT_MAX_TOKENS]!!.toInt())
if (answer.isNotBlank()) { if (answer.isNotBlank()) {
event.sendMessage(answer) event.sendMessage(WordUtils.wrap(answer, 400))
} else { } else {
event.respond("ChatGPT is stumped.") event.respond("ChatGPT is stumped.")
} }
@ -65,6 +66,9 @@ class ChatGpt : AbstractModule() {
e.message?.let { e.message?.let {
event.respond(it) event.respond(it)
} }
} catch (e: NumberFormatException) {
if (logger.isErrorEnabled) logger.error("Invalid $CHATGPT_MAX_TOKENS property.", e)
event.respond("The $name module is misconfigured.")
} }
} else { } else {
helpResponse(event) helpResponse(event)
@ -77,6 +81,11 @@ class ChatGpt : AbstractModule() {
*/ */
const val CHATGPT_API_KEY = "chatgpt-api-key" const val CHATGPT_API_KEY = "chatgpt-api-key"
/**
* The ChatGPT max tokens property.
*/
const val CHATGPT_MAX_TOKENS = "chatgpt-max-tokens"
// ChatGPT command // ChatGPT command
private const val CHATGPT_CMD = "chatgpt" private const val CHATGPT_CMD = "chatgpt"
@ -85,7 +94,7 @@ class ChatGpt : AbstractModule() {
@JvmStatic @JvmStatic
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun chat(query: String, apiKey: String?): String { fun chat(query: String, apiKey: String?, maxTokens: Int): String {
if (!apiKey.isNullOrEmpty()) { if (!apiKey.isNullOrEmpty()) {
val prompt = JSONWriter.valueToString("Q:$query\nA:") val prompt = JSONWriter.valueToString("Q:$query\nA:")
val request = HttpRequest.newBuilder() val request = HttpRequest.newBuilder()
@ -98,7 +107,7 @@ class ChatGpt : AbstractModule() {
"model": "text-davinci-003", "model": "text-davinci-003",
"prompt": $prompt, "prompt": $prompt,
"temperature": 0, "temperature": 0,
"max_tokens": 1024, "max_tokens": $maxTokens,
"top_p": 1, "top_p": 1,
"frequency_penalty": 0, "frequency_penalty": 0,
"presence_penalty": 0 "presence_penalty": 0
@ -145,6 +154,6 @@ class ChatGpt : AbstractModule() {
add(Utils.helpFormat("%c $CHATGPT_CMD explain quantum computing in simple terms")) add(Utils.helpFormat("%c $CHATGPT_CMD explain quantum computing in simple terms"))
add(Utils.helpFormat("%c $CHATGPT_CMD how do I make an HTTP request in Javascript?")) add(Utils.helpFormat("%c $CHATGPT_CMD how do I make an HTTP request in Javascript?"))
} }
initProperties(CHATGPT_API_KEY) initProperties(CHATGPT_API_KEY, CHATGPT_MAX_TOKENS)
} }
} }

View file

@ -34,15 +34,17 @@ package net.thauvin.erik.mobibot.modules
import assertk.assertThat import assertk.assertThat
import assertk.assertions.contains import assertk.assertions.contains
import assertk.assertions.hasNoCause import assertk.assertions.hasNoCause
import assertk.assertions.isEqualTo
import assertk.assertions.isFailure import assertk.assertions.isFailure
import assertk.assertions.isInstanceOf import assertk.assertions.isInstanceOf
import assertk.assertions.message
import net.thauvin.erik.mobibot.LocalProperties import net.thauvin.erik.mobibot.LocalProperties
import org.testng.annotations.Test import org.testng.annotations.Test
class ChatGptTest : LocalProperties() { class ChatGptTest : LocalProperties() {
@Test(groups = ["modules"]) @Test(groups = ["modules"])
fun testApiKey() { fun testApiKey() {
assertThat { ChatGpt.chat("1 gallon to liter", "") } assertThat { ChatGpt.chat("1 gallon to liter", "", 0) }
.isFailure() .isFailure()
.isInstanceOf(ModuleException::class.java) .isInstanceOf(ModuleException::class.java)
.hasNoCause() .hasNoCause()
@ -52,7 +54,14 @@ class ChatGptTest : LocalProperties() {
fun testChat() { fun testChat() {
val apiKey = getProperty(ChatGpt.CHATGPT_API_KEY) val apiKey = getProperty(ChatGpt.CHATGPT_API_KEY)
assertThat( assertThat(
ChatGpt.chat("how do I make an HTTP request in Javascript?", apiKey) ChatGpt.chat("how do I make an HTTP request in Javascript?", apiKey, 100)
).contains("XMLHttpRequest") ).contains("XMLHttpRequest")
assertThat(
ChatGpt.chat("how do I encode a URL in java?", apiKey, 60)
).contains("URLEncoder")
assertThat { ChatGpt.chat("1 liter to gallon", apiKey, 0) }
.isFailure()
.isInstanceOf(ModuleException::class.java)
} }
} }