promote_controller.go 7.7 KB

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