ai.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
  48. err = global.DmSQL["ai"].Create(item).Error
  49. lastId = int64(item.AiChatTopicId)
  50. return
  51. }
  52. func AddAiChat(item *AiChat) (lastId int64, err error) {
  53. err = global.DmSQL["ai"].Create(item).Error
  54. lastId = int64(item.AiChatId)
  55. return
  56. }
  57. type AiChatTopicView struct {
  58. AiChatTopicId int `gorm:"primaryKey;column:ai_chat_topic_id;type:int(11);not null"`
  59. TopicName string `gorm:"column:topic_name;type:varchar(255);default:''"` // 话题名称
  60. CreateTime *global.LocalTime `gorm:"column:create_time;type:datetime"` // 创建时间
  61. ModifyTime *global.LocalTime `gorm:"column:modify_time;type:datetime"` // 修改时间
  62. AssistantId string `gorm:"column:assistant_id;type:varchar(255);default:''"` // 助手id
  63. ThreadId string `gorm:"column:thread_id;type:varchar(255);default:''"` // 进程id
  64. }
  65. func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
  66. sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
  67. err = global.DmSQL["ai"].Raw(sql, sysUserId).Find(&item).Error
  68. return
  69. }
  70. type AiChatTopicListResp struct {
  71. List []*AiChatTopicView
  72. }
  73. type AiChatView struct {
  74. AiChatId int `gorm:"primaryKey;column:ai_chat_id;type:int(11);not null"`
  75. AiChatTopicId int `gorm:"column:ai_chat_topic_id;type:int(11);default:0"`
  76. Ask string `gorm:"column:ask;type:text"` // 提问
  77. Answer string `gorm:"column:answer;type:text"` // 答案
  78. Model string `gorm:"column:model;type:varchar(20);default:''"` // 模型
  79. CreateTime string `gorm:"column:create_time;type:datetime"`
  80. ModifyTime string `gorm:"column:modify_time;type:datetime"`
  81. OpenaiFileId string `gorm:"column:openai_file_id;type:varchar(255);default:''"` // openai返回的文件id
  82. OpenaiFileName string `gorm:"column:openai_file_name;type:varchar(255);default:''"` // 文件名称
  83. OpenaiFilePath string `gorm:"column:openai_file_path;type:varchar(255);default:''"` // 文件路径
  84. }
  85. var ModelViewMap = map[string]string{
  86. "gpt-4-1106-preview": "GPT-4 Turbo",
  87. "moonshot-v1-32k": "Kimi",
  88. }
  89. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  90. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  91. err = global.DmSQL["ai"].Raw(sql, aiChatTopicId).Find(&item).Error
  92. return
  93. }
  94. type AiChatDetailResp struct {
  95. List []*AiChatView
  96. }
  97. type TopicDeleteReq struct {
  98. AiChatTopicId int `description:"主题id"`
  99. }
  100. func DeleteTopic(topicId int) (err error) {
  101. tx := global.DmSQL["ai"].Begin()
  102. defer func() {
  103. if err != nil {
  104. tx.Rollback()
  105. } else {
  106. tx.Commit()
  107. }
  108. }()
  109. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  110. err = tx.Exec(sql, topicId).Error
  111. if err != nil {
  112. return
  113. }
  114. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  115. err = tx.Exec(sql, topicId).Error
  116. if err != nil {
  117. return
  118. }
  119. return
  120. }
  121. type TopicEditReq struct {
  122. AiChatTopicId int `description:"主题id"`
  123. TopicName string `description:"主题名称"`
  124. }
  125. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  126. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  127. err = global.DmSQL["ai"].Raw(sql, topicName).First(&item).Error
  128. return
  129. }
  130. func EditTopic(topicId int, topicName string) (err error) {
  131. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  132. err = global.DmSQL["ai"].Exec(sql, topicName, topicId).Error
  133. return
  134. }
  135. func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
  136. sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  137. err = global.DmSQL["ai"].Raw(sql, obj.AiChatTopicId).Find(&item).Error
  138. return
  139. }
  140. type HistoryChat struct {
  141. Ask string
  142. Answer string
  143. OpenaiFileId []string `description:"文件ID"`
  144. }
  145. func (obj *AiChatTopic) UpdateAssistantAndThreadId(assistantId, threadId string, topicId int) (err error) {
  146. sql := `UPDATE ai_chat_topic SET assistant_id = ?, thread_id = ?, modify_time = NOW() WHERE ai_chat_topic_id = ?`
  147. err = global.DmSQL["ai"].Exec(sql, assistantId, threadId, topicId).Error
  148. return
  149. }