ai.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package aimod
  2. import (
  3. "eta/eta_api/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type AiChatTopic struct {
  8. AiChatTopicId int `orm:"column(ai_chat_topic_id);pk;auto"`
  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. func (obj *AiChatTopic) AiChatTopicView() *AiChatTopicView {
  18. return &AiChatTopicView{
  19. AiChatTopicId: obj.AiChatTopicId,
  20. TopicName: obj.TopicName,
  21. CreateTime: obj.CreateTime.Format(utils.FormatDateTime),
  22. ModifyTime: obj.ModifyTime.Format(utils.FormatDateTime),
  23. AssistantId: obj.AssistantId,
  24. ThreadId: obj.ThreadId,
  25. }
  26. }
  27. func AiChatTopicViewList(topics []*AiChatTopic) (itemList []*AiChatTopicView) {
  28. for _, item := range topics {
  29. itemList = append(itemList, item.AiChatTopicView())
  30. }
  31. return
  32. }
  33. type AiChat struct {
  34. AiChatId int `orm:"column(ai_chat_id);pk;auto"`
  35. AiChatTopicId int
  36. Ask string
  37. AskUuid string
  38. Answer string
  39. Model string
  40. SysUserId int
  41. SysUserRealName string `description:"提问人名称"`
  42. OpenaiFileId string `description:"openai返回的文件id"`
  43. OpenaiFileName string `description:"文件名称"`
  44. OpenaiFilePath string `description:"文件路径"`
  45. CreateTime time.Time
  46. ModifyTime time.Time
  47. }
  48. func (obj *AiChat) AiChatView() *AiChatView {
  49. return &AiChatView{
  50. AiChatId: obj.AiChatId,
  51. AiChatTopicId: obj.AiChatTopicId,
  52. Ask: obj.Ask,
  53. Answer: obj.Answer,
  54. Model: obj.Model,
  55. OpenaiFileId: obj.OpenaiFileId,
  56. OpenaiFileName: obj.OpenaiFileName,
  57. OpenaiFilePath: obj.OpenaiFilePath,
  58. CreateTime: obj.CreateTime.Format(utils.FormatDateTime),
  59. ModifyTime: obj.ModifyTime.Format(utils.FormatDateTime),
  60. }
  61. }
  62. func AiChatViewList(chats []*AiChat) (itemList []*AiChatView) {
  63. for _, item := range chats {
  64. itemList = append(itemList, item.AiChatView())
  65. }
  66. return
  67. }
  68. type ChatReq struct {
  69. AiChatTopicId int `description:"主题id"`
  70. Ask string `description:"提问"`
  71. Model string `description:"模型"`
  72. }
  73. func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
  74. sql := `SELECT * FROM ai_chat WHERE ask_uuid=?`
  75. o := orm.NewOrmUsingDB("ai")
  76. err = o.Raw(sql, askUuid).QueryRow(&item)
  77. return
  78. }
  79. type ChatResp struct {
  80. AiChatTopicId int `description:"主题id"`
  81. Ask string `description:"提问"`
  82. Answer string `description:"回答"`
  83. Model string
  84. }
  85. // AddAiChatTopic 新增主题
  86. func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
  87. o := orm.NewOrmUsingDB("ai")
  88. lastId, err = o.Insert(item)
  89. return
  90. }
  91. // AddAiChat 新增聊天
  92. func AddAiChat(item *AiChat) (lastId int64, err error) {
  93. o := orm.NewOrmUsingDB("ai")
  94. lastId, err = o.Insert(item)
  95. return
  96. }
  97. type AiChatTopicView struct {
  98. AiChatTopicId int `description:"主题id"`
  99. TopicName string `description:"主题名称"`
  100. CreateTime string `description:"创建时间"`
  101. ModifyTime string `description:"修改时间"`
  102. AssistantId string
  103. ThreadId string
  104. }
  105. // [2025-zsh-时间类型修复-chenhan]
  106. func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
  107. var dbItemList []*AiChatTopic
  108. sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
  109. o := orm.NewOrmUsingDB("ai")
  110. _, err = o.Raw(sql, sysUserId).QueryRows(&dbItemList)
  111. if err != nil {
  112. return
  113. }
  114. item = AiChatTopicViewList(dbItemList)
  115. return
  116. }
  117. type AiChatTopicListResp struct {
  118. List []*AiChatTopicView
  119. }
  120. type AiChatView struct {
  121. AiChatId int `description:"记录id"`
  122. AiChatTopicId int `description:"主题id"`
  123. Ask string `description:"提问"`
  124. Answer string `description:"答案"`
  125. Model string
  126. OpenaiFileId string `description:"文件ID"`
  127. OpenaiFileName string `description:"文件名称"`
  128. OpenaiFilePath string `description:"文件路径"`
  129. CreateTime string `description:"创建时间"`
  130. ModifyTime string `description:"修改时间"`
  131. }
  132. var ModelViewMap = map[string]string{
  133. "gpt-4-1106-preview": "GPT-4 Turbo",
  134. "moonshot-v1-32k": "Kimi",
  135. // "moonshot-v1-8k": "Kimi",
  136. }
  137. // [2025-zsh-时间类型修复-chenhan]
  138. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  139. var dbItemList []*AiChat
  140. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  141. o := orm.NewOrmUsingDB("ai")
  142. _, err = o.Raw(sql, aiChatTopicId).QueryRows(&dbItemList)
  143. if err != nil {
  144. return
  145. }
  146. item = AiChatViewList(dbItemList)
  147. return
  148. }
  149. type AiChatDetailResp struct {
  150. List []*AiChatView
  151. }
  152. type TopicDeleteReq struct {
  153. AiChatTopicId int `description:"主题id"`
  154. }
  155. func DeleteTopic(topicId int) (err error) {
  156. o := orm.NewOrmUsingDB("ai")
  157. tx, err := o.Begin()
  158. defer func() {
  159. if err != nil {
  160. tx.Rollback()
  161. } else {
  162. tx.Commit()
  163. }
  164. }()
  165. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  166. _, err = tx.Raw(sql, topicId).Exec()
  167. if err != nil {
  168. return err
  169. }
  170. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  171. _, err = tx.Raw(sql, topicId).Exec()
  172. if err != nil {
  173. return err
  174. }
  175. return err
  176. }
  177. type TopicEditReq struct {
  178. AiChatTopicId int `description:"主题id"`
  179. TopicName string `description:"主题名称"`
  180. }
  181. // [2025-zsh-时间类型修复-chenhan]
  182. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  183. var dbItem *AiChatTopic
  184. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  185. o := orm.NewOrmUsingDB("ai")
  186. err = o.Raw(sql, topicName).QueryRow(&dbItem)
  187. if err != nil {
  188. return
  189. }
  190. item = dbItem.AiChatTopicView()
  191. return
  192. }
  193. func EditTopic(topicId int, topicName string) (err error) {
  194. o := orm.NewOrmUsingDB("ai")
  195. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  196. _, err = o.Raw(sql, topicName, topicId).Exec()
  197. return err
  198. }
  199. // [2025-zsh-时间类型修复-chenhan]
  200. func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
  201. var dbItem *AiChatTopic
  202. o := orm.NewOrmUsingDB("ai")
  203. sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  204. err = o.Raw(sql, obj.AiChatTopicId).QueryRow(&dbItem)
  205. if err != nil {
  206. return
  207. }
  208. item = dbItem.AiChatTopicView()
  209. return
  210. }
  211. type HistoryChat struct {
  212. Ask string
  213. Answer string
  214. OpenaiFileId []string `description:"文件ID"`
  215. }
  216. // 修改
  217. func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
  218. to := orm.NewOrmUsingDB("ai")
  219. ptrStructOrTableName := "ai_chat_topic"
  220. qs := to.QueryTable(ptrStructOrTableName)
  221. for expr, exprV := range whereParam {
  222. qs = qs.Filter(expr, exprV)
  223. }
  224. _, err = qs.Update(updateParams)
  225. return
  226. }