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条,删除所有上下文
|
// 如果上下文超过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) // 删除该用户的所有上下文
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,17 +70,25 @@ 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 {
|
||||||
message, err := tools.GetListValue(key, int64(i))
|
for i := 0; i < int(length); i++ {
|
||||||
if err != nil {
|
message, err := tools.GetListValue(key, int64(i))
|
||||||
log.Println("获取上下文失败:", err)
|
if err != nil {
|
||||||
return "获取上下文失败"
|
log.Println("获取上下文失败:", err)
|
||||||
|
return "获取上下文失败"
|
||||||
|
}
|
||||||
|
messages = append(messages, openai.ChatCompletionMessage{
|
||||||
|
Role: openai.ChatMessageRoleUser,
|
||||||
|
Content: message,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
messages = append(messages, openai.ChatCompletionMessage{
|
messages = append(messages, openai.ChatCompletionMessage{
|
||||||
Role: openai.ChatMessageRoleUser,
|
Role: openai.ChatMessageRoleUser,
|
||||||
Content: message,
|
Content: a.RawMsg[strings.Index(a.RawMsg, " ")+1:],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
messages = append(messages, openai.ChatCompletionMessage{
|
messages = append(messages, openai.ChatCompletionMessage{
|
||||||
Role: openai.ChatMessageRoleUser,
|
Role: openai.ChatMessageRoleUser,
|
||||||
|
|
Loading…
Reference in a new issue