fix: 添加messages为空校验

This commit is contained in:
tongkunming 2023-11-08 16:51:36 +08:00
parent da9d0dc3bc
commit 72418ce4d7

View File

@ -252,15 +252,17 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio
// loading the role context
var messages []types.Message
err := utils.JsonDecode(role.Context, &messages)
if err == nil {
for _, v := range messages {
tks, _ := utils.CalcTokens(v.Content, req.Model)
if tokens+tks >= types.ModelToTokens[req.Model] {
break
if len(messages) > 0 {
err := utils.JsonDecode(role.Context, &messages)
if err == nil {
for _, v := range messages {
tks, _ := utils.CalcTokens(v.Content, req.Model)
if tokens+tks >= types.ModelToTokens[req.Model] {
break
}
tokens += tks
chatCtx = append(chatCtx, v)
}
tokens += tks
chatCtx = append(chatCtx, v)
}
}