promote_controller.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. ArticleId: gcReq.WechatArticleId,
  70. })
  71. if err != nil {
  72. br.Msg = err.Error()
  73. br.ErrMsg = "内容生成失败,Err:" + err.Error()
  74. return
  75. }
  76. aiSendTime := time.Now()
  77. aiContent := llm_http.Content{
  78. Content: res.Answer,
  79. Role: "assistant",
  80. SendTime: aiSendTime.Format(utils.FormatDateTime),
  81. }
  82. br.Data = llm_http.AIGCResp{
  83. Promote: userContent,
  84. Answer: aiContent,
  85. }
  86. saveContentReq := rag.PromoteTrainRecord{
  87. WechatArticleId: gcReq.WechatArticleId,
  88. Title: userContent.Content,
  89. AigcContent: res.Answer,
  90. AigcSendTime: aiSendTime,
  91. TemplatePromote: userContent.Content,
  92. PromoteSendTime: userSendTime,
  93. CreatedTime: time.Now(),
  94. }
  95. err = saveContentReq.SaveContent()
  96. if err != nil {
  97. br.Msg = "保存内容失败"
  98. br.ErrMsg = "保存内容失败,err:" + err.Error()
  99. return
  100. }
  101. br.Ret = 200
  102. br.Success = true
  103. br.Msg = "内容生成成功"
  104. }
  105. // SavePromoteContent @Title 保存问答内容
  106. // @Description 生成问答内容
  107. // @Success 101 {object} response.ListResp
  108. // @router /promote/save_content [post]
  109. func (pCtrl *PromoteController) SavePromoteContent() {
  110. br := new(models.BaseResponse).Init()
  111. defer func() {
  112. pCtrl.Data["json"] = br
  113. pCtrl.ServeJSON()
  114. }()
  115. var gcReq llm_http.SaveContentReq
  116. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  117. if err != nil {
  118. br.Msg = "参数解析异常!"
  119. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  120. return
  121. }
  122. sysUser := pCtrl.SysUser
  123. if sysUser == nil {
  124. br.Msg = "请登录"
  125. br.ErrMsg = "请登录,SysUser Is Empty"
  126. br.Ret = 408
  127. return
  128. }
  129. if gcReq.Promote == nil {
  130. br.Msg = "提示词内容不能为空"
  131. br.ErrMsg = "提示词不能为空"
  132. return
  133. }
  134. if gcReq.AigcContent == nil {
  135. br.Msg = "回答内容不能为空"
  136. br.ErrMsg = "提示词不能为空"
  137. return
  138. }
  139. if gcReq.WechatArticleId <= 0 {
  140. br.Msg = "公众号文章编号非法"
  141. br.ErrMsg = "公众号文章编号非法"
  142. return
  143. }
  144. var userContent, assistantContent llm_http.Content
  145. parseErr := json.Unmarshal(gcReq.AigcContent, &assistantContent)
  146. if parseErr != nil {
  147. br.Msg = "内容参数解析异常!"
  148. br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
  149. return
  150. }
  151. parseErr = json.Unmarshal(gcReq.Promote, &userContent)
  152. if parseErr != nil {
  153. br.Msg = "内容参数解析异常!"
  154. br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
  155. return
  156. }
  157. var titile string
  158. if gcReq.Title != "" {
  159. titile = gcReq.Title
  160. } else {
  161. titile = userContent.Content
  162. }
  163. var userSendTime, assistantSendTime time.Time
  164. if userContent.SendTime != "" {
  165. userSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, userContent.SendTime, time.Local)
  166. if parseErr != nil {
  167. br.Msg = "用户发送时间解析异常!"
  168. br.ErrMsg = "用户发送时间解析异常,err" + parseErr.Error()
  169. return
  170. }
  171. } else {
  172. br.Msg = "用户发送时间不能为空!"
  173. br.ErrMsg = "用户发送时间不能为空"
  174. return
  175. }
  176. if assistantContent.SendTime != "" {
  177. assistantSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, assistantContent.SendTime, time.Local)
  178. if parseErr != nil {
  179. br.Msg = "AI生成时间解析异常!"
  180. br.ErrMsg = "AI生成时间解析异常,err" + parseErr.Error()
  181. return
  182. }
  183. } else {
  184. br.Msg = "AI生成时间不能为空!"
  185. br.ErrMsg = "AI生成时间不能为空"
  186. return
  187. }
  188. saveContentReq := rag.PromoteTrainRecord{
  189. WechatArticleId: gcReq.WechatArticleId,
  190. Title: titile,
  191. AigcContent: assistantContent.Content,
  192. AigcSendTime: assistantSendTime,
  193. TemplatePromote: userContent.Content,
  194. PromoteSendTime: userSendTime,
  195. CreatedTime: time.Now(),
  196. }
  197. err = saveContentReq.SaveContent()
  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. // DeletePromoteContent @Title 删除问答内容
  208. // @Description 生成问答内容
  209. // @Success 101 {object} response.ListResp
  210. // @router /promote/delete_content [post]
  211. func (pCtrl *PromoteController) DeletePromoteContent() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. pCtrl.Data["json"] = br
  215. pCtrl.ServeJSON()
  216. }()
  217. var gcReq llm_http.DeleteContentReq
  218. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  219. if err != nil {
  220. br.Msg = "参数解析异常!"
  221. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  222. return
  223. }
  224. sysUser := pCtrl.SysUser
  225. if sysUser == nil {
  226. br.Msg = "请登录"
  227. br.ErrMsg = "请登录,SysUser Is Empty"
  228. br.Ret = 408
  229. return
  230. }
  231. if gcReq.RecordId <= 0 {
  232. br.Msg = "记录编号非法"
  233. br.ErrMsg = "记录编号非法"
  234. return
  235. }
  236. err = rag.DeleteContent(gcReq.RecordId)
  237. if err != nil {
  238. br.Msg = "删除内容失败"
  239. br.ErrMsg = "删除内容失败,err:" + err.Error()
  240. return
  241. }
  242. br.Ret = 200
  243. br.Success = true
  244. br.Msg = "删除内容成功"
  245. }
  246. // PromoteContentList @Title 删除问答内容
  247. // @Description 生成问答内容
  248. // @Success 101 {object} response.ListResp
  249. // @router /promote/content_list [get]
  250. func (pCtrl *PromoteController) PromoteContentList() {
  251. br := new(models.BaseResponse).Init()
  252. defer func() {
  253. pCtrl.Data["json"] = br
  254. pCtrl.ServeJSON()
  255. }()
  256. wechatArticleId, _ := pCtrl.GetInt("WechatArticleId")
  257. sysUser := pCtrl.SysUser
  258. if sysUser == nil {
  259. br.Msg = "请登录"
  260. br.ErrMsg = "请登录,SysUser Is Empty"
  261. br.Ret = 408
  262. return
  263. }
  264. if wechatArticleId <= 0 {
  265. br.Msg = "记录编号非法"
  266. br.ErrMsg = "记录编号非法"
  267. return
  268. }
  269. list, err := rag.GetRecordList(wechatArticleId)
  270. if err != nil {
  271. br.Msg = "查询列表失败"
  272. br.ErrMsg = "查询列表失败,err:" + err.Error()
  273. return
  274. }
  275. total := len(list)
  276. for _, item := range list {
  277. item.Title = fmt.Sprintf("%d.%s", total, item.Title)
  278. total--
  279. }
  280. br.Data = list
  281. br.Ret = 200
  282. br.Success = true
  283. br.Msg = "查询列表成功"
  284. }