user_chat_controller.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package llm
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/controllers/llm/llm_http"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/llm"
  8. llmService "eta/eta_api/services/llm"
  9. "eta/eta_api/utils"
  10. "time"
  11. )
  12. type UserChatController struct {
  13. controllers.BaseAuthController
  14. }
  15. // NewChat @Title 新建对话框
  16. // @Description 新建对话框
  17. // @Success 101 {object} response.ListResp
  18. // @router /chat/new_chat [post]
  19. func (ucCtrl *UserChatController) NewChat() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. ucCtrl.Data["json"] = br
  23. ucCtrl.ServeJSON()
  24. }()
  25. var req llm_http.UserChatReq
  26. err := json.Unmarshal(ucCtrl.Ctx.Input.RequestBody, &req)
  27. if err != nil {
  28. br.Msg = "参数解析异常!"
  29. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  30. return
  31. }
  32. sysUser := ucCtrl.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. br.Ret = 408
  37. return
  38. }
  39. if req.ChatTitle == "" {
  40. req.ChatTitle = "新会话"
  41. }
  42. session := llm.UserLlmChat{
  43. UserId: sysUser.AdminId,
  44. CreatedTime: time.Now(),
  45. ChatTitle: req.ChatTitle,
  46. }
  47. var chatResp = new(llm_http.UserChatResp)
  48. chatResp.ChatTitle = req.ChatTitle
  49. chatResp.ChatId, err = session.CreateChatSession()
  50. chatResp.SendTime = time.Now().Format(utils.FormatDateTime)
  51. if err != nil {
  52. br.Msg = "创建失败"
  53. br.ErrMsg = "创建失败,Err:" + err.Error()
  54. return
  55. }
  56. _ = llmService.AddChatRecord(&llm.UserChatRecordRedis{
  57. ChatId: session.Id,
  58. ChatUserType: "user",
  59. Content: req.ChatTitle,
  60. SendTime: chatResp.SendTime,
  61. })
  62. br.Data = chatResp
  63. br.Ret = 200
  64. br.Success = true
  65. br.Msg = "创建成功"
  66. }
  67. // RenameChat @Title 新建对话框
  68. // @Description 新建对话框
  69. // @Success 101 {object} response.ListResp
  70. // @router /chat/rename_chat [post]
  71. func (ucCtrl *UserChatController) RenameChat() {
  72. br := new(models.BaseResponse).Init()
  73. defer func() {
  74. ucCtrl.Data["json"] = br
  75. ucCtrl.ServeJSON()
  76. }()
  77. var req llm_http.UserChatReq
  78. err := json.Unmarshal(ucCtrl.Ctx.Input.RequestBody, &req)
  79. if err != nil {
  80. br.Msg = "参数解析异常!"
  81. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  82. return
  83. }
  84. sysUser := ucCtrl.SysUser
  85. if sysUser == nil {
  86. br.Msg = "请登录"
  87. br.ErrMsg = "请登录,SysUser Is Empty"
  88. br.Ret = 408
  89. return
  90. }
  91. if req.ChatId <= 0 {
  92. br.Msg = "非法的对话框Id"
  93. br.ErrMsg = "非法的对话框Id"
  94. return
  95. }
  96. if req.ChatTitle == "" {
  97. br.Msg = "重命名不能为空"
  98. br.ErrMsg = "重命名不能为空"
  99. return
  100. }
  101. session := llm.UserLlmChat{
  102. Id: req.ChatId,
  103. UpdateTime: time.Now(),
  104. UserId: sysUser.AdminId,
  105. ChatTitle: req.ChatTitle,
  106. }
  107. err = session.RenameChatSession()
  108. if err != nil {
  109. br.Msg = "重命名失败"
  110. br.ErrMsg = "重命名失败,Err:" + err.Error()
  111. return
  112. }
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "重命名成功"
  116. }
  117. // DeleteChat @Title 删除对话框
  118. // @Description 删除对话框
  119. // @Success 101 {object} response.ListResp
  120. // @router /chat/delete_chat [post]
  121. func (ucCtrl *UserChatController) DeleteChat() {
  122. br := new(models.BaseResponse).Init()
  123. defer func() {
  124. ucCtrl.Data["json"] = br
  125. ucCtrl.ServeJSON()
  126. }()
  127. var req llm_http.UserChatReq
  128. err := json.Unmarshal(ucCtrl.Ctx.Input.RequestBody, &req)
  129. if err != nil {
  130. br.Msg = "参数解析异常!"
  131. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  132. return
  133. }
  134. sysUser := ucCtrl.SysUser
  135. if sysUser == nil {
  136. br.Msg = "请登录"
  137. br.ErrMsg = "请登录,SysUser Is Empty"
  138. br.Ret = 408
  139. return
  140. }
  141. if req.ChatId <= 0 {
  142. br.Msg = "非法的对话框Id"
  143. br.ErrMsg = "非法的对话框Id"
  144. return
  145. }
  146. session := llm.UserLlmChat{
  147. Id: req.ChatId,
  148. UpdateTime: time.Now(),
  149. IsDeleted: 1,
  150. }
  151. err = session.DeleteChatSession()
  152. if err != nil {
  153. br.Msg = "删除失败"
  154. br.ErrMsg = "删除失败,Err:" + err.Error()
  155. return
  156. }
  157. br.Ret = 200
  158. br.Success = true
  159. br.Msg = "删除成功"
  160. }
  161. // GetUserChatList @Title 获取用户对话框列表
  162. // @Description 获取用户对话框列表
  163. // @Success 101 {object} response.ListResp
  164. // @router /chat/user_chat_list [get]
  165. func (ucCtrl *UserChatController) GetUserChatList() {
  166. br := new(models.BaseResponse).Init()
  167. defer func() {
  168. ucCtrl.Data["json"] = br
  169. ucCtrl.ServeJSON()
  170. }()
  171. sysUser := ucCtrl.SysUser
  172. if sysUser == nil {
  173. br.Msg = "请登录"
  174. br.ErrMsg = "请登录,SysUser Is Empty"
  175. br.Ret = 408
  176. return
  177. }
  178. //周日是0,周六是6
  179. weekDay := time.Now().Weekday()
  180. offset := int(time.Monday - weekDay)
  181. if offset > 0 {
  182. offset -= 7
  183. }
  184. today := time.Now().Format(utils.FormatDate)
  185. monDay := time.Now().AddDate(0, 0, offset).Format(utils.FormatDate)
  186. yesterday := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  187. chatList, err := llm.GetUserChatList(sysUser.AdminId, monDay, time.Now().Format(utils.FormatDate))
  188. if err != nil {
  189. br.Msg = "获取用户聊天列表失败"
  190. br.ErrMsg = "获取用户聊天列表失败,Err:" + err.Error()
  191. return
  192. }
  193. data := new(llm_http.UserChatListResp)
  194. data.WeekList = make([]llm.UserLlmChatListViewItem, 0)
  195. data.YesterdayList = make([]llm.UserLlmChatListViewItem, 0)
  196. data.TodayList = make([]llm.UserLlmChatListViewItem, 0)
  197. for _, v := range chatList {
  198. list, _ := llmService.GetChatRecordsFromRedis(v.Id)
  199. item := llm.CovertItemToView(v)
  200. item.RecordCount = len(list)
  201. if v.CreatedTime.Format(utils.FormatDate) == today {
  202. data.TodayList = append(data.TodayList, item)
  203. } else if v.CreatedTime.Format(utils.FormatDate) == yesterday {
  204. data.YesterdayList = append(data.YesterdayList, item)
  205. } else {
  206. data.WeekList = append(data.WeekList, item)
  207. }
  208. }
  209. br.Data = data
  210. br.Ret = 200
  211. br.Success = true
  212. br.Msg = "获取用户聊天列表成功"
  213. }
  214. // ChatRecordAdd @Title 保存聊天记录
  215. // @Description 保存聊天记录
  216. // @Success 101 {object} response.ListResp
  217. // @router /chat/chat_record_save [post]
  218. func (ucCtrl *UserChatController) ChatRecordAdd() {
  219. br := new(models.BaseResponse).Init()
  220. defer func() {
  221. ucCtrl.Data["json"] = br
  222. ucCtrl.ServeJSON()
  223. }()
  224. var req llm_http.UserChatRecordReq
  225. err := json.Unmarshal(ucCtrl.Ctx.Input.RequestBody, &req)
  226. if err != nil {
  227. br.Msg = "参数解析异常!"
  228. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  229. return
  230. }
  231. sysUser := ucCtrl.SysUser
  232. if sysUser == nil {
  233. br.Msg = "请登录"
  234. br.ErrMsg = "请登录,SysUser Is Empty"
  235. br.Ret = 408
  236. return
  237. }
  238. if req.ChatId <= 0 {
  239. br.Msg = "非法的对话框Id"
  240. br.ErrMsg = "非法的对话框Id"
  241. return
  242. }
  243. if req.Content == "" {
  244. br.Msg = "聊天记录不能为空"
  245. br.ErrMsg = "聊天记录不能为空"
  246. return
  247. }
  248. if req.Id < 0 {
  249. br.Msg = "非法的Id"
  250. br.ErrMsg = "非法的Id"
  251. return
  252. }
  253. if req.ChatUserType != "user" && req.ChatUserType != "assistant" {
  254. br.Msg = "非法的用户类型"
  255. br.ErrMsg = "非法的用户类型,用户类型支持:user/assistant"
  256. return
  257. }
  258. if req.SendTime == "" {
  259. req.SendTime = time.Now().Format(utils.FormatDateTime)
  260. } else {
  261. _, err = time.Parse(utils.FormatDateTime, req.SendTime)
  262. if err != nil {
  263. br.Msg = "非法的发送时间"
  264. br.ErrMsg = "非法的发送时间,Err:" + err.Error()
  265. return
  266. }
  267. }
  268. record := llm.UserChatRecordRedis{
  269. ChatId: req.ChatId,
  270. ChatUserType: req.ChatUserType,
  271. Content: req.Content,
  272. SendTime: req.SendTime,
  273. }
  274. err = llmService.AddChatRecord(&record)
  275. if err != nil {
  276. br.Msg = "添加聊天记录失败"
  277. br.ErrMsg = "添加聊天记录失败,Err:" + err.Error()
  278. return
  279. }
  280. br.Data = llm_http.UserChatAddResp{
  281. SendTime: record.SendTime,
  282. }
  283. br.Ret = 200
  284. br.Success = true
  285. br.Msg = "添加聊天记录成功"
  286. }
  287. // ChatRecordList @Title 获取聊天记录
  288. // @Description 获取聊天记录
  289. // @Success 101 {object} response.ListResp
  290. // @router /chat/chat_record_list [get]
  291. func (ucCtrl *UserChatController) ChatRecordList() {
  292. br := new(models.BaseResponse).Init()
  293. defer func() {
  294. ucCtrl.Data["json"] = br
  295. ucCtrl.ServeJSON()
  296. }()
  297. sysUser := ucCtrl.SysUser
  298. if sysUser == nil {
  299. br.Msg = "请登录"
  300. br.ErrMsg = "请登录,SysUser Is Empty"
  301. br.Ret = 408
  302. return
  303. }
  304. chatId, _ := ucCtrl.GetInt("ChatId", 0)
  305. if chatId <= 0 {
  306. br.Msg = "非法的对话Id"
  307. br.ErrMsg = "非法的对话Id"
  308. return
  309. }
  310. list, err := llmService.GetChatRecordsFromRedis(chatId)
  311. if err != nil {
  312. br.Msg = "获取聊天记录失败"
  313. br.ErrMsg = "获取聊天记录失败,Err:" + err.Error()
  314. return
  315. }
  316. br.Data = list
  317. br.Ret = 200
  318. br.Success = true
  319. br.Msg = "获取聊天记录成功"
  320. }