ai.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. CreateTime time.Time
  12. ModifyTime time.Time
  13. }
  14. type AiChat struct {
  15. AiChatId int `orm:"column(ai_chat_id);pk"`
  16. AiChatTopicId int
  17. Ask string
  18. AskUuid string
  19. Answer string
  20. Model string
  21. SysUserId int
  22. SysUserRealName string
  23. CreateTime time.Time
  24. ModifyTime time.Time
  25. }
  26. type ChatReq struct {
  27. AiChatTopicId int `description:"主题id"`
  28. Model string `description:"模型:gpt-3.5-turbo,eta,gpt-3.5-turbo-16k"`
  29. Ask string `description:"提问"`
  30. }
  31. func GetAiChatByAsk(askUuid, model string) (item *AiChat, err error) {
  32. sql := `SELECT * FROM ai_chat WHERE ask_uuid=? AND model =? `
  33. o := orm.NewOrm()
  34. err = o.Raw(sql, askUuid, model).QueryRow(&item)
  35. return
  36. }
  37. type ChatResp struct {
  38. AiChatTopicId int `description:"主题id"`
  39. Ask string `description:"提问"`
  40. Answer string `description:"回答"`
  41. Model string
  42. }
  43. // AddAiChatTopic 新增主题
  44. func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
  45. o := orm.NewOrm()
  46. lastId, err = o.Insert(item)
  47. return
  48. }
  49. // AddAiChat 新增聊天
  50. func AddAiChat(item *AiChat) (lastId int64, err error) {
  51. o := orm.NewOrm()
  52. lastId, err = o.Insert(item)
  53. return
  54. }
  55. type AiChatTopicView struct {
  56. AiChatTopicId int `description:"主题id"`
  57. TopicName string `description:"主题名称"`
  58. CreateTime string `description:"创建时间"`
  59. ModifyTime string `description:"修改时间"`
  60. }
  61. func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
  62. sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
  63. o := orm.NewOrm()
  64. _, err = o.Raw(sql, sysUserId).QueryRows(&item)
  65. return
  66. }
  67. type AiChatTopicListResp struct {
  68. List []*AiChatTopicView
  69. }
  70. type AiChatView struct {
  71. AiChatId int `description:"记录id"`
  72. AiChatTopicId int `description:"主题id"`
  73. Ask string `description:"提问"`
  74. Answer string `description:"答案"`
  75. Model string
  76. CreateTime string `description:"创建时间"`
  77. ModifyTime string `description:"修改时间"`
  78. }
  79. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  80. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  81. o := orm.NewOrm()
  82. _, err = o.Raw(sql, aiChatTopicId).QueryRows(&item)
  83. return
  84. }
  85. type AiChatDetailResp struct {
  86. List []*AiChatView
  87. }
  88. type TopicDeleteReq struct {
  89. AiChatTopicId int `description:"主题id"`
  90. }
  91. func DeleteTopic(topicId int) (err error) {
  92. o := orm.NewOrm()
  93. tx, err := o.Begin()
  94. defer func() {
  95. if err != nil {
  96. tx.Rollback()
  97. } else {
  98. tx.Commit()
  99. }
  100. }()
  101. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  102. _, err = tx.Raw(sql, topicId).Exec()
  103. if err != nil {
  104. return err
  105. }
  106. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  107. _, err = tx.Raw(sql, topicId).Exec()
  108. if err != nil {
  109. return err
  110. }
  111. return err
  112. }
  113. type TopicEditReq struct {
  114. AiChatTopicId int `description:"主题id"`
  115. TopicName string `description:"主题名称"`
  116. }
  117. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  118. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  119. o := orm.NewOrm()
  120. err = o.Raw(sql, topicName).QueryRow(&item)
  121. return
  122. }
  123. func EditTopic(topicId int, topicName string) (err error) {
  124. o := orm.NewOrm()
  125. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  126. _, err = o.Raw(sql, topicName, topicId).Exec()
  127. return err
  128. }