promote_controller.go 7.6 KB

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