ai.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package ai
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/aimod"
  7. "eta/eta_api/services/aiser"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strconv"
  11. "time"
  12. )
  13. // AI
  14. type AiController struct {
  15. controllers.BaseAuthController
  16. }
  17. // @Title 聊天接口
  18. // @Description 聊天接口
  19. // @Param request body aimod.ChatReq true "type json string"
  20. // @Success 200 {object} response.ListResp
  21. // @router /chat [post]
  22. func (this *AiController) List() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req aimod.ChatReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.Ask == "" {
  36. br.Msg = "请输入提问内容!"
  37. br.ErrMsg = "请输入提问内容"
  38. return
  39. }
  40. if utils.Re == nil {
  41. key := "CACHE_CHAT_" + strconv.Itoa(this.SysUser.AdminId)
  42. cacheVal, err := utils.Rc.RedisInt(key)
  43. fmt.Println("RedisString:", cacheVal, "err:", err)
  44. if err != nil && (err.Error() != "redigo: nil returned" && err.Error() != "redis: nil") {
  45. br.Msg = "获取数据失败!"
  46. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  47. return
  48. }
  49. putVal := 0
  50. if cacheVal <= 0 {
  51. putVal = utils.AiChatLimit
  52. } else {
  53. putVal = cacheVal - 1
  54. }
  55. if putVal <= 0 {
  56. br.Msg = "您今日" + strconv.Itoa(utils.AiChatLimit) + "次问答已达上限,请明天再来!"
  57. br.ErrMsg = "您今日" + strconv.Itoa(utils.AiChatLimit) + "次问答已达上限,请明天再来!"
  58. return
  59. }
  60. lastSecond := utils.GetTodayLastSecond()
  61. utils.Rc.Put(key, putVal, lastSecond)
  62. }
  63. //根据提问,获取信息
  64. askUuid := utils.MD5(req.Ask)
  65. chatMode, err := aimod.GetAiChatByAsk(askUuid)
  66. if err != nil && err.Error() != utils.ErrNoRow() {
  67. br.Msg = "获取数据失败!"
  68. br.ErrMsg = "获取数据失败,GetAiChatByAsk,Err:" + err.Error()
  69. return
  70. }
  71. resp := new(aimod.ChatResp)
  72. var answer string
  73. //answerArr := []string{
  74. // "周度数据显示,成品油现货市场价格跟随原油下跌,但近期相对抗跌,裂解价差走扩。批零价差方面汽油收窄,柴油走扩",
  75. // "出口利润在原油下跌海外成品油矛盾更大的情况下汽柴油出口窗口完全关闭",
  76. // "汽油需求在经历五一假期的一段高峰后将回归平稳,总体没有明显矛盾,后期我们担心更多的还是柴油。"}
  77. if chatMode != nil && chatMode.Answer != "" {
  78. answer = chatMode.Answer
  79. } else {
  80. //获取主题下的所有信息
  81. //AiChatTopicId
  82. historyList, err := aimod.GetAiChatList(req.AiChatTopicId)
  83. if err != nil && err.Error() != utils.ErrNoRow() {
  84. br.Msg = "获取主题历史数据失败!"
  85. br.ErrMsg = "获取主题历史数据失败,Err:" + err.Error()
  86. return
  87. }
  88. historyChatList := make([]aimod.HistoryChat, 0)
  89. for _, v := range historyList {
  90. historyChat := new(aimod.HistoryChat)
  91. historyChat.Ask = v.Ask
  92. historyChat.Answer = v.Answer
  93. historyChatList = append(historyChatList, *historyChat)
  94. }
  95. answer, err = aiser.ChatAutoMsg(req.Ask, historyChatList, req.Model)
  96. if err != nil {
  97. br.Msg = "获取数据失败!"
  98. br.ErrMsg = "获取数据失败,ChatAutoMsg,Err:" + err.Error()
  99. return
  100. }
  101. }
  102. resp.Ask = req.Ask
  103. resp.Answer = answer
  104. resp.Model = req.Model
  105. if req.AiChatTopicId <= 0 { //新增
  106. topic := new(aimod.AiChatTopic)
  107. topic.TopicName = req.Ask
  108. topic.SysUserId = this.SysUser.AdminId
  109. topic.SysUserRealName = this.SysUser.RealName
  110. topic.CreateTime = time.Now()
  111. topic.ModifyTime = time.Now()
  112. topicId, err := aimod.AddAiChatTopic(topic)
  113. if err != nil {
  114. br.Msg = "获取数据失败!"
  115. br.ErrMsg = "生成话题失败,Err:" + err.Error()
  116. return
  117. }
  118. resp.AiChatTopicId = int(topicId)
  119. chatItem := new(aimod.AiChat)
  120. chatItem.AiChatTopicId = resp.AiChatTopicId
  121. chatItem.Ask = req.Ask
  122. chatItem.AskUuid = utils.MD5(req.Ask)
  123. chatItem.Answer = answer
  124. chatItem.Model = EnabledModelsForMap[req.Model]
  125. chatItem.SysUserId = this.SysUser.AdminId
  126. chatItem.SysUserRealName = this.SysUser.RealName
  127. chatItem.CreateTime = time.Now()
  128. chatItem.ModifyTime = time.Now()
  129. _, err = aimod.AddAiChat(chatItem)
  130. if err != nil {
  131. br.Msg = "获取数据失败!"
  132. br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
  133. return
  134. }
  135. } else {
  136. resp.AiChatTopicId = req.AiChatTopicId
  137. chatItem := new(aimod.AiChat)
  138. chatItem.AiChatTopicId = resp.AiChatTopicId
  139. chatItem.Ask = req.Ask
  140. chatItem.AskUuid = utils.MD5(req.Ask)
  141. chatItem.Answer = answer
  142. chatItem.Model = EnabledModelsForMap[req.Model]
  143. chatItem.SysUserId = this.SysUser.AdminId
  144. chatItem.SysUserRealName = this.SysUser.RealName
  145. chatItem.CreateTime = time.Now()
  146. chatItem.ModifyTime = time.Now()
  147. _, err = aimod.AddAiChat(chatItem)
  148. if err != nil {
  149. br.Msg = "获取数据失败!"
  150. br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
  151. return
  152. }
  153. }
  154. br.Ret = 200
  155. br.Success = true
  156. br.Msg = "获取成功"
  157. br.Data = resp
  158. }
  159. // @Title 获取话题列表
  160. // @Description 获取话题列表接口
  161. // @Success 200 {object} aimod.AiChatTopicListResp
  162. // @router /topic/list [get]
  163. func (this *AiController) TopicList() {
  164. br := new(models.BaseResponse).Init()
  165. defer func() {
  166. this.Data["json"] = br
  167. this.ServeJSON()
  168. }()
  169. sysUser := this.SysUser
  170. if sysUser == nil {
  171. br.Msg = "请登录"
  172. br.ErrMsg = "请登录,SysUser Is Empty"
  173. br.Ret = 408
  174. return
  175. }
  176. list, err := aimod.GetAiChatTopicList(sysUser.AdminId)
  177. if err != nil {
  178. br.Msg = "获取数据失败!"
  179. br.ErrMsg = "获取主题记录信息失败,Err:" + err.Error()
  180. return
  181. }
  182. resp := new(aimod.AiChatTopicListResp)
  183. resp.List = list
  184. br.Ret = 200
  185. br.Success = true
  186. br.Msg = "获取成功"
  187. br.Data = resp
  188. }
  189. // @Title 获取话题详情
  190. // @Description 获取话题详情接口
  191. // @Param AiChatTopicId query int true "主题id"
  192. // @Success 200 {object} aimod.AiChatDetailResp
  193. // @router /topic/detail [get]
  194. func (this *AiController) TopicDetail() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. this.Data["json"] = br
  198. this.ServeJSON()
  199. }()
  200. sysUser := this.SysUser
  201. if sysUser == nil {
  202. br.Msg = "请登录"
  203. br.ErrMsg = "请登录,SysUser Is Empty"
  204. br.Ret = 408
  205. return
  206. }
  207. aiChatTopicId, _ := this.GetInt("AiChatTopicId")
  208. list, err := aimod.GetAiChatList(aiChatTopicId)
  209. if err != nil {
  210. br.Msg = "获取数据失败!"
  211. br.ErrMsg = "获取主题记录信息失败,Err:" + err.Error()
  212. return
  213. }
  214. for index := range list {
  215. list[index].Model = aimod.ModelViewMap[list[index].Model]
  216. }
  217. resp := new(aimod.AiChatDetailResp)
  218. resp.List = list
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "获取成功"
  222. br.Data = resp
  223. }
  224. // @Title 删除话题接口
  225. // @Description 删除话题接口
  226. // @Param request body aimod.TopicDeleteReq true "type json string"
  227. // @Success Ret=200 删除成功
  228. // @router /topic/delete [post]
  229. func (this *AiController) TopicDelete() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. sysUser := this.SysUser
  236. if sysUser == nil {
  237. br.Msg = "请登录"
  238. br.ErrMsg = "请登录,SysUser Is Empty"
  239. br.Ret = 408
  240. return
  241. }
  242. var req aimod.TopicDeleteReq
  243. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  244. if err != nil {
  245. br.Msg = "参数解析异常!"
  246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  247. return
  248. }
  249. if req.AiChatTopicId <= 0 {
  250. br.Msg = "参数错误!"
  251. br.ErrMsg = "参数错误!AiChatTopicId:" + strconv.Itoa(req.AiChatTopicId)
  252. return
  253. }
  254. err = aimod.DeleteTopic(req.AiChatTopicId)
  255. if err != nil {
  256. br.Msg = "删除失败!"
  257. br.ErrMsg = "删除失败,Err:" + err.Error()
  258. return
  259. }
  260. br.Ret = 200
  261. br.Success = true
  262. br.Msg = "删除成功"
  263. br.IsAddLog = true
  264. }
  265. // @Title 编辑话题接口
  266. // @Description 编辑话题接口
  267. // @Param request body aimod.TopicEditReq true "type json string"
  268. // @Success Ret=200 编辑成功
  269. // @router /topic/edit [post]
  270. func (this *AiController) TopicEdit() {
  271. br := new(models.BaseResponse).Init()
  272. defer func() {
  273. this.Data["json"] = br
  274. this.ServeJSON()
  275. }()
  276. sysUser := this.SysUser
  277. if sysUser == nil {
  278. br.Msg = "请登录"
  279. br.ErrMsg = "请登录,SysUser Is Empty"
  280. br.Ret = 408
  281. return
  282. }
  283. var req aimod.TopicEditReq
  284. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  285. if err != nil {
  286. br.Msg = "参数解析异常!"
  287. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  288. return
  289. }
  290. if req.AiChatTopicId <= 0 {
  291. br.Msg = "参数错误!"
  292. br.ErrMsg = "参数错误!AiChatTopicId:" + strconv.Itoa(req.AiChatTopicId)
  293. return
  294. }
  295. topic, err := aimod.GetAiChatTopicByTopicName(req.TopicName)
  296. if err != nil && err.Error() != utils.ErrNoRow() {
  297. br.Msg = "编辑失败!"
  298. br.ErrMsg = "获取数据失败!Err:" + err.Error()
  299. return
  300. }
  301. if topic != nil && topic.AiChatTopicId != req.AiChatTopicId {
  302. br.Msg = "话题名称已存在,请重新修改!"
  303. return
  304. }
  305. err = aimod.EditTopic(req.AiChatTopicId, req.TopicName)
  306. if err != nil {
  307. br.Msg = "编辑失败!"
  308. br.ErrMsg = "编辑失败,Err:" + err.Error()
  309. return
  310. }
  311. br.Ret = 200
  312. br.Success = true
  313. br.Msg = "编辑成功"
  314. br.IsAddLog = true
  315. }