promote_controller.go 7.9 KB

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