Added rate limit error

This commit is contained in:
Erik C. Thauvin 2023-01-12 23:06:07 -08:00
parent 4fcdce5229
commit c3d1930592

View file

@ -50,16 +50,16 @@ import java.net.http.HttpResponse
class ChatGpt : AbstractModule() { class ChatGpt : AbstractModule() {
private val logger: Logger = LoggerFactory.getLogger(ChatGpt::class.java) private val logger: Logger = LoggerFactory.getLogger(ChatGpt::class.java)
override val name = "ChatGPT" override val name = CHATGPT_NAME
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], properties[CHATGPT_MAX_TOKENS]!!.toInt()) val answer = chat(args.trim(), properties[API_KEY_PROP], properties[MAX_TOKENS_PROP]!!.toInt())
if (answer.isNotBlank()) { if (answer.isNotBlank()) {
event.sendMessage(WordUtils.wrap(answer, 400)) event.sendMessage(WordUtils.wrap(answer, 400))
} else { } else {
event.respond("ChatGPT is stumped.") event.respond("$name is stumped.")
} }
} catch (e: ModuleException) { } catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
@ -67,7 +67,7 @@ class ChatGpt : AbstractModule() {
event.respond(it) event.respond(it)
} }
} catch (e: NumberFormatException) { } catch (e: NumberFormatException) {
if (logger.isErrorEnabled) logger.error("Invalid $CHATGPT_MAX_TOKENS property.", e) if (logger.isErrorEnabled) logger.error("Invalid $MAX_TOKENS_PROP property.", e)
event.respond("The $name module is misconfigured.") event.respond("The $name module is misconfigured.")
} }
} else { } else {
@ -77,20 +77,26 @@ class ChatGpt : AbstractModule() {
companion object { companion object {
/** /**
* The ChatGPT API Key property. * The service name.
*/ */
const val CHATGPT_API_KEY = "chatgpt-api-key" const val CHATGPT_NAME = "ChatGPT"
/** /**
* The ChatGPT max tokens property. * The API Key property.
*/ */
const val CHATGPT_MAX_TOKENS = "chatgpt-max-tokens" const val API_KEY_PROP = "chatgpt-api-key"
/**
* The max tokens property.
*/
const val MAX_TOKENS_PROP = "chatgpt-max-tokens"
// ChatGPT API URL
private const val API_URL = "https://api.openai.com/v1/completions"
// ChatGPT command // ChatGPT command
private const val CHATGPT_CMD = "chatgpt" private const val CHATGPT_CMD = "chatgpt"
// ChatGPT API URL
private const val API_URL = "https://api.openai.com/v1/completions"
@JvmStatic @JvmStatic
@Throws(ModuleException::class) @Throws(ModuleException::class)
@ -124,23 +130,28 @@ class ChatGpt : AbstractModule() {
return choices.getJSONObject(0).getString("text").trim() return choices.getJSONObject(0).getString("text").trim()
} catch (e: JSONException) { } catch (e: JSONException) {
throw ModuleException( throw ModuleException(
"chatgpt($query): JSON", "$CHATGPT_CMD($query): JSON",
"A JSON error has occurred while conversing with ChatGPT.", "A JSON error has occurred while conversing with $CHATGPT_NAME.",
e e
) )
} }
} else { } else {
throw IOException("Status Code: " + response.statusCode()) if (response.statusCode() == 429) {
throw ModuleException("$CHATGPT_CMD($query): Rate limit reached",
"Rate limit reached. Please try again later.")
} else {
throw IOException("HTTP Status Code: " + response.statusCode())
}
} }
} catch (e: IOException) { } catch (e: IOException) {
throw ModuleException( throw ModuleException(
"chatgpt($query): IO", "$CHATGPT_CMD($query): IO",
"An IO error has occurred while conversing with ChatGPT.", "An IO error has occurred while conversing with $CHATGPT_NAME.",
e e
) )
} }
} else { } else {
throw ModuleException("chatgpt($query)", "No ChatGPT API key specified.") throw ModuleException("$CHATGPT_CMD($query)", "No $CHATGPT_NAME API key specified.")
} }
} }
} }
@ -148,12 +159,12 @@ class ChatGpt : AbstractModule() {
init { init {
commands.add(CHATGPT_CMD) commands.add(CHATGPT_CMD)
with(help) { with(help) {
add("To get answers from ChatGPT:") add("To get answers from $name:")
add(Utils.helpFormat("%c $CHATGPT_CMD <query>")) add(Utils.helpFormat("%c $CHATGPT_CMD <query>"))
add("For example:") add("For example:")
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, CHATGPT_MAX_TOKENS) initProperties(API_KEY_PROP, MAX_TOKENS_PROP)
} }
} }