diff --git a/packages/agent/build.go b/packages/agent/build.go index 343c7ee..4271333 100644 --- a/packages/agent/build.go +++ b/packages/agent/build.go @@ -305,9 +305,6 @@ func Resolve(args Args, requireCred bool) (Resolved, error) { credErr error ) if provName == "ollama" { - if args.BaseURL == "" { - args.BaseURL = "http://localhost:11434" - } cred = firstNonEmpty(args.APIKey, "ollama") method = "apikey" } else { @@ -398,10 +395,15 @@ func Resolve(args Args, requireCred bool) (Resolved, error) { } // If the model defines a base URL (e.g. local ollama) and the - // user didn't pass --base-url, use the model's URL. + // user didn't pass --base-url, use the model's URL. For ollama, + // keep http://localhost:11434 as a fallback only after the model + // metadata has had a chance to provide a custom baseUrl. if args.BaseURL == "" && resolvedModel.BaseURL != "" { args.BaseURL = resolvedModel.BaseURL } + if args.BaseURL == "" && provName == "ollama" { + args.BaseURL = "http://localhost:11434" + } // If the model has a base URL, credentials are optional (local // models like ollama don't need real API keys). diff --git a/packages/agent/build_test.go b/packages/agent/build_test.go index 3e3bd44..eb8e37e 100644 --- a/packages/agent/build_test.go +++ b/packages/agent/build_test.go @@ -5,6 +5,8 @@ import ( "path/filepath" "strings" "testing" + + "github.com/patriceckhart/zot/packages/provider" ) func TestReadAgentsContextLoadsGlobalAndAncestors(t *testing.T) { @@ -109,6 +111,42 @@ func TestResolveExplicitFlagStaleDoesNotRepairConfig(t *testing.T) { } } +func TestResolveOllamaUsesModelBaseURLBeforeDefault(t *testing.T) { + t.Setenv("ZOT_HOME", t.TempDir()) + provider.SetLiveModels(nil) + defer provider.SetLiveModels(nil) + provider.SetUserModels([]provider.Model{{ + Provider: "ollama", + ID: "qwen-local", + DisplayName: "Qwen Local", + ContextWindow: 32768, + MaxOutput: 8192, + BaseURL: "http://localhost:8000/v1", + }}) + + r, err := Resolve(Args{Provider: "ollama", Model: "qwen-local"}, false) + if err != nil { + t.Fatalf("Resolve failed: %v", err) + } + if r.BaseURL != "http://localhost:8000/v1" { + t.Fatalf("BaseURL = %q, want models.json baseUrl", r.BaseURL) + } +} + +func TestResolveOllamaFallsBackToDefaultBaseURL(t *testing.T) { + t.Setenv("ZOT_HOME", t.TempDir()) + provider.SetLiveModels(nil) + defer provider.SetLiveModels(nil) + + r, err := Resolve(Args{Provider: "ollama", Model: "any-local-model"}, false) + if err != nil { + t.Fatalf("Resolve failed: %v", err) + } + if r.BaseURL != "http://localhost:11434" { + t.Fatalf("BaseURL = %q, want ollama default", r.BaseURL) + } +} + func TestCanonicalProviderResolvesAliases(t *testing.T) { cases := map[string]string{ "bedrock": "amazon-bedrock",