ai.go 8.6 KB

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