package llm import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/controllers/llm/llm_http" "eta/eta_api/models" "eta/eta_api/models/rag" "eta/eta_api/services/llm/facade" "eta/eta_api/utils" "fmt" "strings" "time" ) type PromoteController struct { controllers.BaseAuthController } // GenerateContent @Title 生成问答内容 // @Description 生成问答内容 // @Success 101 {object} response.ListResp // @router /promote/generate_content [post] func (pCtrl *PromoteController) GenerateContent() { br := new(models.BaseResponse).Init() defer func() { pCtrl.Data["json"] = br pCtrl.ServeJSON() }() var gcReq llm_http.GenerateContentReq err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } sysUser := pCtrl.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } if gcReq.Promote == "" { br.Msg = "提示词不能为空" br.ErrMsg = "提示词不能为空" return } if gcReq.WechatArticleId <= 0 { br.Msg = "公众号文章编号非法" br.ErrMsg = "公众号文章编号非法" return } userSendTime := time.Now() userContent := llm_http.Content{ Content: gcReq.Promote, Role: "user", SendTime: userSendTime.Format(utils.FormatDateTime), } article, err := rag.GetArticleById(gcReq.WechatArticleId) if err != nil { br.Msg = "获取公众号内容失败" br.ErrMsg = "获取公众号内容失败,Err:" + err.Error() return } if article.TextContent == "" { br.Msg = "暂不支持纯文本以外的内容生成" br.ErrMsg = "暂不支持纯文本以外的内容生成" return } res, err := facade.AIGCBaseOnPromote(facade.AIGC{ Promote: gcReq.Promote, ArticleId: gcReq.WechatArticleId, LLMModel: gcReq.LLMModel, }) if err != nil { br.Msg = err.Error() br.ErrMsg = "内容生成失败,Err:" + err.Error() return } aiSendTime := time.Now() aiContent := llm_http.Content{ Content: res.Answer, Role: "assistant", SendTime: aiSendTime.Format(utils.FormatDateTime), } br.Data = llm_http.AIGCResp{ Promote: userContent, Answer: aiContent, } llm := strings.ReplaceAll(gcReq.LLMModel, ":", "") saveContentReq := rag.PromoteTrainRecord{ WechatArticleId: gcReq.WechatArticleId, Title: userContent.Content, Llm: llm, AigcContent: res.Answer, AigcSendTime: aiSendTime, TemplatePromote: userContent.Content, PromoteSendTime: userSendTime, CreatedTime: time.Now(), } err = saveContentReq.SaveContent() if err != nil { br.Msg = "保存内容失败" br.ErrMsg = "保存内容失败,err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "内容生成成功" } // SavePromoteContent @Title 保存问答内容 // @Description 生成问答内容 // @Success 101 {object} response.ListResp // @router /promote/save_content [post] func (pCtrl *PromoteController) SavePromoteContent() { br := new(models.BaseResponse).Init() defer func() { pCtrl.Data["json"] = br pCtrl.ServeJSON() }() var gcReq llm_http.SaveContentReq err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } sysUser := pCtrl.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } if gcReq.Promote == nil { br.Msg = "提示词内容不能为空" br.ErrMsg = "提示词不能为空" return } if gcReq.AigcContent == nil { br.Msg = "回答内容不能为空" br.ErrMsg = "提示词不能为空" return } if gcReq.WechatArticleId <= 0 { br.Msg = "公众号文章编号非法" br.ErrMsg = "公众号文章编号非法" return } var userContent, assistantContent llm_http.Content parseErr := json.Unmarshal(gcReq.AigcContent, &assistantContent) if parseErr != nil { br.Msg = "内容参数解析异常!" br.ErrMsg = "内容参数解析异,err" + parseErr.Error() return } parseErr = json.Unmarshal(gcReq.Promote, &userContent) if parseErr != nil { br.Msg = "内容参数解析异常!" br.ErrMsg = "内容参数解析异,err" + parseErr.Error() return } var titile string if gcReq.Title != "" { titile = gcReq.Title } else { titile = userContent.Content } var userSendTime, assistantSendTime time.Time if userContent.SendTime != "" { userSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, userContent.SendTime, time.Local) if parseErr != nil { br.Msg = "用户发送时间解析异常!" br.ErrMsg = "用户发送时间解析异常,err" + parseErr.Error() return } } else { br.Msg = "用户发送时间不能为空!" br.ErrMsg = "用户发送时间不能为空" return } if assistantContent.SendTime != "" { assistantSendTime, parseErr = time.ParseInLocation(utils.FormatDateTime, assistantContent.SendTime, time.Local) if parseErr != nil { br.Msg = "AI生成时间解析异常!" br.ErrMsg = "AI生成时间解析异常,err" + parseErr.Error() return } } else { br.Msg = "AI生成时间不能为空!" br.ErrMsg = "AI生成时间不能为空" return } llm := strings.ReplaceAll(gcReq.Llm, ":", "") saveContentReq := rag.PromoteTrainRecord{ WechatArticleId: gcReq.WechatArticleId, Title: titile, Llm: llm, AigcContent: assistantContent.Content, AigcSendTime: assistantSendTime, TemplatePromote: userContent.Content, PromoteSendTime: userSendTime, CreatedTime: time.Now(), } err = saveContentReq.SaveContent() if err != nil { br.Msg = "保存内容失败" br.ErrMsg = "保存内容失败,err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "保存内容成功" } // DeletePromoteContent @Title 删除问答内容 // @Description 生成问答内容 // @Success 101 {object} response.ListResp // @router /promote/delete_content [post] func (pCtrl *PromoteController) DeletePromoteContent() { br := new(models.BaseResponse).Init() defer func() { pCtrl.Data["json"] = br pCtrl.ServeJSON() }() var gcReq llm_http.DeleteContentReq err := json.Unmarshal(pCtrl.Ctx.Input.RequestBody, &gcReq) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } sysUser := pCtrl.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } if gcReq.RecordId <= 0 { br.Msg = "记录编号非法" br.ErrMsg = "记录编号非法" return } err = rag.DeleteContent(gcReq.RecordId) if err != nil { br.Msg = "删除内容失败" br.ErrMsg = "删除内容失败,err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除内容成功" } // PromoteContentList @Title 删除问答内容 // @Description 生成问答内容 // @Success 101 {object} response.ListResp // @router /promote/content_list [get] func (pCtrl *PromoteController) PromoteContentList() { br := new(models.BaseResponse).Init() defer func() { pCtrl.Data["json"] = br pCtrl.ServeJSON() }() wechatArticleId, _ := pCtrl.GetInt("WechatArticleId") sysUser := pCtrl.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } if wechatArticleId <= 0 { br.Msg = "记录编号非法" br.ErrMsg = "记录编号非法" return } list, err := rag.GetRecordList(wechatArticleId) if err != nil { br.Msg = "查询列表失败" br.ErrMsg = "查询列表失败,err:" + err.Error() return } total := len(list) for _, item := range list { item.Title = fmt.Sprintf("%d.%s", total, item.Title) total-- } br.Data = list br.Ret = 200 br.Success = true br.Msg = "查询列表成功" }