b_robot_config.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package internal
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/util/guid"
  6. "yx-dataset-server/app/errors"
  7. "yx-dataset-server/app/model"
  8. "yx-dataset-server/app/schema"
  9. "yx-dataset-server/library/ragflow"
  10. "yx-dataset-server/library/robot"
  11. )
  12. // NewRobotConfig 创建RobotConfig
  13. func NewRobotConfig(
  14. mRobotConfig model.IRobotConfig,
  15. mRobotDataset model.IRobotDataset,
  16. mTrans model.ITrans,
  17. mChatMessage model.IChatMessage,
  18. mChatAssistant model.IChatAssistant,
  19. mChatSession model.IChatSession,
  20. mChatDataset model.IChatDataset,
  21. mUser model.IUser,
  22. mDataset model.IDataset,
  23. ) *RobotConfig {
  24. return &RobotConfig{
  25. RobotConfigModel: mRobotConfig,
  26. robotDatasetModel: mRobotDataset,
  27. transModel: mTrans,
  28. chatMessageModel: mChatMessage,
  29. chatAssistantModel: mChatAssistant,
  30. chatSessionModel: mChatSession,
  31. chatDatasetModel: mChatDataset,
  32. userModel: mUser,
  33. datasetModel: mDataset,
  34. }
  35. }
  36. // RobotConfig 创建RobotConfig对象
  37. type RobotConfig struct {
  38. RobotConfigModel model.IRobotConfig
  39. robotDatasetModel model.IRobotDataset
  40. transModel model.ITrans
  41. chatMessageModel model.IChatMessage
  42. chatAssistantModel model.IChatAssistant
  43. chatSessionModel model.IChatSession
  44. chatDatasetModel model.IChatDataset
  45. userModel model.IUser
  46. datasetModel model.IDataset
  47. }
  48. // Query 查询数据
  49. func (a *RobotConfig) Query(ctx context.Context, params schema.RobotConfigQueryParam, opts ...schema.RobotConfigQueryOptions) (*schema.RobotConfigQueryResult, error) {
  50. return a.RobotConfigModel.Query(ctx, params, opts...)
  51. }
  52. // Get 查询指定数据
  53. func (a *RobotConfig) Get(ctx context.Context, recordID string, opts ...schema.RobotConfigQueryOptions) (*schema.RobotConfig, error) {
  54. item, err := a.RobotConfigModel.Get(ctx, recordID, opts...)
  55. if err != nil {
  56. return nil, err
  57. } else if item == nil {
  58. return nil, errors.ErrNotFound
  59. }
  60. return item, nil
  61. }
  62. func (a *RobotConfig) checkCode(ctx context.Context, code string) error {
  63. result, err := a.RobotConfigModel.Query(ctx, schema.RobotConfigQueryParam{
  64. Code: code,
  65. }, schema.RobotConfigQueryOptions{
  66. PageParam: &schema.PaginationParam{PageSize: -1},
  67. })
  68. if err != nil {
  69. return err
  70. } else if result.PageResult.Total > 0 {
  71. return errors.New400Response("编号已经存在")
  72. }
  73. return nil
  74. }
  75. func (a *RobotConfig) getUpdate(ctx context.Context, recordID string) (*schema.RobotConfig, error) {
  76. return a.Get(ctx, recordID)
  77. }
  78. // Create 创建数据
  79. func (a *RobotConfig) Create(ctx context.Context, item schema.RobotConfig) (*schema.RobotConfig, error) {
  80. item.RecordID = guid.S()
  81. item.CreatorId = GetUserID(ctx)
  82. item.AssistantId = guid.S()
  83. item.SessionId = guid.S()
  84. item.Webhook = fmt.Sprintf("%s%s", "http://113.128.186.214:6666/robot/v1/robots/receive/", item.RecordID)
  85. user := GetRootUser()
  86. var err error
  87. if !CheckIsRootUser(ctx) {
  88. user, err = a.userModel.Get(ctx, GetUserID(ctx))
  89. if err != nil {
  90. return nil, err
  91. }
  92. }
  93. // 创建对话助手及会话
  94. err = a.createAssistant(ctx, item.AssistantId, item.SessionId, *user, item)
  95. if err != nil {
  96. return nil, err
  97. }
  98. err = ExecTrans(ctx, a.transModel, func(ctx context.Context) error {
  99. if len(item.Datasets) > 0 {
  100. for _, dataset := range item.Datasets {
  101. err := a.robotDatasetModel.Create(ctx, schema.RobotDataset{
  102. RecordID: guid.S(),
  103. RobotId: item.RecordID,
  104. DatasetId: dataset.RecordID,
  105. })
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. return a.RobotConfigModel.Create(ctx, item)
  111. }
  112. return nil
  113. })
  114. if err != nil {
  115. return nil, err
  116. }
  117. return a.getUpdate(ctx, item.RecordID)
  118. }
  119. // Update 更新数据
  120. func (a *RobotConfig) Update(ctx context.Context, recordID string, item schema.RobotConfig) (*schema.RobotConfig, error) {
  121. oldItem, err := a.RobotConfigModel.Get(ctx, recordID)
  122. if err != nil {
  123. return nil, err
  124. } else if oldItem == nil {
  125. return nil, errors.ErrNotFound
  126. }
  127. err = a.RobotConfigModel.Update(ctx, recordID, item)
  128. if err != nil {
  129. return nil, err
  130. }
  131. return a.getUpdate(ctx, recordID)
  132. }
  133. // Delete 删除数据
  134. func (a *RobotConfig) Delete(ctx context.Context, recordID string) error {
  135. oldItem, err := a.RobotConfigModel.Get(ctx, recordID)
  136. if err != nil {
  137. return err
  138. } else if oldItem == nil {
  139. return errors.ErrNotFound
  140. }
  141. return a.RobotConfigModel.Delete(ctx, recordID)
  142. }
  143. // UpdateStatus 更新状态
  144. func (a *RobotConfig) UpdateStatus(ctx context.Context, recordID string, status int) error {
  145. oldItem, err := a.RobotConfigModel.Get(ctx, recordID)
  146. if err != nil {
  147. return err
  148. } else if oldItem == nil {
  149. return errors.ErrNotFound
  150. }
  151. return a.RobotConfigModel.UpdateStatus(ctx, recordID, status)
  152. }
  153. func (a *RobotConfig) ReceiveMessage(ctx context.Context, id string, message robot.DingTalkRobotMessage) error {
  154. robotConfig, err := a.RobotConfigModel.Get(ctx, id)
  155. if err != nil {
  156. return err
  157. }
  158. assistant, err := a.chatAssistantModel.Get(ctx, robotConfig.AssistantId)
  159. if err != nil {
  160. return err
  161. }
  162. session, err := a.chatSessionModel.Get(ctx, robotConfig.SessionId)
  163. if err != nil {
  164. return err
  165. }
  166. stream, err := ragflow.GetHttpClient().ChatCompletionsStream(ctx, assistant.RagChatId, &ragflow.ChatCompletionReq{
  167. Question: message.Text.Content,
  168. SessionID: session.RagSessionId,
  169. })
  170. if err != nil {
  171. return err
  172. }
  173. defer stream.Close() // 关闭流
  174. m := new(schema.ChatMessage)
  175. m.RecordID = guid.S()
  176. m.Question = message.Text.Content
  177. m.AssistantId = assistant.RecordID
  178. m.SessionId = session.RecordID
  179. m.UserId = GetUserID(ctx)
  180. m, err = CollectStreamDataAndCreate(ctx, stream, m)
  181. if err != nil {
  182. return err
  183. }
  184. err = a.chatMessageModel.Create(ctx, *m)
  185. if err != nil {
  186. return err
  187. }
  188. replyMsg := robot.DingReplyMsg{
  189. MsgType: "markdown",
  190. Markdown: robot.Markdown{
  191. Title: m.Question,
  192. Text: m.Answer,
  193. },
  194. }
  195. err = robot.SendReplyMsg(message.SessionWebhook, replyMsg)
  196. if err != nil {
  197. fmt.Println(err)
  198. return err
  199. }
  200. return nil
  201. }
  202. func (a *RobotConfig) createAssistant(ctx context.Context, assistantId, sessionId string, user schema.User, item schema.RobotConfig) error {
  203. err := ExecTrans(ctx, a.transModel, func(ctx context.Context) error {
  204. dataset, err := a.datasetModel.Query(ctx, schema.DatasetQueryParam{RecordIds: item.Datasets.ToRecordIds()})
  205. if err != nil {
  206. return err
  207. }
  208. c, err := ragflow.GetHttpClient().CreateChat(ctx, &ragflow.CreateChatReq{
  209. Name: fmt.Sprintf("%s%s", item.Name, "助手"),
  210. DatasetIDs: dataset.Data.ToRagDataIds(),
  211. })
  212. if err != nil {
  213. return err
  214. }
  215. err = a.chatAssistantModel.Create(ctx, schema.ChatAssistant{
  216. RecordID: assistantId,
  217. OrgId: user.OrgId,
  218. Name: fmt.Sprintf("%s%s", item.Name, "助手"),
  219. RagChatId: c.Data.ID,
  220. CreatorId: user.RecordID,
  221. })
  222. if err != nil {
  223. return err
  224. }
  225. if len(item.Datasets) > 0 {
  226. for _, v := range item.Datasets {
  227. err = a.chatDatasetModel.Create(ctx, schema.ChatDataset{
  228. RecordID: guid.S(),
  229. ChatAssistantId: assistantId,
  230. DatasetId: v.RecordID,
  231. })
  232. if err != nil {
  233. return err
  234. }
  235. }
  236. }
  237. s, err := ragflow.GetHttpClient().CreateSession(ctx, c.Data.ID, &ragflow.CreateSessionReq{
  238. Name: fmt.Sprintf("%s%s", item.Name, "会话"),
  239. })
  240. if err != nil {
  241. return err
  242. }
  243. err = a.chatSessionModel.Create(ctx, schema.ChatSession{
  244. RecordID: sessionId,
  245. Name: fmt.Sprintf("%s%s", item.Name, "会话"),
  246. AssistantId: assistantId,
  247. RagSessionId: s.Data.Id,
  248. CreatorId: user.RecordID,
  249. })
  250. return nil
  251. })
  252. return err
  253. }