Merge branch 'v3' into feature/v2-fzy

This commit is contained in:
fuzhongyun 2025-11-21 11:59:31 +08:00
commit ed6ea71bb0
19 changed files with 229 additions and 102 deletions

File diff suppressed because one or more lines are too long

View File

@ -10,7 +10,7 @@ import (
) )
func main() { func main() {
configPath := flag.String("config", "./config/config.yaml", "Path to configuration file") configPath := flag.String("config", "./config/config_test.yaml", "Path to configuration file")
flag.Parse() flag.Parse()
bc, err := config.LoadConfig(*configPath) bc, err := config.LoadConfig(*configPath)
if err != nil { if err != nil {

View File

@ -7,7 +7,7 @@ ollama:
base_url: "http://127.0.0.1:11434" base_url: "http://127.0.0.1:11434"
model: "qwen3-coder:480b-cloud" model: "qwen3-coder:480b-cloud"
generate_model: "qwen3-coder:480b-cloud" generate_model: "qwen3-coder:480b-cloud"
vl_model: "qwen2.5vl:7b" vl_model: "qwen2.5vl:3b"
timeout: "120s" timeout: "120s"
level: "info" level: "info"
format: "json" format: "json"

2
gen.sh
View File

@ -16,4 +16,4 @@
gentool --dsn "root:SD###sdf323r343@tcp(121.199.38.107:3306)/sys_ai?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai" -outPath ${modeldir} -onlyModel -modelPkgName "model" -tables ${prefix}${tables} gentool --dsn "root:SD###sdf323r343@tcp(121.199.38.107:3306)/sys_ai_test?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai" -outPath ${modeldir} -onlyModel -modelPkgName "model" -tables ${prefix}${tables}

BIN
img.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 MiB

View File

@ -5,6 +5,8 @@ import (
"ai_scheduler/internal/data/model" "ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys" "ai_scheduler/internal/entitys"
"context" "context"
"xorm.io/builder"
) )
type ChatHistoryBiz struct { type ChatHistoryBiz struct {
@ -15,35 +17,41 @@ func NewChatHistoryBiz(chatRepo *impl.ChatImpl) *ChatHistoryBiz {
s := &ChatHistoryBiz{ s := &ChatHistoryBiz{
chatRepo: chatRepo, chatRepo: chatRepo,
} }
go s.AsyncProcess(context.Background()) //go s.AsyncProcess(context.Background())
return s return s
} }
func (s *ChatHistoryBiz) create(ctx context.Context, sessionID, role, content string) error { //func (s *ChatHistoryBiz) create(ctx context.Context, sessionID, role, content string) error {
chat := model.AiChatHi{ // chat := model.AiChatHi{
SessionID: sessionID, // SessionID: sessionID,
Role: role, // Role: role,
Content: content, // Content: content,
} // }
//
return s.chatRepo.Create(&chat) // return s.chatRepo.Create(&chat)
} //}
//
// 添加会话历史 //// 添加会话历史
func (s *ChatHistoryBiz) Create(ctx context.Context, chat entitys.ChatHistory) error { //func (s *ChatHistoryBiz) Create(ctx context.Context, chat entitys.ChatHistory) error {
return s.create(ctx, chat.SessionID, chat.Role.String(), chat.Content) // return s.create(ctx, chat.SessionID, chat.Role.String(), chat.Content)
} //}
// 异步添加会话历史 // 异步添加会话历史
func (s *ChatHistoryBiz) AsyncCreate(ctx context.Context, chat entitys.ChatHistory) { //func (s *ChatHistoryBiz) AsyncCreate(ctx context.Context, chat entitys.ChatHistory) {
s.chatRepo.AsyncCreate(ctx, model.AiChatHi{ // s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
SessionID: chat.SessionID, // SessionID: chat.SessionID,
Role: chat.Role.String(), // Role: chat.Role.String(),
Content: chat.Content, // Content: chat.Content,
}) // })
} //}
// 异步处理会话历史 // 异步处理会话历史
func (s *ChatHistoryBiz) AsyncProcess(ctx context.Context) { //func (s *ChatHistoryBiz) AsyncProcess(ctx context.Context) {
s.chatRepo.AsyncProcess(ctx) // s.chatRepo.AsyncProcess(ctx)
//}
func (s *ChatHistoryBiz) Update(ctx context.Context, chat *entitys.UseFulRequest) error {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"his_id": chat.HisId})
return s.chatRepo.UpdateByCond(&cond, &model.AiChatHi{HisID: chat.HisId, Useful: chat.Useful})
} }

View File

@ -94,7 +94,7 @@ func (d *Do) DataAuth(c *websocket.Conn) (err error) {
func (d *Do) MakeCh(c *websocket.Conn) (ctx context.Context, deferFunc func()) { func (d *Do) MakeCh(c *websocket.Conn) (ctx context.Context, deferFunc func()) {
d.Ctx.Ch = make(chan entitys.Response) d.Ctx.Ch = make(chan entitys.Response)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
done := d.startMessageHandler(ctx, c, d.hisImpl) done := d.startMessageHandler(ctx, c)
return ctx, func() { return ctx, func() {
close(d.Ctx.Ch) //关闭主通道 close(d.Ctx.Ch) //关闭主通道
<-done // 等待消息处理完成 <-done // 等待消息处理完成
@ -186,7 +186,7 @@ func (d *Do) getTasks(sysId int32) (tasks []model.AiTask, err error) {
func (d *Do) startMessageHandler( func (d *Do) startMessageHandler(
ctx context.Context, ctx context.Context,
c *websocket.Conn, c *websocket.Conn,
hisImpl *impl.ChatImpl,
) <-chan struct{} { ) <-chan struct{} {
done := make(chan struct{}) done := make(chan struct{})
var chat []string var chat []string
@ -195,23 +195,25 @@ func (d *Do) startMessageHandler(
defer func() { defer func() {
close(done) close(done)
// 保存历史记录 // 保存历史记录
var his = []*model.AiChatHi{ var (
{ hisLog = &entitys.ChatHisLog{}
SessionID: d.Ctx.Session, )
Role: "user",
Content: d.Ctx.Req.Text, // 用户输入在外部处理
},
}
if len(chat) > 0 { if len(chat) > 0 {
his = append(his, &model.AiChatHi{ AiRes := &model.AiChatHi{
SessionID: d.Ctx.Session, SessionID: d.Ctx.Session,
Role: "assistant", Ques: d.Ctx.Req.Text,
Content: strings.Join(chat, ""), Ans: strings.Join(chat, ""),
Files: d.Ctx.Req.Img,
}
d.hisImpl.AddWithData(AiRes)
hisLog.HisId = AiRes.HisID
}
_ = entitys.MsgSend(c, entitys.Response{
Content: pkg.JsonStringIgonErr(hisLog),
Type: entitys.ResponseEnd,
}) })
}
for _, hi := range his {
hisImpl.Add(hi)
}
}() }()
for v := range d.Ctx.Ch { // 自动检测通道关闭 for v := range d.Ctx.Ch { // 自动检测通道关闭

View File

@ -90,7 +90,7 @@ func (r *Handle) HandleMatch(ctx context.Context, requireData *entitys.RequireDa
if pointTask == nil || pointTask.Index == "other" { if pointTask == nil || pointTask.Index == "other" {
return r.OtherTask(ctx, requireData) return r.OtherTask(ctx, requireData)
} }
switch pointTask.Type { switch constants.TaskType(pointTask.Type) {
case constants.TaskTypeApi: case constants.TaskTypeApi:
return r.handleApiTask(ctx, requireData, pointTask) return r.handleApiTask(ctx, requireData, pointTask)
case constants.TaskTypeFunc: case constants.TaskTypeFunc:

View File

@ -1,9 +1,11 @@
package llm_service package llm_service
import ( import (
"ai_scheduler/internal/data/constants"
"ai_scheduler/internal/data/model" "ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys" "ai_scheduler/internal/entitys"
"context" "context"
"time"
) )
type LlmService interface { type LlmService interface {
@ -24,11 +26,18 @@ func buildAssistant(his []model.AiChatHi) (chatHis entitys.ChatHis) {
if len(chatHis.SessionId) == 0 { if len(chatHis.SessionId) == 0 {
chatHis.SessionId = item.SessionID chatHis.SessionId = item.SessionID
} }
chatHis.Messages = append(chatHis.Messages, entitys.HisMessage{ chatHis.Messages = append(chatHis.Messages, []entitys.HisMessage{
Role: item.Role, {
Content: item.Content, Role: constants.RoleUser,
Timestamp: item.CreateAt.Format("2006-01-02 15:04:05"), Content: item.Ques,
}) Timestamp: item.CreateAt.Format(time.DateTime),
},
{
Role: constants.RoleAssistant,
Content: item.Ans,
Timestamp: item.CreateAt.Format(time.DateTime),
},
}...)
} }
chatHis.Context = entitys.HisContext{ chatHis.Context = entitys.HisContext{
UserLanguage: "zh-CN", UserLanguage: "zh-CN",
@ -36,3 +45,23 @@ func buildAssistant(his []model.AiChatHi) (chatHis entitys.ChatHis) {
} }
return return
} }
func BuildChatHisMessage(his []model.AiChatHi) (chatHis []entitys.HisMessage) {
for _, item := range his {
chatHis = append(chatHis, []entitys.HisMessage{
{
Role: constants.RoleUser,
Content: item.Ques,
Timestamp: item.CreateAt.Format(time.DateTime),
},
{
Role: constants.RoleAssistant,
Content: item.Ans,
Timestamp: item.CreateAt.Format(time.DateTime),
},
}...)
}
return
}

View File

@ -2,6 +2,7 @@ package llm_service
import ( import (
"ai_scheduler/internal/config" "ai_scheduler/internal/config"
"ai_scheduler/internal/data/impl"
"ai_scheduler/internal/data/model" "ai_scheduler/internal/data/model"
"ai_scheduler/internal/entitys" "ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg" "ai_scheduler/internal/pkg"
@ -13,20 +14,24 @@ import (
"time" "time"
"github.com/ollama/ollama/api" "github.com/ollama/ollama/api"
"xorm.io/builder"
) )
type OllamaService struct { type OllamaService struct {
client *utils_ollama.Client client *utils_ollama.Client
config *config.Config config *config.Config
chatHis *impl.ChatImpl
} }
func NewOllamaGenerate( func NewOllamaGenerate(
client *utils_ollama.Client, client *utils_ollama.Client,
config *config.Config, config *config.Config,
chatHis *impl.ChatImpl,
) *OllamaService { ) *OllamaService {
return &OllamaService{ return &OllamaService{
client: client, client: client,
config: config, config: config,
chatHis: chatHis,
} }
} }
@ -66,6 +71,10 @@ func (r *OllamaService) getPrompt(ctx context.Context, requireData *entitys.Requ
var ( var (
prompt = make([]api.Message, 0) prompt = make([]api.Message, 0)
) )
content, err := r.getUserContent(ctx, requireData)
if err != nil {
return nil, err
}
prompt = append(prompt, api.Message{ prompt = append(prompt, api.Message{
Role: "system", Role: "system",
Content: buildSystemPrompt(requireData.Sys.SysPrompt), Content: buildSystemPrompt(requireData.Sys.SysPrompt),
@ -74,32 +83,17 @@ func (r *OllamaService) getPrompt(ctx context.Context, requireData *entitys.Requ
Content: "### 聊天记录:" + pkg.JsonStringIgonErr(buildAssistant(requireData.Histories)), Content: "### 聊天记录:" + pkg.JsonStringIgonErr(buildAssistant(requireData.Histories)),
}, api.Message{ }, api.Message{
Role: "user", Role: "user",
Content: r.getUserContent(requireData), Content: content,
//Images: requireData.ImgByte,
}) })
if len(requireData.ImgByte) > 0 {
desc, err := r.RecognizeWithImg(ctx, requireData)
if err != nil {
return nil, err
}
var imgs strings.Builder
imgs.WriteString("### 用户上传图片解析内容:\n")
prompt = append(prompt, api.Message{
Role: "image_desc",
Content: "" + desc.Response,
})
}
return prompt, nil return prompt, nil
} }
func (r *OllamaService) getUserContent(requireData *entitys.RequireData) string { func (r *OllamaService) getUserContent(ctx context.Context, requireData *entitys.RequireData) (string, error) {
var content strings.Builder var content strings.Builder
content.WriteString(requireData.Req.Text) content.WriteString(requireData.Req.Text)
if len(requireData.ImgByte) > 0 { if len(requireData.ImgByte) > 0 {
content.WriteString("\n") content.WriteString("\n")
content.WriteString("### 图片内容已经解析到image_desc里")
} }
if len(requireData.Req.Tags) > 0 { if len(requireData.Req.Tags) > 0 {
@ -107,7 +101,29 @@ func (r *OllamaService) getUserContent(requireData *entitys.RequireData) string
content.WriteString("### 工具必须使用:") content.WriteString("### 工具必须使用:")
content.WriteString(requireData.Req.Tags) content.WriteString(requireData.Req.Tags)
} }
return content.String()
if len(requireData.ImgByte) > 0 {
desc, err := r.RecognizeWithImg(ctx, requireData)
if err != nil {
return "", err
}
content.WriteString("### 上传图片解析内容:\n")
content.WriteString(requireData.Req.Tags)
content.WriteString(desc.Response)
}
if requireData.Req.MarkHis > 0 {
var his model.AiChatHi
cond := builder.NewCond()
cond = cond.And(builder.Eq{"his_id": requireData.Req.MarkHis})
err := r.chatHis.GetOneBySearchToStrut(&cond, &his)
if err != nil {
return "", err
}
content.WriteString("### 引用历史聊天记录:\n")
content.WriteString(pkg.JsonStringIgonErr(BuildChatHisMessage([]model.AiChatHi{his})))
}
return content.String(), nil
} }
func (r *OllamaService) RecognizeWithImg(ctx context.Context, requireData *entitys.RequireData) (desc api.GenerateResponse, err error) { func (r *OllamaService) RecognizeWithImg(ctx context.Context, requireData *entitys.RequireData) (desc api.GenerateResponse, err error) {

View File

@ -80,11 +80,11 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe
result.Prologue = sysConfig.Prologue result.Prologue = sysConfig.Prologue
// 开场白写入会话历史 // 开场白写入会话历史
s.chatRepo.AsyncCreate(ctx, model.AiChatHi{ //s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
SessionID: chat.SessionID, // SessionID: chat.SessionID,
Role: chat.Role.String(), // Role: chat.Role.String(),
Content: chat.Content, // Content: chat.Content,
}) //})
} else { } else {
result.SessionId = session.SessionID result.SessionId = session.SessionID
@ -102,12 +102,20 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe
// 转换为 entitys.ChatHistory 类型 // 转换为 entitys.ChatHistory 类型
for _, chat := range chatList { for _, chat := range chatList {
result.Chat = append(result.Chat, entitys.ChatHistory{ result.Chat = append(result.Chat, []entitys.ChatHistory{
{
SessionID: chat.SessionID, SessionID: chat.SessionID,
Role: constants.Caller(chat.Role), Role: constants.RoleUser,
Content: chat.Content, Content: chat.Ques,
Prologue: sysConfig.Prologue, Prologue: sysConfig.Prologue,
}) },
{
SessionID: chat.SessionID,
Role: constants.RoleAssistant,
Content: chat.Ans,
Prologue: sysConfig.Prologue,
},
}...)
} }
} }

View File

@ -11,8 +11,22 @@ const (
type TaskType int32 type TaskType int32
const ( const (
TaskTypeApi = 1 TaskTypeApi TaskType = 1
TaskTypeKnowle = 2 TaskTypeKnowle TaskType = 2
TaskTypeFunc = 3 TaskTypeFunc TaskType = 3
TaskTypeBot = 4 TaskTypeBot TaskType = 4
) )
type UseFul int32
const (
UseFulNotSolve UseFul = 2
UseFulNotUnclear UseFul = 3
UseFulNotError UseFul = 4
)
var UseFulMap = map[UseFul]string{
UseFulNotSolve: "未解决问题",
UseFulNotUnclear: "回答不明确",
UseFulNotError: "理解错误",
}

View File

@ -12,11 +12,14 @@ const TableNameAiChatHi = "ai_chat_his"
// AiChatHi mapped from table <ai_chat_his> // AiChatHi mapped from table <ai_chat_his>
type AiChatHi struct { type AiChatHi struct {
HisID int64 `gorm:"column:his_id;primaryKey" json:"his_id"` HisID int64 `gorm:"column:his_id;primaryKey;autoIncrement:true" json:"his_id"`
SessionID string `gorm:"column:session_id;not null" json:"session_id"` SessionID string `gorm:"column:session_id;not null" json:"session_id"`
Role string `gorm:"column:role;not null;comment:system系统输出assistant助手输出,user用户输入" json:"role"` // system系统输出assistant助手输出,user用户输入 Ques string `gorm:"column:ques;not null" json:"ques"`
Content string `gorm:"column:content;not null" json:"content"` Ans string `gorm:"column:ans;not null" json:"ans"`
CreateAt *time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"` Files string `gorm:"column:files;not null" json:"files"`
Useful int32 `gorm:"column:useful;not null;comment:0不评价,1有用其他为无用" json:"useful"` // 0不评价,1有用其他为无用
CreateAt time.Time `gorm:"column:create_at;default:CURRENT_TIMESTAMP" json:"create_at"`
UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP" json:"updated_at"`
} }
// TableName AiChatHi's table name // TableName AiChatHi's table name

View File

@ -10,3 +10,7 @@ type ChatHistory struct {
Content string `json:"content"` Content string `json:"content"`
Prologue string `json:"prologue"` Prologue string `json:"prologue"`
} }
type ChatHisLog struct {
HisId int64 `json:"his_id"`
}

View File

@ -23,3 +23,8 @@ type SessionListRequest struct {
type TaskRequest struct { type TaskRequest struct {
SysId int32 `json:"sys_id"` SysId int32 `json:"sys_id"`
} }
type UseFulRequest struct {
HisId int64 `json:"his_id"`
Useful int32 `json:"useful"`
}

View File

@ -1,6 +1,7 @@
package entitys package entitys
import ( import (
"ai_scheduler/internal/data/constants"
"ai_scheduler/internal/data/model" "ai_scheduler/internal/data/model"
"context" "context"
@ -31,7 +32,9 @@ type FirstSockRequest struct {
type ChatSockRequest struct { type ChatSockRequest struct {
Text string `json:"text" binding:"required"` Text string `json:"text" binding:"required"`
Img string `json:"img" binding:"required"` Img string `json:"img" binding:"required"`
File string `json:"file" binding:"required"`
Tags string `json:"tags" binding:"required"` Tags string `json:"tags" binding:"required"`
MarkHis int64 `json:"mark_his" `
Caller string `json:"caller" binding:"required"` Caller string `json:"caller" binding:"required"`
SessionID string `json:"session_id"` SessionID string `json:"session_id"`
} }
@ -130,7 +133,7 @@ type ChatHis struct {
Context HisContext `json:"context"` Context HisContext `json:"context"`
} }
type HisMessage struct { type HisMessage struct {
Role string `json:"role"` Role constants.Caller `json:"role"`
Content string `json:"content"` Content string `json:"content"`
Timestamp string `json:"timestamp"` Timestamp string `json:"timestamp"`
} }

View File

@ -50,7 +50,11 @@ func SetupRoutes(app *fiber.App, ChatService *services.ChatService, sessionServi
r.Post("/session/init", sessionService.SessionInit) // 会话初始化,不存在则创建,存在则返回会话ID和默认条数会话历史 r.Post("/session/init", sessionService.SessionInit) // 会话初始化,不存在则创建,存在则返回会话ID和默认条数会话历史
r.Post("/session/list", sessionService.SessionList) r.Post("/session/list", sessionService.SessionList)
r.Post("/sys/tasks", task.Tasks) r.Post("/sys/tasks", task.Tasks)
// 评价
r.Post("/chat/useful/list", ChatService.UsefulList)
r.Post("/chat/useful", ChatService.Useful)
// 回调 // 回调
r.Post("/callback", callbackService.Callback) r.Post("/callback", callbackService.Callback)
//广播 //广播

View File

@ -13,6 +13,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2" "github.com/gofiber/websocket/v2"
) )
@ -21,13 +22,19 @@ type ChatService struct {
routerBiz *biz.AiRouterBiz routerBiz *biz.AiRouterBiz
Gw *gateway.Gateway Gw *gateway.Gateway
mu sync.Mutex mu sync.Mutex
ChatHis *biz.ChatHistoryBiz
} }
// NewChatHandler 创建聊天处理器 // NewChatHandler 创建聊天处理器
func NewChatService(routerService *biz.AiRouterBiz, gw *gateway.Gateway) *ChatService { func NewChatService(
routerService *biz.AiRouterBiz,
chatHis *biz.ChatHistoryBiz,
gw *gateway.Gateway,
) *ChatService {
return &ChatService{ return &ChatService{
routerBiz: routerService, routerBiz: routerService,
Gw: gw, Gw: gw,
ChatHis: chatHis,
} }
} }
@ -118,10 +125,7 @@ func (h *ChatService) Chat(c *websocket.Conn) {
Type: entitys.ResponseText, Type: entitys.ResponseText,
}) })
} }
_ = entitys.MsgSend(c, entitys.Response{
Content: "",
Type: entitys.ResponseEnd,
})
} }
h.Gw.RemoveClient(clientID) h.Gw.RemoveClient(clientID)
_ = c.Close() _ = c.Close()
@ -148,3 +152,23 @@ func (h *ChatService) handleMessageToString(c *websocket.Conn, msgType int, msg
} }
return msg.([]byte), constants.ConnStatusIgnore return msg.([]byte), constants.ConnStatusIgnore
} }
func (s *ChatService) Useful(c *fiber.Ctx) error {
req := &entitys.UseFulRequest{}
if err := c.BodyParser(req); err != nil {
return err
}
err := s.ChatHis.Update(c.Context(), req)
if err != nil {
return err
}
return nil
}
func (s *ChatService) UsefulList(c *fiber.Ctx) error {
return c.JSON(constants.UseFulMap)
}

View File

@ -68,6 +68,14 @@ func (k DataTemp) Add(data interface{}) (id int, err error) {
return primary.Id, add.Error return primary.Id, add.Error
} }
func (k DataTemp) AddWithData(data interface{}) (interface{}, error) {
result := k.Db.Model(k.Model).Create(data)
if result.Error != nil {
return data, result.Error
}
return data, nil
}
func (k DataTemp) GetList(cond *builder.Cond, pageBoIn *ReqPageBo) (list []map[string]interface{}, pageBoOut *RespPageBo, err error) { func (k DataTemp) GetList(cond *builder.Cond, pageBoIn *ReqPageBo) (list []map[string]interface{}, pageBoOut *RespPageBo, err error) {
var ( var (
query, _ = builder.ToBoundSQL(*cond) query, _ = builder.ToBoundSQL(*cond)