Skip to content

Commit

Permalink
fix: fix skylark format issue when the assistant role message is the …
Browse files Browse the repository at this point in the history
…first message (#155)

Co-Authored-By: Minghan Zhang <[email protected]>
  • Loading branch information
Sh1n3zZ and zmh-program committed Mar 31, 2024
1 parent 48a4b6d commit abd05d8
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions adapter/skylark/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,49 @@ import (
"chat/globals"
"chat/utils"
"fmt"

"github.com/volcengine/volc-sdk-golang/service/maas"
"github.com/volcengine/volc-sdk-golang/service/maas/models/api"
)

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 {
Expand Down

0 comments on commit abd05d8

Please sign in to comment.