ai.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package aimod
  2. import (
  3. "eta_gn/eta_api/global"
  4. "time"
  5. )
  6. type AiChatTopic struct {
  7. AiChatTopicId int `gorm:"primaryKey;column:ai_chat_topic_id;type:int(11);not null"`
  8. TopicName string `gorm:"column:topic_name;type:varchar(255);default:''"` // 话题名称
  9. SysUserId int `gorm:"column:sys_user_id;type:int(11);default:0"` // 创建人id
  10. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(100);default:''"` // 创建人姓名
  11. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  12. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间
  13. AssistantId string `gorm:"column:assistant_id;type:varchar(255);default:''"` // 助手id
  14. ThreadId string `gorm:"column:thread_id;type:varchar(255);default:''"` // 进程id
  15. }
  16. type AiChat struct {
  17. AiChatId int `gorm:"primaryKey;column:ai_chat_id;type:int(11);not null"`
  18. AiChatTopicId int `gorm:"column:ai_chat_topic_id;type:int(11);default:0"`
  19. Ask string `gorm:"column:ask;type:text"` // 提问
  20. AskUuid string `gorm:"index:idx_ask_uuid;column:ask_uuid;type:varchar(50);default:''"` // ask,md5值
  21. Answer string `gorm:"column:answer;type:text"` // 答案
  22. SysUserId int `gorm:"column:sys_user_id;type:int(11);default:0"` // 提问人id
  23. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(100);default:''"` // 提问人名称
  24. Model string `gorm:"column:model;type:varchar(20);default:''"` // 模型
  25. CreateTime time.Time `gorm:"column:create_time;type:datetime"`
  26. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"`
  27. OpenaiFileId string `gorm:"column:openai_file_id;type:varchar(255);default:''"` // openai返回的文件id
  28. OpenaiFileName string `gorm:"column:openai_file_name;type:varchar(255);default:''"` // 文件名称
  29. OpenaiFilePath string `gorm:"column:openai_file_path;type:varchar(255);default:''"` // 文件路径
  30. }
  31. type ChatReq struct {
  32. AiChatTopicId int `description:"主题id"`
  33. Ask string `description:"提问"`
  34. Model string `description:"模型"`
  35. }
  36. func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
  37. sql := `SELECT * FROM ai_chat WHERE ask_uuid=?`
  38. err = global.DmSQL["ai"].Raw(sql, askUuid).First(&item).Error
  39. return
  40. }
  41. type ChatResp struct {
  42. AiChatTopicId int `description:"主题id"`
  43. Ask string `description:"提问"`
  44. Answer string `description:"回答"`
  45. Model string
  46. }
  47. // AddAiChatTopic 新增主题
  48. func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
  49. err = global.DmSQL["ai"].Create(item).Error
  50. lastId = int64(item.AiChatTopicId)
  51. return
  52. }
  53. // AddAiChat 新增聊天
  54. func AddAiChat(item *AiChat) (lastId int64, err error) {
  55. err = global.DmSQL["ai"].Create(item).Error
  56. lastId = int64(item.AiChatId)
  57. return
  58. }
  59. type AiChatTopicView struct {
  60. AiChatTopicId int `gorm:"primaryKey;column:ai_chat_topic_id;type:int(11);not null"`
  61. TopicName string `gorm:"column:topic_name;type:varchar(255);default:''"` // 话题名称
  62. CreateTime *global.LocalTime `gorm:"column:create_time;type:datetime"` // 创建时间
  63. ModifyTime *global.LocalTime `gorm:"column:modify_time;type:datetime"` // 修改时间
  64. AssistantId string `gorm:"column:assistant_id;type:varchar(255);default:''"` // 助手id
  65. ThreadId string `gorm:"column:thread_id;type:varchar(255);default:''"` // 进程id
  66. }
  67. func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
  68. sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
  69. err = global.DmSQL["ai"].Raw(sql, sysUserId).Find(&item).Error
  70. return
  71. }
  72. type AiChatTopicListResp struct {
  73. List []*AiChatTopicView
  74. }
  75. type AiChatView struct {
  76. AiChatId int `gorm:"primaryKey;column:ai_chat_id;type:int(11);not null"`
  77. AiChatTopicId int `gorm:"column:ai_chat_topic_id;type:int(11);default:0"`
  78. Ask string `gorm:"column:ask;type:text"` // 提问
  79. Answer string `gorm:"column:answer;type:text"` // 答案
  80. Model string `gorm:"column:model;type:varchar(20);default:''"` // 模型
  81. CreateTime string `gorm:"column:create_time;type:datetime"`
  82. ModifyTime string `gorm:"column:modify_time;type:datetime"`
  83. OpenaiFileId string `gorm:"column:openai_file_id;type:varchar(255);default:''"` // openai返回的文件id
  84. OpenaiFileName string `gorm:"column:openai_file_name;type:varchar(255);default:''"` // 文件名称
  85. OpenaiFilePath string `gorm:"column:openai_file_path;type:varchar(255);default:''"` // 文件路径
  86. }
  87. var ModelViewMap = map[string]string{
  88. "gpt-4-1106-preview": "GPT-4 Turbo",
  89. "moonshot-v1-32k": "Kimi",
  90. }
  91. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  92. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  93. err = global.DmSQL["ai"].Raw(sql, aiChatTopicId).Find(&item).Error
  94. return
  95. }
  96. type AiChatDetailResp struct {
  97. List []*AiChatView
  98. }
  99. type TopicDeleteReq struct {
  100. AiChatTopicId int `description:"主题id"`
  101. }
  102. func DeleteTopic(topicId int) (err error) {
  103. tx := global.DmSQL["ai"].Begin()
  104. defer func() {
  105. if err != nil {
  106. tx.Rollback()
  107. } else {
  108. tx.Commit()
  109. }
  110. }()
  111. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  112. err = tx.Exec(sql, topicId).Error
  113. if err != nil {
  114. return
  115. }
  116. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  117. err = tx.Exec(sql, topicId).Error
  118. if err != nil {
  119. return
  120. }
  121. return
  122. }
  123. type TopicEditReq struct {
  124. AiChatTopicId int `description:"主题id"`
  125. TopicName string `description:"主题名称"`
  126. }
  127. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  128. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  129. err = global.DmSQL["ai"].Raw(sql, topicName).First(&item).Error
  130. return
  131. }
  132. func EditTopic(topicId int, topicName string) (err error) {
  133. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  134. err = global.DmSQL["ai"].Exec(sql, topicName, topicId).Error
  135. return
  136. }
  137. func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
  138. sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  139. err = global.DmSQL["ai"].Raw(sql, obj.AiChatTopicId).Find(&item).Error
  140. return
  141. }
  142. type HistoryChat struct {
  143. Ask string
  144. Answer string
  145. OpenaiFileId []string `description:"文件ID"`
  146. }
  147. func (obj *AiChatTopic) UpdateAssistantAndThreadId(assistantId, threadId string, topicId int) (err error) {
  148. sql := `UPDATE ai_chat_topic SET assistant_id = ?, thread_id = ?, modify_time = NOW() WHERE ai_chat_topic_id = ?`
  149. err = global.DmSQL["ai"].Exec(sql, assistantId, threadId, topicId).Error
  150. return
  151. }