Skip to content

Commit

Permalink
Better behavour for anthropic with multiple messages and mixed bot us…
Browse files Browse the repository at this point in the history
…age (#235)
  • Loading branch information
crspeller authored Aug 21, 2024
1 parent 3ecb618 commit d1070ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
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

0 comments on commit d1070ee

Please sign in to comment.