[BUG] Persona model settings not applied to Ollama - Context window and parameters ignored
Summary
Persona model settings (specifically context_window, temperature, top_p, etc.) are not being passed correctly to the Ollama API, causing system prompts to be truncated and personas to not apply as expected.
Root Cause
Incorrect Parameter Structure:
- BrainDrive passes parameters at the top level of the API request
- Ollama API requires ALL model parameters to be wrapped in an
optionsobject - The parameter
context_windowneeds to be mapped to Ollama'snum_ctx
Current (Wrong):
{
"model": "llama3.2:3b",
"prompt": "...",
"context_window": 8192,
"temperature": 0.7
}
Expected (Correct):
{
"model": "llama3.2:3b",
"prompt": "...",
"options": {
"num_ctx": 8192,
"temperature": 0.7
}
}
Impact
Critical Issues:
-
System prompts (personas) get truncated
- Ollama defaults to 2048 token context window when
num_ctxis not set - When system prompt + context + message exceeds 2048 tokens, Ollama truncates from the beginning
- This causes persona instructions to be cut off, making the AI ignore persona settings
- Ollama defaults to 2048 token context window when
-
Temperature and other settings ignored
- User-configured temperature, top_p, top_k settings don't apply
- Models use their default values instead of persona-configured values
-
RAG applications severely affected
- With default 2048 context, only ~2-3 document chunks fit
- Large system prompts + document context easily exceed 2048 tokens
- Critical context gets dropped
Steps to Reproduce
-
Create a persona with:
- Long system prompt (300+ words)
context_window: 8192temperature: 0.8
-
Send a chat request with retrieved context (RAG)
-
Observe:
- System prompt instructions not followed
- Context window still limited to 2048 tokens
- Temperature setting ignored
Expected Behavior
- Persona
context_windowshould map to Ollama'snum_ctxparameter - All model settings should be passed in the
optionsobject - System prompts should never be truncated (protected context allocation)
- Settings like temperature, top_p, top_k should apply correctly
Solution Implemented
Created _build_ollama_options() method that:
-
Maps BrainDrive parameters to Ollama format:
context_window→num_ctxmax_tokens→num_predictfrequency_penalty/presence_penalty→repeat_penalty(with proper conversion)stop_sequences→stop
-
Wraps all parameters in
optionsobject per Ollama API specification -
Maintains backward compatibility with existing persona configurations
Technical Details
Files Changed:
backend/app/ai_providers/ollama.py
Methods Updated:
_call_ollama_api()- Non-streaming requests_stream_ollama_api()- Streaming requests
New Method:
_build_ollama_options()- Parameter mapping and transformation
Reference Documentation
- Ollama API Docs: https://github.com/ollama/ollama/blob/main/docs/api.md
- Ollama Modelfile Parameters: https://github.com/ollama/ollama/blob/main/docs/modelfile.md
Testing Recommendations
-
Test with long system prompts:
- Create persona with 500+ word system prompt
- Set
context_window: 8192 - Verify system prompt is fully applied
-
Test parameter application:
- Set
temperature: 1.5in persona - Verify creative responses match high temperature
- Test with low temperature (0.2) and verify coherent, focused responses
- Set
-
Test RAG scenarios:
- Send query with 4-5 document chunks
- Set
context_window: 8192 - Verify all chunks are included in context
-
Test with different models:
- llama3.2:3b with
num_ctx: 8192 - qwen2.5:3b with
num_ctx: 8192 - mistral:7b with
num_ctx: 16384
- llama3.2:3b with
Additional Notes
Parameter Compatibility:
- Ollama does NOT support
frequency_penaltyorpresence_penalty(OpenAI-specific) - These are now mapped to Ollama's
repeat_penaltywith proper range conversion context_windowin persona schema is preserved - mapping happens at provider level
No Schema Changes Required:
- Persona model remains unchanged
- Only provider implementation updated
- Existing personas work without modification
Priority: High Severity: Critical - Affects core persona and RAG functionality