Skip to content

Commit 4419236

Browse files
dsuhininDSuhinin
and
DSuhinin
authored
Fix build pipeline (#132)
Signed-off-by: Software Developer <[email protected]> Signed-off-by: dsuhinin <[email protected]> Co-authored-by: DSuhinin <[email protected]>
1 parent c585c8f commit 4419236

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sql
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/mlflow/mlflow-go-backend/pkg/contract"
8+
"github.com/mlflow/mlflow-go-backend/pkg/protos"
9+
)
10+
11+
// HandleResourceAlreadyExistError handles name conflicts in the Model Registry.
12+
func HandleResourceAlreadyExistError(name string, isExistingEntityPrompt, isNewEntityPrompt bool) *contract.Error {
13+
// Determine the entity types
14+
oldEntity := "Registered Model"
15+
if isExistingEntityPrompt {
16+
oldEntity = "Prompt"
17+
}
18+
19+
newEntity := "Registered Model"
20+
if isNewEntityPrompt {
21+
newEntity = "Prompt"
22+
}
23+
24+
// Check if there is a conflict between different entity types
25+
if oldEntity != newEntity {
26+
return contract.NewError(
27+
protos.ErrorCode_RESOURCE_ALREADY_EXISTS,
28+
fmt.Sprintf(
29+
"Tried to create a %s with name '%s', but the name is already taken by a %s. "+
30+
"MLflow does not allow creating a model and a prompt with the same name.",
31+
strings.ToLower(newEntity), name, strings.ToLower(oldEntity)),
32+
)
33+
}
34+
35+
// Raise an error if the entity already exists
36+
return contract.NewError(
37+
protos.ErrorCode_RESOURCE_ALREADY_EXISTS,
38+
fmt.Sprintf("%s (name=%s) already exists.", newEntity, name),
39+
)
40+
}

pkg/model_registry/store/sql/registered_models.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"github.com/mlflow/mlflow-go-backend/pkg/entities"
1616
"github.com/mlflow/mlflow-go-backend/pkg/model_registry/store/sql/models"
1717
"github.com/mlflow/mlflow-go-backend/pkg/protos"
18+
"github.com/mlflow/mlflow-go-backend/pkg/utils"
19+
)
20+
21+
const (
22+
IsPromptTagKey = "mlflow.prompt.is_prompt"
1823
)
1924

2025
func (m *ModelRegistrySQLStore) GetRegisteredModel(
@@ -251,9 +256,19 @@ func (m *ModelRegistrySQLStore) CreateRegisteredModel(
251256

252257
if err := m.db.WithContext(ctx).Create(&registeredModel).Error; err != nil {
253258
if errors.Is(err, gorm.ErrDuplicatedKey) {
254-
return nil, contract.NewError(
255-
protos.ErrorCode_RESOURCE_ALREADY_EXISTS,
256-
fmt.Sprintf("Registered Model (name=%s) already exists.", registeredModel.Name),
259+
existingRegisteredModel, err := m.GetRegisteredModel(ctx, name)
260+
if err != nil {
261+
return nil, err
262+
}
263+
264+
return nil, HandleResourceAlreadyExistError(
265+
name,
266+
utils.FindElementByProperty(existingRegisteredModel.Tags, func(tag *entities.RegisteredModelTag) bool {
267+
return tag.Key == IsPromptTagKey
268+
}),
269+
utils.FindElementByProperty(tags, func(tag *entities.RegisteredModelTag) bool {
270+
return tag.Key == IsPromptTagKey
271+
}),
257272
)
258273
}
259274

pkg/utils/slice.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package utils
2+
3+
// FindElementByProperty searches for an element in a slice based on a given match function.
4+
func FindElementByProperty[T any](items []T, matchFunc func(T) bool) bool {
5+
for _, item := range items {
6+
if matchFunc(item) {
7+
return true
8+
}
9+
}
10+
11+
return false
12+
}

0 commit comments

Comments
 (0)