upload_agent_data_logic.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package agent
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "fmt"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "mime/multipart"
  8. agentModel "wechat-api/ent/agent"
  9. "wechat-api/hook/fastgpt"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type UploadAgentDataLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewUploadAgentDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadAgentDataLogic {
  21. return &UploadAgentDataLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *UploadAgentDataLogic) UploadAgentData(req *types.UploadDataReq, file multipart.File, agentId uint64) (*types.BaseDataInfo, error) {
  27. var count uint64
  28. reader := csv.NewReader(file)
  29. records, err := reader.ReadAll()
  30. if err != nil {
  31. return nil, err
  32. }
  33. agent, err := l.svcCtx.DB.Agent.Query().Where(agentModel.ID(agentId)).Only(l.ctx)
  34. if err != nil {
  35. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  36. }
  37. //TODO
  38. token := agent.CollectionID
  39. var params fastgpt.CreateBulkDataReq
  40. params.CollectionID = agent.CollectionID
  41. params.TrainingMode = "chunk"
  42. qas := make([]fastgpt.DataQuestion, 0, 100)
  43. for idx, record := range records {
  44. // 第一行标题内容过滤
  45. //fmt.Printf("idx=%v, q=%v, a=%v \n", idx, record[0], record[1])
  46. //fmt.Println(idx == 0, record[0] == "问题", record[1] == "答案")
  47. //fmt.Printf("record=%+v\n", record)
  48. if idx == 0 && record[1] == "答案" {
  49. continue
  50. }
  51. // 空内容过滤
  52. if record[0] == "" || record[1] == "" {
  53. continue
  54. }
  55. qas = append(qas, fastgpt.DataQuestion{
  56. Q: record[0],
  57. A: record[1],
  58. })
  59. if idx%100 == 0 && len(qas) > 0 {
  60. params.Data = qas
  61. response, err := fastgpt.CreateBulkData(&params)
  62. if err != nil {
  63. l.Logger.Errorf("batch insert data to fastgpt failed. collection=%s error=%s", agent.CollectionID, err.Error())
  64. return nil, err
  65. }
  66. count += response.Data.InsertLen
  67. qas = make([]fastgpt.DataQuestion, 0, 100)
  68. }
  69. }
  70. if len(qas) > 0 {
  71. params.Data = qas
  72. response, err := fastgpt.DiyCreateBulkData(token, &params)
  73. if err != nil {
  74. l.Logger.Errorf("batch insert data to fastgpt failed. collection=%s error=%s", agent.CollectionID, err.Error())
  75. return nil, err
  76. }
  77. count += response.Data.InsertLen
  78. }
  79. resp := &types.BaseDataInfo{}
  80. resp.Code = 0
  81. resp.Msg = errormsg.Success
  82. resp.Data = fmt.Sprintf("upload %d rows", count)
  83. return resp, nil
  84. }