abstract.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. tmpTotal, list, err := elastic.WechatArticleAbstractEsSearch(keyWord, tagId, startSize, pageSize, sortMap)
  83. if err != nil {
  84. br.Msg = "获取失败"
  85. br.ErrMsg = "获取失败,Err:" + err.Error()
  86. return
  87. }
  88. total = int(tmpTotal)
  89. if list != nil && len(list) > 0 {
  90. viewList = list[0].ToViewList(list)
  91. }
  92. }
  93. page := paging.GetPaging(currentIndex, pageSize, total)
  94. resp := response.AbstractListListResp{
  95. List: viewList,
  96. Paging: page,
  97. }
  98. br.Ret = 200
  99. br.Success = true
  100. br.Msg = "获取成功"
  101. br.Data = resp
  102. }
  103. // Del
  104. // @Title 删除摘要
  105. // @Description 删除摘要
  106. // @Param request body request.DelAbstractReq true "type json string"
  107. // @Success 200 Ret=200 新增成功
  108. // @router /abstract/del [post]
  109. func (c *AbstractController) Del() {
  110. br := new(models.BaseResponse).Init()
  111. defer func() {
  112. c.Data["json"] = br
  113. c.ServeJSON()
  114. }()
  115. var req request.DelAbstractReq
  116. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  117. if err != nil {
  118. br.Msg = "参数解析异常!"
  119. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  120. return
  121. }
  122. if len(req.WechatArticleAbstractIdList) <= 0 {
  123. br.Msg = "请选择摘要"
  124. br.IsSendEmail = false
  125. return
  126. }
  127. obj := rag.WechatArticleAbstract{}
  128. list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
  129. if err != nil {
  130. br.Msg = "修改失败"
  131. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  132. if utils.IsErrNoRow(err) {
  133. br.Msg = "问题不存在"
  134. br.IsSendEmail = false
  135. }
  136. return
  137. }
  138. if len(list) > 0 {
  139. // 删除向量库
  140. err = services.DelDoc(list)
  141. if err != nil {
  142. br.Msg = "删除失败"
  143. br.ErrMsg = "删除向量库失败,Err:" + err.Error()
  144. return
  145. }
  146. // 删除摘要
  147. err = obj.DelByIdList(req.WechatArticleAbstractIdList)
  148. if err != nil {
  149. br.Msg = "删除失败"
  150. br.ErrMsg = "删除失败,Err:" + err.Error()
  151. return
  152. }
  153. for _, v := range list {
  154. go services.AddOrEditEsWechatArticleAbstract(v.WechatArticleAbstractId)
  155. }
  156. }
  157. br.Ret = 200
  158. br.Success = true
  159. br.Msg = `删除成功`
  160. }
  161. // VectorDel
  162. // @Title 删除摘要向量库
  163. // @Description 删除摘要向量库
  164. // @Param request body request.EditQuestionReq true "type json string"
  165. // @Success 200 Ret=200 新增成功
  166. // @router /abstract/vector/del [post]
  167. func (c *AbstractController) VectorDel() {
  168. br := new(models.BaseResponse).Init()
  169. defer func() {
  170. c.Data["json"] = br
  171. c.ServeJSON()
  172. }()
  173. var req request.DelAbstractReq
  174. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  175. if err != nil {
  176. br.Msg = "参数解析异常!"
  177. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  178. return
  179. }
  180. if len(req.WechatArticleAbstractIdList) <= 0 {
  181. br.Msg = "请选择摘要"
  182. br.IsSendEmail = false
  183. return
  184. }
  185. obj := rag.WechatArticleAbstract{}
  186. list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
  187. if err != nil {
  188. br.Msg = "修改失败"
  189. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  190. if utils.IsErrNoRow(err) {
  191. br.Msg = "问题不存在"
  192. br.IsSendEmail = false
  193. }
  194. return
  195. }
  196. // 删除向量库
  197. err = services.DelDoc(list)
  198. if err != nil {
  199. br.Msg = "删除失败"
  200. br.ErrMsg = "删除失败,Err:" + err.Error()
  201. return
  202. }
  203. br.Ret = 200
  204. br.Success = true
  205. br.Msg = `删除成功`
  206. }
  207. // AddVector
  208. // @Title 删除摘要向量库
  209. // @Description 删除摘要向量库
  210. // @Param request body request.EditQuestionReq true "type json string"
  211. // @Success 200 Ret=200 新增成功
  212. // @router /abstract/vector/add [post]
  213. func (c *AbstractController) AddVector() {
  214. br := new(models.BaseResponse).Init()
  215. defer func() {
  216. c.Data["json"] = br
  217. c.ServeJSON()
  218. }()
  219. var req request.DelAbstractReq
  220. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  221. if err != nil {
  222. br.Msg = "参数解析异常!"
  223. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  224. return
  225. }
  226. if len(req.WechatArticleAbstractIdList) <= 0 {
  227. br.Msg = "请选择摘要"
  228. br.IsSendEmail = false
  229. return
  230. }
  231. obj := rag.WechatArticleAbstract{}
  232. list, err := obj.GetByIdList(req.WechatArticleAbstractIdList)
  233. if err != nil {
  234. br.Msg = "修改失败"
  235. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  236. if utils.IsErrNoRow(err) {
  237. br.Msg = "问题不存在"
  238. br.IsSendEmail = false
  239. }
  240. return
  241. }
  242. for _, item := range list {
  243. cache.AddWechatArticleLlmOpToCache(item.WechatArticleId, ``)
  244. }
  245. br.Ret = 200
  246. br.Success = true
  247. br.Msg = `添加向量库中,请稍后查看`
  248. }