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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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) GetAiType() string {
  37. return TEXTTOTEXT_AITYPE
  38. }
  39. func (tt *TextToText) SaveAiTask(id int64, adapterName string) error {
  40. if len(tt.cs) == 0 {
  41. clusterId := tt.opt.AiClusterIds[0]
  42. clusterName, _ := tt.storage.GetClusterNameById(tt.opt.AiClusterIds[0])
  43. err := tt.storage.SaveAiTask(id, tt.opt, adapterName, clusterId, clusterName, "", constants.Failed, "")
  44. if err != nil {
  45. return err
  46. }
  47. tt.storage.AddNoticeInfo(tt.opt.AdapterId, adapterName, "", "", tt.opt.TaskName, "failed", "任务失败")
  48. }
  49. for _, c := range tt.cs {
  50. clusterName, _ := tt.storage.GetClusterNameById(c.clusterId)
  51. err := tt.storage.SaveAiTask(id, tt.opt, adapterName, c.clusterId, clusterName, "", constants.Saved, "")
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }
  58. func filterClusters(opt *option.InferOption, storage *database.AiStorage, inferAdapter map[string]map[string]inference.ICluster) ([]*FilteredCluster, error) {
  59. var wg sync.WaitGroup
  60. var ch = make(chan *FilteredCluster, len(opt.AiClusterIds))
  61. var cs []*FilteredCluster
  62. inferMap := inferAdapter[opt.AdapterId]
  63. for _, clusterId := range opt.AiClusterIds {
  64. wg.Add(1)
  65. go func(cId string) {
  66. r := http.Request{}
  67. urls, err := inferMap[cId].GetInferUrl(r.Context(), opt)
  68. if err != nil {
  69. wg.Done()
  70. return
  71. }
  72. for i, _ := range urls {
  73. urls[i].Url = urls[i].Url + inference.FORWARD_SLASH + CHAT
  74. }
  75. clusterName, _ := storage.GetClusterNameById(cId)
  76. var f FilteredCluster
  77. f.urls = urls
  78. f.clusterId = cId
  79. f.clusterName = clusterName
  80. ch <- &f
  81. wg.Done()
  82. return
  83. }(clusterId)
  84. }
  85. wg.Wait()
  86. close(ch)
  87. for s := range ch {
  88. cs = append(cs, s)
  89. }
  90. return cs, nil
  91. }
  92. func (tt *TextToText) UpdateStatus(aiTaskList []*models.TaskAi, adapterName string) error {
  93. for i, t := range aiTaskList {
  94. if strconv.Itoa(int(t.ClusterId)) == tt.cs[i].clusterId {
  95. t.Status = constants.Completed
  96. t.EndTime = time.Now().Format(time.RFC3339)
  97. url := tt.cs[i].urls[0].Url
  98. t.InferUrl = url
  99. err := tt.storage.UpdateAiTask(t)
  100. if err != nil {
  101. logx.Errorf(err.Error())
  102. return err
  103. }
  104. }
  105. }
  106. tt.storage.AddNoticeInfo(tt.opt.AdapterId, adapterName, "", "", tt.opt.TaskName, "completed", "任务完成")
  107. return nil
  108. }

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.