Roc 3 дней назад
Родитель
Сommit
e05c5e9ba2

+ 1 - 89
controllers/llm/abstract.go

@@ -206,102 +206,14 @@ func (c *AbstractController) Del() {
 		return
 	}
 
-	vectorKeyList := make([]string, 0)
-	wechatArticleAbstractIdList := make([]int, 0)
-
-	obj := rag.WechatArticleAbstract{}
-
-	list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
-	if err != nil {
-		br.Msg = "修改失败"
-		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
-		if utils.IsErrNoRow(err) {
-			br.Msg = "问题不存在"
-			br.IsSendEmail = false
-		}
-		return
-	}
-	if len(list) > 0 {
-		for _, v := range list {
-			// 有加入到向量库,那么就加入到待删除的向量库list中
-			if v.VectorKey != `` {
-				vectorKeyList = append(vectorKeyList, v.VectorKey)
-			}
-			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.WechatArticleAbstractId)
-		}
-	}
-
-	//if !req.IsSelectAll {
-	//	list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
-	//	if err != nil {
-	//		br.Msg = "修改失败"
-	//		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
-	//		if utils.IsErrNoRow(err) {
-	//			br.Msg = "问题不存在"
-	//			br.IsSendEmail = false
-	//		}
-	//		return
-	//	}
-	//	if len(list) > 0 {
-	//		for _, v := range list {
-	//			// 有加入到向量库,那么就加入到待删除的向量库list中
-	//			if v.VectorKey != `` {
-	//				vectorKeyList = append(vectorKeyList, v.VectorKey)
-	//			}
-	//			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.WechatArticleAbstractId)
-	//		}
-	//	}
-	//} else {
-	//	notIdMap := make(map[int]bool)
-	//	for _, v := range req.NotWechatArticleAbstractIdList {
-	//		notIdMap[v] = true
-	//	}
-	//
-	//	_, list, err := getAbstractList(req.KeyWord, req.TagId, 0, 100000)
-	//	if err != nil {
-	//		br.Msg = "修改失败"
-	//		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
-	//		if utils.IsErrNoRow(err) {
-	//			br.Msg = "问题不存在"
-	//			br.IsSendEmail = false
-	//		}
-	//		return
-	//	}
-	//	if len(list) > 0 {
-	//		for _, v := range list {
-	//			if notIdMap[v.WechatArticleAbstractId] {
-	//				continue
-	//			}
-	//			// 有加入到向量库,那么就加入到待删除的向量库list中
-	//			if v.VectorKey != `` {
-	//				vectorKeyList = append(vectorKeyList, v.VectorKey)
-	//			}
-	//			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.WechatArticleAbstractId)
-	//		}
-	//	}
-	//}
-
-	// 删除向量库
-	err = services.DelLlmDoc(vectorKeyList, wechatArticleAbstractIdList)
-	if err != nil {
-		br.Msg = "删除失败"
-		br.ErrMsg = "删除向量库失败,Err:" + err.Error()
-		return
-	}
-
 	// 删除摘要
-	err = obj.DelByIdList(wechatArticleAbstractIdList)
+	err = services.DelWechatArticleAbstract(req.WechatArticleAbstractIdList)
 	if err != nil {
 		br.Msg = "删除失败"
 		br.ErrMsg = "删除失败,Err:" + err.Error()
 		return
 	}
 
-	// 删除es数据
-	for _, wechatArticleAbstractId := range wechatArticleAbstractIdList {
-		go services.DelEsWechatArticleAbstract(wechatArticleAbstractId)
-	}
-
 	br.Ret = 200
 	br.Success = true
 	br.Msg = `删除成功`

+ 17 - 8
controllers/llm/question.go

@@ -492,6 +492,16 @@ func (c *QuestionController) SetDefault() {
 		return
 	}
 
+	// 判断是否正在取消默认提示词(删除历史摘要)
+	{
+		cacheKey := services.GetDelAbstractByQuestionIdCacheKey(item.QuestionId)
+		if utils.Rc.IsExist(cacheKey) {
+			br.Msg = "取消设置默认提示词后,删除历史摘要中,请稍后再试!"
+			br.IsSendEmail = false
+			return
+		}
+	}
+
 	if item.IsDefault == 1 {
 		br.Msg = "该提示词已经是默认提示词,无需设置"
 		br.IsSendEmail = false
@@ -551,11 +561,11 @@ func (c *QuestionController) UnSetDefault() {
 		return
 	}
 
-	if item.IsDefault == 0 {
-		br.Msg = "该提示词不是默认提示词,无需取消"
-		br.IsSendEmail = false
-		return
-	}
+	//if item.IsDefault == 0 {
+	//	br.Msg = "该提示词不是默认提示词,无需取消"
+	//	br.IsSendEmail = false
+	//	return
+	//}
 
 	// 如果是取消已经设置成默认的提示词,那么需要判断是否有正在生成摘要的提示词任务,如果存在的话,那么就不允许取消
 	auth, err := services.CheckOpQuestionAuth()
@@ -581,7 +591,8 @@ func (c *QuestionController) UnSetDefault() {
 	// 新增/编辑ES数据
 	go services.AddOrEditEsRagQuestion(item.QuestionId)
 
-	// todo 对应的提示词生成的摘要库和向量库内容也取消,同时需要加锁,不允许重复操作
+	// 对应的提示词生成的摘要库和向量库内容也取消,同时需要加锁,不允许重复操作
+	go services.DelAbstractByQuestionId(item.QuestionId)
 
 	br.Ret = 200
 	br.Success = true
@@ -663,8 +674,6 @@ func (c *QuestionController) GenerateAbstract() {
 	// 添加任务
 	services.AddGenerateAbstractTask(item, c.SysUser)
 
-	// todo 开始任务
-
 	br.Ret = 200
 	br.Success = true
 	br.Msg = `摘要生成中`

+ 10 - 0
models/rag/ai_task_record.go

@@ -74,6 +74,16 @@ func (m *AiTaskRecord) GetByCondition(condition string, pars []interface{}) (ite
 	return
 }
 
+func (m *AiTaskRecord) GetAllListByCondition(field, condition string, pars []interface{}) (items []*AiTaskRecord, err error) {
+	if field == "" {
+		field = "*"
+	}
+	sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s order by AiTaskRecord_id desc `, field, m.TableName(), condition)
+	err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
+
+	return
+}
+
 func (m *AiTaskRecord) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*AiTaskRecord, err error) {
 	if field == "" {
 		field = "*"

+ 6 - 0
models/rag/rag_eta_report_abstract.go

@@ -85,6 +85,12 @@ func (m *RagEtaReportAbstract) GetByIdList(idList []int) (items []*RagEtaReportA
 	return
 }
 
+func (m *RagEtaReportAbstract) GetListByQuestionId(questionId int) (items []*RagEtaReportAbstract, err error) {
+	err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ? ", RagEtaReportAbstractColumns.QuestionId), questionId).Find(&items).Error
+
+	return
+}
+
 func (m *RagEtaReportAbstract) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*RagEtaReportAbstract, err error) {
 	if field == "" {
 		field = "*"

+ 6 - 0
models/rag/wechat_article_abstract.go

@@ -81,6 +81,12 @@ func (m *WechatArticleAbstract) GetByIdList(idList []int) (items []*WechatArticl
 	return
 }
 
+func (m *WechatArticleAbstract) GetListByQuestionId(questionId int) (items []*WechatArticleAbstract, err error) {
+	err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ? ", WechatArticleAbstractColumns.QuestionId), questionId).Find(&items).Error
+
+	return
+}
+
 func (m *WechatArticleAbstract) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*WechatArticleAbstract, err error) {
 	if field == "" {
 		field = "*"

+ 21 - 0
services/llm.go

@@ -2,6 +2,7 @@ package services
 
 import (
 	"encoding/json"
+	"eta/eta_api/cache"
 	"eta/eta_api/models/rag"
 	"eta/eta_api/models/system"
 	"eta/eta_api/utils"
@@ -104,9 +105,29 @@ func AddGenerateAbstractTask(question *rag.Question, sysUser *system.Admin) {
 		return
 	}
 
+	// 添加到缓存队列中
+	go addTaskToCache(aiTask.AiTaskID)
+
 	return
 }
 
+func addTaskToCache(aiTaskId int) {
+	var err error
+	defer func() {
+		if err != nil {
+			utils.FileLog.Error("addTaskToCache error: %v", err)
+		}
+	}()
+	obj := rag.AiTaskRecord{}
+	list, err := obj.GetAllListByCondition("*", ` ai_task_id = ? `, []interface{}{aiTaskId})
+	if err != nil {
+		return
+	}
+	for _, item := range list {
+		cache.AddAiTaskRecordOpToCache(item.AiTaskRecordID)
+	}
+}
+
 // getAllWechatArticleIdList
 // @Description: 获取所有的微信文章Id列表
 // @author: Roc

+ 85 - 54
services/llm_report.go

@@ -663,9 +663,6 @@ func DelRagReportLlmDoc(vectorKeyList []string, abstractIdList []int) (err error
 // @param abstractIdList []int
 // @return err error
 func DelRagEtaReportAbstract(abstractIdList []int) (err error) {
-	vectorKeyList := make([]string, 0)
-	newAbstractIdList := make([]int, 0)
-
 	obj := rag.RagEtaReportAbstract{}
 
 	list, err := obj.GetByIdList(abstractIdList)
@@ -677,6 +674,48 @@ func DelRagEtaReportAbstract(abstractIdList []int) (err error) {
 		}
 		return
 	}
+
+	err = delRagEtaReportAbstract(list)
+
+	return
+}
+
+// DelRagEtaReportAbstractByQuestionId
+// @Description: 根据提示词ID删除ETA报告摘要
+// @author: Roc
+// @datetime 2025-04-23 17:36:22
+// @param abstractIdList []int
+// @return err error
+func DelRagEtaReportAbstractByQuestionId(questionId int) (err error) {
+	obj := rag.RagEtaReportAbstract{}
+
+	list, err := obj.GetListByQuestionId(questionId)
+	if err != nil {
+		if !utils.IsErrNoRow(err) {
+			err = errors.New("删除向量库失败,Err:" + err.Error())
+		} else {
+			err = nil
+		}
+		return
+	}
+
+	err = delRagEtaReportAbstract(list)
+
+	return
+}
+
+// delRagEtaReportAbstract
+// @Description: 删除摘要
+// @author: Roc
+// @datetime 2025-04-24 15:19:19
+// @param list []*rag.RagEtaReportAbstract
+// @return err error
+func delRagEtaReportAbstract(list []*rag.RagEtaReportAbstract) (err error) {
+	obj := rag.RagEtaReportAbstract{}
+
+	vectorKeyList := make([]string, 0)
+	newAbstractIdList := make([]int, 0)
+
 	if len(list) > 0 {
 		for _, v := range list {
 			// 有加入到向量库,那么就加入到待删除的向量库list中
@@ -686,57 +725,6 @@ func DelRagEtaReportAbstract(abstractIdList []int) (err error) {
 			newAbstractIdList = append(newAbstractIdList, v.RagEtaReportAbstractId)
 		}
 	}
-
-	//if !req.IsSelectAll {
-	//	list, err := obj.GetByIdList(req.RagEtaReportAbstractIdList)
-	//	if err != nil {
-	//		br.Msg = "修改失败"
-	//		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
-	//		if utils.IsErrNoRow(err) {
-	//			br.Msg = "问题不存在"
-	//			br.IsSendEmail = false
-	//		}
-	//		return
-	//	}
-	//	if len(list) > 0 {
-	//		for _, v := range list {
-	//			// 有加入到向量库,那么就加入到待删除的向量库list中
-	//			if v.VectorKey != `` {
-	//				vectorKeyList = append(vectorKeyList, v.VectorKey)
-	//			}
-	//			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.RagEtaReportAbstractId)
-	//		}
-	//	}
-	//} else {
-	//	notIdMap := make(map[int]bool)
-	//	for _, v := range req.NotRagEtaReportAbstractIdList {
-	//		notIdMap[v] = true
-	//	}
-	//
-	//	_, list, err := getRagEtaReportAbstractList(req.KeyWord, req.TagId, 0, 100000)
-	//	if err != nil {
-	//		br.Msg = "修改失败"
-	//		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
-	//		if utils.IsErrNoRow(err) {
-	//			br.Msg = "问题不存在"
-	//			br.IsSendEmail = false
-	//		}
-	//		return
-	//	}
-	//	if len(list) > 0 {
-	//		for _, v := range list {
-	//			if notIdMap[v.RagEtaReportAbstractId] {
-	//				continue
-	//			}
-	//			// 有加入到向量库,那么就加入到待删除的向量库list中
-	//			if v.VectorKey != `` {
-	//				vectorKeyList = append(vectorKeyList, v.VectorKey)
-	//			}
-	//			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.RagEtaReportAbstractId)
-	//		}
-	//	}
-	//}
-
 	// 删除向量库
 	err = DelRagReportLlmDoc(vectorKeyList, newAbstractIdList)
 	if err != nil {
@@ -758,3 +746,46 @@ func DelRagEtaReportAbstract(abstractIdList []int) (err error) {
 
 	return
 }
+
+// GetDelAbstractByQuestionIdCacheKey
+// @Description: 获取删除微信文章/ETA报告摘要的缓存key
+// @author: Roc
+// @datetime 2025-04-24 15:44:41
+// @param questionId int
+// @return string
+func GetDelAbstractByQuestionIdCacheKey(questionId int) string {
+	return fmt.Sprintf("%s%d", utils.CACHE_AI_ARTICLE_ABSTRACT_DEL, questionId)
+}
+
+// DelAbstractByQuestionId
+// @Description: 根据提示词ID删除微信文章/报告摘要
+// @author: Roc
+// @datetime 2025-04-24 15:37:28
+// @param questionId int
+func DelAbstractByQuestionId(questionId int) {
+	cacheKey := GetDelAbstractByQuestionIdCacheKey(questionId)
+	if !utils.Rc.SetNX(cacheKey, 1, 30*time.Minute) {
+		utils.FileLog.Error("根据提示词删除摘要失败,提示词ID:%d,系统处理中,请稍后重试!", questionId)
+		return
+	}
+
+	defer func() {
+		utils.Rc.Delete(cacheKey)
+	}()
+
+	// 删除微信文章摘要
+	err := DelWechatArticleAbstractByQuestionId(questionId)
+	if err != nil {
+		utils.FileLog.Error("根据提示词摘要删除微信文章摘要失败,提示词ID:%d;原因:%s", questionId, err.Error())
+	}
+
+	// 删除ETA报告摘要
+	err = DelRagEtaReportAbstractByQuestionId(questionId)
+	if err != nil {
+		utils.FileLog.Error("根据提示词删除ETA报告摘要失败,提示词ID:%d;原因:%s", questionId, err.Error())
+	}
+
+	//time.Sleep(30 * time.Second)
+
+	return
+}

+ 0 - 3
services/task.go

@@ -920,6 +920,3 @@ func handleAiArticleAbstractLlmOp(b []byte) {
 
 	}
 }
-func init() {
-	cache.AddAiTaskRecordOpToCache(12208)
-}

+ 143 - 0
services/wechat_platform.go

@@ -3,6 +3,7 @@ package services
 import (
 	"bytes"
 	"encoding/json"
+	"errors"
 	"eta/eta_api/cache"
 	"eta/eta_api/models"
 	"eta/eta_api/models/rag"
@@ -1130,6 +1131,148 @@ func AddOrEditEsWechatArticleAbstract(articleAbstractId int) {
 	err = elastic.WechatArticleAbstractEsAddOrEdit(strconv.Itoa(articleAbstractId), esItem)
 }
 
+// DelWechatArticleAbstract
+// @Description: 删除微信文章摘要
+// @author: Roc
+// @datetime 2025-04-23 17:36:22
+// @param abstractIdList []int
+// @return err error
+func DelWechatArticleAbstract(abstractIdList []int) (err error) {
+	obj := rag.WechatArticleAbstract{}
+
+	list, err := obj.GetByIdList(abstractIdList)
+	if err != nil {
+		if !utils.IsErrNoRow(err) {
+			err = errors.New("删除向量库失败,Err:" + err.Error())
+		} else {
+			err = nil
+		}
+		return
+	}
+
+	err = delWechatArticleAbstract(list)
+
+	return
+}
+
+// DelWechatArticleAbstract
+// @Description: 删除微信文章摘要
+// @author: Roc
+// @datetime 2025-04-23 17:36:22
+// @param abstractIdList []int
+// @return err error
+func DelWechatArticleAbstractByQuestionId(questionId int) (err error) {
+	obj := rag.WechatArticleAbstract{}
+
+	list, err := obj.GetListByQuestionId(questionId)
+	if err != nil {
+		if !utils.IsErrNoRow(err) {
+			err = errors.New("删除向量库失败,Err:" + err.Error())
+		} else {
+			err = nil
+		}
+		return
+	}
+
+	err = delWechatArticleAbstract(list)
+
+	return
+}
+
+// delRagEtaReportAbstract
+// @Description: 删除摘要
+// @author: Roc
+// @datetime 2025-04-24 15:19:19
+// @param list []*rag.RagEtaReportAbstract
+// @return err error
+func delWechatArticleAbstract(list []*rag.WechatArticleAbstract) (err error) {
+	obj := rag.RagEtaReportAbstract{}
+
+	vectorKeyList := make([]string, 0)
+	newAbstractIdList := make([]int, 0)
+
+	if len(list) > 0 {
+		for _, v := range list {
+			// 有加入到向量库,那么就加入到待删除的向量库list中
+			if v.VectorKey != `` {
+				vectorKeyList = append(vectorKeyList, v.VectorKey)
+			}
+			newAbstractIdList = append(newAbstractIdList, v.WechatArticleAbstractId)
+		}
+	}
+
+	//if !req.IsSelectAll {
+	//	list, err := obj.GetByIdList(req.RagEtaReportAbstractIdList)
+	//	if err != nil {
+	//		br.Msg = "修改失败"
+	//		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
+	//		if utils.IsErrNoRow(err) {
+	//			br.Msg = "问题不存在"
+	//			br.IsSendEmail = false
+	//		}
+	//		return
+	//	}
+	//	if len(list) > 0 {
+	//		for _, v := range list {
+	//			// 有加入到向量库,那么就加入到待删除的向量库list中
+	//			if v.VectorKey != `` {
+	//				vectorKeyList = append(vectorKeyList, v.VectorKey)
+	//			}
+	//			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.RagEtaReportAbstractId)
+	//		}
+	//	}
+	//} else {
+	//	notIdMap := make(map[int]bool)
+	//	for _, v := range req.NotRagEtaReportAbstractIdList {
+	//		notIdMap[v] = true
+	//	}
+	//
+	//	_, list, err := getRagEtaReportAbstractList(req.KeyWord, req.TagId, 0, 100000)
+	//	if err != nil {
+	//		br.Msg = "修改失败"
+	//		br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
+	//		if utils.IsErrNoRow(err) {
+	//			br.Msg = "问题不存在"
+	//			br.IsSendEmail = false
+	//		}
+	//		return
+	//	}
+	//	if len(list) > 0 {
+	//		for _, v := range list {
+	//			if notIdMap[v.RagEtaReportAbstractId] {
+	//				continue
+	//			}
+	//			// 有加入到向量库,那么就加入到待删除的向量库list中
+	//			if v.VectorKey != `` {
+	//				vectorKeyList = append(vectorKeyList, v.VectorKey)
+	//			}
+	//			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.RagEtaReportAbstractId)
+	//		}
+	//	}
+	//}
+
+	// 删除向量库
+	err = DelLlmDoc(vectorKeyList, newAbstractIdList)
+	if err != nil {
+		err = errors.New("删除向量库失败,Err:" + err.Error())
+		return
+	}
+
+	// 删除摘要
+	err = obj.DelByIdList(newAbstractIdList)
+	if err != nil {
+		err = errors.New("删除失败,Err:" + err.Error())
+		return
+	}
+
+	// 删除es数据
+	for _, wechatArticleAbstractId := range newAbstractIdList {
+		go DelEsWechatArticleAbstract(wechatArticleAbstractId)
+	}
+
+	return
+}
+
 // DelEsWechatArticleAbstract
 // @Description: 删除ES中的微信文章摘要
 // @author: Roc

+ 1 - 0
utils/constants.go

@@ -270,6 +270,7 @@ const (
 	CACHE_ETA_REPORT_KNOWLEDGE              = "eta:report:knowledge:op:"              //eta报告入AI库处理
 	CACHE_ETA_REPORT_KNOWLEDGE_LLM          = "eta:report:knowledge:llm:op:"          //eta报告入知识库处理
 	CACHE_AI_ARTICLE_ABSTRACT_LLM_TASK      = "eta:ai:article:abstract:llm:task:op:"  //微信文章/eta报告的摘要重新生成处理(任务调度)
+	CACHE_AI_ARTICLE_ABSTRACT_DEL           = "eta:ai:article:abstract:del:op:"       //微信文章/eta报告的摘要删删除缓存,避免有人在删除的过程中,又将该提示词做摘要生成
 	CACHE_CHART_AUTH                        = "eta:chart:auth:"                       //图表数据授权
 	CACHE_REPORT_SHARE_AUTH                 = "eta:report:auth:share:"                //报告短链与报告图表授权映射key
 	CACHE_REPORT_AUTH                       = "eta:report:auth:"                      //报告图表数据授权