Skip to content

Commit 72e4c40

Browse files
committed
expose the actual model name instead of the resource name for the /v1/models endpoint
1 parent 77ae8d9 commit 72e4c40

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

maas-api/internal/handlers/models_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"testing"
8+
"time"
89

910
"github.com/openai/openai-go/v2"
1011
"github.com/openai/openai-go/v2/packages/pagination"
@@ -13,6 +14,7 @@ import (
1314
"github.com/opendatahub-io/maas-billing/maas-api/test/fixtures"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
17+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1618
"knative.dev/pkg/apis"
1719
)
1820

@@ -51,6 +53,33 @@ func TestListingModels(t *testing.T) {
5153
}
5254
llmInferenceServices := fixtures.CreateLLMInferenceServices(llmTestScenarios...)
5355

56+
//Add a model where .spec.model.name is unset; should default to metadata.name
57+
unsetSpecModelName := "unset-spec-model-name"
58+
unsetSpecModelNameNamespace := fixtures.TestNamespace
59+
60+
unsetSpecModelNameISVC := unstructured.Unstructured{
61+
Object: map[string]any{
62+
"apiVersion": "serving.kserve.io/v1beta1",
63+
"kind": "InferenceService",
64+
"metadata": map[string]any{
65+
"name": unsetSpecModelName,
66+
"namespace": unsetSpecModelNameNamespace,
67+
"creationTimestamp": time.Now().UTC().Format(time.RFC3339),
68+
},
69+
"spec": map[string]any{
70+
"model": map[string]any{
71+
//left out the "name" key
72+
},
73+
},
74+
"status": map[string]any{
75+
"url": "http://" + unsetSpecModelName + "." + unsetSpecModelNameNamespace + ".acme.com/v1",
76+
},
77+
},
78+
}
79+
80+
// Append the model to the objects used by the fake server
81+
llmInferenceServices = append(llmInferenceServices, unsetSpecModelNameISVC)
82+
5483
config := fixtures.TestServerConfig{
5584
Objects: llmInferenceServices,
5685
}
@@ -102,6 +131,20 @@ func TestListingModels(t *testing.T) {
102131
})
103132
}
104133

134+
//After loop that builds testCases from llmTestScenarios completes, append an expectedModel for the unsetSpecModelName case:
135+
testCases = append(testCases, expectedModel{
136+
name: unsetSpecModelName, // key used to pull from modelsByName
137+
expectedModel: models.Model{
138+
Model: openai.Model{
139+
ID: unsetSpecModelName, // should equal metadata.name because spec.model.name is missing
140+
Object: "model",
141+
OwnedBy: unsetSpecModelNameNamespace,
142+
},
143+
URL: mustParseURL("http://" + unsetSpecModelName + "." + unsetSpecModelNameNamespace + ".acme.com/v1"),
144+
Ready: false,
145+
},
146+
})
147+
105148
for _, tc := range testCases {
106149
t.Run(tc.name, func(t *testing.T) {
107150
actualModel, exists := modelsByName[tc.name]

maas-api/internal/models/kserve.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,24 @@ func toModels(list *unstructured.UnstructuredList) ([]Model, error) {
7878
for _, item := range list.Items {
7979
url, errURL := findURL(item)
8080
if errURL != nil {
81-
log.Printf("DEBUG: Failed to find URL for %s: %v", item.GetKind(), errURL)
81+
log.Printf("DEBUG: Failed to find URL for %s %s/%s: %v",
82+
item.GetKind(), item.GetNamespace(), item.GetName(), errURL)
83+
}
84+
85+
// Default to metadata.name
86+
modelID := item.GetName()
87+
88+
// Check if .spec.model.name exists
89+
if name, found, err := unstructured.NestedString(item.Object, "spec", "model", "name"); err != nil {
90+
log.Printf("DEBUG: Error reading spec.model.name for %s %s/%s: %v",
91+
item.GetKind(), item.GetNamespace(), item.GetName(), err)
92+
} else if found {
93+
modelID = name
8294
}
8395

8496
models = append(models, Model{
8597
Model: openai.Model{
86-
ID: item.GetName(),
98+
ID: modelID,
8799
Object: "model",
88100
OwnedBy: item.GetNamespace(),
89101
Created: item.GetCreationTimestamp().Unix(),

0 commit comments

Comments
 (0)