From abd05d83349c1c3cdcc7250877c7b6f722d65c06 Mon Sep 17 00:00:00 2001 From: Deng Junhai Date: Sun, 31 Mar 2024 13:43:55 +0800 Subject: [PATCH] fix: fix skylark format issue when the assistant role message is the first message (#155) Co-Authored-By: Minghan Zhang <112773885+zmh-program@users.noreply.github.com> --- adapter/skylark/chat.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/adapter/skylark/chat.go b/adapter/skylark/chat.go index d889d6c8..122e6507 100644 --- a/adapter/skylark/chat.go +++ b/adapter/skylark/chat.go @@ -5,6 +5,7 @@ import ( "chat/globals" "chat/utils" "fmt" + "github.com/volcengine/volc-sdk-golang/service/maas" "github.com/volcengine/volc-sdk-golang/service/maas/models/api" ) @@ -12,17 +13,41 @@ import ( const defaultMaxTokens int64 = 1500 func getMessages(messages []globals.Message) []*api.Message { - return utils.Each[globals.Message, *api.Message](messages, func(message globals.Message) *api.Message { + result := make([]*api.Message, 0) + + for _, message := range messages { if message.Role == globals.Tool { message.Role = maas.ChatRoleOfFunction } - return &api.Message{ + msg := &api.Message{ Role: message.Role, Content: message.Content, FunctionCall: getFunctionCall(message.ToolCalls), } - }) + + hasPrevious := len(result) > 0 + + // a message should not followed by the same role message, merge them + if hasPrevious && result[len(result)-1].Role == message.Role { + prev := result[len(result)-1] + prev.Content += msg.Content + if message.ToolCalls != nil { + prev.FunctionCall = msg.FunctionCall + } + + continue + } + + // `assistant` message should follow a user or function message, if not has previous message, change the role to `user` + if !hasPrevious && message.Role == maas.ChatRoleOfAssistant { + msg.Role = maas.ChatRoleOfUser + } + + result = append(result, msg) + } + + return result } func (c *ChatInstance) GetMaxTokens(token *int) int64 {