fix: allow configuring Ollama context size via OLLAMA_NUM_CTX env var#1088
Open
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
Open
fix: allow configuring Ollama context size via OLLAMA_NUM_CTX env var#1088octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
Conversation
…fixes ItzCrazyKns#981) The num_ctx parameter was hardcoded to 32000 across all Ollama LLM methods, causing Ollama to reload models with a 32K context even when a larger context was configured in the model's settings. This change reads the context size from the OLLAMA_NUM_CTX environment variable, falling back to 32000 if not set.
Contributor
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/lib/models/providers/ollama/ollamaLLM.ts">
<violation number="1" location="src/lib/models/providers/ollama/ollamaLLM.ts:24">
P2: `OLLAMA_NUM_CTX` parsing lacks validation; non-numeric truthy values produce `NaN`, which is then sent as `num_ctx` for all Ollama calls.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
|
||
| const DEFAULT_OLLAMA_NUM_CTX = 32000; | ||
| const numCtx = process.env.OLLAMA_NUM_CTX | ||
| ? parseInt(process.env.OLLAMA_NUM_CTX, 10) |
Contributor
There was a problem hiding this comment.
P2: OLLAMA_NUM_CTX parsing lacks validation; non-numeric truthy values produce NaN, which is then sent as num_ctx for all Ollama calls.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/models/providers/ollama/ollamaLLM.ts, line 24:
<comment>`OLLAMA_NUM_CTX` parsing lacks validation; non-numeric truthy values produce `NaN`, which is then sent as `num_ctx` for all Ollama calls.</comment>
<file context>
@@ -19,6 +19,11 @@ type OllamaConfig = {
+const DEFAULT_OLLAMA_NUM_CTX = 32000;
+const numCtx = process.env.OLLAMA_NUM_CTX
+ ? parseInt(process.env.OLLAMA_NUM_CTX, 10)
+ : DEFAULT_OLLAMA_NUM_CTX;
+
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #981
Problem
The
num_ctxparameter in the Ollama LLM provider was hardcoded to32000across all four generation methods (generateText,streamText,generateObject,streamObject). This caused Ollama to unload and reload models with a 32K context window, even when users had configured a larger context size in their Ollama model settings. Using the OpenAI-compatible provider with the same model would respect the configured context size, but the native Ollama provider would not.Solution
Read the context size from the
OLLAMA_NUM_CTXenvironment variable, falling back to32000if not set. This maintains backward compatibility while allowing users to configure the context size to match their model's capabilities:# Example: set 128K context for all Ollama models OLLAMA_NUM_CTX=131072The fix also adds
num_ctxtogenerateObjectandstreamObjectmethods, which previously omitted it entirely (inconsistent with the other methods).Testing
OLLAMA_NUM_CTXis not setOLLAMA_NUM_CTX=131072will pass 131072 as the context size to all Ollama callsSummary by cubic
Make Ollama context size configurable via
OLLAMA_NUM_CTXinstead of hardcoded 32K, avoiding unnecessary model reloads and matching each model’s configured window. Also addsnum_ctxto object generation methods for consistency.OLLAMA_NUM_CTXat module load; fallback to 32000 if unset.num_ctxtogenerateText,streamText,generateObject, andstreamObject.OLLAMA_NUM_CTXto match your model’s context size.Written for commit a520ace. Summary will update on new commits.