Skip to content

Commit d324f6d

Browse files
authored
feat: adding num_ctx to set the size of the model context window (#75)
* feat: adding num_ctx to set the size of the model context window. * docs: updating main README.md * feat: update help removing ollama num_ctx default, since this can change in the future
1 parent 6e995b2 commit d324f6d

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,20 @@ MCP Client for Ollama (`ollmcp`) is a modern, interactive terminal application (
5656

5757
- 🌐 **Multi-Server Support**: Connect to multiple MCP servers simultaneously
5858
- 🚀 **Multiple Transport Types**: Supports STDIO, SSE, and Streamable HTTP server connections
59-
- 🎨 **Rich Terminal Interface**: Interactive console UI
59+
- 🎨 **Rich Terminal Interface**: Interactive console UI with modern styling
6060
- 🌊 **Streaming Responses**: View model outputs in real-time as they're generated
6161
- 🛠️ **Tool Management**: Enable/disable specific tools or entire servers during chat sessions
6262
- 🧑‍💻 **Human-in-the-Loop (HIL)**: Review and approve tool executions before they run for enhanced control and safety
63-
- 🎮 **Advanced Model Configuration**: Fine-tune 10+ model parameters including temperature, sampling, repetition control, and more
63+
- 🎮 **Advanced Model Configuration**: Fine-tune 15+ model parameters including context window size, temperature, sampling, repetition control, and more
6464
- 💬 **System Prompt Customization**: Define and edit the system prompt to control model behavior and persona
65+
- 🧠 **Context Window Control**: Adjust the context window size (num_ctx) to handle longer conversations and complex tasks
6566
- 🎨 **Enhanced Tool Display**: Beautiful, structured visualization of tool executions with JSON syntax highlighting
6667
- 🧠 **Context Management**: Control conversation memory with configurable retention settings
6768
- 🤔 **Thinking Mode**: Advanced reasoning capabilities with visible thought processes for supported models (e.g., gpt-oss, deepseek-r1, qwen3, etc.)
6869
- 🗣️ **Cross-Language Support**: Seamlessly work with both Python and JavaScript MCP servers
6970
- 🔍 **Auto-Discovery**: Automatically find and use Claude's existing MCP server configurations
7071
- 🔁 **Dynamic Model Switching**: Switch between any installed Ollama model without restarting
71-
- 💾 **Configuration Persistence**: Save and load tool preferences between sessions
72+
- 💾 **Configuration Persistence**: Save and load tool preferences and model settings between sessions
7273
- 🔄 **Server Reloading**: Hot-reload MCP servers during development without restarting the client
7374
-**Fuzzy Autocomplete**: Interactive, arrow-key command autocomplete with descriptions
7475
- 🏷️ **Dynamic Prompt**: Shows current model, thinking mode, and enabled tools
@@ -298,6 +299,8 @@ The `model-config` (`mc`) command opens the advanced model settings interface, a
298299

299300
#### Key Parameters
300301

302+
- **System Prompt**: Set the model's role and behavior to guide responses.
303+
- **Context Window (num_ctx)**: Set how much chat history the model uses. Balance with memory usage and performance.
301304
- **Keep Tokens**: Prevent important tokens from being dropped
302305
- **Max Tokens**: Limit response length (0 = auto)
303306
- **Seed**: Make outputs reproducible (set to -1 for random)
@@ -309,7 +312,7 @@ The `model-config` (`mc`) command opens the advanced model settings interface, a
309312

310313
#### Commands
311314

312-
- Enter parameter numbers `1-14` to edit settings
315+
- Enter parameter numbers `1-15` to edit settings
313316
- Enter `sp` to edit the system prompt
314317
- Use `u1`, `u2`, etc. to unset parameters, or `uall` to reset all
315318
- `h`/`help`: Show parameter details and tips
@@ -324,6 +327,7 @@ The `model-config` (`mc`) command opens the advanced model settings interface, a
324327
- **Reduce Repeats:** `repeat_penalty: 1.1-1.3`, `presence_penalty: 0.2`, `frequency_penalty: 0.3`
325328
- **Balanced:** `temperature: 0.7`, `top_p: 0.9`, `typical_p: 0.7`
326329
- **Reproducible:** `seed: 42`, `temperature: 0.0`
330+
- **Large Context:** `num_ctx: 8192` or higher for complex conversations requiring more context
327331

328332
> [!TIP]
329333
> All parameters default to unset, letting Ollama use its own optimized values. Use `help` in the config menu for details and recommendations. Changes are saved with your configuration.

mcp_client_for_ollama/config/defaults.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def default_config() -> dict:
3737
"repeat_penalty": None,
3838
"presence_penalty": None,
3939
"frequency_penalty": None,
40-
"stop": None
40+
"stop": None,
41+
"num_ctx": None
4142
},
4243
"displaySettings": {
4344
"showToolExecution": True,

mcp_client_for_ollama/config/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ def _validate_config(self, config_data: Dict[str, Any]) -> Dict[str, Any]:
241241
validated["modelConfig"]["frequency_penalty"] = model_config["frequency_penalty"] if model_config["frequency_penalty"] is not None else None
242242
if "stop" in model_config:
243243
validated["modelConfig"]["stop"] = model_config["stop"] if model_config["stop"] is not None else None
244+
if "num_ctx" in model_config:
245+
validated["modelConfig"]["num_ctx"] = model_config["num_ctx"] if model_config["num_ctx"] is not None else None
244246

245247
if "displaySettings" in config_data and isinstance(config_data["displaySettings"], dict):
246248
if "showToolExecution" in config_data["displaySettings"]:

mcp_client_for_ollama/models/config_manager.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, console: Optional[Console] = None):
4040
self.presence_penalty = None # float
4141
self.frequency_penalty = None # float
4242
self.stop = None # list[str]
43+
self.num_ctx = None # int
4344

4445
# Parameter explanations
4546
self.parameter_explanations = {
@@ -126,6 +127,12 @@ def __init__(self, console: Optional[Console] = None):
126127
"range": "0–8 strings, each ≤ 255 characters",
127128
"effect": "Provides precise control over where generation ends.",
128129
"recommendation": "Use for controlling dialog format or preventing the model from continuing beyond desired points."
130+
},
131+
"num_ctx": {
132+
"description": "Sets the size of the context window used to generate the next token.",
133+
"range": "1 – model maximum (e.g., 1 – 32768 for qwen3:0.6b); model-dependent",
134+
"effect": "Controls how much conversation history and context the model can access when generating responses.",
135+
"recommendation": "Use higher values for complex conversations requiring more context; balance with memory usage and performance."
129136
}
130137
}
131138

@@ -149,7 +156,8 @@ def get_config(self) -> Dict[str, Any]:
149156
"repeat_penalty": self.repeat_penalty,
150157
"presence_penalty": self.presence_penalty,
151158
"frequency_penalty": self.frequency_penalty,
152-
"stop": self.stop
159+
"stop": self.stop,
160+
"num_ctx": self.num_ctx
153161
}
154162

155163
def get_ollama_options(self) -> Dict[str, Any]:
@@ -188,6 +196,8 @@ def get_ollama_options(self) -> Dict[str, Any]:
188196
options["frequency_penalty"] = self.frequency_penalty
189197
if self.stop is not None:
190198
options["stop"] = self.stop
199+
if self.num_ctx is not None:
200+
options["num_ctx"] = self.num_ctx
191201
return options
192202

193203
def get_system_prompt(self) -> str:
@@ -232,6 +242,8 @@ def set_config(self, config: Dict[str, Any]) -> None:
232242
self.frequency_penalty = config["frequency_penalty"]
233243
if "stop" in config:
234244
self.stop = config["stop"]
245+
if "num_ctx" in config:
246+
self.num_ctx = config["num_ctx"]
235247

236248
def display_current_config(self) -> None:
237249
"""Display the current model configuration."""
@@ -260,7 +272,8 @@ def format_value(value):
260272
f"[bold][orange3]10.[/orange3] repeat_penalty:[/bold] {format_value(self.repeat_penalty)}\n"
261273
f"[bold][orange3]11.[/orange3] presence_penalty:[/bold] {format_value(self.presence_penalty)}\n"
262274
f"[bold][orange3]12.[/orange3] frequency_penalty:[/bold] {format_value(self.frequency_penalty)}\n"
263-
f"[bold][orange3]13.[/orange3] stop:[/bold] {format_value(self.stop)}",
275+
f"[bold][orange3]13.[/orange3] stop:[/bold] {format_value(self.stop)}\n"
276+
f"[bold][orange3]14.[/orange3] num_ctx:[/bold] {format_value(self.num_ctx)}",
264277
title="[bold blue]🎮 Model Parameters[/bold blue]",
265278
border_style="blue", expand=False))
266279
self.console.print("\n[bold yellow]Note:[/bold yellow] Unset values will use Ollama's defaults.")
@@ -312,7 +325,7 @@ def display_parameter_explanations(self) -> None:
312325
for param in [
313326
"num_keep", "seed", "num_predict", "top_k", "top_p", "min_p",
314327
"typical_p", "repeat_last_n", "temperature", "repeat_penalty",
315-
"presence_penalty", "frequency_penalty", "stop"
328+
"presence_penalty", "frequency_penalty", "stop", "num_ctx"
316329
]:
317330
info = self.parameter_explanations[param]
318331
table.add_row(
@@ -433,6 +446,7 @@ def configure_model_interactive(self, clear_console_func: Optional[Callable] = N
433446
self.presence_penalty = None
434447
self.frequency_penalty = None
435448
self.stop = None
449+
self.num_ctx = None
436450
result_message = "[green]All parameters unset (using Ollama defaults).[/green]"
437451
result_style = "green"
438452
continue
@@ -505,6 +519,10 @@ def configure_model_interactive(self, clear_console_func: Optional[Callable] = N
505519
self.stop = None
506520
result_message = "[green]stop unset (using Ollama default).[/green]"
507521
result_style = "green"
522+
case 14:
523+
self.num_ctx = None
524+
result_message = "[green]num_ctx unset (using Ollama default).[/green]"
525+
result_style = "green"
508526
case _:
509527
result_message = "[red]Invalid parameter number.[/red]"
510528
result_style = "red"
@@ -709,6 +727,20 @@ def configure_model_interactive(self, clear_console_func: Optional[Callable] = N
709727
result_message = "[green]stop sequences cleared.[/green]"
710728
result_style = "green"
711729

730+
case "14":
731+
try:
732+
new_value = IntPrompt.ask("Context Size (num_ctx, size of context window)", default=self.num_ctx)
733+
if new_value >= 1:
734+
self.num_ctx = new_value
735+
result_message = f"[green]num_ctx set to {new_value}.[/green]"
736+
result_style = "green"
737+
else:
738+
result_message = "[red]num_ctx must be a positive integer.[/red]"
739+
result_style = "red"
740+
except ValueError:
741+
result_message = "[red]Please enter a valid integer.[/red]"
742+
result_style = "red"
743+
712744
case _:
713745
result_message = "[red]Invalid selection. Please choose a valid option.[/red]"
714746
result_style = "red"

0 commit comments

Comments
 (0)