abstract.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package llm
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/cache"
  5. "eta/eta_api/controllers"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/rag"
  8. "eta/eta_api/models/rag/request"
  9. "eta/eta_api/models/rag/response"
  10. "eta/eta_api/services"
  11. "eta/eta_api/services/elastic"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. )
  16. // AbstractController
  17. // @Description: 摘要管理
  18. type AbstractController struct {
  19. controllers.BaseAuthController
  20. }
  21. // List
  22. // @Title 列表
  23. // @Description 列表
  24. // @Param PageSize query int true "每页数据条数"
  25. // @Param CurrentIndex query int true "当前页页码,从1开始"
  26. // @Param KeyWord query string true "搜索关键词"
  27. // @Success 200 {object} []*rag.QuestionListListResp
  28. // @router /abstract/list [get]
  29. func (c *AbstractController) List() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. c.Data["json"] = br
  33. c.ServeJSON()
  34. }()
  35. sysUser := c.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录,SysUser Is Empty"
  39. return
  40. }
  41. pageSize, _ := c.GetInt("PageSize")
  42. currentIndex, _ := c.GetInt("CurrentIndex")
  43. keyWord := c.GetString("KeyWord")
  44. tagId, _ := c.GetInt("TagId")
  45. var startSize int
  46. if pageSize <= 0 {
  47. pageSize = utils.PageSize20
  48. }
  49. if currentIndex <= 0 {
  50. currentIndex = 1
  51. }
  52. startSize = utils.StartIndex(currentIndex, pageSize)
  53. var total int
  54. viewList := make([]rag.WechatArticleAbstractView, 0)
  55. if keyWord == `` {
  56. var condition string
  57. var pars []interface{}
  58. condition += fmt.Sprintf(` AND c.%s = ?`, rag.WechatPlatformColumns.Enabled)
  59. pars = append(pars, 1)
  60. if keyWord != "" {
  61. condition += fmt.Sprintf(` AND a.%s like ?`, rag.WechatArticleAbstractColumns.Content)
  62. pars = append(pars, `%`+keyWord+`%`)
  63. }
  64. if tagId > 0 {
  65. condition += fmt.Sprintf(` AND d.%s = ?`, rag.WechatPlatformTagMappingColumns.TagID)
  66. pars = append(pars, tagId)
  67. }
  68. obj := new(rag.WechatArticleAbstract)
  69. tmpTotal, list, err := obj.GetPageListByTagAndPlatformCondition(condition, pars, startSize, pageSize)
  70. if err != nil {
  71. br.Msg = "获取失败"
  72. br.ErrMsg = "获取失败,Err:" + err.Error()
  73. return
  74. }
  75. total = tmpTotal
  76. viewList = obj.WechatArticleAbstractItem(list)
  77. } else {
  78. sortMap := map[string]string{
  79. "ModifyTime": "desc",
  80. "WechatArticleAbstractId": "desc",
  81. }
  82. obj := new(rag.WechatPlatform)
  83. platformList, err := obj.GetListByCondition(` AND enabled = 1 `, []interface{}{}, 0, 100000)
  84. if err != nil {
  85. br.Msg = "获取失败"
  86. br.ErrMsg = "获取失败,Err:" + err.Error()
  87. return
  88. }
  89. platformIdList := make([]int, 0)
  90. for _, v := range platformList {
  91. platformIdList = append(platformIdList, v.WechatPlatformId)
  92. }
  93. tagList := make([]int, 0)
  94. if tagId > 0 {
  95. tagList = append(tagList, tagId)
  96. }
  97. tmpTotal, list, err := elastic.WechatArticleAbstractEsSearch(keyWord, tagList, platformIdList, startSize, pageSize, sortMap)
  98. if err != nil {
  99. br.Msg = "获取失败"
  100. br.ErrMsg = "获取失败,Err:" + err.Error()
  101. return
  102. }
  103. total = int(tmpTotal)
  104. if list != nil && len(list) > 0 {
  105. viewList = list[0].ToViewList(list)
  106. }
  107. }
  108. page := paging.GetPaging(currentIndex, pageSize, total)
  109. resp := response.AbstractListListResp{
  110. List: viewList,
  111. Paging: page,
  112. }
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "获取成功"
  116. br.Data = resp
  117. }
  118. // Del
  119. // @Title 删除摘要
  120. // @Description 删除摘要
  121. // @Param request body request.DelAbstractReq true "type json string"
  122. // @Success 200 Ret=200 新增成功
  123. // @router /abstract/del [post]
  124. func (c *AbstractController) Del() {
  125. br := new(models.BaseResponse).Init()
  126. defer func() {
  127. c.Data["json"] = br
  128. c.ServeJSON()
  129. }()
  130. var req request.DelAbstractReq
  131. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  132. if err != nil {
  133. br.Msg = "参数解析异常!"
  134. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  135. return
  136. }
  137. if len(req.WechatArticleAbstractIdList) <= 0 {
  138. br.Msg = "请选择摘要"
  139. br.IsSendEmail = false
  140. return
  141. }
  142. obj := rag.WechatArticleAbstract{}
  143. list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
  144. if err != nil {
  145. br.Msg = "修改失败"
  146. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  147. if utils.IsErrNoRow(err) {
  148. br.Msg = "问题不存在"
  149. br.IsSendEmail = false
  150. }
  151. return
  152. }
  153. if len(list) > 0 {
  154. // 删除向量库
  155. err = services.DelDoc(list)
  156. if err != nil {
  157. br.Msg = "删除失败"
  158. br.ErrMsg = "删除向量库失败,Err:" + err.Error()
  159. return
  160. }
  161. // 删除摘要
  162. err = obj.DelByIdList(req.WechatArticleAbstractIdList)
  163. if err != nil {
  164. br.Msg = "删除失败"
  165. br.ErrMsg = "删除失败,Err:" + err.Error()
  166. return
  167. }
  168. // 删除es数据
  169. for _, v := range list {
  170. go services.DelEsWechatArticleAbstract(v.WechatArticleAbstractId)
  171. }
  172. }
  173. br.Ret = 200
  174. br.Success = true
  175. br.Msg = `删除成功`
  176. }
  177. // VectorDel
  178. // @Title 删除摘要向量库
  179. // @Description 删除摘要向量库
  180. // @Param request body request.EditQuestionReq true "type json string"
  181. // @Success 200 Ret=200 新增成功
  182. // @router /abstract/vector/del [post]
  183. func (c *AbstractController) VectorDel() {
  184. br := new(models.BaseResponse).Init()
  185. defer func() {
  186. c.Data["json"] = br
  187. c.ServeJSON()
  188. }()
  189. var req request.DelAbstractReq
  190. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  191. if err != nil {
  192. br.Msg = "参数解析异常!"
  193. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  194. return
  195. }
  196. if len(req.WechatArticleAbstractIdList) <= 0 {
  197. br.Msg = "请选择摘要"
  198. br.IsSendEmail = false
  199. return
  200. }
  201. obj := rag.WechatArticleAbstract{}
  202. list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
  203. if err != nil {
  204. br.Msg = "修改失败"
  205. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  206. if utils.IsErrNoRow(err) {
  207. br.Msg = "问题不存在"
  208. br.IsSendEmail = false
  209. }
  210. return
  211. }
  212. // 删除向量库
  213. err = services.DelDoc(list)
  214. if err != nil {
  215. br.Msg = "删除失败"
  216. br.ErrMsg = "删除失败,Err:" + err.Error()
  217. return
  218. }
  219. // 修改ES数据
  220. for _, v := range list {
  221. go services.AddOrEditEsWechatArticleAbstract(v.WechatArticleAbstractId)
  222. }
  223. br.Ret = 200
  224. br.Success = true
  225. br.Msg = `删除成功`
  226. }
  227. // AddVector
  228. // @Title 删除摘要向量库
  229. // @Description 删除摘要向量库
  230. // @Param request body request.EditQuestionReq true "type json string"
  231. // @Success 200 Ret=200 新增成功
  232. // @router /abstract/vector/add [post]
  233. func (c *AbstractController) AddVector() {
  234. br := new(models.BaseResponse).Init()
  235. defer func() {
  236. c.Data["json"] = br
  237. c.ServeJSON()
  238. }()
  239. var req request.DelAbstractReq
  240. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  241. if err != nil {
  242. br.Msg = "参数解析异常!"
  243. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  244. return
  245. }
  246. if len(req.WechatArticleAbstractIdList) <= 0 {
  247. br.Msg = "请选择摘要"
  248. br.IsSendEmail = false
  249. return
  250. }
  251. obj := rag.WechatArticleAbstract{}
  252. list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
  253. if err != nil {
  254. br.Msg = "修改失败"
  255. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  256. if utils.IsErrNoRow(err) {
  257. br.Msg = "问题不存在"
  258. br.IsSendEmail = false
  259. }
  260. return
  261. }
  262. for _, item := range list {
  263. cache.AddWechatArticleLlmOpToCache(item.WechatArticleId, ``)
  264. }
  265. br.Ret = 200
  266. br.Success = true
  267. br.Msg = `添加向量库中,请稍后查看`
  268. }