You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

textToText.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package textInference
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/schedulers/option"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/inference"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  9. "net/http"
  10. "strconv"
  11. "sync"
  12. "time"
  13. )
  14. const (
  15. CHAT = "chat"
  16. TEXTTOTEXT_AITYPE = "12"
  17. )
  18. type TextToText struct {
  19. opt *option.InferOption
  20. storage *database.AiStorage
  21. inferAdapter map[string]map[string]inference.ICluster
  22. cs []*FilteredCluster
  23. }
  24. func NewTextToText(opt *option.InferOption, storage *database.AiStorage, inferAdapter map[string]map[string]inference.ICluster) (*TextToText, error) {
  25. cs, err := filterClusters(opt, storage, inferAdapter)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &TextToText{
  30. opt: opt,
  31. storage: storage,
  32. inferAdapter: inferAdapter,
  33. cs: cs,
  34. }, nil
  35. }
  36. func (tt *TextToText) AppendRoute(urls []*inference.InferUrl) error {
  37. for i, _ := range urls {
  38. urls[i].Url = urls[i].Url + inference.FORWARD_SLASH + CHAT
  39. }
  40. return nil
  41. }
  42. func (tt *TextToText) AiType() string {
  43. return TEXTTOTEXT_AITYPE
  44. }
  45. func (tt *TextToText) SaveAiTask(id int64, adapterName string) error {
  46. if len(tt.cs) == 0 {
  47. clusterId := tt.opt.AiClusterIds[0]
  48. clusterName, _ := tt.storage.GetClusterNameById(tt.opt.AiClusterIds[0])
  49. err := tt.storage.SaveAiTask(id, tt.opt, adapterName, clusterId, clusterName, "", constants.Failed, "")
  50. if err != nil {
  51. return err
  52. }
  53. tt.storage.AddNoticeInfo(tt.opt.AdapterId, adapterName, "", "", tt.opt.TaskName, "failed", "任务失败")
  54. }
  55. for _, c := range tt.cs {
  56. clusterName, _ := tt.storage.GetClusterNameById(c.clusterId)
  57. err := tt.storage.SaveAiTask(id, tt.opt, adapterName, c.clusterId, clusterName, "", constants.Saved, "")
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }
  64. func filterClusters(opt *option.InferOption, storage *database.AiStorage, inferAdapter map[string]map[string]inference.ICluster) ([]*FilteredCluster, error) {
  65. var wg sync.WaitGroup
  66. var ch = make(chan *FilteredCluster, len(opt.AiClusterIds))
  67. var cs []*FilteredCluster
  68. inferMap := inferAdapter[opt.AdapterId]
  69. for _, clusterId := range opt.AiClusterIds {
  70. wg.Add(1)
  71. go func(cId string) {
  72. r := http.Request{}
  73. urls, err := inferMap[cId].GetInferUrl(r.Context(), opt)
  74. if err != nil {
  75. wg.Done()
  76. return
  77. }
  78. for i, _ := range urls {
  79. urls[i].Url = urls[i].Url + inference.FORWARD_SLASH + CHAT
  80. }
  81. clusterName, _ := storage.GetClusterNameById(cId)
  82. var f FilteredCluster
  83. f.urls = urls
  84. f.clusterId = cId
  85. f.clusterName = clusterName
  86. ch <- &f
  87. wg.Done()
  88. return
  89. }(clusterId)
  90. }
  91. wg.Wait()
  92. close(ch)
  93. for s := range ch {
  94. cs = append(cs, s)
  95. }
  96. return cs, nil
  97. }
  98. func (tt *TextToText) UpdateStatus(aiTaskList []*models.TaskAi, adapterName string) error {
  99. for i, t := range aiTaskList {
  100. if strconv.Itoa(int(t.ClusterId)) == tt.cs[i].clusterId {
  101. t.Status = constants.Completed
  102. t.EndTime = time.Now().Format(time.RFC3339)
  103. url := tt.cs[i].urls[0].Url
  104. t.InferUrl = url
  105. err := tt.storage.UpdateAiTask(t)
  106. if err != nil {
  107. logx.Errorf(err.Error())
  108. return err
  109. }
  110. }
  111. }
  112. tt.storage.AddNoticeInfo(tt.opt.AdapterId, adapterName, "", "", tt.opt.TaskName, "completed", "任务完成")
  113. return nil
  114. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.