119 lines
4.6 KiB
Go
119 lines
4.6 KiB
Go
package redis
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
)
|
||
|
||
// HistoryMessage 历史消息结构(用于上下文注入)
|
||
type HistoryMessage struct {
|
||
Question string `json:"question"` // 用户问题
|
||
Answer string `json:"answer"` // AI 回复
|
||
}
|
||
|
||
// SendStreamMessage 发送到 Redis Stream 的消息结构
|
||
type SendStreamMessage struct {
|
||
UserId string `json:"userId"` // 用户ID
|
||
Content string `json:"content"` // 消息内容
|
||
Timestamp int64 `json:"timestamp"` // 时间戳(秒)
|
||
MessageId string `json:"messageId"` // 消息唯一ID
|
||
Platform string `json:"platform,omitempty"` // 平台标识
|
||
AccountId string `json:"accountId,omitempty"` // 账号ID
|
||
TenantId string `json:"tenantId,omitempty"` // 租户ID(数据隔离)
|
||
AccountName string `json:"accountName,omitempty"` // 客服账号名称
|
||
ChatId string `json:"chatId,omitempty"` // RAGFlow Chat ID(从ragflow_config查询)
|
||
ReplyQueue string `json:"replyQueue,omitempty"` // 响应队列名称(支持多实例独立队列)
|
||
History []HistoryMessage `json:"history,omitempty"` // 历史对话(归档后恢复时携带)
|
||
}
|
||
|
||
// BatchStreamMessage 批量消息结构
|
||
type BatchStreamMessage struct {
|
||
UserId string `json:"userId"` // 用户ID
|
||
Content string `json:"content"` // 消息内容
|
||
Timestamp int64 `json:"timestamp"` // 时间戳(秒)
|
||
BatchId string `json:"batchId"` // 批次ID
|
||
Index int `json:"index"` // 批次内序号
|
||
}
|
||
|
||
// ResponseStreamMessage RAGFlow 响应消息结构(MQ 消息)
|
||
type ResponseStreamMessage struct {
|
||
UserId string `json:"userId"` // 用户ID
|
||
Platform string `json:"platform"` // 平台标识
|
||
TenantId string `json:"tenantId"` // 租户ID
|
||
AccountId string `json:"accountId,omitempty"` // 账号ID
|
||
AccountName string `json:"accountName,omitempty"` // 客服账号名称
|
||
Question string `json:"question"` // 用户问题
|
||
Content string `json:"content"` // RAGFlow 回复内容
|
||
SessionId string `json:"sessionId"` // RAGFlow Session ID
|
||
Timestamp int64 `json:"timestamp"` // 时间戳(秒)
|
||
MessageId string `json:"messageId"` // 原始消息ID
|
||
}
|
||
|
||
// FollowUpMessage 追问消息结构(RabbitMQ 延时队列)
|
||
type FollowUpMessage struct {
|
||
TenantId string `json:"tenantId"` // 租户ID
|
||
UserId string `json:"userId"` // 用户ID
|
||
Platform string `json:"platform"` // 平台标识
|
||
Content string `json:"content"` // 追问内容
|
||
FollowUpType int `json:"followUpType"` // 追问类型:1=30s, 2=60s, 3=180s
|
||
Timestamp int64 `json:"timestamp"` // 发送时间戳
|
||
}
|
||
|
||
// 追问类型常量
|
||
const (
|
||
FollowUpType1 = 1 // 第一次追问
|
||
FollowUpType2 = 2 // 第二次追问
|
||
FollowUpType3 = 3 // 第三次追问
|
||
)
|
||
|
||
// GetFollowUpContent 获取追问话术(从 config.yml 读取)
|
||
func GetFollowUpContent(followUpType int) string {
|
||
ctx := context.Background()
|
||
contents := g.Cfg().MustGet(ctx, "followUp.contents").Strings()
|
||
if len(contents) == 0 {
|
||
return ""
|
||
}
|
||
// followUpType: 1,2,3 对应数组索引 0,1,2
|
||
index := followUpType - 1
|
||
if index >= 0 && index < len(contents) {
|
||
return contents[index]
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// GetFollowUpDelay 获取追问延时(从 config.yml 读取)
|
||
func GetFollowUpDelay(followUpType int) int {
|
||
ctx := context.Background()
|
||
delays := g.Cfg().MustGet(ctx, "followUp.delays").Ints()
|
||
if len(delays) == 0 {
|
||
return 30 // 默认30秒
|
||
}
|
||
// followUpType: 1,2,3 对应数组索引 0,1,2
|
||
index := followUpType - 1
|
||
if index >= 0 && index < len(delays) {
|
||
return delays[index]
|
||
}
|
||
return 30
|
||
}
|
||
|
||
// ArchiveMessage 会话归档消息结构(RabbitMQ 延时队列)
|
||
type ArchiveMessage struct {
|
||
UserId string `json:"userId"` // 用户ID
|
||
Platform string `json:"platform"` // 平台标识
|
||
SessionId string `json:"sessionId"` // RAGFlow Session ID
|
||
Timestamp int64 `json:"timestamp"` // 发送时间戳
|
||
}
|
||
|
||
// GetArchiveDelay 获取归档延时(从 config.yml 读取)
|
||
func GetArchiveDelay() int {
|
||
ctx := context.Background()
|
||
return g.Cfg().MustGet(ctx, "archive.delay", 3600).Int() // 默认3600秒(1小时)
|
||
}
|
||
|
||
// GetHistoryContextLimit 获取历史上下文轮数(从 config.yml 读取)
|
||
func GetHistoryContextLimit() int64 {
|
||
ctx := context.Background()
|
||
return g.Cfg().MustGet(ctx, "history.contextLimit", 5).Int64() // 默认5轮对话
|
||
}
|