diff --git a/tools/redisClient.go b/tools/redisClient.go index 22f5e26..6de3fc0 100644 --- a/tools/redisClient.go +++ b/tools/redisClient.go @@ -54,7 +54,7 @@ func AddToContext(key string, message string, contextlenth int64) { // 如果上下文超过5条,删除所有上下文 listLength := GetListLength(key) - log.Println("listLength:", listLength) + // log.Println("listLength:", listLength) if listLength >= contextlenth { rdb.Del(ctx, key) // 删除该用户的所有上下文 } diff --git a/workers/ai.go b/workers/ai.go index 43c17b2..381761d 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -70,17 +70,25 @@ func (a *AI) GetMsg() string { // } // println("RawMsg:", a.RawMsg[strings.Index(a.RawMsg, " ")+1:]) length := tools.GetListLength(key) - for i := 1; i < int(length); i++ { - message, err := tools.GetListValue(key, int64(i)) - if err != nil { - log.Println("获取上下文失败:", err) - return "获取上下文失败" + if length > 0 { + for i := 0; i < int(length); i++ { + message, err := tools.GetListValue(key, int64(i)) + if err != nil { + log.Println("获取上下文失败:", err) + return "获取上下文失败" + } + messages = append(messages, openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleUser, + Content: message, + }) } + } else { messages = append(messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser, - Content: message, + Content: a.RawMsg[strings.Index(a.RawMsg, " ")+1:], }) } + } else { messages = append(messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser,