ai.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. CreateTime string `description:"创建时间"`
  85. ModifyTime string `description:"修改时间"`
  86. }
  87. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  88. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  89. o := orm.NewOrmUsingDB("ai")
  90. _, err = o.Raw(sql, aiChatTopicId).QueryRows(&item)
  91. return
  92. }
  93. type AiChatDetailResp struct {
  94. List []*AiChatView
  95. }
  96. type TopicDeleteReq struct {
  97. AiChatTopicId int `description:"主题id"`
  98. }
  99. func DeleteTopic(topicId int) (err error) {
  100. o := orm.NewOrmUsingDB("ai")
  101. tx, err := o.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.Raw(sql, topicId).Exec()
  111. if err != nil {
  112. return err
  113. }
  114. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  115. _, err = tx.Raw(sql, topicId).Exec()
  116. if err != nil {
  117. return err
  118. }
  119. return err
  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. o := orm.NewOrmUsingDB("ai")
  128. err = o.Raw(sql, topicName).QueryRow(&item)
  129. return
  130. }
  131. func EditTopic(topicId int, topicName string) (err error) {
  132. o := orm.NewOrmUsingDB("ai")
  133. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  134. _, err = o.Raw(sql, topicName, topicId).Exec()
  135. return err
  136. }
  137. func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
  138. o := orm.NewOrmUsingDB("ai")
  139. sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  140. err = o.Raw(sql, obj.AiChatTopicId).QueryRow(&item)
  141. return
  142. }
  143. type HistoryChat struct {
  144. Ask string
  145. Answer string
  146. }
  147. // 修改
  148. func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
  149. to := orm.NewOrm()
  150. ptrStructOrTableName := "ai_chat_topic"
  151. qs := to.QueryTable(ptrStructOrTableName)
  152. for expr, exprV := range whereParam {
  153. qs = qs.Filter(expr, exprV)
  154. }
  155. _, err = qs.Update(updateParams)
  156. return
  157. }