ai.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package ai
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/aimod"
  7. "eta_gn/eta_api/services/aiser"
  8. "eta_gn/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 && !utils.IsErrNoRow(err) {
  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 && !utils.IsErrNoRow(err) {
  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. name, err := aiser.QueryAskContent(req.Ask)
  107. if err != nil {
  108. br.Msg = "获取数据失败!"
  109. br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error()
  110. return
  111. }
  112. topic := new(aimod.AiChatTopic)
  113. topic.TopicName = name
  114. topic.SysUserId = this.SysUser.AdminId
  115. topic.SysUserRealName = this.SysUser.RealName
  116. topic.CreateTime = time.Now()
  117. topic.ModifyTime = time.Now()
  118. topicId, err := aimod.AddAiChatTopic(topic)
  119. if err != nil {
  120. br.Msg = "获取数据失败!"
  121. br.ErrMsg = "生成话题失败,Err:" + err.Error()
  122. return
  123. }
  124. resp.AiChatTopicId = int(topicId)
  125. chatItem := new(aimod.AiChat)
  126. chatItem.AiChatTopicId = resp.AiChatTopicId
  127. chatItem.Ask = req.Ask
  128. chatItem.AskUuid = utils.MD5(req.Ask)
  129. chatItem.Answer = answer
  130. chatItem.Model = EnabledModelsForMap[req.Model]
  131. chatItem.SysUserId = this.SysUser.AdminId
  132. chatItem.SysUserRealName = this.SysUser.RealName
  133. chatItem.CreateTime = time.Now()
  134. chatItem.ModifyTime = time.Now()
  135. _, err = aimod.AddAiChat(chatItem)
  136. if err != nil {
  137. br.Msg = "获取数据失败!"
  138. br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
  139. return
  140. }
  141. } else {
  142. resp.AiChatTopicId = req.AiChatTopicId
  143. chatItem := new(aimod.AiChat)
  144. chatItem.AiChatTopicId = resp.AiChatTopicId
  145. chatItem.Ask = req.Ask
  146. chatItem.AskUuid = utils.MD5(req.Ask)
  147. chatItem.Answer = answer
  148. chatItem.Model = EnabledModelsForMap[req.Model]
  149. chatItem.SysUserId = this.SysUser.AdminId
  150. chatItem.SysUserRealName = this.SysUser.RealName
  151. chatItem.CreateTime = time.Now()
  152. chatItem.ModifyTime = time.Now()
  153. _, err = aimod.AddAiChat(chatItem)
  154. if err != nil {
  155. br.Msg = "获取数据失败!"
  156. br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
  157. return
  158. }
  159. }
  160. br.Ret = 200
  161. br.Success = true
  162. br.Msg = "获取成功"
  163. br.Data = resp
  164. }
  165. // @Title 获取话题列表
  166. // @Description 获取话题列表接口
  167. // @Success 200 {object} aimod.AiChatTopicListResp
  168. // @router /topic/list [get]
  169. func (this *AiController) TopicList() {
  170. br := new(models.BaseResponse).Init()
  171. defer func() {
  172. this.Data["json"] = br
  173. this.ServeJSON()
  174. }()
  175. sysUser := this.SysUser
  176. if sysUser == nil {
  177. br.Msg = "请登录"
  178. br.ErrMsg = "请登录,SysUser Is Empty"
  179. br.Ret = 408
  180. return
  181. }
  182. list, err := aimod.GetAiChatTopicList(sysUser.AdminId)
  183. if err != nil {
  184. br.Msg = "获取数据失败!"
  185. br.ErrMsg = "获取主题记录信息失败,Err:" + err.Error()
  186. return
  187. }
  188. resp := new(aimod.AiChatTopicListResp)
  189. resp.List = list
  190. br.Ret = 200
  191. br.Success = true
  192. br.Msg = "获取成功"
  193. br.Data = resp
  194. }
  195. // @Title 获取话题详情
  196. // @Description 获取话题详情接口
  197. // @Param AiChatTopicId query int true "主题id"
  198. // @Success 200 {object} aimod.AiChatDetailResp
  199. // @router /topic/detail [get]
  200. func (this *AiController) TopicDetail() {
  201. br := new(models.BaseResponse).Init()
  202. defer func() {
  203. this.Data["json"] = br
  204. this.ServeJSON()
  205. }()
  206. sysUser := this.SysUser
  207. if sysUser == nil {
  208. br.Msg = "请登录"
  209. br.ErrMsg = "请登录,SysUser Is Empty"
  210. br.Ret = 408
  211. return
  212. }
  213. aiChatTopicId, _ := this.GetInt("AiChatTopicId")
  214. list, err := aimod.GetAiChatList(aiChatTopicId)
  215. if err != nil {
  216. br.Msg = "获取数据失败!"
  217. br.ErrMsg = "获取主题记录信息失败,Err:" + err.Error()
  218. return
  219. }
  220. for index := range list {
  221. list[index].Model = aimod.ModelViewMap[list[index].Model]
  222. }
  223. resp := new(aimod.AiChatDetailResp)
  224. resp.List = list
  225. br.Ret = 200
  226. br.Success = true
  227. br.Msg = "获取成功"
  228. br.Data = resp
  229. }
  230. // @Title 删除话题接口
  231. // @Description 删除话题接口
  232. // @Param request body aimod.TopicDeleteReq true "type json string"
  233. // @Success Ret=200 删除成功
  234. // @router /topic/delete [post]
  235. func (this *AiController) TopicDelete() {
  236. br := new(models.BaseResponse).Init()
  237. defer func() {
  238. this.Data["json"] = br
  239. this.ServeJSON()
  240. }()
  241. sysUser := this.SysUser
  242. if sysUser == nil {
  243. br.Msg = "请登录"
  244. br.ErrMsg = "请登录,SysUser Is Empty"
  245. br.Ret = 408
  246. return
  247. }
  248. var req aimod.TopicDeleteReq
  249. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  250. if err != nil {
  251. br.Msg = "参数解析异常!"
  252. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  253. return
  254. }
  255. if req.AiChatTopicId <= 0 {
  256. br.Msg = "参数错误!"
  257. br.ErrMsg = "参数错误!AiChatTopicId:" + strconv.Itoa(req.AiChatTopicId)
  258. return
  259. }
  260. err = aimod.DeleteTopic(req.AiChatTopicId)
  261. if err != nil {
  262. br.Msg = "删除失败!"
  263. br.ErrMsg = "删除失败,Err:" + err.Error()
  264. return
  265. }
  266. br.Ret = 200
  267. br.Success = true
  268. br.Msg = "删除成功"
  269. br.IsAddLog = true
  270. }
  271. // @Title 编辑话题接口
  272. // @Description 编辑话题接口
  273. // @Param request body aimod.TopicEditReq true "type json string"
  274. // @Success Ret=200 编辑成功
  275. // @router /topic/edit [post]
  276. func (this *AiController) TopicEdit() {
  277. br := new(models.BaseResponse).Init()
  278. defer func() {
  279. this.Data["json"] = br
  280. this.ServeJSON()
  281. }()
  282. sysUser := this.SysUser
  283. if sysUser == nil {
  284. br.Msg = "请登录"
  285. br.ErrMsg = "请登录,SysUser Is Empty"
  286. br.Ret = 408
  287. return
  288. }
  289. var req aimod.TopicEditReq
  290. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  291. if err != nil {
  292. br.Msg = "参数解析异常!"
  293. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  294. return
  295. }
  296. if req.AiChatTopicId <= 0 {
  297. br.Msg = "参数错误!"
  298. br.ErrMsg = "参数错误!AiChatTopicId:" + strconv.Itoa(req.AiChatTopicId)
  299. return
  300. }
  301. topic, err := aimod.GetAiChatTopicByTopicName(req.TopicName)
  302. if err != nil && !utils.IsErrNoRow(err) {
  303. br.Msg = "编辑失败!"
  304. br.ErrMsg = "获取数据失败!Err:" + err.Error()
  305. return
  306. }
  307. if topic != nil && topic.AiChatTopicId > 0 && topic.AiChatTopicId != req.AiChatTopicId {
  308. br.Msg = "话题名称已存在,请重新修改!"
  309. return
  310. }
  311. err = aimod.EditTopic(req.AiChatTopicId, req.TopicName)
  312. if err != nil {
  313. br.Msg = "编辑失败!"
  314. br.ErrMsg = "编辑失败,Err:" + err.Error()
  315. return
  316. }
  317. br.Ret = 200
  318. br.Success = true
  319. br.Msg = "编辑成功"
  320. br.IsAddLog = true
  321. }