Add backend provider and cost configuration
This commit is contained in:
@@ -291,6 +291,9 @@ func validateBackendRecord(backend backendConfig, base configkit.Path, validatio
|
||||
if backend.Endpoint == nil || *backend.Endpoint == "" {
|
||||
validation.Error(base.Key("endpoint"), "endpoint is required")
|
||||
}
|
||||
if backend.Provider != nil && !llm.SupportedProvider(*backend.Provider) {
|
||||
validation.Error(base.Key("provider"), "unsupported provider")
|
||||
}
|
||||
if backend.APIKeyEnv != nil && *backend.APIKeyEnv != "" && !envName.MatchString(*backend.APIKeyEnv) {
|
||||
validation.Error(base.Key("api_key_env"), "environment variable name is invalid")
|
||||
}
|
||||
@@ -319,10 +322,8 @@ func validateBackendRecord(backend backendConfig, base configkit.Path, validatio
|
||||
}
|
||||
if authentication := smokeTests.Authentication; authentication != nil {
|
||||
kindPath := smokePath.Key("authentication").Key("kind")
|
||||
if authentication.Kind == nil {
|
||||
validation.Error(kindPath, "authentication kind is required")
|
||||
} else if *authentication.Kind != "none" && *authentication.Kind != "openrouter_key" {
|
||||
validation.Error(kindPath, "authentication kind must be none or openrouter_key")
|
||||
if authentication.Kind != nil && *authentication.Kind != "none" && *authentication.Kind != "openrouter_key" && *authentication.Kind != "vllm_models" {
|
||||
validation.Error(kindPath, "unsupported authentication kind")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,6 +532,54 @@ func TestLoadInterpretsBackendNormalizationAndNullDefault(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendProviderFinalConfiguration(t *testing.T) {
|
||||
underlays := []configkit.Underlay{{Inputs: []configkit.Input{mapping("defaults", map[string]any{
|
||||
"backends": []any{map[string]any{"id": "base", "endpoint": "https://example.invalid/v1", "provider": "bad", "zero_cost": true}},
|
||||
})}}}
|
||||
main := mapping("override", map[string]any{"backends": []any{
|
||||
map[string]any{"id": "base", "endpoint": "https://example.invalid/v1", "provider": " OPENROUTER ", "zero_cost": false, "smoke_tests": map[string]any{"authentication": map[string]any{}}},
|
||||
map[string]any{"id": "child", "extends": map[string]any{"id": "base"}},
|
||||
map[string]any{"id": "cleared", "extends": map[string]any{"id": "base"}, "provider": nil, "zero_cost": nil},
|
||||
map[string]any{"id": "generic", "endpoint": "https://example.invalid/v1"},
|
||||
}})
|
||||
state, err := load(main, underlays)
|
||||
if err != nil {
|
||||
var detail *configkit.LoadError
|
||||
if errors.As(err, &detail) {
|
||||
t.Fatalf("%v: %+v", err, detail.Diagnostics())
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"base", "child"} {
|
||||
b := state.backendRecords[name]
|
||||
if b.Provider == nil || *b.Provider != "openrouter" || b.ZeroCost == nil || *b.ZeroCost {
|
||||
t.Fatalf("%s: %+v", name, b)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"cleared", "generic"} {
|
||||
b := state.backendRecords[name]
|
||||
if b.Provider != nil || b.ZeroCost != nil {
|
||||
t.Fatalf("%s: %+v", name, b)
|
||||
}
|
||||
}
|
||||
field, found := state.inspection.Metadata().Lookup(configkit.Keys("promptkit", "backends").Index(0).Key("provider"))
|
||||
if !found || field.Origin.Operation != configkit.ChangeNormalization {
|
||||
t.Fatalf("provider normalization: %+v", field)
|
||||
}
|
||||
for _, provider := range []string{"openai-compatible", "openrouter", "vllm", " ", ""} {
|
||||
_, err := load(mapping("provider", map[string]any{"backends": []any{map[string]any{"id": "x", "endpoint": "https://example.invalid/v1", "provider": provider, "smoke_tests": map[string]any{"authentication": map[string]any{"kind": " VLLM_MODELS "}}}}}), nil)
|
||||
if err != nil {
|
||||
t.Errorf("provider %q: %v", provider, err)
|
||||
}
|
||||
}
|
||||
for _, fields := range []map[string]any{{"provider": "unsupported"}, {"zero_cost": "false"}} {
|
||||
fields["id"], fields["endpoint"] = "x", "https://example.invalid/v1"
|
||||
if _, err := load(mapping("invalid", map[string]any{"backends": []any{fields}}), nil); err == nil {
|
||||
t.Fatalf("accepted invalid fields: %v", fields)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadBackendSmokeTestsOmissionAndNull(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
@@ -638,11 +686,6 @@ func TestLoadRejectsInvalidFinalBackendSmokeTests(t *testing.T) {
|
||||
data: map[string]any{"default_profile": " \t "},
|
||||
path: "/promptkit/backends/0/smoke_tests/default_profile",
|
||||
},
|
||||
{
|
||||
name: "authentication kind required",
|
||||
data: map[string]any{"authentication": map[string]any{}},
|
||||
path: "/promptkit/backends/0/smoke_tests/authentication/kind",
|
||||
},
|
||||
{
|
||||
name: "unknown authentication kind",
|
||||
data: map[string]any{"authentication": map[string]any{"kind": "bearer"}},
|
||||
|
||||
@@ -15,6 +15,9 @@ func resolveBackends(entries []backendConfig, base configkit.Path, resolution *c
|
||||
for i := range entries {
|
||||
entry := &entries[i]
|
||||
path := base.Index(i)
|
||||
if entry.Provider != nil {
|
||||
recordStringNormalization(&entry.Provider, path.Key("provider"), resolution, strings.ToLower(strings.TrimSpace(*entry.Provider)))
|
||||
}
|
||||
if entry.Endpoint != nil {
|
||||
if normalized, err := domain.NormalizeOpenAICompatibleBaseEndpoint(*entry.Endpoint); err == nil {
|
||||
recordStringNormalization(&entry.Endpoint, path.Key("endpoint"), resolution, normalized)
|
||||
|
||||
Reference in New Issue
Block a user