Skip to main content

Overview

SmartEat AI uses Ollama to run large language models (LLMs) locally, providing private, fast AI inference without external API dependencies. The system is optimized for environments with limited GPU memory (8GB VRAM).
Why Ollama?
  • Privacy: All data stays on your infrastructure
  • Cost: No per-token API fees
  • Speed: Low-latency inference for real-time chat
  • Flexibility: Easy model switching and customization

Docker Configuration

Ollama runs as a containerized service with GPU support.

docker-compose.yml

  • Port 11434: Ollama API endpoint
  • Volume: Persists downloaded models across container restarts
  • OLLAMA_CONTEXT_LENGTH: Maximum tokens for context (32K)
  • GPU Reservation: Enables NVIDIA GPU acceleration
  • NUM_PARALLEL=1: Prevents OOM with limited VRAM

Model Setup

Step 1: Start the Ollama Service

Step 2: Download the Model

Access the Ollama container and pull the model:

Step 3: Verify Installation

Backend Integration

Environment Variables

Add to your .env file (in project root):

LangChain Configuration

File: backend/app/core/config_ollama.py
Vector Database: ChromaDB integration exists but is not active. Direct PostgreSQL queries are used instead due to filtering limitations.

GPU Support

NVIDIA GPU Setup

1

Install NVIDIA Drivers

Ensure you have NVIDIA drivers installed on your host:
Should display GPU information.
2

Install NVIDIA Container Toolkit

3

Verify GPU in Container

Should show GPU details from inside the container.

CPU-Only Mode

If you don’t have a GPU, modify docker-compose.yml:
Performance Impact: CPU inference is 10-50x slower than GPU. Expect 2-10 seconds per response instead of 200-500ms.

Performance Tuning

Context Window Optimization

The system uses a multi-layered approach to manage context:

Ollama Level

OLLAMA_CONTEXT_LENGTH=32768Maximum supported by the service

Model Level

num_ctx=16384Actual context used per request

Agent Level

MAX_CONTEXT_TOKENS=10000Conversation history limit

Memory Management

8GB VRAM Configuration (recommended):
4GB VRAM Configuration (smaller model):

Troubleshooting

Cause: Ollama service is not running.Solution:
Cause: Model is too large for available VRAM.Solutions:
  1. Use a smaller model (Phi-3)
  2. Reduce num_ctx in config
  3. Set OLLAMA_NUM_PARALLEL=1
  4. Close other GPU applications
Cause: Model not downloaded or wrong name in .env.Solution:
Possible causes:
  • Running on CPU instead of GPU
  • Context window too large
  • Model loading overhead
Solutions:
  1. Verify GPU is being used:
  2. Reduce num_ctx to 8192 or 4096
  3. Keep OLLAMA_MAX_LOADED_MODELS=1 to avoid reloading

Usage in Agent

The agent uses Ollama for two purposes:

1. Conversational AI (Primary)

2. Dietary Validation (Secondary)

Used in generate_weekly_plan.py to validate recipes against restrictions:

Monitoring

Real-time Metrics

Performance Benchmarks

Typical performance on an 8GB GPU:

Advanced Configuration

Custom Model Parameters

Create a Modelfile for fine-tuned behavior:

Multiple Model Support

Run different models for different tasks:

Production Considerations

Load Balancing

Run multiple Ollama instances behind a load balancer for high traffic

Model Caching

Keep models loaded in memory with OLLAMA_MAX_LOADED_MODELS=1

Monitoring

Track inference latency, GPU utilization, and error rates

Fallback

Implement graceful degradation if Ollama is unavailable
Rate Limiting: With OLLAMA_NUM_PARALLEL=1, only one request is processed at a time. Consider horizontal scaling for production.

Resources

Ollama Documentation

Official Ollama setup and API reference

LangChain Ollama

LangChain integration guide

Model Library

Browse available models

NVIDIA Toolkit

GPU container setup

Related Documentation