BrainDrive-Core icon indicating copy to clipboard operation
BrainDrive-Core copied to clipboard

[BUG] Persona model settings not applied to Ollama - Context window and parameters ignored

Open bekmuradov opened this issue 1 year ago • 0 comments

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 options object
  • The parameter context_window needs to be mapped to Ollama's num_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:

  1. System prompts (personas) get truncated

    • Ollama defaults to 2048 token context window when num_ctx is 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
  2. 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
  3. 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

  1. Create a persona with:

    • Long system prompt (300+ words)
    • context_window: 8192
    • temperature: 0.8
  2. Send a chat request with retrieved context (RAG)

  3. Observe:

    • System prompt instructions not followed
    • Context window still limited to 2048 tokens
    • Temperature setting ignored

Expected Behavior

  • Persona context_window should map to Ollama's num_ctx parameter
  • All model settings should be passed in the options object
  • 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:

  1. Maps BrainDrive parameters to Ollama format:

    • context_window → num_ctx
    • max_tokens → num_predict
    • frequency_penalty/presence_penalty → repeat_penalty (with proper conversion)
    • stop_sequences → stop
  2. Wraps all parameters in options object per Ollama API specification

  3. 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

  1. Test with long system prompts:

    • Create persona with 500+ word system prompt
    • Set context_window: 8192
    • Verify system prompt is fully applied
  2. Test parameter application:

    • Set temperature: 1.5 in persona
    • Verify creative responses match high temperature
    • Test with low temperature (0.2) and verify coherent, focused responses
  3. Test RAG scenarios:

    • Send query with 4-5 document chunks
    • Set context_window: 8192
    • Verify all chunks are included in context
  4. Test with different models:

    • llama3.2:3b with num_ctx: 8192
    • qwen2.5:3b with num_ctx: 8192
    • mistral:7b with num_ctx: 16384

Additional Notes

Parameter Compatibility:

  • Ollama does NOT support frequency_penalty or presence_penalty (OpenAI-specific)
  • These are now mapped to Ollama's repeat_penalty with proper range conversion
  • context_window in 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

bekmuradov avatar Nov 11 '25 07:11 bekmuradov