123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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"
- "time"
- )
- type PromoteController struct {
- controllers.BaseAuthController
- }
- // PromoteTrainRecordList @Title 获取聊天记录
- // @Description 获取聊天记录
- // @Success 101 {object} response.ListResp
- // @router /promote/train_list [get]
- func (pCtrl *PromoteController) PromoteTrainRecordList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- pCtrl.Data["json"] = br
- pCtrl.ServeJSON()
- }()
- sysUser := pCtrl.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- pageSize, _ := pCtrl.GetInt("PageSize")
- currentIndex, _ := pCtrl.GetInt("CurrentIndex")
- //var total, startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize5
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- //startSize = paging.StartIndex(currentIndex, pageSize)
- //page := paging.GetPaging(currentIndex, pageSize, total)
- //total, err := rag.CountQuestionList()
- //if err != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "获取失败,Err:" + err.Error()
- // return
- //}
- //list, err := models.GetPptV2List(condition, pars, startSize, pageSize)
- //if err != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "获取失败,Err:" + err.Error()
- // return
- //}
- br.Data = nil
- br.Ret = 200
- br.Success = true
- br.Msg = "获取聊天记录成功"
- }
- // 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
- }
- userContent := llm_http.Content{
- Content: gcReq.Promote,
- Role: "user",
- SendTime: time.Now().Format(utils.FormatDateTime),
- }
- res, err := facade.AIGCBaseOnPromote(facade.AIGC{
- Promote: gcReq.Promote,
- ArticleId: gcReq.WechatArticleId,
- })
- if err != nil {
- br.Msg = "内容生成失败"
- br.ErrMsg = "内容生成失败,Err:" + err.Error()
- return
- }
- aiContent := llm_http.Content{
- Content: res.Answer,
- Role: "assistant",
- SendTime: time.Now().Format(utils.FormatDateTime),
- }
- br.Data = llm_http.AIGCResp{
- Promote: userContent,
- Answer: aiContent,
- }
- 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([]byte(gcReq.AigcContent), &assistantContent)
- if parseErr != nil {
- br.Msg = "内容参数解析异常!"
- br.ErrMsg = "内容参数解析异,err" + parseErr.Error()
- return
- }
- parseErr = json.Unmarshal([]byte(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
- }
- saveContentReq := rag.PromoteTrainRecord{
- WechatArticleId: gcReq.WechatArticleId,
- Title: titile,
- 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 = "保存内容成功"
- }
|