nice working 16k

This commit is contained in:
Kristian 2023-08-10 05:39:36 +02:00
parent aa3d85c10b
commit 94e4c177dd
3 changed files with 42 additions and 79 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.env .env
pb_data

View File

@ -26,6 +26,7 @@ func Run() {
discord.Open() discord.Open()
defer discord.Close() defer discord.Close()
log.Info("BitBot is running...") log.Info("BitBot is running...")
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
@ -56,15 +57,7 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
conversationHistory = append(conversationHistory, userMessage) conversationHistory = append(conversationHistory, userMessage)
if strings.Contains(message.Content, "!bit") || isPrivateChannel { if strings.Contains(message.Content, "!bit") || isPrivateChannel {
gptResponse := chatGPT(discord, message.ChannelID, message.Content, conversationHistory) chatGPT(discord, message.ChannelID, message.Content, conversationHistory)
discord.ChannelTyping(message.ChannelID)
discord.ChannelMessageSendComplex(message.ChannelID, gptResponse)
botMessage := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: gptResponse.Content,
}
conversationHistory = append(conversationHistory, botMessage)
} else if strings.Contains(message.Content, "!cry") { } else if strings.Contains(message.Content, "!cry") {
currentCryptoPrice := getCurrentCryptoPrice(message.Content) currentCryptoPrice := getCurrentCryptoPrice(message.Content)
discord.ChannelMessageSendComplex(message.ChannelID, currentCryptoPrice) discord.ChannelMessageSendComplex(message.ChannelID, currentCryptoPrice)

View File

@ -9,14 +9,14 @@ import (
) )
const ( const (
maxTokens = 3000 maxTokens = 2000
maxContextTokens = 4097 maxContextTokens = 16000
maxMessageTokens = 1000 maxMessageTokens = 2000
systemMessageText = "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."
) )
func populateConversationHistory(session *discordgo.Session, channelID string, conversationHistory []openai.ChatCompletionMessage) []openai.ChatCompletionMessage { func populateConversationHistory(session *discordgo.Session, channelID string, conversationHistory []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
messages, err := session.ChannelMessages(channelID, 100, "", "", "") messages, err := session.ChannelMessages(channelID, 50, "", "", "")
if err != nil { if err != nil {
log.Error("Error retrieving channel history:", err) log.Error("Error retrieving channel history:", err)
return conversationHistory return conversationHistory
@ -28,6 +28,9 @@ func populateConversationHistory(session *discordgo.Session, channelID string, c
} }
maxHistoryTokens := maxTokens - totalTokens maxHistoryTokens := maxTokens - totalTokens
if maxHistoryTokens < 0 {
maxHistoryTokens = 0
}
for _, message := range messages { for _, message := range messages {
if message.Author.ID == session.State.User.ID { if message.Author.ID == session.State.User.ID {
@ -56,74 +59,37 @@ func populateConversationHistory(session *discordgo.Session, channelID string, c
return conversationHistory return conversationHistory
} }
func chatGPT(session *discordgo.Session, channelID string, message string, conversationHistory []openai.ChatCompletionMessage) *discordgo.MessageSend { func chatGPT(session *discordgo.Session, channelID string, message string, conversationHistory []openai.ChatCompletionMessage) {
conversationHistory = populateConversationHistory(session, channelID, conversationHistory)
client := openai.NewClient(OpenAIToken) client := openai.NewClient(OpenAIToken)
// Calculate the total tokens in the conversation history // Retrieve recent messages from the channel
totalTokens := 0 channelMessages, err := session.ChannelMessages(channelID, 50, "", "", "")
for _, msg := range conversationHistory { if err != nil {
totalTokens += len(msg.Content) + len(msg.Role) + 2 log.Error("Error retrieving channel messages:", err)
} }
log.Info("Total tokens in conversation history:", totalTokens) // Convert channel messages to chat completion messages
for _, msg := range channelMessages {
// Calculate the tokens in the completion message if msg.Author.ID != session.State.User.ID && len(msg.Content) > 0 {
completionTokens := len(message) conversationHistory = append(conversationHistory, openai.ChatCompletionMessage{
log.Info("Tokens in completion message:", completionTokens) Role: openai.ChatMessageRoleUser,
Content: msg.Content,
// Calculate the total tokens including the new message })
totalMessageTokens := len(message) + 2 // Account for role and content tokens
// Ensure the total tokens of messages including new message doesn't exceed maxMessageTokens
for totalTokens+totalMessageTokens > maxMessageTokens {
tokensToRemove := totalTokens + totalMessageTokens - maxMessageTokens
tokensRemoved := 0
trimmedMessages := []openai.ChatCompletionMessage{} // Store trimmed messages
for _, msg := range conversationHistory {
tokens := len(msg.Content) + len(msg.Role) + 2
if tokensRemoved+tokens <= tokensToRemove {
tokensRemoved += tokens
log.Info("Removing message with tokens:", tokens)
} else {
trimmedMessages = append(trimmedMessages, msg)
}
}
if tokensRemoved > 0 {
conversationHistory = trimmedMessages
totalTokens -= tokensRemoved
} else {
break
} }
} }
// Combine messages from conversation history
messages := []openai.ChatCompletionMessage{}
// Add conversation history to messages
messages = append(messages, conversationHistory...)
// Add user message to conversation history // Add user message to conversation history
userMessage := openai.ChatCompletionMessage{ userMessage := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser, Role: openai.ChatMessageRoleUser,
Content: message, Content: message,
} }
conversationHistory = append(conversationHistory, userMessage) messages = append(messages, userMessage)
// Construct system message
systemMessage := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: systemMessageText,
}
// Combine messages, ensuring they don't exceed maxTokens
messages := []openai.ChatCompletionMessage{systemMessage}
totalTokens = len(systemMessage.Content) + len(systemMessage.Role) + 2
for _, msg := range conversationHistory {
tokens := len(msg.Content) + len(msg.Role) + 2
if totalTokens+tokens <= maxTokens {
messages = append(messages, msg)
totalTokens += tokens
} else {
break
}
}
// Perform GPT-3.5 Turbo completion // Perform GPT-3.5 Turbo completion
log.Info("Starting GPT-3.5 Turbo completion...") log.Info("Starting GPT-3.5 Turbo completion...")
@ -133,7 +99,7 @@ func chatGPT(session *discordgo.Session, channelID string, message string, conve
MaxTokens: maxTokens, MaxTokens: maxTokens,
FrequencyPenalty: 0.3, FrequencyPenalty: 0.3,
PresencePenalty: 0.6, PresencePenalty: 0.6,
Model: openai.GPT3Dot5Turbo, Model: openai.GPT3Dot5Turbo16K,
Messages: messages, Messages: messages,
}, },
) )
@ -142,15 +108,18 @@ func chatGPT(session *discordgo.Session, channelID string, message string, conve
// Handle API errors // Handle API errors
if err != nil { if err != nil {
log.Error("Error connecting to the OpenAI API:", err) log.Error("Error connecting to the OpenAI API:", err)
return &discordgo.MessageSend{ return
Content: "Sorry, there was an error trying to connect to the API",
}
} }
// Construct and return the bot's response // Construct and send the bot's response as an embed
gptResponse := resp.Choices[0].Message.Content gptResponse := resp.Choices[0].Message.Content
embed := &discordgo.MessageSend{ embed := &discordgo.MessageEmbed{
Content: gptResponse, Title: "BitBot's Response",
Description: gptResponse,
Color: 0x00ff00, // Green color
}
_, err = session.ChannelMessageSendEmbed(channelID, embed)
if err != nil {
log.Error("Error sending embed message:", err)
} }
return embed
} }