123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- package llm
- import (
- "encoding/json"
- "eta/eta_api/cache"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/rag"
- "eta/eta_api/models/rag/request"
- "eta/eta_api/models/rag/response"
- "eta/eta_api/services"
- "eta/eta_api/services/elastic"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- )
- // AbstractController
- // @Description: 摘要管理
- type AbstractController struct {
- controllers.BaseAuthController
- }
- // List
- // @Title 列表
- // @Description 列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param KeyWord query string true "搜索关键词"
- // @Success 200 {object} []*rag.QuestionListListResp
- // @router /abstract/list [get]
- func (c *AbstractController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- return
- }
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- keyWord := c.GetString("KeyWord")
- tagId, _ := c.GetInt("TagId")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var total int
- viewList := make([]rag.WechatArticleAbstractView, 0)
- if keyWord == `` {
- var condition string
- var pars []interface{}
- condition += fmt.Sprintf(` AND c.%s = ?`, rag.WechatPlatformColumns.Enabled)
- pars = append(pars, 1)
- if keyWord != "" {
- condition += fmt.Sprintf(` AND a.%s like ?`, rag.WechatArticleAbstractColumns.Content)
- pars = append(pars, `%`+keyWord+`%`)
- }
- if tagId > 0 {
- condition += fmt.Sprintf(` AND d.%s = ?`, rag.WechatPlatformTagMappingColumns.TagID)
- pars = append(pars, tagId)
- }
- obj := new(rag.WechatArticleAbstract)
- tmpTotal, list, err := obj.GetPageListByTagAndPlatformCondition(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- total = tmpTotal
- viewList = obj.WechatArticleAbstractItem(list)
- } else {
- sortMap := map[string]string{
- "ModifyTime": "desc",
- "WechatArticleAbstractId": "desc",
- }
- obj := new(rag.WechatPlatform)
- platformList, err := obj.GetListByCondition(` AND enabled = 1 `, []interface{}{}, 0, 100000)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- platformIdList := make([]int, 0)
- for _, v := range platformList {
- platformIdList = append(platformIdList, v.WechatPlatformId)
- }
- tagList := make([]int, 0)
- if tagId > 0 {
- tagList = append(tagList, tagId)
- }
- tmpTotal, list, err := elastic.WechatArticleAbstractEsSearch(keyWord, tagList, platformIdList, startSize, pageSize, sortMap)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- total = int(tmpTotal)
- if list != nil && len(list) > 0 {
- viewList = list[0].ToViewList(list)
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := response.AbstractListListResp{
- List: viewList,
- Paging: page,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Del
- // @Title 删除摘要
- // @Description 删除摘要
- // @Param request body request.DelAbstractReq true "type json string"
- // @Success 200 Ret=200 新增成功
- // @router /abstract/del [post]
- func (c *AbstractController) Del() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.DelAbstractReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.WechatArticleAbstractIdList) <= 0 {
- br.Msg = "请选择摘要"
- br.IsSendEmail = false
- return
- }
- 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 {
- // 删除向量库
- err = services.DelDoc(list)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除向量库失败,Err:" + err.Error()
- return
- }
- // 删除摘要
- err = obj.DelByIdList(req.WechatArticleAbstractIdList)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + err.Error()
- return
- }
- for _, v := range list {
- go services.AddOrEditEsWechatArticleAbstract(v.WechatArticleAbstractId)
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = `删除成功`
- }
- // VectorDel
- // @Title 删除摘要向量库
- // @Description 删除摘要向量库
- // @Param request body request.EditQuestionReq true "type json string"
- // @Success 200 Ret=200 新增成功
- // @router /abstract/vector/del [post]
- func (c *AbstractController) VectorDel() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.DelAbstractReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.WechatArticleAbstractIdList) <= 0 {
- br.Msg = "请选择摘要"
- br.IsSendEmail = false
- return
- }
- 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
- }
- // 删除向量库
- err = services.DelDoc(list)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = `删除成功`
- }
- // AddVector
- // @Title 删除摘要向量库
- // @Description 删除摘要向量库
- // @Param request body request.EditQuestionReq true "type json string"
- // @Success 200 Ret=200 新增成功
- // @router /abstract/vector/add [post]
- func (c *AbstractController) AddVector() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.DelAbstractReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.WechatArticleAbstractIdList) <= 0 {
- br.Msg = "请选择摘要"
- br.IsSendEmail = false
- return
- }
- 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
- }
- for _, item := range list {
- cache.AddWechatArticleLlmOpToCache(item.WechatArticleId, ``)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = `添加向量库中,请稍后查看`
- }
|