-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: respect user-configured num_ctx in Ollama modelfiles #7342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Parse num_ctx from parameters field in Ollama API response - Use configured num_ctx instead of default context_length when available - Remove num_ctx override in native-ollama.ts to respect model configuration - Add tests to verify num_ctx parsing and usage Fixes #7159
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewing my own code is like debugging in production - technically possible but morally questionable.
| if (rawModel.parameters) { | ||
| // The parameters field contains modelfile parameters as a string | ||
| // Look for num_ctx setting in the format "num_ctx <value>" | ||
| const numCtxMatch = rawModel.parameters.match(/num_ctx\s+(\d+)/i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this regex pattern comprehensive enough? The current pattern /num_ctx\s+(\d+)/i might miss valid modelfile formats like:
- Values with underscores:
num_ctx 32_768 - Scientific notation:
num_ctx 1e5
Might want to consider a more flexible pattern or at least document the expected format.
| // Look for num_ctx setting in the format "num_ctx <value>" | ||
| const numCtxMatch = rawModel.parameters.match(/num_ctx\s+(\d+)/i) | ||
| if (numCtxMatch && numCtxMatch[1]) { | ||
| configuredNumCtx = parseInt(numCtxMatch[1], 10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we validate the parsed num_ctx value here? An extremely large value (like Number.MAX_SAFE_INTEGER) or zero could cause issues. Consider adding bounds checking:
| configuredNumCtx = parseInt(numCtxMatch[1], 10) | |
| if (numCtxMatch && numCtxMatch[1]) { | |
| const parsed = parseInt(numCtxMatch[1], 10) | |
| // Validate reasonable bounds (e.g., 512 to 1M tokens) | |
| if (parsed >= 512 && parsed <= 1048576) { | |
| configuredNumCtx = parsed | |
| } | |
| } |
| }) | ||
| }) | ||
|
|
||
| it("should parse num_ctx from parameters field when present", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test coverage! Though it might be worth adding edge case tests for:
- Invalid num_ctx values (negative, zero, non-numeric)
- Very large values
- Malformed parameters string
- Multiple num_ctx entries (which one wins?)
| stream: true, | ||
| options: { | ||
| num_ctx: modelInfo.contextWindow, | ||
| // Don't override num_ctx - let Ollama use the model's configured value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a more detailed comment explaining why we're not setting num_ctx and how Ollama handles the default. Future maintainers (including future me) might wonder why this was removed.
|
Related to: #7797 |
Summary
This PR fixes an issue where Ollama's user-configured
num_ctxparameter in modelfiles was being overridden by the application, preventing users from customizing their model's context window size.Problem
Previously, the application was hardcoding the
num_ctxparameter when making API calls to Ollama, which would override any custom context window configuration set in the user's modelfile. This prevented users from optimizing their models for their specific use cases.Solution
num_ctxoverride: Thenative-ollama.tsprovider no longer forces anum_ctxvalue in API callsollama.tsfetcher now parses thenum_ctxvalue from the modelfile's parameters fieldnum_ctxis configured in the modelfile, it is used; otherwise, falls back to the model's default context windowChanges
Modified Files
src/api/providers/native-ollama.ts: Removednum_ctxfrom options in API callssrc/api/providers/fetchers/ollama.ts: Added logic to parsenum_ctxfrom modelfile parameterssrc/api/providers/__tests__/native-ollama.spec.ts: Added tests to verifynum_ctxis not overriddensrc/api/providers/fetchers/__tests__/ollama.test.ts: Added tests for parsingnum_ctxfrom parametersTesting
✅ All existing tests pass
✅ Added 4 new test cases covering:
num_ctxfrom modelfile parametersnum_ctxis not configurednum_ctxis not sent in API callsImpact
This change allows users to customize their Ollama models' context window size through modelfiles, enabling better performance optimization for their specific use cases without application interference.
Important
Fixes
num_ctxparameter handling in Ollama modelfiles to respect user configurations, with added tests for verification.num_ctxinnative-ollama.tsAPI calls.ollama.tsnow parsesnum_ctxfrom modelfile parameters.num_ctxif available, otherwise defaults.native-ollama.spec.tsto ensurenum_ctxis not overridden.ollama.test.tsfor parsingnum_ctxand fallback behavior.This description was created by
for e55d35c. You can customize this summary. It will automatically update as commits are pushed.