From 8b9f4ab625519ef1fce5077c01493836f7120959 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 25 Mar 2026 15:37:03 -0700 Subject: [PATCH] fix: lazy-load Ollama /api/show to reduce unnecessary requests (#11836) * fix: lazy-load Ollama /api/show to reduce unnecessary requests Move the /api/show call from the Ollama constructor to a lazy ensureModelInfo() method that is only invoked on first actual model usage (_streamComplete, _streamChat, _streamFim). The promise is cached so subsequent calls reuse the same result. This fixes the issue where AUTODETECT with many models caused hundreds of /api/show requests on every config reload, since each model instantiation fired a request in the constructor. Additional fixes: - Null-check regex match result to prevent runtime errors - Use const instead of let for immutable bindings - Don't override explicitly configured contextLength with /api/show value - Silently ignore unparseable stop parameters - Return empty array from listModels() on failure instead of throwing Closes #8765 * revert silent error handling, remove verbose comments --- core/llm/llms/Ollama.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 9473a50cd..95945f708 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -162,12 +162,23 @@ class Ollama extends BaseLLM implements ModelInstaller { private fimSupported: boolean = false; private templateSupportsTools: boolean | undefined = undefined; + private modelInfoPromise: Promise | undefined = undefined; + private explicitContextLength: boolean; + constructor(options: LLMOptions) { super(options); + this.explicitContextLength = options.contextLength !== undefined; + } - if (options.model === "AUTODETECT") { - return; + private ensureModelInfo(): Promise { + if (this.modelInfoPromise) { + return this.modelInfoPromise; } + + if (this.model === "AUTODETECT") { + return Promise.resolve(); + } + const headers: Record = { "Content-Type": "application/json", }; @@ -176,7 +187,7 @@ class Ollama extends BaseLLM implements ModelInstaller { headers.Authorization = `Bearer ${this.apiKey}`; } - this.fetch(this.getEndpoint("api/show"), { + this.modelInfoPromise = this.fetch(this.getEndpoint("api/show"), { method: "POST", headers: headers, body: JSON.stringify({ name: this._getModel() }), @@ -194,15 +205,16 @@ class Ollama extends BaseLLM implements ModelInstaller { const params = []; for (const line of body.parameters.split("\n")) { let parts = line.match(/^(\S+)\s+((?:".*")|\S+)$/); - if (parts.length < 2) { + if (!parts || parts.length < 2) { continue; } let key = parts[1]; let value = parts[2]; switch (key) { case "num_ctx": - this._contextLength = - options.contextLength ?? Number.parseInt(value); + if (!this.explicitContextLength) { + this._contextLength = Number.parseInt(value); + } break; case "stop": if (!this.completionOptions.stop) { @@ -237,6 +249,8 @@ class Ollama extends BaseLLM implements ModelInstaller { .catch((e) => { // console.warn("Error calling the Ollama /api/show endpoint: ", e); }); + + return this.modelInfoPromise; } // Map of "continue model name" to Ollama actual model name @@ -410,6 +424,7 @@ class Ollama extends BaseLLM implements ModelInstaller { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { + await this.ensureModelInfo(); const headers: Record = { "Content-Type": "application/json", }; @@ -488,6 +503,7 @@ class Ollama extends BaseLLM implements ModelInstaller { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { + await this.ensureModelInfo(); const ollamaMessages = this._reorderMessagesForToolCompat( messages.map(this._convertToOllamaMessage), ); @@ -654,6 +670,7 @@ class Ollama extends BaseLLM implements ModelInstaller { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { + await this.ensureModelInfo(); const headers: Record = { "Content-Type": "application/json", };