promote_controller.go 7.2 KB

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