Ver código fonte

fix:摘要

Roc 5 dias atrás
pai
commit
2913dda535
5 arquivos alterados com 197 adições e 101 exclusões
  1. 32 2
      cache/llm.go
  2. 5 93
      controllers/llm/rag_eta_report_abstract.go
  3. 110 0
      services/llm_report.go
  4. 48 5
      services/task.go
  5. 2 1
      utils/constants.go

+ 32 - 2
cache/llm.go

@@ -61,7 +61,7 @@ func AddWechatArticleLlmOpToCache(wechatPlatformId int, source string) bool {
 	return false
 }
 
-type RagEtaReportOpOp struct {
+type RagEtaReportOp struct {
 	Source          string
 	ReportId        int
 	ReportChapterId int
@@ -76,7 +76,7 @@ type RagEtaReportOpOp struct {
 // @param source string
 // @return bool
 func RagEtaReportOpToCache(reportId, reportChapterId int, source string) bool {
-	record := new(RagEtaReportOpOp)
+	record := new(RagEtaReportOp)
 	record.Source = source
 	record.ReportId = reportId
 	record.ReportChapterId = reportChapterId
@@ -91,3 +91,33 @@ func RagEtaReportOpToCache(reportId, reportChapterId int, source string) bool {
 	}
 	return false
 }
+
+// RagEtaReportLlmOp
+// @Description:
+type RagEtaReportLlmOp struct {
+	RagEtaReportId int
+}
+
+// AddRagEtaReportLlmOpToCache
+// @Description: 将ETA报告llm操作加入缓存
+// @param wechatPlatformId
+// @param source
+// @return bool
+func AddRagEtaReportLlmOpToCache(ragEtaReportId int) bool {
+	// 如果不在发布和调试模式,那么就不加入缓存
+	if !utils.InArrayByStr([]string{utils.BusinessCodeRelease, utils.BusinessCodeDebug}, utils.BusinessCode) {
+		return true
+	}
+	record := new(RagEtaReportLlmOp)
+	record.RagEtaReportId = ragEtaReportId
+	if utils.Re == nil {
+		err := utils.Rc.LPush(utils.CACHE_ETA_REPORT_KNOWLEDGE_LLM, record)
+
+		utils.FileLog.Info(fmt.Sprintf("将eta报告llm操作加入缓存 加入缓存 RagEtaReportLlmOpToCache LPush: ETA报告id:%d", ragEtaReportId))
+		if err != nil {
+			fmt.Println("RagEtaReportLlmOpToCache LPush Err:" + err.Error())
+		}
+		return true
+	}
+	return false
+}

+ 5 - 93
controllers/llm/rag_eta_report_abstract.go

@@ -196,102 +196,14 @@ func (c *RagEtaReportAbstractController) Del() {
 		return
 	}
 
-	vectorKeyList := make([]string, 0)
-	wechatArticleAbstractIdList := make([]int, 0)
-
-	obj := rag.RagEtaReportAbstract{}
-
-	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)
-		}
-	}
-
-	//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 = services.DelRagReportLlmDoc(vectorKeyList, wechatArticleAbstractIdList)
-	if err != nil {
-		br.Msg = "删除失败"
-		br.ErrMsg = "删除向量库失败,Err:" + err.Error()
-		return
-	}
-
 	// 删除摘要
-	err = obj.DelByIdList(wechatArticleAbstractIdList)
+	err = services.DelRagEtaReportAbstract(req.RagEtaReportAbstractIdList)
 	if err != nil {
 		br.Msg = "删除失败"
 		br.ErrMsg = "删除失败,Err:" + err.Error()
 		return
 	}
 
-	// 删除es数据
-	for _, wechatArticleAbstractId := range wechatArticleAbstractIdList {
-		go services.DelEsRagEtaReportAbstract(wechatArticleAbstractId)
-	}
-
 	br.Ret = 200
 	br.Success = true
 	br.Msg = `删除成功`
@@ -439,7 +351,7 @@ func (c *RagEtaReportAbstractController) AddVector() {
 		return
 	}
 
-	wechatArticleAbstractIdList := make([]int, 0)
+	ragEtaReportAbstractIdList := make([]int, 0)
 
 	obj := rag.RagEtaReportAbstract{}
 	list, err := obj.GetByIdList(req.RagEtaReportAbstractIdList)
@@ -454,7 +366,7 @@ func (c *RagEtaReportAbstractController) AddVector() {
 	}
 	if len(list) > 0 {
 		for _, v := range list {
-			wechatArticleAbstractIdList = append(wechatArticleAbstractIdList, v.RagEtaReportAbstractId)
+			ragEtaReportAbstractIdList = append(ragEtaReportAbstractIdList, v.RagEtaReportAbstractId)
 		}
 	}
 
@@ -500,8 +412,8 @@ func (c *RagEtaReportAbstractController) AddVector() {
 	//	}
 	//}
 
-	for _, wechatArticleAbstractId := range wechatArticleAbstractIdList {
-		cache.AddWechatArticleLlmOpToCache(wechatArticleAbstractId, ``)
+	for _, ragEtaReportAbstractId := range ragEtaReportAbstractIdList {
+		cache.AddRagEtaReportLlmOpToCache(ragEtaReportAbstractId)
 	}
 
 	br.Ret = 200

+ 110 - 0
services/llm_report.go

@@ -2,6 +2,8 @@ package services
 
 import (
 	"encoding/json"
+	"errors"
+	"eta/eta_api/cache"
 	"eta/eta_api/models"
 	"eta/eta_api/models/rag"
 	"eta/eta_api/services/elastic"
@@ -184,6 +186,8 @@ func handleReportAddOrModifyKnowledge(reportId, reportChapterId int, title, auth
 		err = item.Create()
 	}
 
+	cache.AddRagEtaReportLlmOpToCache(item.RagEtaReportId)
+
 	return
 }
 
@@ -249,6 +253,9 @@ func ReportUnPublishedKnowledgeByReportId(reportId int) {
 			errList = append(errList, fmt.Sprintf("第%d章:%s,异常:\n%s", item.ReportChapterId, item.Title, err.Error()))
 			continue
 		}
+
+		// 删除摘要
+		err = DelRagEtaReportAbstract([]int{item.RagEtaReportId})
 	}
 
 	return
@@ -639,3 +646,106 @@ func DelRagReportLlmDoc(vectorKeyList []string, abstractIdList []int) (err error
 
 	return
 }
+
+// DelRagEtaReportAbstract
+// @Description: 删除ETA报告摘要
+// @author: Roc
+// @datetime 2025-04-23 17:36:22
+// @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)
+	if err != nil {
+		if !utils.IsErrNoRow(err) {
+			err = errors.New("删除向量库失败,Err:" + err.Error())
+		} else {
+			err = nil
+		}
+		return
+	}
+	if len(list) > 0 {
+		for _, v := range list {
+			// 有加入到向量库,那么就加入到待删除的向量库list中
+			if v.VectorKey != `` {
+				vectorKeyList = append(vectorKeyList, v.VectorKey)
+			}
+			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 {
+		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 DelEsRagEtaReportAbstract(wechatArticleAbstractId)
+	}
+
+	return
+}

+ 48 - 5
services/task.go

@@ -75,6 +75,9 @@ func Task() {
 	go HandleWechatArticleLLmOp()
 
 	// 队列任务将eta报告同步到知识库操作
+	go HandleEtaReportUpdateOp()
+
+	// 定时任务进行eta报告进行LLM操作
 	go HandleEtaReportKnowledgeLLmOp()
 
 	// 权益报告监听入库
@@ -589,8 +592,8 @@ func ModifyEsEnglishReport() {
 //	return rnd.Float64()*11000 - 1000
 //}
 
-// HandleSearchByWechatOp
-// @Description: 处理微信爬虫
+// HandleWechatArticleOp
+// @Description: 处理ETA报告加入到知识库
 func HandleWechatArticleOp() {
 	defer func() {
 		if err := recover(); err != nil {
@@ -652,9 +655,9 @@ func HandleWechatArticleLLmOp() {
 	}
 }
 
-// HandleEtaReportKnowledgeLLmOp
+// HandleEtaReportUpdateOp
 // @Description: 处理eta报告加入知识库操作
-func HandleEtaReportKnowledgeLLmOp() {
+func HandleEtaReportUpdateOp() {
 	defer func() {
 		if err := recover(); err != nil {
 			fmt.Println("[HandleEtaReportKnowledgeLLmOp]", err)
@@ -662,7 +665,7 @@ func HandleEtaReportKnowledgeLLmOp() {
 	}()
 	for {
 		utils.Rc.Brpop(utils.CACHE_ETA_REPORT_KNOWLEDGE, func(b []byte) {
-			ragEtaReportOpOp := new(cache.RagEtaReportOpOp)
+			ragEtaReportOpOp := new(cache.RagEtaReportOp)
 			if err := json.Unmarshal(b, &ragEtaReportOpOp); err != nil {
 				fmt.Println("json unmarshal wrong!")
 				return
@@ -677,3 +680,43 @@ func HandleEtaReportKnowledgeLLmOp() {
 		})
 	}
 }
+
+// HandleEtaReportKnowledgeLLmOp
+// @Description: 处理微信文章加入知识库
+func HandleEtaReportKnowledgeLLmOp() {
+	defer func() {
+		if err := recover(); err != nil {
+			fmt.Println("[HandleWechatArticleLLmOp]", err)
+		}
+	}()
+	obj := rag.RagEtaReport{}
+	for {
+		utils.Rc.Brpop(utils.CACHE_ETA_REPORT_KNOWLEDGE_LLM, func(b []byte) {
+			wechatArticleOp := new(cache.RagEtaReportLlmOp)
+			if err := json.Unmarshal(b, &wechatArticleOp); err != nil {
+				fmt.Println("json unmarshal wrong!")
+				return
+			}
+			item, tmpErr := obj.GetById(wechatArticleOp.RagEtaReportId)
+			if tmpErr != nil {
+				// 找不到就处理失败
+				return
+			}
+
+			// 已经删除的就不做操作了
+			if item.IsDeleted == 1 {
+				return
+			}
+
+			// 未发布的就不操作了
+			if item.IsPublished != 1 {
+				return
+			}
+
+			// 文章加入到知识库
+			//ArticleToKnowledge(item)
+			// 生成摘要
+			GenerateRagEtaReportAbstract(item, true)
+		})
+	}
+}

+ 2 - 1
utils/constants.go

@@ -267,7 +267,8 @@ const (
 	CACHE_EXCEL_REFRESH                     = "CACHE_EXCEL_REFRESH"                   // 表格刷新
 	CACHE_WECHAT_PLATFORM_ARTICLE           = "wechat_platform:article:op:"           //微信文章处理
 	CACHE_WECHAT_PLATFORM_ARTICLE_KNOWLEDGE = "wechat_platform:article:knowledge:op:" //微信文章入知识库处理
-	CACHE_ETA_REPORT_KNOWLEDGE              = "eta:report:knowledge:op:"              //eta报告入知识库处理
+	CACHE_ETA_REPORT_KNOWLEDGE              = "eta:report:knowledge:op:"              //eta报告入AI库处理
+	CACHE_ETA_REPORT_KNOWLEDGE_LLM          = "eta:report:knowledge:llm:op:"          //eta报告入知识库处理
 	CACHE_CHART_AUTH                        = "eta:chart:auth:"                       //图表数据授权
 	CACHE_REPORT_SHARE_AUTH                 = "eta:report:auth:share:"                //报告短链与报告图表授权映射key
 	CACHE_REPORT_AUTH                       = "eta:report:auth:"                      //报告图表数据授权