话术redis更新

This commit is contained in:
Cold
2025-12-16 15:20:16 +08:00
committed by 张斌
parent e7c2fe280f
commit 8823f54370
2 changed files with 67 additions and 27 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ ragflow/client_backup.go.bak
ragflow/为什么不能使用gclient.md
ragflow/agent文档.md
/.idea/MarsCodeWorkspaceAppSettings.xml
consul/配置中心方案.md

View File

@@ -479,45 +479,84 @@ func Unlock(ctx context.Context, key string) {
// ============== 对话计数相关(用于卡片触发)==============
const (
// ConversationCountKeyPrefix 对话计数 Key 前缀
ConversationCountKeyPrefix = "ragflow:conversation:count:"
// UserStateKeyPrefix 用户会话状态 Key 前缀(融合阶段+计数)
UserStateKeyPrefix = "ragflow:user:state:"
// UserStateExpireSeconds 用户状态过期时间5分钟
UserStateExpireSeconds = 300
)
// IncrConversationCount 增加用户对话计数,返回当前轮数
// 用于判断是否触发发送卡片如对话5轮后发送
// expireSeconds: 过期时间建议与会话超时一致如7200秒=2小时
func IncrConversationCount(ctx context.Context, userId, platform string, expireSeconds int64) (count int64, err error) {
key := ConversationCountKeyPrefix + userId + "_" + platform
result, err := redisClient.Do(ctx, "INCR", key)
// UserState 用户会话状态(阶段+对话计数统一5分钟过期
type UserState struct {
Stage int `json:"stage"` // 用户阶段0=AI模型 1=打招呼 2=业务 3=发卡片
Count int64 `json:"count"` // 对话计数
}
// GetUserState 获取用户状态(阶段+计数)
func GetUserState(ctx context.Context, userId, platform string) (state *UserState, err error) {
key := UserStateKeyPrefix + userId + "_" + platform
result, err := redisClient.Do(ctx, "HGETALL", key)
if err != nil {
return
}
count = result.Int64()
// 首次设置过期时间
if count == 1 {
redisClient.Do(ctx, "EXPIRE", key, expireSeconds)
}
return
}
// GetConversationCount 获取用户当前对话轮数
func GetConversationCount(ctx context.Context, userId, platform string) (count int64, err error) {
key := ConversationCountKeyPrefix + userId + "_" + platform
result, err := redisClient.Get(ctx, key)
if err != nil {
return
}
state = &UserState{}
if result.IsEmpty() {
return 0, nil
return // 返回默认值 stage=0, count=0
}
count = result.Int64()
m := result.Map()
state.Stage = gconv.Int(m["stage"])
state.Count = gconv.Int64(m["count"])
return
}
// ResetConversationCount 重置用户对话计数(归档/卡片发送后调用)
func ResetConversationCount(ctx context.Context, userId, platform string) error {
key := ConversationCountKeyPrefix + userId + "_" + platform
// SetUserStage 设置用户阶段,并刷新过期时间
func SetUserStage(ctx context.Context, userId, platform string, stage int) error {
key := UserStateKeyPrefix + userId + "_" + platform
_, err := redisClient.Do(ctx, "HSET", key, "stage", stage)
if err != nil {
return err
}
_, err = redisClient.Do(ctx, "EXPIRE", key, UserStateExpireSeconds)
return err
}
// IncrUserCount 增加用户对话计数,返回当前轮数,并刷新过期时间
func IncrUserCount(ctx context.Context, userId, platform string) (count int64, err error) {
key := UserStateKeyPrefix + userId + "_" + platform
result, err := redisClient.Do(ctx, "HINCRBY", key, "count", 1)
if err != nil {
return
}
count = result.Int64()
_, err = redisClient.Do(ctx, "EXPIRE", key, UserStateExpireSeconds)
return
}
// ResetUserState 重置用户状态(归档时调用)
func ResetUserState(ctx context.Context, userId, platform string) error {
key := UserStateKeyPrefix + userId + "_" + platform
_, err := redisClient.Del(ctx, key)
return err
}
// ========== 以下为兼容旧接口(内部调用新实现)==========
// IncrConversationCount 增加用户对话计数(兼容旧接口)
func IncrConversationCount(ctx context.Context, userId, platform string, _ int64) (count int64, err error) {
return IncrUserCount(ctx, userId, platform)
}
// GetConversationCount 获取用户当前对话轮数(兼容旧接口)
func GetConversationCount(ctx context.Context, userId, platform string) (count int64, err error) {
state, err := GetUserState(ctx, userId, platform)
if err != nil {
return
}
return state.Count, nil
}
// ResetConversationCount 重置用户对话计数(兼容旧接口)
func ResetConversationCount(ctx context.Context, userId, platform string) error {
return ResetUserState(ctx, userId, platform)
}