Add vLLM models authentication diagnostics
This commit is contained in:
@@ -73,7 +73,14 @@ func (c *OpenAICompatibleClient) ProbeAuthentication(ctx context.Context, adapte
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if recognizedOpenRouterKeyResponse(contents) {
|
||||
recognized := false
|
||||
switch adapter {
|
||||
case "openrouter_key":
|
||||
recognized = recognizedOpenRouterKeyResponse(contents)
|
||||
case "vllm_models":
|
||||
recognized = recognizedVLLMModelsResponse(contents)
|
||||
}
|
||||
if recognized {
|
||||
accepted := true
|
||||
result.Accepted = &accepted
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package llm
|
||||
|
||||
import "go.maximumdirect.net/promptkit/internal/domain"
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
)
|
||||
|
||||
// EffectiveProvider resolves an omitted provider to the generic conventions.
|
||||
func EffectiveProvider(provider string) string {
|
||||
@@ -26,8 +30,11 @@ func AuthenticationAdapter(provider string, smoke *domain.BackendSmokeTestConfig
|
||||
if smoke != nil && smoke.Authentication != nil && smoke.Authentication.Kind != nil {
|
||||
return *smoke.Authentication.Kind
|
||||
}
|
||||
if provider == "openrouter" {
|
||||
switch provider {
|
||||
case "openrouter":
|
||||
return "openrouter_key"
|
||||
case "vllm":
|
||||
return "vllm_models"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -40,8 +47,13 @@ func AuthenticationEndpoint(adapter, base string, smoke *domain.BackendSmokeTest
|
||||
if adapter == "openrouter_key" {
|
||||
return OpenRouterKeyEndpoint(base)
|
||||
}
|
||||
if adapter == "vllm_models" {
|
||||
return url.JoinPath(base, "models")
|
||||
}
|
||||
return base, nil
|
||||
}
|
||||
|
||||
// SupportedAuthenticationAdapter identifies implemented authentication probes.
|
||||
func SupportedAuthenticationAdapter(adapter string) bool { return adapter == "openrouter_key" }
|
||||
func SupportedAuthenticationAdapter(adapter string) bool {
|
||||
return adapter == "openrouter_key" || adapter == "vllm_models"
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestAuthenticationSelection(t *testing.T) {
|
||||
none, explicit, endpoint := "none", "openrouter_key", "https://diagnostics.example/probe"
|
||||
none, explicit, models, endpoint := "none", "openrouter_key", "vllm_models", "https://diagnostics.example/probe"
|
||||
for _, tc := range []struct {
|
||||
name, provider, adapter, destination string
|
||||
metadata *domain.BackendSmokeTestConfig
|
||||
@@ -18,6 +18,10 @@ func TestAuthenticationSelection(t *testing.T) {
|
||||
{name: "explicit adapter", metadata: &domain.BackendSmokeTestConfig{Authentication: &domain.AuthenticationProbeConfig{Kind: &explicit}}, adapter: "openrouter_key", destination: "https://example.test/v1/key"},
|
||||
{name: "disabled", provider: "openrouter", metadata: &domain.BackendSmokeTestConfig{Authentication: &domain.AuthenticationProbeConfig{Kind: &none}}, adapter: "none", destination: "https://example.test/v1"},
|
||||
{name: "generic unsupported", destination: "https://example.test/v1"},
|
||||
{name: "vllm default", provider: "vllm", adapter: "vllm_models", destination: "https://example.test/v1/models"},
|
||||
{name: "vllm override", provider: "vllm", metadata: &domain.BackendSmokeTestConfig{Authentication: &domain.AuthenticationProbeConfig{Endpoint: &endpoint}}, adapter: "vllm_models", destination: endpoint},
|
||||
{name: "explicit vllm on generic", metadata: &domain.BackendSmokeTestConfig{Authentication: &domain.AuthenticationProbeConfig{Kind: &models}}, adapter: "vllm_models", destination: "https://example.test/v1/models"},
|
||||
{name: "disabled vllm", provider: "vllm", metadata: &domain.BackendSmokeTestConfig{Authentication: &domain.AuthenticationProbeConfig{Kind: &none}}, adapter: "none", destination: "https://example.test/v1"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
kind := AuthenticationAdapter(tc.provider, tc.metadata)
|
||||
|
||||
16
internal/llm/vllm_authentication.go
Normal file
16
internal/llm/vllm_authentication.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package llm
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func recognizedVLLMModelsResponse(contents []byte) bool {
|
||||
var envelope map[string]json.RawMessage
|
||||
if json.Unmarshal(contents, &envelope) != nil || envelope == nil {
|
||||
return false
|
||||
}
|
||||
var object string
|
||||
if json.Unmarshal(envelope["object"], &object) != nil || object != "list" {
|
||||
return false
|
||||
}
|
||||
var data []json.RawMessage
|
||||
return json.Unmarshal(envelope["data"], &data) == nil && data != nil
|
||||
}
|
||||
51
internal/llm/vllm_authentication_test.go
Normal file
51
internal/llm/vllm_authentication_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
)
|
||||
|
||||
func TestVLLMModelsAuthentication(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, body, key string
|
||||
status int
|
||||
accepted *bool
|
||||
unrecognized, overflow bool
|
||||
}{
|
||||
{name: "empty list", status: 200, body: `{"object":"list","data":[]}`, accepted: boolPtr(true)},
|
||||
{name: "nonempty list", status: 200, body: `{"object":"list","data":[{"id":"model"}]}`, key: "key", accepted: boolPtr(true)},
|
||||
{name: "wrong object", status: 200, body: `{"object":"model","data":[]}`, unrecognized: true},
|
||||
{name: "null data", status: 200, body: `{"object":"list","data":null}`, unrecognized: true},
|
||||
{name: "object data", status: 200, body: `{"object":"list","data":{}}`, unrecognized: true},
|
||||
{name: "invalid framing", status: 200, body: `{"object":"list","data":[]} {}`, unrecognized: true},
|
||||
{name: "overflow", status: 200, body: strings.Repeat(" ", maxAuthenticationResponseBytes+1), overflow: true},
|
||||
{name: "unauthorized", status: 401, accepted: boolPtr(false)},
|
||||
{name: "other status", status: 503},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var credential string
|
||||
client := NewOpenAICompatibleClient(OpenAICompatibleConfig{HTTPClient: &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
credential = req.Header.Get("Authorization")
|
||||
return &http.Response{StatusCode: tc.status, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(tc.body))}, nil
|
||||
})}})
|
||||
got, err := client.ProbeAuthentication(context.Background(), "vllm_models", "https://example.invalid/v1/models", 0, domain.ExecutionTarget{APIKey: tc.key})
|
||||
if err != nil || got.Unrecognized != tc.unrecognized || got.Overflow != tc.overflow || (got.Accepted == nil) != (tc.accepted == nil) || (got.Accepted != nil && *got.Accepted != *tc.accepted) {
|
||||
t.Fatalf("result=%+v err=%v", got, err)
|
||||
}
|
||||
want := ""
|
||||
if tc.key != "" {
|
||||
want = "Bearer " + tc.key
|
||||
}
|
||||
if credential != want {
|
||||
t.Fatalf("authorization=%q", credential)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func boolPtr(value bool) *bool { return &value }
|
||||
Reference in New Issue
Block a user