Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better behavour for anthropic with multiple messages and mixed bot usage #235

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/ai/anthropic/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,32 @@ func conversationToMessages(conversation ai.BotConversation) (string, []InputMes
systemMessage := ""
messages := make([]InputMessage, 0, len(conversation.Posts))
for _, post := range conversation.Posts {
previousRole := ""
previousContent := ""
if len(messages) > 0 {
previous := messages[len(messages)-1]
previousRole = previous.Role
previousContent = previous.Content
}
switch post.Role {
case ai.PostRoleSystem:
systemMessage += post.Message
case ai.PostRoleBot:
if previousRole == RoleAssistant {
previousContent += post.Message
continue
}
messages = append(messages,
InputMessage{
Role: RoleAssistant,
Content: post.Message,
},
)
case ai.PostRoleUser:
if previousRole == RoleUser {
previousContent += post.Message
continue
}
messages = append(messages,
InputMessage{
Role: RoleUser,
Expand Down
7 changes: 0 additions & 7 deletions server/ai/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,6 @@ func (b *BotConversation) Truncate(maxTokens int, countTokens func(string) int)
return false
}

func GetPostRole(botID string, post *model.Post) PostRole {
if post.UserId == botID {
return PostRoleBot
}
return PostRoleUser
}

func FormatPostBody(post *model.Post) string {
attachments := post.Attachments()
if len(attachments) > 0 {
Expand Down
7 changes: 6 additions & 1 deletion server/post_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,13 @@ func (p *Plugin) PostToAIPost(bot *Bot, post *model.Post) ai.Post {
}
}

role := ai.PostRoleUser
if p.IsAnyBot(post.UserId) {
role = ai.PostRoleBot
}

return ai.Post{
Role: ai.GetPostRole(bot.mmBot.UserId, post),
Role: role,
Message: ai.FormatPostBody(post),
Files: files,
}
Expand Down