nice working 16k
This commit is contained in:
parent
aa3d85c10b
commit
94e4c177dd
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
.env
|
||||
.env
|
||||
pb_data
|
||||
11
bot/bot.go
11
bot/bot.go
@ -26,6 +26,7 @@ func Run() {
|
||||
|
||||
discord.Open()
|
||||
defer discord.Close()
|
||||
|
||||
log.Info("BitBot is running...")
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
@ -56,15 +57,7 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
||||
conversationHistory = append(conversationHistory, userMessage)
|
||||
|
||||
if strings.Contains(message.Content, "!bit") || isPrivateChannel {
|
||||
gptResponse := 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)
|
||||
chatGPT(discord, message.ChannelID, message.Content, conversationHistory)
|
||||
} else if strings.Contains(message.Content, "!cry") {
|
||||
currentCryptoPrice := getCurrentCryptoPrice(message.Content)
|
||||
discord.ChannelMessageSendComplex(message.ChannelID, currentCryptoPrice)
|
||||
|
||||
107
bot/chat.go
107
bot/chat.go
@ -9,14 +9,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
maxTokens = 3000
|
||||
maxContextTokens = 4097
|
||||
maxMessageTokens = 1000
|
||||
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."
|
||||
maxTokens = 2000
|
||||
maxContextTokens = 16000
|
||||
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."
|
||||
)
|
||||
|
||||
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 {
|
||||
log.Error("Error retrieving channel history:", err)
|
||||
return conversationHistory
|
||||
@ -28,6 +28,9 @@ func populateConversationHistory(session *discordgo.Session, channelID string, c
|
||||
}
|
||||
|
||||
maxHistoryTokens := maxTokens - totalTokens
|
||||
if maxHistoryTokens < 0 {
|
||||
maxHistoryTokens = 0
|
||||
}
|
||||
|
||||
for _, message := range messages {
|
||||
if message.Author.ID == session.State.User.ID {
|
||||
@ -56,74 +59,37 @@ func populateConversationHistory(session *discordgo.Session, channelID string, c
|
||||
return conversationHistory
|
||||
}
|
||||
|
||||
func chatGPT(session *discordgo.Session, channelID string, message string, conversationHistory []openai.ChatCompletionMessage) *discordgo.MessageSend {
|
||||
conversationHistory = populateConversationHistory(session, channelID, conversationHistory)
|
||||
|
||||
func chatGPT(session *discordgo.Session, channelID string, message string, conversationHistory []openai.ChatCompletionMessage) {
|
||||
client := openai.NewClient(OpenAIToken)
|
||||
|
||||
// Calculate the total tokens in the conversation history
|
||||
totalTokens := 0
|
||||
for _, msg := range conversationHistory {
|
||||
totalTokens += len(msg.Content) + len(msg.Role) + 2
|
||||
// Retrieve recent messages from the channel
|
||||
channelMessages, err := session.ChannelMessages(channelID, 50, "", "", "")
|
||||
if err != nil {
|
||||
log.Error("Error retrieving channel messages:", err)
|
||||
}
|
||||
|
||||
log.Info("Total tokens in conversation history:", totalTokens)
|
||||
|
||||
// Calculate the tokens in the completion message
|
||||
completionTokens := len(message)
|
||||
log.Info("Tokens in completion message:", completionTokens)
|
||||
|
||||
// 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
|
||||
// Convert channel messages to chat completion messages
|
||||
for _, msg := range channelMessages {
|
||||
if msg.Author.ID != session.State.User.ID && len(msg.Content) > 0 {
|
||||
conversationHistory = append(conversationHistory, openai.ChatCompletionMessage{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: msg.Content,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Combine messages from conversation history
|
||||
messages := []openai.ChatCompletionMessage{}
|
||||
|
||||
// Add conversation history to messages
|
||||
messages = append(messages, conversationHistory...)
|
||||
|
||||
// Add user message to conversation history
|
||||
userMessage := openai.ChatCompletionMessage{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: message,
|
||||
}
|
||||
conversationHistory = append(conversationHistory, 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
|
||||
}
|
||||
}
|
||||
messages = append(messages, userMessage)
|
||||
|
||||
// Perform 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,
|
||||
FrequencyPenalty: 0.3,
|
||||
PresencePenalty: 0.6,
|
||||
Model: openai.GPT3Dot5Turbo,
|
||||
Model: openai.GPT3Dot5Turbo16K,
|
||||
Messages: messages,
|
||||
},
|
||||
)
|
||||
@ -142,15 +108,18 @@ func chatGPT(session *discordgo.Session, channelID string, message string, conve
|
||||
// Handle API errors
|
||||
if err != nil {
|
||||
log.Error("Error connecting to the OpenAI API:", err)
|
||||
return &discordgo.MessageSend{
|
||||
Content: "Sorry, there was an error trying to connect to the API",
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Construct and return the bot's response
|
||||
// Construct and send the bot's response as an embed
|
||||
gptResponse := resp.Choices[0].Message.Content
|
||||
embed := &discordgo.MessageSend{
|
||||
Content: gptResponse,
|
||||
embed := &discordgo.MessageEmbed{
|
||||
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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user