ai.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package aimod
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  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. }
  35. func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
  36. sql := `SELECT * FROM ai_chat WHERE ask_uuid=?`
  37. o := orm.NewOrmUsingDB("ai")
  38. err = o.Raw(sql, askUuid).QueryRow(&item)
  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. o := orm.NewOrmUsingDB("ai")
  50. lastId, err = o.Insert(item)
  51. return
  52. }
  53. // AddAiChat 新增聊天
  54. func AddAiChat(item *AiChat) (lastId int64, err error) {
  55. o := orm.NewOrmUsingDB("ai")
  56. lastId, err = o.Insert(item)
  57. return
  58. }
  59. type AiChatTopicView struct {
  60. AiChatTopicId int `description:"主题id"`
  61. TopicName string `description:"主题名称"`
  62. CreateTime string `description:"创建时间"`
  63. ModifyTime string `description:"修改时间"`
  64. AssistantId string
  65. ThreadId string
  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. o := orm.NewOrmUsingDB("ai")
  70. _, err = o.Raw(sql, sysUserId).QueryRows(&item)
  71. return
  72. }
  73. type AiChatTopicListResp struct {
  74. List []*AiChatTopicView
  75. }
  76. type AiChatView struct {
  77. AiChatId int `description:"记录id"`
  78. AiChatTopicId int `description:"主题id"`
  79. Ask string `description:"提问"`
  80. Answer string `description:"答案"`
  81. Model string
  82. OpenaiFileId string `description:"文件ID"`
  83. OpenaiFileName string `description:"文件名称"`
  84. OpenaiFilePath string `description:"文件路径"`
  85. CreateTime string `description:"创建时间"`
  86. ModifyTime string `description:"修改时间"`
  87. }
  88. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  89. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  90. o := orm.NewOrmUsingDB("ai")
  91. _, err = o.Raw(sql, aiChatTopicId).QueryRows(&item)
  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. o := orm.NewOrmUsingDB("ai")
  102. tx, err := o.Begin()
  103. defer func() {
  104. if err != nil {
  105. tx.Rollback()
  106. } else {
  107. tx.Commit()
  108. }
  109. }()
  110. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  111. _, err = tx.Raw(sql, topicId).Exec()
  112. if err != nil {
  113. return err
  114. }
  115. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  116. _, err = tx.Raw(sql, topicId).Exec()
  117. if err != nil {
  118. return err
  119. }
  120. return err
  121. }
  122. type TopicEditReq struct {
  123. AiChatTopicId int `description:"主题id"`
  124. TopicName string `description:"主题名称"`
  125. }
  126. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  127. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  128. o := orm.NewOrmUsingDB("ai")
  129. err = o.Raw(sql, topicName).QueryRow(&item)
  130. return
  131. }
  132. func EditTopic(topicId int, topicName string) (err error) {
  133. o := orm.NewOrmUsingDB("ai")
  134. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  135. _, err = o.Raw(sql, topicName, topicId).Exec()
  136. return err
  137. }
  138. func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
  139. o := orm.NewOrmUsingDB("ai")
  140. sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  141. err = o.Raw(sql, obj.AiChatTopicId).QueryRow(&item)
  142. return
  143. }
  144. type HistoryChat struct {
  145. Ask string
  146. Answer string
  147. OpenaiFileId []string `description:"文件ID"`
  148. }
  149. // 修改
  150. func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
  151. to := orm.NewOrmUsingDB("ai")
  152. ptrStructOrTableName := "ai_chat_topic"
  153. qs := to.QueryTable(ptrStructOrTableName)
  154. for expr, exprV := range whereParam {
  155. qs = qs.Filter(expr, exprV)
  156. }
  157. _, err = qs.Update(updateParams)
  158. return
  159. }