added roll command, still trying to fix context

This commit is contained in:
Kristian 2023-11-27 20:09:46 +01:00
parent ed5351f790
commit d8ed1c9560
3 changed files with 42 additions and 43 deletions

View File

@ -3,8 +3,10 @@ package bot
import ( import (
"bitbot/pb" "bitbot/pb"
"fmt" "fmt"
"math/rand"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings" "strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -67,12 +69,7 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
channelID := message.ChannelID channelID := message.ChannelID
conversationHistory = populateConversationHistory(discord, channelID, conversationHistory) conversationHistory = populateConversationHistory(discord, channelID, conversationHistory)
userMessage := openai.ChatCompletionMessage{ log.Info("Conversation history from bot.go:", conversationHistory)
Role: openai.ChatMessageRoleUser,
Content: message.Content,
}
conversationHistory = append(conversationHistory, userMessage)
if strings.HasPrefix(message.Content, "!cry") { if strings.HasPrefix(message.Content, "!cry") {
currentCryptoPrice := getCurrentCryptoPrice(message.Content) currentCryptoPrice := getCurrentCryptoPrice(message.Content)
@ -234,12 +231,33 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
generalHelpMessage := "Available commands:\n" + generalHelpMessage := "Available commands:\n" +
"!cry - Get information about cryptocurrency prices.\n" + "!cry - Get information about cryptocurrency prices.\n" +
"!bit - Interact with the BitBot chatbot.\n" + "!bit - Interact with the BitBot chatbot.\n" +
"!roll - Random number from 1-100 or specify a number (!roll 9001).\n" +
"!help - Show available commands.\n" + "!help - Show available commands.\n" +
"!help admin - Show admin commands.\n" "!help admin - Show admin commands.\n"
discord.ChannelMessageSend(message.ChannelID, generalHelpMessage) discord.ChannelMessageSend(message.ChannelID, generalHelpMessage)
} }
} else if strings.HasPrefix(message.Content, "!roll") {
handleRollCommand(discord, message)
} }
conversationHistoryMap[userID] = conversationHistory conversationHistoryMap[userID] = conversationHistory
} }
func handleRollCommand(session *discordgo.Session, message *discordgo.MessageCreate) {
// Extract the argument from the message
args := strings.Fields(message.Content[len("!roll"):])
if len(args) == 0 {
// No argument provided, generate random number between 1 and 100
randomNumber := rand.Intn(100) + 1
session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("You rolled a %d!", randomNumber))
} else {
// Argument provided, try to parse it as a number
if num, err := strconv.Atoi(args[0]); err == nil {
// Generate random number between 1 and the provided number
randomNumber := rand.Intn(num) + 1
session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("%d", randomNumber))
} else {
session.ChannelMessageSend(message.ChannelID, "Invalid argument. Please provide a valid number.")
}
}
}

View File

@ -49,10 +49,6 @@ func populateConversationHistory(session *discordgo.Session, channelID string, c
// Add new messages from the channel // Add new messages from the channel
addedTokens := 0 addedTokens := 0
for _, message := range messages { for _, message := range messages {
if message.Author.ID == session.State.User.ID || message.Author.Bot {
continue // Skip the bot's own messages
}
if len(message.Content) > 0 { if len(message.Content) > 0 {
tokens := len(message.Content) + 2 // Account for role and content tokens tokens := len(message.Content) + 2 // Account for role and content tokens
if totalTokens+tokens <= maxContextTokens && addedTokens+tokens <= maxContextTokens { if totalTokens+tokens <= maxContextTokens && addedTokens+tokens <= maxContextTokens {
@ -79,46 +75,31 @@ func populateConversationHistory(session *discordgo.Session, channelID string, c
func chatGPT(session *discordgo.Session, channelID string, message string, conversationHistory []openai.ChatCompletionMessage) { func chatGPT(session *discordgo.Session, channelID string, message string, conversationHistory []openai.ChatCompletionMessage) {
client := openai.NewClient(OpenAIToken) client := openai.NewClient(OpenAIToken)
// Retrieve recent messages from the channel
channelMessages, err := session.ChannelMessages(channelID, 50, "", "", "")
if err != nil {
log.Error("Error retrieving channel messages:", err)
}
// 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,
})
}
}
// Trim conversation history if it exceeds maxContextTokens // Trim conversation history if it exceeds maxContextTokens
totalTokens := 0 // totalTokens := 0
trimmedMessages := []openai.ChatCompletionMessage{} // trimmedMessages := []openai.ChatCompletionMessage{}
for i := len(conversationHistory) - 1; i >= 0; i-- { // for i := len(conversationHistory) - 1; i >= 0; i-- {
msg := conversationHistory[i] // msg := conversationHistory[i]
tokens := len(msg.Content) + len(msg.Role) + 2 // Account for role and content tokens // tokens := len(msg.Content) + len(msg.Role) + 2 // Account for role and content tokens
if totalTokens+tokens <= maxContextTokens { // if totalTokens+tokens <= maxContextTokens {
trimmedMessages = append([]openai.ChatCompletionMessage{msg}, trimmedMessages...) // trimmedMessages = append([]openai.ChatCompletionMessage{msg}, trimmedMessages...)
totalTokens += tokens // totalTokens += tokens
} else { // } else {
break // break
} // }
} // }
// Update conversationHistory with trimmed conversation history // Update conversationHistory with trimmed conversation history
conversationHistory = trimmedMessages //conversationHistory = trimmedMessages
// 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) //conversationHistory = append(conversationHistory, userMessage)
// Perform GPT-4 completion // Perform GPT-4 completion
log.Info("Starting completion...", conversationHistory) log.Info("Starting completion...", conversationHistory)

Binary file not shown.