ai.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package aimod
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "time"
  6. )
  7. type AiChatTopic struct {
  8. AiChatTopicId int `orm:"column(ai_chat_topic_id);pk" gorm:"primaryKey"`
  9. TopicName string
  10. SysUserId int
  11. SysUserRealName string
  12. AssistantId string
  13. ThreadId string
  14. CreateTime time.Time
  15. ModifyTime time.Time
  16. }
  17. type AiChat struct {
  18. AiChatId int `orm:"column(ai_chat_id);pk" gorm:"primaryKey"`
  19. AiChatTopicId int
  20. Ask string
  21. AskUuid string
  22. Answer string
  23. Model string
  24. SysUserId int
  25. SysUserRealName string `description:"提问人名称"`
  26. OpenaiFileId string `description:"openai返回的文件id"`
  27. OpenaiFileName string `description:"文件名称"`
  28. OpenaiFilePath string `description:"文件路径"`
  29. CreateTime time.Time
  30. ModifyTime time.Time
  31. }
  32. type ChatReq struct {
  33. AiChatTopicId int `description:"主题id"`
  34. Ask string `description:"提问"`
  35. Model string `description:"模型"`
  36. }
  37. func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
  38. sql := `SELECT * FROM ai_chat WHERE ask_uuid=?`
  39. o := global.DbMap[utils.DbNameAI]
  40. err = o.Raw(sql, askUuid).First(&item).Error
  41. return
  42. }
  43. type ChatResp struct {
  44. AiChatTopicId int `description:"主题id"`
  45. Ask string `description:"提问"`
  46. Answer string `description:"回答"`
  47. Model string
  48. }
  49. // AddAiChatTopic 新增主题
  50. func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
  51. o := global.DbMap[utils.DbNameAI]
  52. err = o.Create(item).Error
  53. if err != nil {
  54. return
  55. }
  56. lastId = int64(item.AiChatTopicId)
  57. return
  58. }
  59. // AddAiChat 新增聊天
  60. func AddAiChat(item *AiChat) (lastId int64, err error) {
  61. o := global.DbMap[utils.DbNameAI]
  62. err = o.Create(item).Error
  63. if err != nil {
  64. return
  65. }
  66. lastId = int64(item.AiChatId)
  67. return
  68. }
  69. type AiChatTopicView struct {
  70. AiChatTopicId int `description:"主题id"`
  71. TopicName string `description:"主题名称"`
  72. CreateTime string `description:"创建时间"`
  73. ModifyTime string `description:"修改时间"`
  74. AssistantId string
  75. ThreadId string
  76. }
  77. func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
  78. sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
  79. o := global.DbMap[utils.DbNameAI]
  80. err = o.Raw(sql, sysUserId).Find(&item).Error
  81. return
  82. }
  83. type AiChatTopicListResp struct {
  84. List []*AiChatTopicView
  85. }
  86. type AiChatView struct {
  87. AiChatId int `description:"记录id"`
  88. AiChatTopicId int `description:"主题id"`
  89. Ask string `description:"提问"`
  90. Answer string `description:"答案"`
  91. Model string
  92. OpenaiFileId string `description:"文件ID"`
  93. OpenaiFileName string `description:"文件名称"`
  94. OpenaiFilePath string `description:"文件路径"`
  95. CreateTime string `description:"创建时间"`
  96. ModifyTime string `description:"修改时间"`
  97. }
  98. var ModelViewMap = map[string]string{
  99. "gpt-4-1106-preview": "GPT-4 Turbo",
  100. "moonshot-v1-32k": "Kimi",
  101. // "moonshot-v1-8k": "Kimi",
  102. }
  103. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  104. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  105. o := global.DbMap[utils.DbNameAI]
  106. err = o.Raw(sql, aiChatTopicId).Find(&item).Error
  107. return
  108. }
  109. type AiChatDetailResp struct {
  110. List []*AiChatView
  111. }
  112. type TopicDeleteReq struct {
  113. AiChatTopicId int `description:"主题id"`
  114. }
  115. func DeleteTopic(topicId int) (err error) {
  116. o := global.DbMap[utils.DbNameAI]
  117. tx := o.Begin()
  118. defer func() {
  119. if err != nil {
  120. tx.Rollback()
  121. } else {
  122. tx.Commit()
  123. }
  124. }()
  125. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  126. err = tx.Exec(sql, topicId).Error
  127. if err != nil {
  128. return err
  129. }
  130. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  131. err = tx.Exec(sql, topicId).Error
  132. if err != nil {
  133. return err
  134. }
  135. return err
  136. }
  137. type TopicEditReq struct {
  138. AiChatTopicId int `description:"主题id"`
  139. TopicName string `description:"主题名称"`
  140. }
  141. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  142. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  143. o := global.DbMap[utils.DbNameAI]
  144. err = o.Raw(sql, topicName).First(&item).Error
  145. return
  146. }
  147. func EditTopic(topicId int, topicName string) (err error) {
  148. o := global.DbMap[utils.DbNameAI]
  149. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  150. err = o.Exec(sql, topicName, topicId).Error
  151. return err
  152. }
  153. func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
  154. o := global.DbMap[utils.DbNameAI]
  155. sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  156. err = o.Raw(sql, obj.AiChatTopicId).First(&item).Error
  157. return
  158. }
  159. type HistoryChat struct {
  160. Ask string
  161. Answer string
  162. OpenaiFileId []string `description:"文件ID"`
  163. }
  164. // 修改
  165. func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
  166. to := global.DbMap[utils.DbNameAI]
  167. /*ptrStructOrTableName := "ai_chat_topic"
  168. qs := to.QueryTable(ptrStructOrTableName)
  169. for expr, exprV := range whereParam {
  170. qs = qs.Filter(expr, exprV)
  171. }
  172. err = qs.Update(updateParams)*/
  173. err = to.Model(obj).Where(whereParam).Updates(updateParams).Error
  174. return
  175. }