abstract.go 7.0 KB

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