last working
This commit is contained in:
parent
de6498156d
commit
ba77f04e30
@ -38,10 +38,12 @@ func Run() {
|
|||||||
var conversationHistoryMap = make(map[string][]openai.ChatCompletionMessage)
|
var conversationHistoryMap = make(map[string][]openai.ChatCompletionMessage)
|
||||||
|
|
||||||
func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
||||||
if message.Author.ID == discord.State.User.ID {
|
if message.Author.ID == discord.State.User.ID || message.Content == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isPrivateChannel := message.GuildID == ""
|
||||||
|
|
||||||
conversationHistory := conversationHistoryMap[message.Author.ID]
|
conversationHistory := conversationHistoryMap[message.Author.ID]
|
||||||
|
|
||||||
userMessage := openai.ChatCompletionMessage{
|
userMessage := openai.ChatCompletionMessage{
|
||||||
@ -50,8 +52,7 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
conversationHistory = append(conversationHistory, userMessage)
|
conversationHistory = append(conversationHistory, userMessage)
|
||||||
|
|
||||||
switch {
|
if strings.Contains(message.Content, "!bit") || isPrivateChannel {
|
||||||
case strings.Contains(message.Content, "!bit"):
|
|
||||||
gptResponse := chatGPT(message.Content, conversationHistory)
|
gptResponse := chatGPT(message.Content, conversationHistory)
|
||||||
discord.ChannelTyping(message.ChannelID)
|
discord.ChannelTyping(message.ChannelID)
|
||||||
discord.ChannelMessageSendComplex(message.ChannelID, gptResponse)
|
discord.ChannelMessageSendComplex(message.ChannelID, gptResponse)
|
||||||
@ -61,7 +62,7 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
|||||||
Content: gptResponse.Content,
|
Content: gptResponse.Content,
|
||||||
}
|
}
|
||||||
conversationHistory = append(conversationHistory, botMessage)
|
conversationHistory = append(conversationHistory, botMessage)
|
||||||
case 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)
|
||||||
}
|
}
|
||||||
|
|||||||
21
bot/chat.go
21
bot/chat.go
@ -8,7 +8,7 @@ import (
|
|||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxTokens = 2000
|
const maxTokens = 3000
|
||||||
const maxContextTokens = 4097
|
const maxContextTokens = 4097
|
||||||
|
|
||||||
func chatGPT(message string, conversationHistory []openai.ChatCompletionMessage) *discordgo.MessageSend {
|
func chatGPT(message string, conversationHistory []openai.ChatCompletionMessage) *discordgo.MessageSend {
|
||||||
@ -17,7 +17,7 @@ func chatGPT(message string, conversationHistory []openai.ChatCompletionMessage)
|
|||||||
// Calculate the total number of tokens used in the conversation history and completion.
|
// Calculate the total number of tokens used in the conversation history and completion.
|
||||||
totalTokens := 0
|
totalTokens := 0
|
||||||
for _, msg := range conversationHistory {
|
for _, msg := range conversationHistory {
|
||||||
totalTokens += len(msg.Content)
|
totalTokens += len(msg.Content) + len(msg.Role) + 2 // Account for role and content tokens, plus two extra for delimiters.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the number of tokens used in the completion.
|
// Calculate the number of tokens used in the completion.
|
||||||
@ -31,19 +31,26 @@ func chatGPT(message string, conversationHistory []openai.ChatCompletionMessage)
|
|||||||
completionTokens = maxTokens
|
completionTokens = maxTokens
|
||||||
} else {
|
} else {
|
||||||
// If removing the last message reduces the context within the limit, remove it.
|
// If removing the last message reduces the context within the limit, remove it.
|
||||||
if totalTokens-len(conversationHistory[len(conversationHistory)-1].Content) <= maxTokens {
|
if totalTokens-len(conversationHistory[len(conversationHistory)-1].Content)-len(conversationHistory[len(conversationHistory)-1].Role)-2 <= maxTokens {
|
||||||
totalTokens -= len(conversationHistory[len(conversationHistory)-1].Content)
|
totalTokens -= len(conversationHistory[len(conversationHistory)-1].Content) + len(conversationHistory[len(conversationHistory)-1].Role) + 2
|
||||||
conversationHistory = conversationHistory[:len(conversationHistory)-1]
|
conversationHistory = conversationHistory[:len(conversationHistory)-1]
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, remove the first message from the conversation history.
|
// Otherwise, remove the first message from the conversation history.
|
||||||
totalTokens -= len(conversationHistory[0].Content)
|
totalTokens -= len(conversationHistory[0].Content) + len(conversationHistory[0].Role) + 2
|
||||||
conversationHistory = conversationHistory[1:]
|
conversationHistory = conversationHistory[1:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine the previous conversation history with the current user message.
|
// Add a system message at the beginning of the conversation history with the instructions.
|
||||||
messages := append(conversationHistory, openai.ChatCompletionMessage{
|
systemMessage := openai.ChatCompletionMessage{
|
||||||
|
Role: openai.ChatMessageRoleSystem,
|
||||||
|
Content: "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.",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine the system message, previous conversation history, and the current user message.
|
||||||
|
messages := append([]openai.ChatCompletionMessage{systemMessage}, conversationHistory...)
|
||||||
|
messages = append(messages, openai.ChatCompletionMessage{
|
||||||
Role: openai.ChatMessageRoleUser,
|
Role: openai.ChatMessageRoleUser,
|
||||||
Content: message,
|
Content: message,
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user