mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-20 07:00:58 +08:00
fix(ollama): keyless env config and /v1 base URL normalization
Two review fixes on top of the Ollama provider: FromEnv no longer inherits OPENAI_API_KEY for the ollama format. Ollama ignores the bearer token, so inheriting the cloud key could only ship it to the local or LAN endpoint, contradicting the comment in NewProvider and the documented behavior. ConfigFrom now normalizes the base URL to the /v1 root, so the natural Ollama URL (host:11434 without /v1) works for chat as well. Previously the provider appended /chat/completions to it and the request landed on a path Ollama does not serve, while pgListModels already appended /v1. Tests: bare-host normalization assertion, the /v1/chat/completions path assertion, and a new test that the env config carries no inherited key.
This commit is contained in:
+11
-5
@@ -224,7 +224,10 @@ func FromEnv() (Config, bool) {
|
||||
}
|
||||
case "ollama":
|
||||
c.Format = FormatOllama
|
||||
c.APIKey = oaiKey // optional; a local Ollama server is usable keyless
|
||||
// Deliberately NOT inheriting OPENAI_API_KEY: Ollama ignores the bearer
|
||||
// token, so the only thing the inheritance could do is ship a real cloud
|
||||
// key to the local (or LAN) endpoint. An authenticated Ollama gateway can
|
||||
// still be configured as a saved profile with an explicit key.
|
||||
if c.BaseURL == "" {
|
||||
c.BaseURL = DefaultOllamaBaseURL
|
||||
}
|
||||
@@ -275,12 +278,15 @@ func ConfigFrom(provider, model, baseURL, apiKey, proxy string) Config {
|
||||
case "ollama":
|
||||
c.Format = FormatOllama
|
||||
// Ollama's OpenAI-compatible endpoint lives at /v1 and the provider
|
||||
// appends "/chat/completions"; tolerate a full endpoint URL. Never leave
|
||||
// it empty: an empty BaseURL would let the SDK fall back to OpenAI's
|
||||
// public endpoint with this profile's request.
|
||||
c.BaseURL = strings.TrimRight(strings.TrimSuffix(c.BaseURL, "/chat/completions"), "/")
|
||||
// appends "/chat/completions"; tolerate a full endpoint URL and the
|
||||
// natural Ollama URL without the /v1 suffix. Never leave it empty: an
|
||||
// empty BaseURL would let the SDK fall back to OpenAI's public endpoint
|
||||
// with this profile's request.
|
||||
c.BaseURL = strings.TrimRight(strings.TrimSuffix(strings.TrimSpace(c.BaseURL), "/chat/completions"), "/")
|
||||
if c.BaseURL == "" {
|
||||
c.BaseURL = DefaultOllamaBaseURL
|
||||
} else if !strings.HasSuffix(c.BaseURL, "/v1") {
|
||||
c.BaseURL += "/v1"
|
||||
}
|
||||
if c.Model == "" {
|
||||
c.Model = "llama3.2"
|
||||
|
||||
@@ -44,6 +44,12 @@ func TestOllamaConfigFromKeylessDefaults(t *testing.T) {
|
||||
if k.APIKey != "ollama-secret" {
|
||||
t.Fatalf("custom key lost: %q", k.APIKey)
|
||||
}
|
||||
// The natural Ollama URL has no /v1, but the provider appends
|
||||
// /chat/completions, so the base must be normalized to the /v1 root.
|
||||
n := agent.ConfigFrom("ollama", "m", "http://box:11434", "", "")
|
||||
if n.BaseURL != "http://box:11434/v1" {
|
||||
t.Fatalf("bare host URL not normalized: %q", n.BaseURL)
|
||||
}
|
||||
}
|
||||
|
||||
// The provider built from an Ollama config speaks OpenAI Chat Completions on
|
||||
@@ -79,8 +85,11 @@ func TestOllamaProviderWiresOpenAIProtocol(t *testing.T) {
|
||||
if _, _, _, err = prov.Complete(ctx, req); err != nil {
|
||||
t.Fatalf("Complete: %v", err)
|
||||
}
|
||||
if p, _ := gotPath.Load().(string); p != "/chat/completions" {
|
||||
t.Fatalf("path=%q, want /chat/completions (base URL must NOT be the OpenAI fallback)", p)
|
||||
// ConfigFrom normalizes the fake server URL (no /v1) onto the OpenAI root,
|
||||
// so the request must land on /v1/chat/completions. A path of /chat/completions
|
||||
// would mean the base URL leaked through unnormalized (or fell back to OpenAI).
|
||||
if p, _ := gotPath.Load().(string); p != "/v1/chat/completions" {
|
||||
t.Fatalf("path=%q, want /v1/chat/completions", p)
|
||||
}
|
||||
if a, _ := gotAuth.Load().(string); a != "Bearer ollama" {
|
||||
t.Fatalf("Authorization=%q, want placeholder Bearer ollama", a)
|
||||
@@ -221,3 +230,19 @@ func TestOllamaListModels(t *testing.T) {
|
||||
t.Fatalf("both calls must hit /v1/models, got %v", paths)
|
||||
}
|
||||
}
|
||||
|
||||
// An Ollama config resolved from the environment must not inherit OPENAI_API_KEY:
|
||||
// Ollama ignores the bearer token, so inheritance would only ship the cloud key
|
||||
// to the local or LAN endpoint.
|
||||
func TestOllamaFromEnvDoesNotInheritOpenAIKey(t *testing.T) {
|
||||
t.Setenv("ARTEX_LLM_PROVIDER", "ollama")
|
||||
t.Setenv("OPENAI_API_KEY", "sk-cloud-secret")
|
||||
t.Setenv("ARTEX_LLM_BASE_URL", "")
|
||||
c, ok := agent.FromEnv()
|
||||
if !ok {
|
||||
t.Fatal("ollama env config must be usable without a key")
|
||||
}
|
||||
if c.APIKey != "" {
|
||||
t.Fatalf("ollama inherited a cloud key: %q", c.APIKey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2104,7 +2104,7 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) {
|
||||
// without the /v1 suffix (and a full endpoint URL). No auth needed; when a
|
||||
// key was provided anyway, send it as Bearer (Ollama ignores it).
|
||||
if baseURL == "" {
|
||||
baseURL = "http://127.0.0.1:11434/v1"
|
||||
baseURL = agent.DefaultOllamaBaseURL
|
||||
}
|
||||
b := strings.TrimSuffix(baseURL, "/chat/completions")
|
||||
if !strings.HasSuffix(b, "/v1") {
|
||||
|
||||
Reference in New Issue
Block a user