promote_controller.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. "time"
  11. )
  12. type PromoteController struct {
  13. controllers.BaseAuthController
  14. }
  15. // PromoteTrainRecordList @Title 获取聊天记录
  16. // @Description 获取聊天记录
  17. // @Success 101 {object} response.ListResp
  18. // @router /promote/train_list [get]
  19. func (pCtrl *PromoteController) PromoteTrainRecordList() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. pCtrl.Data["json"] = br
  23. pCtrl.ServeJSON()
  24. }()
  25. sysUser := pCtrl.SysUser
  26. if sysUser == nil {
  27. br.Msg = "请登录"
  28. br.ErrMsg = "请登录,SysUser Is Empty"
  29. br.Ret = 408
  30. return
  31. }
  32. pageSize, _ := pCtrl.GetInt("PageSize")
  33. currentIndex, _ := pCtrl.GetInt("CurrentIndex")
  34. //var total, startSize int
  35. if pageSize <= 0 {
  36. pageSize = utils.PageSize5
  37. }
  38. if currentIndex <= 0 {
  39. currentIndex = 1
  40. }
  41. //startSize = paging.StartIndex(currentIndex, pageSize)
  42. //page := paging.GetPaging(currentIndex, pageSize, total)
  43. //total, err := rag.CountQuestionList()
  44. //if err != nil {
  45. // br.Msg = "获取失败"
  46. // br.ErrMsg = "获取失败,Err:" + err.Error()
  47. // return
  48. //}
  49. //list, err := models.GetPptV2List(condition, pars, startSize, pageSize)
  50. //if err != nil {
  51. // br.Msg = "获取失败"
  52. // br.ErrMsg = "获取失败,Err:" + err.Error()
  53. // return
  54. //}
  55. br.Data = nil
  56. br.Ret = 200
  57. br.Success = true
  58. br.Msg = "获取聊天记录成功"
  59. }
  60. // GenerateContent @Title 生成问答内容
  61. // @Description 生成问答内容
  62. // @Success 101 {object} response.ListResp
  63. // @router /promote/generate_content [post]
  64. func (pCtrl *PromoteController) GenerateContent() {
  65. br := new(models.BaseResponse).Init()
  66. defer func() {
  67. pCtrl.Data["json"] = br
  68. pCtrl.ServeJSON()
  69. }()
  70. var gcReq llm_http.GenerateContentReq
  71. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  72. if err != nil {
  73. br.Msg = "参数解析异常!"
  74. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  75. return
  76. }
  77. sysUser := pCtrl.SysUser
  78. if sysUser == nil {
  79. br.Msg = "请登录"
  80. br.ErrMsg = "请登录,SysUser Is Empty"
  81. br.Ret = 408
  82. return
  83. }
  84. if gcReq.Promote == "" {
  85. br.Msg = "提示词不能为空"
  86. br.ErrMsg = "提示词不能为空"
  87. return
  88. }
  89. if gcReq.WechatArticleId <= 0 {
  90. br.Msg = "公众号文章编号非法"
  91. br.ErrMsg = "公众号文章编号非法"
  92. return
  93. }
  94. userContent := llm_http.Content{
  95. Content: gcReq.Promote,
  96. Role: "user",
  97. SendTime: time.Now().Format(utils.FormatDateTime),
  98. }
  99. res, err := facade.AIGCBaseOnPromote(facade.AIGC{
  100. Promote: gcReq.Promote,
  101. ArticleId: gcReq.WechatArticleId,
  102. })
  103. if err != nil {
  104. br.Msg = "内容生成失败"
  105. br.ErrMsg = "内容生成失败,Err:" + err.Error()
  106. return
  107. }
  108. aiContent := llm_http.Content{
  109. Content: res.Answer,
  110. Role: "assistant",
  111. SendTime: time.Now().Format(utils.FormatDateTime),
  112. }
  113. br.Data = llm_http.AIGCResp{
  114. Promote: userContent,
  115. Answer: aiContent,
  116. }
  117. br.Ret = 200
  118. br.Success = true
  119. br.Msg = "内容生成成功"
  120. }
  121. // SavePromoteContent @Title 保存问答内容
  122. // @Description 生成问答内容
  123. // @Success 101 {object} response.ListResp
  124. // @router /promote/save_content [post]
  125. func (pCtrl *PromoteController) SavePromoteContent() {
  126. br := new(models.BaseResponse).Init()
  127. defer func() {
  128. pCtrl.Data["json"] = br
  129. pCtrl.ServeJSON()
  130. }()
  131. var gcReq llm_http.SaveContentReq
  132. err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq)
  133. if err != nil {
  134. br.Msg = "参数解析异常!"
  135. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  136. return
  137. }
  138. sysUser := pCtrl.SysUser
  139. if sysUser == nil {
  140. br.Msg = "请登录"
  141. br.ErrMsg = "请登录,SysUser Is Empty"
  142. br.Ret = 408
  143. return
  144. }
  145. if gcReq.Promote == nil {
  146. br.Msg = "提示词内容不能为空"
  147. br.ErrMsg = "提示词不能为空"
  148. return
  149. }
  150. if gcReq.AigcContent == nil {
  151. br.Msg = "回答内容不能为空"
  152. br.ErrMsg = "提示词不能为空"
  153. return
  154. }
  155. if gcReq.WechatArticleId <= 0 {
  156. br.Msg = "公众号文章编号非法"
  157. br.ErrMsg = "公众号文章编号非法"
  158. return
  159. }
  160. var userContent, assistantContent llm_http.Content
  161. parseErr := json.Unmarshal([]byte(gcReq.AigcContent), &assistantContent)
  162. if parseErr != nil {
  163. br.Msg = "内容参数解析异常!"
  164. br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
  165. return
  166. }
  167. parseErr = json.Unmarshal([]byte(gcReq.Promote), &userContent)
  168. if parseErr != nil {
  169. br.Msg = "内容参数解析异常!"
  170. br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
  171. return
  172. }
  173. var titile string
  174. if gcReq.Title != "" {
  175. titile = gcReq.Title
  176. } else {
  177. titile = userContent.Content
  178. }
  179. var userSendTime, assistantSendTime time.Time
  180. if userContent.SendTime != "" {
  181. userSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, userContent.SendTime, time.Local)
  182. if parseErr != nil {
  183. br.Msg = "用户发送时间解析异常!"
  184. br.ErrMsg = "用户发送时间解析异常,err" + parseErr.Error()
  185. return
  186. }
  187. } else {
  188. br.Msg = "用户发送时间不能为空!"
  189. br.ErrMsg = "用户发送时间不能为空"
  190. return
  191. }
  192. if assistantContent.SendTime != "" {
  193. assistantSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, assistantContent.SendTime, time.Local)
  194. if parseErr != nil {
  195. br.Msg = "AI生成时间解析异常!"
  196. br.ErrMsg = "AI生成时间解析异常,err" + parseErr.Error()
  197. return
  198. }
  199. } else {
  200. br.Msg = "AI生成时间不能为空!"
  201. br.ErrMsg = "AI生成时间不能为空"
  202. return
  203. }
  204. saveContentReq := rag.PromoteTrainRecord{
  205. WechatArticleId: gcReq.WechatArticleId,
  206. Title: titile,
  207. AigcContent: assistantContent.Content,
  208. AigcSendTime: assistantSendTime,
  209. TemplatePromote: userContent.Content,
  210. PromoteSendTime: userSendTime,
  211. CreatedTime: time.Now(),
  212. }
  213. err = saveContentReq.SaveContent()
  214. if err != nil {
  215. br.Msg = "保存内容失败"
  216. br.ErrMsg = "保存内容失败,err:" + err.Error()
  217. return
  218. }
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "保存内容成功"
  222. }