Skip to content

Commit 7430348

Browse files
Alexey Panfilovclaude
andcommitted
feat: add keep_alive config for Ollama to prevent model unloading
Add keep_alive field to ModelConfig and ollamaRequest. Set to -1 for the classifier (qwen3:0.6b) so it stays loaded in memory permanently instead of being evicted after 5 min of inactivity. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 567d635 commit 7430348

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

internal/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ type ModelConfig struct {
8383
APIKey string `yaml:"api_key"`
8484
MaxTokens int `yaml:"max_tokens"`
8585
BaseURL string `yaml:"base_url"`
86-
Vision bool `yaml:"vision"` // true if the model supports image input
87-
NoThink bool `yaml:"no_think"` // disable thinking mode (qwen3)
86+
Vision bool `yaml:"vision"` // true if the model supports image input
87+
NoThink bool `yaml:"no_think"` // disable thinking mode (qwen3)
88+
KeepAlive int `yaml:"keep_alive"` // seconds to keep model in memory; -1 = forever; 0 = provider default
8889
}
8990

9091
type ModelsConfig struct {

internal/llm/ollama.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ type ollamaProvider struct {
169169
provName string
170170
vision bool
171171
noThink bool
172+
keepAlive int // seconds; -1 = keep forever; 0 = provider default
172173
}
173174

174175
// NewOllama creates a provider for a local Ollama instance.
@@ -190,6 +191,7 @@ func NewOllama(cfg config.ModelConfig) (*ollamaProvider, error) {
190191
provName: "ollama",
191192
vision: cfg.Vision,
192193
noThink: cfg.NoThink,
194+
keepAlive: cfg.KeepAlive,
193195
}, nil
194196
}
195197

@@ -220,12 +222,13 @@ func (p *ollamaProvider) OllamaCloudModel() (string, bool) {
220222
// --- Ollama-native request/response types ---
221223

222224
type ollamaRequest struct {
223-
Model string `json:"model"`
224-
Messages []ollamaMessage `json:"messages"`
225-
Stream bool `json:"stream"`
226-
Think *bool `json:"think,omitempty"`
227-
Tools []ollamaTool `json:"tools,omitempty"`
228-
Options *ollamaOptions `json:"options,omitempty"`
225+
Model string `json:"model"`
226+
Messages []ollamaMessage `json:"messages"`
227+
Stream bool `json:"stream"`
228+
Think *bool `json:"think,omitempty"`
229+
Tools []ollamaTool `json:"tools,omitempty"`
230+
Options *ollamaOptions `json:"options,omitempty"`
231+
KeepAlive *int `json:"keep_alive,omitempty"` // seconds; -1 = forever
229232
}
230233

231234
type ollamaMessage struct {
@@ -281,6 +284,10 @@ func (p *ollamaProvider) Chat(ctx context.Context, messages []Message, systemPro
281284
f := false
282285
req.Think = &f
283286
}
287+
if p.keepAlive != 0 {
288+
ka := p.keepAlive
289+
req.KeepAlive = &ka
290+
}
284291
if len(tools) > 0 {
285292
req.Tools = buildOllamaTools(tools)
286293
}

0 commit comments

Comments
 (0)