Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 22, 2025

Summary

This PR fixes an issue where Ollama's user-configured num_ctx parameter 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_ctx parameter 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

  • Removed hardcoded num_ctx override: The native-ollama.ts provider no longer forces a num_ctx value in API calls
  • Added modelfile parameter parsing: The ollama.ts fetcher now parses the num_ctx value from the modelfile's parameters field
  • Respects user configuration: When a custom num_ctx is configured in the modelfile, it is used; otherwise, falls back to the model's default context window

Changes

Modified Files

  • src/api/providers/native-ollama.ts: Removed num_ctx from options in API calls
  • src/api/providers/fetchers/ollama.ts: Added logic to parse num_ctx from modelfile parameters
  • src/api/providers/__tests__/native-ollama.spec.ts: Added tests to verify num_ctx is not overridden
  • src/api/providers/fetchers/__tests__/ollama.test.ts: Added tests for parsing num_ctx from parameters

Testing

✅ All existing tests pass
✅ Added 4 new test cases covering:

  • Parsing num_ctx from modelfile parameters
  • Fallback behavior when num_ctx is not configured
  • Verification that num_ctx is not sent in API calls
  • Both streaming and non-streaming scenarios

Impact

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_ctx parameter handling in Ollama modelfiles to respect user configurations, with added tests for verification.

  • Behavior:
    • Removed hardcoded num_ctx in native-ollama.ts API calls.
    • ollama.ts now parses num_ctx from modelfile parameters.
    • Uses user-configured num_ctx if available, otherwise defaults.
  • Testing:
    • Added tests in native-ollama.spec.ts to ensure num_ctx is not overridden.
    • Added tests in ollama.test.ts for parsing num_ctx and fallback behavior.
  • Impact:
    • Allows user customization of context window size in Ollama models.

This description was created by Ellipsis for e55d35c. You can customize this summary. It will automatically update as commits are pushed.

- 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
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 22, 2025 22:23
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Aug 22, 2025
Copy link
Contributor Author

@roomote roomote bot left a 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)
Copy link
Contributor Author

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)
Copy link
Contributor Author

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:

Suggested change
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", () => {
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 22, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Aug 23, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 23, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Draft / In Progress] in Roo Code Roadmap Aug 24, 2025
@daniel-lxs daniel-lxs marked this pull request as draft August 24, 2025 21:07
@github-project-automation github-project-automation bot moved this from PR [Draft / In Progress] to Done in Roo Code Roadmap Sep 8, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 8, 2025
@hannesrudolph
Copy link
Collaborator

Related to: #7797

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Draft / In Progress size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants