promote_controller.go 8.0 KB

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