refactor(ai): 优化上下文消息处理逻辑
This commit is contained in:
parent
472610e757
commit
90e51bc485
2 changed files with 15 additions and 7 deletions
|
@ -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) // 删除该用户的所有上下文
|
||||
}
|
||||
|
|
|
@ -70,7 +70,8 @@ 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++ {
|
||||
if length > 0 {
|
||||
for i := 0; i < int(length); i++ {
|
||||
message, err := tools.GetListValue(key, int64(i))
|
||||
if err != nil {
|
||||
log.Println("获取上下文失败:", err)
|
||||
|
@ -87,6 +88,13 @@ func (a *AI) GetMsg() string {
|
|||
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)
|
||||
// for i, msg := range messages {
|
||||
// fmt.Printf("消息 %d:\n", i+1)
|
||||
|
|
Loading…
Reference in a new issue