feat(cognitive): add local provider registry and discovery
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package cognitive_providers_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.cin.su/m/Assistant/adapters/models"
|
||||
"git.cin.su/m/Assistant/contracts/capability"
|
||||
)
|
||||
|
||||
func TestLiveReadOnlyDiscovery(t *testing.T) {
|
||||
if os.Getenv("ASSISTANT_LIVE_LOCAL_DISCOVERY") != "1" {
|
||||
t.Skip("set ASSISTANT_LIVE_LOCAL_DISCOVERY=1 for read-only local provider probes")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
cpu, err := models.NewOpenAICompatibleLocalAdapter("http://docker.cin.su:18082/v1", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cpuModels, cpuProbe := cpu.Discover(ctx)
|
||||
if cpuProbe.Health != capability.HealthAvailable || len(cpuModels) == 0 {
|
||||
t.Fatalf("CPU probe=%+v models=%+v", cpuProbe, cpuModels)
|
||||
}
|
||||
t.Logf("CPU probe=%+v models=%+v", cpuProbe, cpuModels)
|
||||
gpu, err := models.NewOllamaAdapter("http://192.168.220.91:11434", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gpuModels, gpuProbe := gpu.Discover(ctx)
|
||||
if gpuProbe.Health != capability.HealthAvailable || len(gpuModels) == 0 {
|
||||
t.Fatalf("GPU probe=%+v models=%+v", gpuProbe, gpuModels)
|
||||
}
|
||||
t.Logf("GPU probe=%+v models=%+v", gpuProbe, gpuModels)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package cognitive_providers_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cin.su/m/Assistant/adapters/models"
|
||||
"git.cin.su/m/Assistant/contracts/capability"
|
||||
capabilitydomain "git.cin.su/m/Assistant/modules/capability/domain"
|
||||
)
|
||||
|
||||
func TestCPUDiscoveryIsReadOnlyAndCredentialFree(t *testing.T) {
|
||||
requests := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests++
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/v1/models" || r.Header.Get("Authorization") != "" {
|
||||
t.Fatalf("unexpected request %s %s auth=%q", r.Method, r.URL.Path, r.Header.Get("Authorization"))
|
||||
}
|
||||
_, _ = io.WriteString(w, `{"data":[{"id":"same-name","created":1,"owned_by":"local","meta":{"n_ctx":2048,"n_params":3,"size":10,"ftype":"Q4"}}]}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
adapter, err := models.NewOpenAICompatibleLocalAdapter(server.URL+"/v1", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
discovered, probe := adapter.Discover(context.Background())
|
||||
if probe.Health != capability.HealthAvailable || len(discovered) != 1 || requests != 1 {
|
||||
t.Fatalf("probe=%+v models=%+v requests=%d", probe, discovered, requests)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOllamaDiscoveryDoesNotRunInference(t *testing.T) {
|
||||
paths := []string{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.URL.Path)
|
||||
switch r.URL.Path {
|
||||
case "/api/version":
|
||||
_, _ = io.WriteString(w, `{"version":"1.2.3"}`)
|
||||
case "/api/tags":
|
||||
_, _ = io.WriteString(w, `{"models":[{"name":"same-name","model":"same-name","size":20,"digest":"digest-a","details":{"format":"gguf","quantization_level":"Q4","context_length":4096},"capabilities":["completion"]}]}`)
|
||||
default:
|
||||
t.Fatalf("unexpected expensive endpoint %s", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
adapter, err := models.NewOllamaAdapter(server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
discovered, probe := adapter.Discover(context.Background())
|
||||
if probe.Health != capability.HealthAvailable || len(discovered) != 1 || strings.Join(paths, ",") != "/api/version,/api/tags" {
|
||||
t.Fatalf("probe=%+v models=%+v paths=%v", probe, discovered, paths)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryIsIdempotentAndProviderScoped(t *testing.T) {
|
||||
base := capability.CognitiveDiscoverySnapshot{ProviderInstanceID: "cpu", ProviderKind: "kind", EndpointRef: "endpoint", Enabled: true, OwnerPrincipalID: "owner", CapabilityID: "creator-advisory", DataPolicy: capability.DataPolicyLocalOnly, LimitPolicyRef: "limits", Health: capability.HealthAvailable, ObservedAtUnixMillis: 1, Models: []capability.DiscoveredModelSnapshot{{ProviderModelID: "same-name", Fingerprint: "digest-a", MaxContextBytes: 100, Modalities: []capability.ModelModality{capability.ModalityText}, Capabilities: []capability.ModelCapability{capability.CapabilityUnknown}, Installed: capability.OperationalYes, Loaded: capability.OperationalNotObserved, Invokable: capability.OperationalNotObserved}}}
|
||||
first, err := capabilitydomain.BuildDiscoveryRegistration(base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := capabilitydomain.BuildDiscoveryRegistration(base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.Models[0].ID != second.Models[0].ID || first.Models[0].Revision != second.Models[0].Revision || first.Bindings[0].Activation != capability.ActivationInactive {
|
||||
t.Fatalf("discovery not idempotent/inactive: %+v %+v", first, second)
|
||||
}
|
||||
gpu := base
|
||||
gpu.ProviderInstanceID = "gpu"
|
||||
other, _ := capabilitydomain.BuildDiscoveryRegistration(gpu)
|
||||
if first.Models[0].ID == other.Models[0].ID {
|
||||
t.Fatal("same model name collided across providers")
|
||||
}
|
||||
changed := base
|
||||
changed.Models = append([]capability.DiscoveredModelSnapshot(nil), base.Models...)
|
||||
changed.Models[0].Fingerprint = "digest-b"
|
||||
revision, _ := capabilitydomain.BuildDiscoveryRegistration(changed)
|
||||
if revision.Models[0].Revision == first.Models[0].Revision {
|
||||
t.Fatal("digest change silently reused revision")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user