fix: fix claude system prompt wrong life cycle issue (merge pr #154)

fix: skip the first claude system message
This commit is contained in:
Minghan Zhang 2024-03-30 23:12:53 +08:00 committed by GitHub
commit c27fdf3bec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -54,12 +54,16 @@ func (c *ChatInstance) GetTokens(props *adaptercommon.ChatProps) int {
} }
func (c *ChatInstance) ConvertMessages(props *adaptercommon.ChatProps) []globals.Message { func (c *ChatInstance) ConvertMessages(props *adaptercommon.ChatProps) []globals.Message {
// anthropic api: top message must be user message, system message is not allowed // anthropic api: top message must be user message, only `user` and `assistant` role messages are allowd
start := false start := false
result := make([]globals.Message, 0) result := make([]globals.Message, 0)
for _, message := range props.Message { for _, message := range props.Message {
if message.Role == globals.System {
continue
}
// if is first message, set it to user message // if is first message, set it to user message
if !start { if !start {
start = true start = true
@ -70,11 +74,6 @@ func (c *ChatInstance) ConvertMessages(props *adaptercommon.ChatProps) []globals
continue continue
} }
// if is system message, set it to user message
if message.Role == globals.System {
continue
}
// anthropic api does not allow multi-same role messages // anthropic api does not allow multi-same role messages
if len(result) > 0 && result[len(result)-1].Role == message.Role { if len(result) > 0 && result[len(result)-1].Role == message.Role {
result[len(result)-1].Content += "\n" + message.Content result[len(result)-1].Content += "\n" + message.Content