promote_controller.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package llm
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/controllers/llm/llm_http"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/rag"
  8. "eta/eta_api/services/llm/facade"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "time"
  12. )
  13. type PromoteController struct {
  14. controllers.BaseAuthController
  15. }
  16. // GenerateContent @Title 生成问答内容
  17. // @Description 生成问答内容
  18. // @Success 101 {object} response.ListResp
  19. // @router /promote/generate_content [post]
  20. func (pCtrl *PromoteController) GenerateContent() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. pCtrl.Data["json"] = br
  24. pCtrl.ServeJSON()
  25. }()
  26. var gcReq llm_http.GenerateContentReq
  27. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  28. if err != nil {
  29. br.Msg = "参数解析异常!"
  30. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  31. return
  32. }
  33. sysUser := pCtrl.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. if gcReq.Promote == "" {
  41. br.Msg = "提示词不能为空"
  42. br.ErrMsg = "提示词不能为空"
  43. return
  44. }
  45. if gcReq.WechatArticleId <= 0 {
  46. br.Msg = "公众号文章编号非法"
  47. br.ErrMsg = "公众号文章编号非法"
  48. return
  49. }
  50. userSendTime := time.Now()
  51. userContent := llm_http.Content{
  52. Content: gcReq.Promote,
  53. Role: "user",
  54. SendTime: userSendTime.Format(utils.FormatDateTime),
  55. }
  56. article, err := rag.GetArticleById(gcReq.WechatArticleId)
  57. if err != nil {
  58. br.Msg = "获取公众号内容失败"
  59. br.ErrMsg = "获取公众号内容失败,Err:" + err.Error()
  60. return
  61. }
  62. if article.TextContent == "" {
  63. br.Msg = "暂不支持纯文本以外的内容生成"
  64. br.ErrMsg = "暂不支持纯文本以外的内容生成"
  65. return
  66. }
  67. res, err := facade.AIGCBaseOnPromote(facade.AIGC{
  68. Promote: gcReq.Promote,
  69. Source: gcReq.Source,
  70. ArticleId: gcReq.WechatArticleId,
  71. })
  72. if err != nil {
  73. br.Msg = err.Error()
  74. br.ErrMsg = "内容生成失败,Err:" + err.Error()
  75. return
  76. }
  77. aiSendTime := time.Now()
  78. aiContent := llm_http.Content{
  79. Content: res.Answer,
  80. Role: "assistant",
  81. SendTime: aiSendTime.Format(utils.FormatDateTime),
  82. }
  83. br.Data = llm_http.AIGCResp{
  84. Promote: userContent,
  85. Answer: aiContent,
  86. }
  87. saveContentReq := rag.PromoteTrainRecord{
  88. Source: gcReq.Source,
  89. WechatArticleId: gcReq.WechatArticleId,
  90. Title: userContent.Content,
  91. AigcContent: res.Answer,
  92. AigcSendTime: aiSendTime,
  93. TemplatePromote: userContent.Content,
  94. PromoteSendTime: userSendTime,
  95. CreatedTime: time.Now(),
  96. }
  97. err = saveContentReq.SaveContent()
  98. if err != nil {
  99. br.Msg = "保存内容失败"
  100. br.ErrMsg = "保存内容失败,err:" + err.Error()
  101. return
  102. }
  103. br.Ret = 200
  104. br.Success = true
  105. br.Msg = "内容生成成功"
  106. }
  107. // SavePromoteContent @Title 保存问答内容
  108. // @Description 生成问答内容
  109. // @Success 101 {object} response.ListResp
  110. // @router /promote/save_content [post]
  111. func (pCtrl *PromoteController) SavePromoteContent() {
  112. br := new(models.BaseResponse).Init()
  113. defer func() {
  114. pCtrl.Data["json"] = br
  115. pCtrl.ServeJSON()
  116. }()
  117. var gcReq llm_http.SaveContentReq
  118. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  119. if err != nil {
  120. br.Msg = "参数解析异常!"
  121. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  122. return
  123. }
  124. sysUser := pCtrl.SysUser
  125. if sysUser == nil {
  126. br.Msg = "请登录"
  127. br.ErrMsg = "请登录,SysUser Is Empty"
  128. br.Ret = 408
  129. return
  130. }
  131. if gcReq.Promote == nil {
  132. br.Msg = "提示词内容不能为空"
  133. br.ErrMsg = "提示词不能为空"
  134. return
  135. }
  136. if gcReq.AigcContent == nil {
  137. br.Msg = "回答内容不能为空"
  138. br.ErrMsg = "提示词不能为空"
  139. return
  140. }
  141. if gcReq.WechatArticleId <= 0 {
  142. br.Msg = "公众号文章编号非法"
  143. br.ErrMsg = "公众号文章编号非法"
  144. return
  145. }
  146. var userContent, assistantContent llm_http.Content
  147. parseErr := json.Unmarshal(gcReq.AigcContent, &assistantContent)
  148. if parseErr != nil {
  149. br.Msg = "内容参数解析异常!"
  150. br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
  151. return
  152. }
  153. parseErr = json.Unmarshal(gcReq.Promote, &userContent)
  154. if parseErr != nil {
  155. br.Msg = "内容参数解析异常!"
  156. br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
  157. return
  158. }
  159. var titile string
  160. if gcReq.Title != "" {
  161. titile = gcReq.Title
  162. } else {
  163. titile = userContent.Content
  164. }
  165. var userSendTime, assistantSendTime time.Time
  166. if userContent.SendTime != "" {
  167. userSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, userContent.SendTime, time.Local)
  168. if parseErr != nil {
  169. br.Msg = "用户发送时间解析异常!"
  170. br.ErrMsg = "用户发送时间解析异常,err" + parseErr.Error()
  171. return
  172. }
  173. } else {
  174. br.Msg = "用户发送时间不能为空!"
  175. br.ErrMsg = "用户发送时间不能为空"
  176. return
  177. }
  178. if assistantContent.SendTime != "" {
  179. assistantSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, assistantContent.SendTime, time.Local)
  180. if parseErr != nil {
  181. br.Msg = "AI生成时间解析异常!"
  182. br.ErrMsg = "AI生成时间解析异常,err" + parseErr.Error()
  183. return
  184. }
  185. } else {
  186. br.Msg = "AI生成时间不能为空!"
  187. br.ErrMsg = "AI生成时间不能为空"
  188. return
  189. }
  190. saveContentReq := rag.PromoteTrainRecord{
  191. Source: gcReq.Source,
  192. WechatArticleId: gcReq.WechatArticleId,
  193. Title: titile,
  194. AigcContent: assistantContent.Content,
  195. AigcSendTime: assistantSendTime,
  196. TemplatePromote: userContent.Content,
  197. PromoteSendTime: userSendTime,
  198. CreatedTime: time.Now(),
  199. }
  200. err = saveContentReq.SaveContent()
  201. if err != nil {
  202. br.Msg = "保存内容失败"
  203. br.ErrMsg = "保存内容失败,err:" + err.Error()
  204. return
  205. }
  206. br.Ret = 200
  207. br.Success = true
  208. br.Msg = "保存内容成功"
  209. }
  210. // DeletePromoteContent @Title 删除问答内容
  211. // @Description 生成问答内容
  212. // @Success 101 {object} response.ListResp
  213. // @router /promote/delete_content [post]
  214. func (pCtrl *PromoteController) DeletePromoteContent() {
  215. br := new(models.BaseResponse).Init()
  216. defer func() {
  217. pCtrl.Data["json"] = br
  218. pCtrl.ServeJSON()
  219. }()
  220. var gcReq llm_http.DeleteContentReq
  221. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  222. if err != nil {
  223. br.Msg = "参数解析异常!"
  224. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  225. return
  226. }
  227. sysUser := pCtrl.SysUser
  228. if sysUser == nil {
  229. br.Msg = "请登录"
  230. br.ErrMsg = "请登录,SysUser Is Empty"
  231. br.Ret = 408
  232. return
  233. }
  234. if gcReq.RecordId <= 0 {
  235. br.Msg = "记录编号非法"
  236. br.ErrMsg = "记录编号非法"
  237. return
  238. }
  239. err = rag.DeleteContent(gcReq.RecordId)
  240. if err != nil {
  241. br.Msg = "删除内容失败"
  242. br.ErrMsg = "删除内容失败,err:" + err.Error()
  243. return
  244. }
  245. br.Ret = 200
  246. br.Success = true
  247. br.Msg = "删除内容成功"
  248. }
  249. // PromoteContentList @Title 删除问答内容
  250. // @Description 生成问答内容
  251. // @Success 101 {object} response.ListResp
  252. // @router /promote/content_list [get]
  253. func (pCtrl *PromoteController) PromoteContentList() {
  254. br := new(models.BaseResponse).Init()
  255. defer func() {
  256. pCtrl.Data["json"] = br
  257. pCtrl.ServeJSON()
  258. }()
  259. wechatArticleId, _ := pCtrl.GetInt("WechatArticleId")
  260. source, _ := pCtrl.GetInt("Source")
  261. sysUser := pCtrl.SysUser
  262. if sysUser == nil {
  263. br.Msg = "请登录"
  264. br.ErrMsg = "请登录,SysUser Is Empty"
  265. br.Ret = 408
  266. return
  267. }
  268. if wechatArticleId <= 0 {
  269. br.Msg = "记录编号非法"
  270. br.ErrMsg = "记录编号非法"
  271. return
  272. }
  273. list, err := rag.GetRecordList(wechatArticleId, source)
  274. if err != nil {
  275. br.Msg = "查询列表失败"
  276. br.ErrMsg = "查询列表失败,err:" + err.Error()
  277. return
  278. }
  279. total := len(list)
  280. for _, item := range list {
  281. item.Title = fmt.Sprintf("%d.%s", total, item.Title)
  282. total--
  283. }
  284. br.Data = list
  285. br.Ret = 200
  286. br.Success = true
  287. br.Msg = "查询列表成功"
  288. }