From 02730ff48853b7c9e57df7f0028ee04933308504 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Fri, 24 Mar 2023 16:47:26 +0100 Subject: [PATCH] add limits related to current messages to config --- src/config_example.ts | 10 ++++++++++ src/index.ts | 4 ++-- src/toOpenAIMessages.ts | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/config_example.ts b/src/config_example.ts index 7d3701e..2edce9d 100644 --- a/src/config_example.ts +++ b/src/config_example.ts @@ -31,4 +31,14 @@ export default class config { model: "gpt-3.5-turbo", max_tokens: 384, }; + + /** limits for message selection */ + static readonly limits = { + /** maximum time in the past for messages (in miliseconds) */ + time: 60*60*1000, + /** maximum number of messages to select (maximum 100) */ + messages: 50, + /** maximum total token usage for messages */ + tokens: 2048, + }; } diff --git a/src/index.ts b/src/index.ts index 45115d1..fe5a3bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,9 +26,9 @@ discord.on("messageCreate", async message => { if (!message.mentions.has(message.client.user)) return; try { - let messages: DiscordApi.Collection = await message.channel.messages.fetch({ limit: 50, cache: false }); + let messages: DiscordApi.Collection = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false }); - messages = messages.filter(m => message.createdTimestamp - m.createdTimestamp < 1000*60*60 ); + messages = messages.filter(m => message.createdTimestamp - m.createdTimestamp < config.limits.time ); messages.forEach(m => Moderation.checkMessage(m)); diff --git a/src/toOpenAIMessages.ts b/src/toOpenAIMessages.ts index 9a8f99e..54e2ba4 100644 --- a/src/toOpenAIMessages.ts +++ b/src/toOpenAIMessages.ts @@ -89,7 +89,7 @@ export default function toOpenAIMessages(messages: Collection 2048) break; + if (tokenCount > config.limits.tokens) break; rvalue.push({ role: message.author.id == message.client.user.id ? "assistant" : "user", content: content,