fix trimming of messages

This commit is contained in:
Kristian 2023-10-14 02:23:28 +02:00
parent 4163134d33
commit 81e39f058a

View File

@ -10,8 +10,8 @@ import (
) )
const ( const (
maxTokens = 4000 maxTokens = 2000
maxContextTokens = 2000 maxContextTokens = 4000
maxMessageTokens = 2000 maxMessageTokens = 2000
systemMessageText = "0. your name is !bit you are a discord bot 1. Identify the key points or main ideas of the original answers.\n2. Summarize each answer using concise and informative language.\n3. Prioritize clarity and brevity, capturing the essence of the information provided.\n4. Trim down unnecessary details and avoid elaboration.\n5. Make sure the summarized answers still convey accurate and meaningful information." systemMessageText = "0. your name is !bit you are a discord bot 1. Identify the key points or main ideas of the original answers.\n2. Summarize each answer using concise and informative language.\n3. Prioritize clarity and brevity, capturing the essence of the information provided.\n4. Trim down unnecessary details and avoid elaboration.\n5. Make sure the summarized answers still convey accurate and meaningful information."
) )
@ -94,6 +94,25 @@ func chatGPT(session *discordgo.Session, channelID string, message string, conve
} }
messages = append(messages, userMessage) messages = append(messages, userMessage)
// Trim conversation history if it exceeds maxContextTokens
totalTokens := 0
trimmedMessages := []openai.ChatCompletionMessage{}
for i := 0; i < len(messages); i++ {
msg := messages[i]
tokens := len(msg.Content) + len(msg.Role) + 2 // Account for role and content tokens
if totalTokens+tokens <= maxContextTokens {
trimmedMessages = append(trimmedMessages, msg)
totalTokens += tokens
} else {
break
}
}
// Update messages with trimmed conversation history
messages = trimmedMessages
// Perform GPT-4 completion // Perform GPT-4 completion
log.Info("Starting completion...") log.Info("Starting completion...")
resp, err := client.CreateChatCompletion( resp, err := client.CreateChatCompletion(