From 90e51bc48590d710852243c7b1c56c35581f3ad4 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 22 Sep 2024 16:48:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ai):=20=E4=BC=98=E5=8C=96=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/redisClient.go | 2 +- workers/ai.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) 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,