enforce anthropic's required alternating user/assistant roles

This commit is contained in:
Steve Dudenhoeffer 2024-10-07 14:38:23 -04:00
parent 09447b0424
commit 8e1001b5b7

View File

@ -65,9 +65,19 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
Data: msg.ImageBase64,
}))
}
// if this has the same role as the previous message, we can append it to the previous message
// as anthropic expects alternating assistant and user roles
if len(msgs) > 0 && msgs[len(msgs)-1].Role == role {
m2 := &msgs[len(msgs)-1]
m2.Content = append(m2.Content, m.Content...)
} else {
msgs = append(msgs, m)
}
}
}
for _, tool := range req.Toolbox {
res.Tools = append(res.Tools, anth.ToolDefinition{