refactor(ai): 优化上下文消息处理逻辑

This commit is contained in:
liyp 2024-09-22 16:48:21 +08:00
parent 472610e757
commit 90e51bc485
2 changed files with 15 additions and 7 deletions

View file

@ -54,7 +54,7 @@ func AddToContext(key string, message string, contextlenth int64) {
// 如果上下文超过5条删除所有上下文 // 如果上下文超过5条删除所有上下文
listLength := GetListLength(key) listLength := GetListLength(key)
log.Println("listLength:", listLength) // log.Println("listLength:", listLength)
if listLength >= contextlenth { if listLength >= contextlenth {
rdb.Del(ctx, key) // 删除该用户的所有上下文 rdb.Del(ctx, key) // 删除该用户的所有上下文
} }

View file

@ -70,7 +70,8 @@ func (a *AI) GetMsg() string {
// } // }
// println("RawMsg:", a.RawMsg[strings.Index(a.RawMsg, " ")+1:]) // println("RawMsg:", a.RawMsg[strings.Index(a.RawMsg, " ")+1:])
length := tools.GetListLength(key) length := tools.GetListLength(key)
for i := 1; i < int(length); i++ { if length > 0 {
for i := 0; i < int(length); i++ {
message, err := tools.GetListValue(key, int64(i)) message, err := tools.GetListValue(key, int64(i))
if err != nil { if err != nil {
log.Println("获取上下文失败:", err) log.Println("获取上下文失败:", err)
@ -87,6 +88,13 @@ func (a *AI) GetMsg() string {
Content: a.RawMsg[strings.Index(a.RawMsg, " ")+1:], Content: a.RawMsg[strings.Index(a.RawMsg, " ")+1:],
}) })
} }
} else {
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: a.RawMsg[strings.Index(a.RawMsg, " ")+1:],
})
}
// println("messages:", messages) // println("messages:", messages)
// for i, msg := range messages { // for i, msg := range messages {
// fmt.Printf("消息 %d:\n", i+1) // fmt.Printf("消息 %d:\n", i+1)