diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 4038f6581..f95a3eda0 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -104,9 +104,19 @@ apart, so its decision models are the ones its `/v1/models/status` gives [`omlx_decide.go`](../../internal/provider/omlx_decide.go)). MLX-Serve marks them in its `/v1/models` with `capabilities: ["decisions"]`, kept under `.decisions` (`mlxServeDecides`, `listedMLXServeDecide` in -[`mlxserve_decide.go`](../../internal/provider/mlxserve_decide.go)). A server -with none lists none, and no Jev is made up for it. An oMLX or MLX-Serve -provider saved before the preset had a decision base gets its chat base as one. +[`mlxserve_decide.go`](../../internal/provider/mlxserve_decide.go)). Ollama +and Ollama Cloud (Nanka on Discord; Ollama 0.35.0+, 0.40.5+ for cloud models) +serve System One at `/v1/systemone` beside their chat API, and list decision +models (Tev, Nimble, Clef) in `/v1/models` like any other. Their `/api/tags`, +asked with the same key, gives each model `capabilities`, `decision` for +these and `vision` for one that reads images. Those are kept under +`.decisions`, with the cloud's 65,536-token window when the list gives +none (`ollamaDecides`, `listedOllamaDecide` in +[`ollama_decide.go`](../../internal/provider/ollama_decide.go)). An Ollama +older than its System One lists no capabilities, so none. A server +with none lists none, and no Jev is made up for it. An oMLX, MLX-Serve or +Ollama provider saved before the preset had a decision base gets its chat +base as one. The CLI and TUI report model-list fetch errors for remote and other mixed providers (such as OpenRouter, serving conversation and decision models). Only a diff --git a/internal/gui/local_address_test.go b/internal/gui/local_address_test.go index 5e56516d7..77113bf82 100644 --- a/internal/gui/local_address_test.go +++ b/internal/gui/local_address_test.go @@ -30,7 +30,7 @@ func TestProviderSaveLocalPresetAtItsURLs(t *testing.T) { } } for _, c := range []struct{ id, name, chat, responses, anthropic, decide string }{ - {"ollama", "Ollama", "http://127.0.0.1:1/v1", "", "http://127.0.0.1:1", ""}, + {"ollama", "Ollama", "http://127.0.0.1:1/v1", "", "http://127.0.0.1:1", "http://127.0.0.1:1/v1"}, {"omlx", "oMLX", "http://127.0.0.1:2/v1", "http://127.0.0.1:2/v1", "http://127.0.0.1:2", "http://127.0.0.1:2/v1"}, {"lmstudio", "LM Studio", "http://127.0.0.1:3/v1", "", "", ""}, } { @@ -48,7 +48,8 @@ func TestProviderSaveLocalPresetAtItsURLs(t *testing.T) { if moved.Chat != c.chat || moved.Responses != c.responses || moved.Anthropic != c.anthropic { t.Fatalf("%s at %q %q %q", c.id, moved.Chat, moved.Responses, moved.Anthropic) } - // oMLX's decision API is the same server's, so it moves too (#1641) + // oMLX's and Ollama's decision API is the same server's, so it moves + // too (#1641; Nanka on Discord) if moved.Decide != c.decide { t.Fatalf("%s decides at %q, want %q", c.id, moved.Decide, c.decide) } diff --git a/internal/provider/decide.go b/internal/provider/decide.go index 3485b2be4..04334b0c8 100644 --- a/internal/provider/decide.go +++ b/internal/provider/decide.go @@ -150,10 +150,18 @@ func openRouterDecisions(base string) string { } // listsDecisions reports whether p's decision models are a list of their -// own, OpenRouter's, oMLX's (omlxDecides) or MLX-Serve's (mlxServeDecides), +// own, OpenRouter's or a server's that marks them (serverMarksDecisions), // rather than Jev's ids among its chat models. func (p Provider) listsDecisions() bool { - return openRouterDecisions(p.Decide) != "" || p.omlxDecides() || p.mlxServeDecides() + return openRouterDecisions(p.Decide) != "" || p.serverMarksDecisions() +} + +// serverMarksDecisions reports whether p's server says which of the models +// it serves are decision models, oMLX's (omlxDecides), MLX-Serve's +// (mlxServeDecides) or Ollama's (ollamaDecides): it has no Jev of its own, +// and lists none while it has none. +func (p Provider) serverMarksDecisions() bool { + return p.omlxDecides() || p.mlxServeDecides() || p.ollamaDecides() } // decisionsID is where a provider that serves conversations too keeps the @@ -195,7 +203,7 @@ func (p Provider) decideListDue() bool { return false } live, _, ok := catalog.Live(id) - if p.omlxDecides() || p.mlxServeDecides() { + if p.serverMarksDecisions() { // a server with no decision model loaded lists none, for good return !ok } @@ -281,8 +289,8 @@ func (p Provider) Jev() string { } // OpenRouter's Jev is ~typesafe/jev-latest, in its list of decision // models; its Jev Router is a chat model - if p.omlxDecides() || p.mlxServeDecides() { - // oMLX and MLX-Serve have no Jev of their own: their first decision + if p.serverMarksDecisions() { + // oMLX, MLX-Serve and Ollama have no Jev of their own: their first decision // model, if any if ms := p.decideListed(); len(ms) > 0 { return ms[0].ID @@ -389,7 +397,7 @@ func (p Provider) decideListed() []catalog.Model { if live, _, ok := catalog.Live(decisionsID(p.ID)); ok && len(live) > 0 { return live } - if p.omlxDecides() || p.mlxServeDecides() { + if p.serverMarksDecisions() { return nil // none until its server says which } return []catalog.Model{{ID: OpenRouterJev, Name: "Jev"}} @@ -818,6 +826,8 @@ func (p Provider) fetchDecide(ctx context.Context) ([]catalog.Model, error) { list = omlxDecisions(p.Decide) } else if p.mlxServeDecides() { list = mlxServeDecisions(p.Decide) + } else if p.ollamaDecides() { + list = ollamaDecisions(p.Decide) } else if bailianDecides(p.Decide) { // Bailian lists its chat models only: its decision model is asked return p.decideAsked(ctx) @@ -860,6 +870,14 @@ func (p Provider) fetchDecide(ctx context.Context) ([]catalog.Model, error) { } return ms, catalog.SaveLive(decisionsID(p.ID), p.Decide, ms) } + if p.ollamaDecides() { + // none is an answer too: no model of the server's is one + ms, err := listedOllamaDecide(p.Decide, b) + if err != nil { + return nil, fmt.Errorf("%s: %s", p.Name, err) + } + return ms, catalog.SaveLive(decisionsID(p.ID), p.Decide, ms) + } ms, err := listedDecide(b, all) if (err != nil || len(ms) == 0) && own { return p.decideAsked(ctx) diff --git a/internal/provider/ollama_decide.go b/internal/provider/ollama_decide.go new file mode 100644 index 000000000..465d1791a --- /dev/null +++ b/internal/provider/ollama_decide.go @@ -0,0 +1,74 @@ +package provider + +import ( + "encoding/json" + "fmt" + "slices" + "strings" + + "github.com/yetone/magpie/internal/catalog" +) + +// Ollama (0.35.0 and later, 0.40.5 for its cloud models) serves decision +// models (Nimble, Clef, Tev …) at POST /v1/systemone, System One's +// questions and answers, on the local server and at ollama.com with an API +// key alike (docs.ollama.com/api/systemone; Nanka on Discord). Its +// /v1/models lists them among its chat models with nothing to tell them +// apart; its /api/tags, asked with the same key, gives each model its +// capabilities, "decision" for these (and "vision" for one that reads +// images, as Clef does). That list is the provider's decision models, as +// oMLX's and MLX-Serve's are (listsDecisions), and a model it doesn't mark +// stays a chat model. An Ollama older than its System One lists no +// capabilities, so none. +func (p Provider) ollamaDecides() bool { + return (p.Preset == "ollama" || p.Preset == "ollama-cloud") && p.Decide != "" && !p.DecideOnly() +} + +// ollamaCloudContext is the window Ollama's docs give a System One model +// in its cloud. +const ollamaCloudContext = 65_536 + +// ollamaDecisions is where an Ollama server lists its models with their +// capabilities: /api/tags at the root its /v1 is under. +func ollamaDecisions(base string) string { + return strings.TrimSuffix(strings.TrimRight(base, "/"), "/v1") + "/api/tags" +} + +// listedOllamaDecide are the decision models in Ollama's /api/tags, by the +// name /v1/models gives them too, with the window the list gives (a GGUF's +// details.context_length), else, in Ollama's cloud (at ollama.com, or a +// local server's cloud model, which names its remote_host), the cloud's. +func listedOllamaDecide(base string, b []byte) ([]catalog.Model, error) { + var out struct { + Models *[]struct { + Name string `json:"name"` + Model string `json:"model"` + RemoteHost string `json:"remote_host"` + Capabilities []string `json:"capabilities"` + Details struct { + ContextLength int `json:"context_length"` + } `json:"details"` + } `json:"models"` + } + if err := json.Unmarshal(b, &out); err != nil || out.Models == nil { + return nil, fmt.Errorf("not Ollama's model list") + } + cloud := HostOf(base) == "ollama.com" + ms := []catalog.Model{} + for _, m := range *out.Models { + id := m.Model + if id == "" { + id = m.Name + } + if id == "" || !slices.Contains(m.Capabilities, "decision") { + continue + } + images := slices.Contains(m.Capabilities, "vision") + ctx := m.Details.ContextLength + if ctx <= 0 && (cloud || m.RemoteHost != "") { + ctx = ollamaCloudContext + } + ms = append(ms, catalog.Model{ID: id, Name: id, Context: ctx, ImageInput: &images, Images: images}) + } + return ms, nil +} diff --git a/internal/provider/ollama_decide_test.go b/internal/provider/ollama_decide_test.go new file mode 100644 index 000000000..692f5940a --- /dev/null +++ b/internal/provider/ollama_decide_test.go @@ -0,0 +1,160 @@ +package provider + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "slices" + "sync" + "testing" +) + +// The bytes below were saved from Ollama 0.40.6 (a local server with +// tev1:0.8b and smollm:135m pulled) and from ollama.com/api/tags on +// 2026-10-19, trimmed to the rows a test needs. +const ( + ollamaLocalTags = `{"models":[{"name":"smollm:135m","model":"smollm:135m","modified_at":"2026-10-19T23:15:35.885392239+08:00","size":91739413,"digest":"b0b2a46174385c0adcaa77ff245ffeced5fc4a61447b6f221b2beb5c5a760133","details":{"parent_model":"","format":"gguf","family":"llama","families":["llama"],"parameter_size":"134.52M","quantization_level":"Q4_0","context_length":2048,"embedding_length":576,"runner":"ggml"},"capabilities":["completion"]},{"name":"tev1:0.8b","model":"tev1:0.8b","modified_at":"2026-10-19T23:14:31.147707454+08:00","size":1524372929,"digest":"b661d56508169d2b6c4cab5a047409f50bd9694a215bc232469d44291f4bdbf1","details":{"parent_model":"","format":"safetensors","family":"qwen3_5","families":["qwen3_5"],"parameter_size":"752M","quantization_level":"BF16","runner":"mlx"},"capabilities":["decision"]}]}` + ollamaLocalModels = `{"object":"list","data":[{"id":"smollm:135m","object":"model","created":1792422935,"owned_by":"library"},{"id":"tev1:0.8b","object":"model","created":1792422871,"owned_by":"library"}]}` + ollamaSystemOne = `{"model":"tev1:0.8b","answers":{"ok":{"type":"noul","noul":0.2234409293546222}},"usage":{"input_tokens":107,"output_tokens":0},"prompt_eval_cached_count":0}` + ollamaNoChat = `{"error":{"message":"\"tev1:0.8b\" does not support chat","type":"invalid_request_error","param":null,"code":null}}` + ollamaCloudTags = `{"models":[{"name":"glm-5.3","model":"glm-5.3","modified_at":"2026-08-28T08:00:00-07:00","size":755433728000,"digest":"632dfda18c6d","details":{"parent_model":"","format":"","family":"","families":null,"parameter_size":"","quantization_level":""},"capabilities":["completion","thinking","tools"]},{"name":"clef:27b","model":"clef:27b","modified_at":"2026-10-04T09:00:00-07:00","size":54969731792,"digest":"490a326b58a0","details":{"parent_model":"","format":"","family":"","families":null,"parameter_size":"","quantization_level":""},"capabilities":["decision","vision"]}]}` + // Ollama before System One (0.6.8) lists no capabilities + ollamaOldTags = `{"models":[{"name":"qwen3:8b","model":"qwen3:8b","modified_at":"2025-05-01T10:00:00+08:00","size":5225388164,"digest":"500a1f067a9f","details":{"parent_model":"","format":"gguf","family":"qwen3","families":["qwen3"],"parameter_size":"8.2B","quantization_level":"Q4_K_M"}}]}` +) + +// fakeOllama answers as Ollama 0.40.6 does: /api/tags and /v1/models list +// a chat model and a decision model, /v1/systemone answers System One, and +// chat completions turn the decision model away. +func fakeOllama(t *testing.T) (base string, asked func() []string) { + t.Helper() + var mu sync.Mutex + var models []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/api/tags": + w.Write([]byte(ollamaLocalTags)) + case "/v1/models": + w.Write([]byte(ollamaLocalModels)) + case "/v1/systemone": + var q struct{ Model string } + json.NewDecoder(r.Body).Decode(&q) + mu.Lock() + models = append(models, q.Model) + mu.Unlock() + w.Write([]byte(ollamaSystemOne)) + case "/v1/chat/completions": + var q struct{ Model string } + json.NewDecoder(r.Body).Decode(&q) + if q.Model == "tev1:0.8b" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(ollamaNoChat)) + return + } + w.Write([]byte(`{"id":"chatcmpl-382","object":"chat.completion","created":1792422957,"model":"smollm:135m","system_fingerprint":"fp_ollama","choices":[{"index":0,"message":{"role":"assistant","content":"Hello! How can I"},"finish_reason":"length"}],"usage":{"prompt_tokens":10,"completion_tokens":5,"total_tokens":15}}`)) + default: + http.NotFound(w, r) + } + })) + t.Cleanup(srv.Close) + return srv.URL, func() []string { + mu.Lock() + defer mu.Unlock() + return slices.Clone(models) + } +} + +// An Ollama provider saved before its preset had System One (Nanka on +// Discord): the models its /api/tags marks "decision" classify routing +// groups through /v1/systemone, and agents no longer see them as chat +// models. +func TestOllamaDecisionModels(t *testing.T) { + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + base, asked := fakeOllama(t) + + if err := Save(Provider{ID: "ollama", Name: "Ollama", Preset: "ollama", Chat: base + "/v1", Anthropic: base}); err != nil { + t.Fatal(err) + } + p, err := Find("ollama") + if err != nil || !p.Decides() || p.DecideOnly() || p.Decide != base+"/v1" { + t.Fatalf("Ollama has no decision API at its own server: %+v", p) + } + if _, _, _, err := p.refetch(context.Background()); err != nil { + t.Fatal(err) + } + p, _ = Find("ollama") + + if !p.DecidesModel("tev1:0.8b") || p.DecidesModel("smollm:135m") { + t.Fatal("decision models are not the ones Ollama's capabilities say are") + } + var deciders []string + for _, e := range Deciders() { + deciders = append(deciders, e.ID) + } + if !slices.Equal(deciders, []string{"ollama/tev1:0.8b"}) { + t.Fatalf("deciders: %v", deciders) + } + var agents []string + for _, e := range providerEntries() { + agents = append(agents, e.Model) + } + if !slices.Equal(agents, []string{"smollm:135m"}) { + t.Fatalf("agents' models: %v", agents) + } + + q, m, err := RouteDecider("ollama/tev1:0.8b") + if err != nil || q.ID != "ollama" || m != "tev1:0.8b" { + t.Fatalf("routed to %s %s: %v", q.ID, m, err) + } + if err := q.AskSystemOne(context.Background(), m); err != nil { + t.Fatal(err) + } + if _, m, _ := RouteDecider("ollama"); m != "tev1:0.8b" { + t.Fatalf("Ollama's default decision model: %q", m) + } + if got := asked(); !slices.Equal(got, []string{"tev1:0.8b"}) { + t.Fatalf("asked: %v", got) + } + results := p.TestModels(context.Background(), []string{"tev1:0.8b"}) + if len(results) != 1 || !results[0].OK || results[0].Protocol != "decide" { + t.Fatalf("TestModels tev1:0.8b: %+v", results) + } +} + +// Ollama Cloud's list marks Clef a decision model that reads images, with +// the cloud's window; a chat model, and every model of an Ollama from +// before System One, is none. +func TestListedOllamaDecide(t *testing.T) { + ms, err := listedOllamaDecide("https://ollama.com/v1", []byte(ollamaCloudTags)) + if err != nil || len(ms) != 1 || ms[0].ID != "clef:27b" || ms[0].Context != 65_536 || ms[0].ImageInput == nil || !*ms[0].ImageInput { + t.Fatalf("cloud: %+v %v", ms, err) + } + ms, err = listedOllamaDecide("http://localhost:11434/v1", []byte(ollamaLocalTags)) + if err != nil || len(ms) != 1 || ms[0].ID != "tev1:0.8b" || ms[0].Context != 0 || ms[0].ImageInput == nil || *ms[0].ImageInput { + t.Fatalf("local: %+v %v", ms, err) + } + if ms, err := listedOllamaDecide("http://localhost:11434/v1", []byte(ollamaOldTags)); err != nil || len(ms) != 0 { + t.Fatalf("old Ollama: %+v %v", ms, err) + } + if _, err := listedOllamaDecide("http://localhost:11434/v1", []byte(`404 page not found`)); err == nil { + t.Fatal("a 404's body read as a list") + } + if got := ollamaDecisions("https://ollama.com/v1"); got != "https://ollama.com/api/tags" { + t.Fatalf("list at %s", got) + } + for _, id := range []string{"ollama", "ollama-cloud"} { + pr := Preset(id) + if pr == nil || pr.Decide == "" || pr.Decide != pr.Chat { + t.Fatalf("%s's preset has no System One beside its chat: %+v", id, pr) + } + } + p := Provider{ID: "ollama-x", Preset: "ollama-cloud", Chat: "http://127.0.0.1:1/v1", Decide: "http://127.0.0.1:1/v1"} + if got := p.decideListed(); len(got) != 0 || p.Jev() != "" { + t.Fatalf("made up: %v %q", got, p.Jev()) + } +} diff --git a/internal/provider/presets.go b/internal/provider/presets.go index 25ca6b3a4..79e7609cf 100644 --- a/internal/provider/presets.go +++ b/internal/provider/presets.go @@ -415,9 +415,12 @@ var presets = []PresetDef{ Endpoint: "https://.openai.azure.com", EndpointHint: "Your resource's endpoint, from Keys and Endpoint in the Azure portal. Once added, magpie asks the resource for its deployments with this key, over its v1 API; the deployments' names are the model ids.", Website: "https://ai.azure.com", KeysURL: "https://portal.azure.com/#view/Microsoft_Azure_ProjectOxford/CognitiveServicesHub/~/OpenAI"}, - // Ollama's own hosted models: the local server's API, at ollama.com with a key + // Ollama's own hosted models: the local server's API, at ollama.com with + // a key. Its decision models (Clef), served at /v1/systemone, are the + // ones its /api/tags gives the "decision" capability (ollama_decide.go) {ID: "ollama-cloud", Name: "Ollama Cloud", Icon: "ollama", Kind: KindVendor, Catalog: "ollama-cloud", Hosts: true, Chat: "https://ollama.com/v1", Anthropic: "https://ollama.com", + Decide: "https://ollama.com/v1", Note: "cloud models, with an API key", Website: "https://docs.ollama.com/cloud", KeysURL: "https://ollama.com/settings/keys"}, // Nous Research's Nous Portal (#392): its own Hermes models and other @@ -569,9 +572,13 @@ var presets = []PresetDef{ Decide: "https://maas.qianwenaiapi.com/compatible-mode/v1", Note: "routes groups · picks model and effort", Website: "https://www.qianwenai.com/models/decision-model-preview", KeysURL: "https://platform.qianwenai.com/home/api-keys"}, + // its decision models (Nimble, Clef, Tev …), served at /v1/systemone + // since 0.35.0, are the ones its /api/tags gives the "decision" + // capability (ollama_decide.go) {ID: "ollama", Name: "Ollama", Icon: "ollama", Kind: KindLocal, NoKey: true, Chat: "http://localhost:11434/v1", Anthropic: "http://localhost:11434", - Note: "your local models", Website: "https://ollama.com"}, + Decide: "http://localhost:11434/v1", + Note: "your local models", Website: "https://ollama.com"}, {ID: "lmstudio", Name: "LM Studio", Icon: "lmstudio", Kind: KindLocal, NoKey: true, Chat: "http://localhost:1234/v1", Note: "local server, port 1234 by default", Website: "https://lmstudio.ai"}, diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 4f30fb1b8..dc4241bf3 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1275,9 +1275,10 @@ func normalize(p Provider) Provider { p.Decide = pr.Decide } } - // an oMLX or MLX-Serve provider saved before its preset had System One (#1641) - // asks it where its chat completions are, at whatever port it was given - if (p.Preset == OMLXPreset || p.Preset == MLXServePreset) && p.Decide == "" && p.Chat != "" { + // an oMLX, MLX-Serve or Ollama provider saved before its preset had + // System One (#1641; Nanka on Discord) asks it where its chat + // completions are, at whatever address it was given + if (p.Preset == OMLXPreset || p.Preset == MLXServePreset || p.Preset == "ollama" || p.Preset == "ollama-cloud") && p.Decide == "" && p.Chat != "" { p.Decide = p.Chat } if p.Routing != Ordered && p.Routing != Rotate && p.Routing != LeastUsed && p.Routing != Pace && p.Routing != Weighted && !PluginRouting(p.Routing) {