image.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "yunion.io/x/jsonutils"
  7. "yunion.io/x/pkg/errors"
  8. "yunion.io/x/sqlchemy"
  9. identityapi "yunion.io/x/onecloud/pkg/apis/identity"
  10. api "yunion.io/x/onecloud/pkg/apis/llm"
  11. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  12. "yunion.io/x/onecloud/pkg/httperrors"
  13. "yunion.io/x/onecloud/pkg/llm/options"
  14. "yunion.io/x/onecloud/pkg/mcclient"
  15. "yunion.io/x/onecloud/pkg/mcclient/auth"
  16. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  17. )
  18. func init() {
  19. GetLLMImageManager()
  20. }
  21. var llmImageManager *SLLMImageManager
  22. func GetLLMImageManager() *SLLMImageManager {
  23. if llmImageManager != nil {
  24. return llmImageManager
  25. }
  26. llmImageManager = &SLLMImageManager{
  27. SSharableVirtualResourceBaseManager: db.NewSharableVirtualResourceBaseManager(
  28. SLLMImage{},
  29. "llm_images_tbl",
  30. "llm_image",
  31. "llm_images",
  32. ),
  33. }
  34. llmImageManager.SetVirtualObject(llmImageManager)
  35. return llmImageManager
  36. }
  37. type SLLMImageManager struct {
  38. db.SSharableVirtualResourceBaseManager
  39. }
  40. type SLLMImage struct {
  41. db.SSharableVirtualResourceBase
  42. ImageName string `width:"128" charset:"utf8" nullable:"false" list:"user" create:"admin_optional" update:"user"`
  43. ImageLabel string `width:"64" charset:"utf8" nullable:"false" list:"user" create:"admin_optional" update:"user"`
  44. CredentialId string `width:"128" charset:"utf8" nullable:"true" list:"user" create:"admin_optional" update:"user"`
  45. LLMType string `width:"128" charset:"ascii" nullable:"false" list:"user" create:"admin_optional" update:"user"`
  46. }
  47. func fetchImageCredential(ctx context.Context, userCred mcclient.TokenCredential, cid string) (*identityapi.CredentialDetails, error) {
  48. s := auth.GetSession(ctx, userCred, options.Options.Region)
  49. credJson, err := identity.Credentials.Get(s, cid, nil)
  50. if err != nil {
  51. return nil, errors.Wrap(err, "Credentials.Get")
  52. }
  53. details := identityapi.CredentialDetails{}
  54. err = credJson.Unmarshal(&details)
  55. if err != nil {
  56. return nil, errors.Wrap(err, "Unmarshal")
  57. }
  58. return &details, nil
  59. }
  60. func (man *SLLMImageManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input *api.LLMImageCreateInput) (*api.LLMImageCreateInput, error) {
  61. var err error
  62. input.SharableVirtualResourceCreateInput, err = man.SSharableVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.SharableVirtualResourceCreateInput)
  63. if nil != err {
  64. return input, errors.Wrap(err, "validate SharableVirtualResourceCreateInput")
  65. }
  66. if len(input.CredentialId) > 0 {
  67. cred, err := fetchImageCredential(ctx, userCred, input.CredentialId)
  68. if err != nil {
  69. return input, errors.Wrap(err, "fetchImageCredential")
  70. }
  71. input.CredentialId = cred.Id
  72. }
  73. if len(input.LLMType) > 0 {
  74. if !api.IsLLMImageType(input.LLMType) {
  75. return input, errors.Wrap(httperrors.ErrInputParameter, "llm_type must be one of "+strings.Join(api.LLM_IMAGE_TYPES.List(), ","))
  76. }
  77. }
  78. input.Status = api.STATUS_READY
  79. return input, nil
  80. }
  81. func (man *SLLMImageManager) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input *api.LLMImageUpdateInput) (*api.LLMImageUpdateInput, error) {
  82. var err error
  83. input.SharableVirtualResourceCreateInput, err = man.SSharableVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.SharableVirtualResourceCreateInput)
  84. if nil != err {
  85. return input, errors.Wrap(err, "validate SharableVirtualResourceCreateInput")
  86. }
  87. if nil != input.CredentialId && len(*input.CredentialId) > 0 {
  88. cred, err := fetchImageCredential(ctx, userCred, *input.CredentialId)
  89. if err != nil {
  90. return input, errors.Wrap(err, "fetchImageCredential")
  91. }
  92. input.CredentialId = &cred.Id
  93. }
  94. if nil != input.LLMType && len(*input.LLMType) > 0 {
  95. if !api.IsLLMImageType(*input.LLMType) {
  96. return input, errors.Wrap(httperrors.ErrInputParameter, "llm_type must be one of "+strings.Join(api.LLM_IMAGE_TYPES.List(), ","))
  97. }
  98. }
  99. return input, nil
  100. }
  101. func (man *SLLMImageManager) ListItemFilter(
  102. ctx context.Context,
  103. q *sqlchemy.SQuery,
  104. userCred mcclient.TokenCredential,
  105. input api.LLMImageListInput,
  106. ) (*sqlchemy.SQuery, error) {
  107. q, err := man.SSharableVirtualResourceBaseManager.ListItemFilter(ctx, q, userCred, input.SharableVirtualResourceListInput)
  108. if err != nil {
  109. return nil, errors.Wrapf(err, "SSharableBaseResourceManager.ListItemFilter")
  110. }
  111. if input.IsPublic != nil {
  112. if *input.IsPublic {
  113. q = q.IsTrue("is_public")
  114. } else {
  115. q = q.IsFalse("is_public")
  116. }
  117. }
  118. if len(input.ImageLabel) > 0 {
  119. q = q.Equals("image_label", input.ImageLabel)
  120. }
  121. if len(input.ImageName) > 0 {
  122. q = q.Equals("image_name", input.ImageName)
  123. }
  124. if len(input.LLMType) > 0 {
  125. q = q.Equals("llm_type", input.LLMType)
  126. }
  127. return q, nil
  128. }
  129. func (image *SLLMImage) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
  130. for _, field := range []string{"llm_image_id"} {
  131. count, err := GetLLMManager().Query().Equals(field, image.Id).CountWithError()
  132. if err != nil {
  133. return errors.Wrap(err, "fetch llms")
  134. }
  135. if count > 0 {
  136. return errors.Wrapf(errors.ErrNotSupported, "This image is currently in use by %s in llms", field)
  137. }
  138. count, err = GetLLMSkuManager().Query().Equals("llm_image_id", image.Id).CountWithError()
  139. if err != nil {
  140. return errors.Wrap(err, "fetch llm models")
  141. }
  142. if count > 0 {
  143. return errors.Wrapf(errors.ErrNotSupported, "This image is currently in use by %s in llm models", field)
  144. }
  145. }
  146. return nil
  147. }
  148. func (image *SLLMImage) ToContainerImage() string {
  149. return fmt.Sprintf("%s:%s", image.ImageName, image.ImageLabel)
  150. }