Skip to content

Commit df38bfe

Browse files
committed
fix: do not override Ollama model num_ctx configuration
The native Ollama handler was explicitly setting num_ctx to modelInfo.contextWindow, which overrode any user-configured num_ctx value in their Ollama model configuration. This caused issues for users who had specifically configured their models with custom context sizes to fit within their available memory. This fix removes the num_ctx override, allowing Ollama to use the model's configured value as intended. Fixes #7159
1 parent 185365a commit df38bfe

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/api/providers/__tests__/native-ollama.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,69 @@ describe("NativeOllamaHandler", () => {
122122
})
123123
})
124124

125+
describe("num_ctx handling", () => {
126+
it("should not override num_ctx in createMessage", async () => {
127+
// Mock the chat response
128+
mockChat.mockImplementation(async function* () {
129+
yield {
130+
message: { content: "Test response" },
131+
eval_count: 1,
132+
prompt_eval_count: 1,
133+
}
134+
})
135+
136+
const systemPrompt = "You are a helpful assistant"
137+
const messages = [{ role: "user" as const, content: "Test" }]
138+
139+
const stream = handler.createMessage(systemPrompt, messages)
140+
141+
// Consume the stream
142+
for await (const _ of stream) {
143+
// Just consume
144+
}
145+
146+
// Verify that num_ctx was NOT set in the options
147+
expect(mockChat).toHaveBeenCalledWith(
148+
expect.objectContaining({
149+
model: "llama2",
150+
messages: expect.any(Array),
151+
stream: true,
152+
options: expect.objectContaining({
153+
temperature: 0,
154+
}),
155+
}),
156+
)
157+
158+
// Specifically check that num_ctx is not in the options
159+
const callArgs = mockChat.mock.calls[0][0]
160+
expect(callArgs.options).not.toHaveProperty("num_ctx")
161+
})
162+
163+
it("should not override num_ctx in completePrompt", async () => {
164+
mockChat.mockResolvedValue({
165+
message: { content: "Test response" },
166+
})
167+
168+
await handler.completePrompt("Test prompt")
169+
170+
// Verify that num_ctx was NOT set in the options
171+
expect(mockChat).toHaveBeenCalledWith(
172+
expect.objectContaining({
173+
model: "llama2",
174+
messages: expect.any(Array),
175+
stream: false,
176+
options: expect.objectContaining({
177+
temperature: 0,
178+
}),
179+
}),
180+
)
181+
182+
// Specifically check that num_ctx is not in the options
183+
const callArgs = mockChat.mock.calls[0][0]
184+
expect(callArgs.options).not.toHaveProperty("num_ctx")
185+
})
186+
})
187+
125188
describe("error handling", () => {
126189
it("should handle connection refused errors", async () => {
127190
const error = new Error("ECONNREFUSED") as any

src/api/providers/native-ollama.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
181181
messages: ollamaMessages,
182182
stream: true,
183183
options: {
184-
num_ctx: modelInfo.contextWindow,
184+
// Don't override num_ctx - let Ollama use the model's configured value
185185
temperature: this.options.modelTemperature ?? (useR1Format ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0),
186186
},
187187
})
@@ -270,6 +270,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
270270
messages: [{ role: "user", content: prompt }],
271271
stream: false,
272272
options: {
273+
// Don't override num_ctx - let Ollama use the model's configured value
273274
temperature: this.options.modelTemperature ?? (useR1Format ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0),
274275
},
275276
})

0 commit comments

Comments
 (0)