feat:意图识别拆分

This commit is contained in:
wolter 2025-12-10 15:23:02 +08:00
parent d7b39f4d60
commit 7c973750dd
4 changed files with 114 additions and 10 deletions

View File

@ -2,8 +2,11 @@ package biz
import ( import (
"ai_scheduler/internal/biz/do" "ai_scheduler/internal/biz/do"
"ai_scheduler/internal/data/constants"
"ai_scheduler/internal/gateway" "ai_scheduler/internal/gateway"
"context" "context"
"encoding/json"
"time"
"ai_scheduler/internal/entitys" "ai_scheduler/internal/entitys"
@ -77,6 +80,99 @@ func (r *AiRouterBiz) RouteWithSocket(client *gateway.Client, req *entitys.ChatS
} }
func (r *AiRouterBiz) SetRec(ctx context.Context, requireData *entitys.RequireData) (match entitys.Recognize, err error) { func (r *AiRouterBiz) SetRec(ctx context.Context, requireData *entitys.RequireData) (match entitys.Recognize, err error) {
//TODO 叙平 // 参数空值检查
if requireData == nil || requireData.Req == nil {
return match, err
}
// 1. 系统提示词
match.SystemPrompt = requireData.Sys.SysPrompt
// 2. 用户输入和文件处理
match.UserContent, err = r.buildUserContent(requireData)
if err != nil {
log.Errorf("构建用户内容失败: %s", err.Error())
return
}
// 3. 聊天记录 - 只有在有历史记录时才构建
if len(requireData.Histories) > 0 {
match.ChatHis = r.buildChatHistory(requireData)
}
// 4. 任务列表 - 预分配切片容量
if len(requireData.Tasks) > 0 {
match.Tasks = make([]entitys.RegistrationTask, 0, len(requireData.Tasks))
for _, task := range requireData.Tasks {
taskConfig := entitys.TaskConfigDetail{}
if err = json.Unmarshal([]byte(task.Config), &taskConfig); err != nil {
log.Errorf("解析任务配置失败: %s, 任务ID: %s", err.Error(), task.Index)
continue // 解析失败时跳过该任务,而不是直接返回错误
}
match.Tasks = append(match.Tasks, entitys.RegistrationTask{
Name: task.Index,
Desc: task.Desc,
TaskConfigDetail: taskConfig, // 直接使用解析后的配置,避免重复构建
})
}
}
match.Ch = requireData.Ch
return return
} }
// buildUserContent 构建用户内容
func (r *AiRouterBiz) buildUserContent(requireData *entitys.RequireData) (*entitys.RecognizeUserContent, error) {
// 预分配文件切片容量最多2个文件File和Img
files := make([]*entitys.RecognizeFile, 0, 2)
// 处理文件和图片
fileUrls := []string{requireData.Req.File, requireData.Req.Img}
for _, url := range fileUrls {
if url != "" {
files = append(files, &entitys.RecognizeFile{FileUrl: url})
}
}
// 构建并返回用户内容
return &entitys.RecognizeUserContent{
Text: requireData.Req.Text,
File: files,
ActionCardUrl: "", // TODO: 后续实现操作卡片功能
Tag: requireData.Req.Tags,
}, nil
}
// buildChatHistory 构建聊天历史
func (r *AiRouterBiz) buildChatHistory(requireData *entitys.RequireData) entitys.ChatHis {
// 预分配消息切片容量每个历史记录生成2条消息
messages := make([]entitys.HisMessage, 0, len(requireData.Histories)*2)
// 构建聊天记录
for _, h := range requireData.Histories {
// 用户消息
messages = append(messages, entitys.HisMessage{
Role: constants.RoleUser, // 用户角色
Content: h.Ans, // 用户输入内容
Timestamp: h.CreateAt.Format(time.DateTime),
})
// 助手消息
messages = append(messages, entitys.HisMessage{
Role: constants.RoleAssistant, // 助手角色
Content: h.Ques, // 助手回复内容
Timestamp: h.CreateAt.Format(time.DateTime),
})
}
// 构建聊天历史上下文
return entitys.ChatHis{
SessionId: requireData.Session,
Messages: messages,
Context: entitys.HisContext{
UserLanguage: constants.LangZhCN, // 默认中文
SystemMode: constants.SystemModeTechnicalSupport, // 默认技术支持模式
},
}
}

View File

@ -13,6 +13,14 @@ const (
// 分页默认条数 // 分页默认条数
ChatHistoryLimit = 10 ChatHistoryLimit = 10
// 语言
LangZhCN = "zh-CN" // 中文
// 系统模式
SystemModeDefault = "default" // 默认模式
// 系统模式 "technical_support", // 技术支持模式
SystemModeTechnicalSupport = "technical_support" // 技术支持模式
) )
func (c Caller) String() string { func (c Caller) String() string {

View File

@ -5,9 +5,9 @@ import (
) )
type Recognize struct { type Recognize struct {
SystemPrompt string SystemPrompt string // 系统提示内容
UserContent *RecognizeUserContent UserContent *RecognizeUserContent // 用户输入内容
ChatHis ChatHis ChatHis ChatHis // 会话历史记录
Tasks []RegistrationTask Tasks []RegistrationTask
Ch chan Response Ch chan Response
} }
@ -19,10 +19,10 @@ type RegistrationTask struct {
} }
type RecognizeUserContent struct { type RecognizeUserContent struct {
Text string Text string // 用户输入的文本内容
File []*RecognizeFile File []*RecognizeFile // 文件内容
ActionCardUrl string ActionCardUrl string // 操作卡片链接
Tag string Tag string // 工具标签
} }
type FileData []byte type FileData []byte

View File

@ -138,8 +138,8 @@ type HisMessage struct {
} }
type HisContext struct { type HisContext struct {
UserLanguage string `json:"user_language"` UserLanguage string `json:"user_language"` // 用户语言
SystemMode string `json:"system_mode"` SystemMode string `json:"system_mode"` // 系统模式,
} }
type RequireData struct { type RequireData struct {