Version: 0.4.1.dev.260304
feat: 💬 新增对话创建与上下文记忆机制 * 新增对话的创建与使用功能,实现会话级上下文隔离 * 实现上下文保存与传递机制,使模型具备持续对话记忆能力 * 引入滑动窗口策略控制上下文规模 * 当前窗口大小限制为 20 条消息,超过后自动丢弃最早消息以控制上下文长度 docs: 📝 更新示例配置文件 * 更新示例配置文件,新增 `agent` 相关配置信息 * 明确 Agent 模块运行所需参数,方便本地部署与环境初始化 undo: ⚠️ Agent 上下文读取性能待优化 * 当前测试中模型响应速度偏慢 * 计划后续将上下文暂存至缓存层,以减少读取与拼接开销并提升响应速度
This commit is contained in:
@@ -4,28 +4,77 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/agent"
|
||||
"github.com/LoveLosita/smartflow/backend/conv"
|
||||
"github.com/LoveLosita/smartflow/backend/dao"
|
||||
"github.com/LoveLosita/smartflow/backend/inits"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
type AgentService struct {
|
||||
AIHub *inits.AIHub
|
||||
repo *dao.AgentDAO
|
||||
}
|
||||
|
||||
func NewAgentService(aiHub *inits.AIHub) *AgentService {
|
||||
func NewAgentService(aiHub *inits.AIHub, repo *dao.AgentDAO) *AgentService {
|
||||
return &AgentService{
|
||||
AIHub: aiHub,
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AgentService) AgentChat(ctx context.Context, userMessage string) (<-chan string, <-chan error) {
|
||||
func (s *AgentService) AgentChat(ctx context.Context, userMessage string, userID int, chatID string) (<-chan string, <-chan error) {
|
||||
//1. 创建一个输出通道
|
||||
outChan := make(chan string, 5)
|
||||
errChan := make(chan error)
|
||||
//2. 启动一个 goroutine 来处理聊天逻辑
|
||||
errChan := make(chan error, 1)
|
||||
//2. 先确保这个会话存在(如果不存在就创建一个新的)
|
||||
result, err := s.repo.IfChatExists(ctx, userID, chatID)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
close(errChan)
|
||||
return outChan, errChan
|
||||
}
|
||||
var chatHistory []*schema.Message
|
||||
if result {
|
||||
//4. 提取出历史消息,构建上下文
|
||||
//先从数据库拿到历史消息
|
||||
histories, err := s.repo.GetUserChatHistories(ctx, userID, 20, chatID)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
close(errChan)
|
||||
return outChan, errChan
|
||||
}
|
||||
//再转换成 Eino 的消息格式
|
||||
chatHistory = conv.ToEinoMessages(histories)
|
||||
} else {
|
||||
//如果会话不存在,先创建一个新的会话
|
||||
_, err := s.repo.CreateNewChat(userID, chatID)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
close(errChan)
|
||||
return outChan, errChan
|
||||
}
|
||||
}
|
||||
//3. 将用户消息落库
|
||||
err = s.repo.SaveChatHistory(ctx, userID, chatID, "user", userMessage)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(outChan)
|
||||
close(errChan)
|
||||
return outChan, errChan
|
||||
}
|
||||
//5. 启动一个 goroutine 来处理聊天逻辑
|
||||
go func() {
|
||||
defer close(outChan) // 确保在函数结束时关闭通道
|
||||
//3. 调用 StreamChat 函数进行流式聊天
|
||||
err := agent.StreamChat(ctx, s.AIHub.Worker, userMessage, outChan)
|
||||
fullText, err := agent.StreamChat(ctx, s.AIHub.Worker, userMessage, chatHistory, outChan)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
err = s.repo.SaveChatHistory(ctx, userID, chatID, "assistant", fullText)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
@@ -33,3 +82,7 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string) (<-cha
|
||||
}()
|
||||
return outChan, errChan
|
||||
}
|
||||
|
||||
func (s *AgentService) CreateNewChat(userID int, chatID string) (int64, error) {
|
||||
return s.repo.CreateNewChat(userID, chatID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user