ai.go 4.9 KB

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