Add Ollama API integration #96
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds comprehensive Ollama integration to the MIRIX desktop agent, enabling users to run local Large Language Models (LLMs) via Ollama as a privacy-focused, cost-effective alternative to cloud-based models. The implementation includes full backend support, real image recognition for vision models, bug fixes for multimodal content handling, frontend UI updates, and flexible configuration options.
What is Ollama?
Ollama is a tool that allows you to run LLMs locally on your machine. This PR enables MIRIX to use Ollama-hosted models (like Qwen, DeepSeek, Llama, etc.) instead of relying solely on cloud APIs.
🎯 Key Highlight: Vision Model Support
Ollama vision models can now see and analyze images! The implementation converts images from the database to base64 format and sends them to Ollama, enabling models like
qwen3-vl:235b-cloudto perform real image analysis, OCR, and visual understanding tasks.Key Changes
🔧 Backend Integration
1. Core Ollama Provider Support (
mirix/llm_api/llm_api_tools.py)ollamaas a new LLM provider option/v1, allowing us to reuse existing OpenAI client codehttp://localhost:11434/v12. Vision Model Image Support (
mirix/llm_api/ollama_helpers.py- NEW FILE)image_idto base64-encoded images that Ollama can actually see and analyzeFileManagerto retrieve image file paths from the databasedata:image/png;base64,<data>)source_url(direct URLs) and local file pathsqwen3-vl:235b-cloudto perform real image analysis3. Model Provider Detection (
mirix/agent/agent_wrapper.py)_determine_model_provider()to recognize Ollama models_create_llm_config_for_provider()to generate proper Ollama configurationsmodel_settings.ollama_base_url4. Model Constants (
mirix/agent/app_constants.py)OLLAMA_MODELSlist containing supported models:qwen3-vl:235b-cloud(multimodal vision model)deepseek-v3.1:671b-cloud(large reasoning model)llama3.2,mistral, and other popular Ollama models🐛 Critical Bug Fixes
1. Fixed UnboundLocalError in Retry Logic (
mirix/agent/agent.py)UnboundLocalError: cannot access local variable 'response'continuestatement after handling HTTP errorscontinueto properly restart the retry loop2. Fixed ValueError in Multimodal Token Counting (
mirix/utils/common.py)num_tokens_from_messages()crashed withValueError: Message has non-string valuewhen processing multimodal messages3. Fixed 400 Bad Request Errors from Ollama
400 Bad Requestwhen receiving messages with Gemini-specific image formatsCloudFileContentorimage_idfields in image contentollama_helpers.pyto preprocess messages and convert invalid formats to text placeholders4. Improved Multimodal Content Handling (
mirix/schemas/message.py)to_openai_dict()method to properly handle various image content types🎨 Frontend Updates
Settings Panel (
frontend/src/components/SettingsPanel.js)baseModelsandbaseMemoryModelsarraysApp Initialization (
frontend/src/App.js)gpt-4o-mini)⚙️ Configuration Files
1. New Dedicated Config (
mirix/configs/mirix_ollama.yaml- NEW FILE)2. Updated Existing Configs (
mirix.yamlandmirix_monitor.yaml)How to Use Ollama with MIRIX
Prerequisites
ollama pull qwen3-vl:235b-cloudcurl http://localhost:11434/api/tagsOption 1: Uncomment in Existing Config
Edit
mirix/configs/mirix_monitor.yaml(ormirix.yaml):Option 2: Use Dedicated Ollama Config
The
mirix_ollama.yamlfile is already configured - just use it when starting MIRIX.Option 3: Change via Frontend Settings
qwen3-vl:235b-cloud)Testing & Validation
✅ Functional Testing
qwen3-vl:235b-cloudmultimodal model✅ Bug Fix Validation
UnboundLocalErrorno longer occurs during API retriesValueErrorno longer occurs with multimodal messages✅ Backward Compatibility
gemini-2.0-flashfor desktop-agent✅ Configuration Testing
✅ Vision Model Testing
qwen3-vl:235b-cloudvision modelImportant Notes
Backward Compatibility
Design Decisions
Known Limitations
CloudFileContent) are converted to text placeholders for OllamaFiles Changed
New Files:
mirix/llm_api/ollama_helpers.py(91 lines - includes real image conversion to base64)mirix/configs/mirix_ollama.yaml(7 lines)Modified Files:
mirix/llm_api/llm_api_tools.py- Added Ollama provider handlingmirix/agent/agent_wrapper.py- Updated model provider detectionmirix/agent/app_constants.py- Added Ollama model constantsmirix/agent/agent.py- Fixed retry logic bugmirix/utils/common.py- Fixed multimodal token countingmirix/schemas/message.py- Improved multimodal handlingfrontend/src/components/SettingsPanel.js- Added Ollama models to UIfrontend/src/App.js- Preserved default settingsmirix/configs/mirix.yaml- Added commented Ollama optionsmirix/configs/mirix_monitor.yaml- Added commented Ollama optionsTotal Changes: 12 files changed, 271 insertions(+), 40 deletions(-)
Related Issues
This PR addresses the need for: