From 8e1001b5b7e49b09077994d78946bb8aeaeea7d5 Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Mon, 7 Oct 2024 14:38:23 -0400 Subject: [PATCH] enforce anthropic's required alternating user/assistant roles --- anthropic.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/anthropic.go b/anthropic.go index 79c248f..c361553 100644 --- a/anthropic.go +++ b/anthropic.go @@ -65,7 +65,17 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { Data: msg.ImageBase64, })) } - msgs = append(msgs, m) + + // 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) + } } }