snippets

How do I use Ollama or llama.cpp as the ML backend?

The openai backend of Mail::MIMEDefang::ML talks to any self-hosted server that implements the OpenAI chat completions API: Ollama, llama.cpp, vLLM or LM Studio. Nothing is sent to third parties: point it at a server you run. Use a model with at least 7-8 billion parameters, and remember that generative models take seconds per message on CPU.

use Mail::MIMEDefang::ML;

$Mail::MIMEDefang::ML::Config{backend}            = 'openai';
$Mail::MIMEDefang::ML::Config{openai}{base_url}   = 'http://127.0.0.1:11434/v1';  # Ollama
$Mail::MIMEDefang::ML::Config{openai}{model}      = 'qwen3:8b';
# Generative models are slow on CPU: allow more time, but keep it
# below the multiplexor busy timeout
$Mail::MIMEDefang::ML::Config{timeout}            = 60;
$Mail::MIMEDefang::ML::Config{openai}{body_head_chars} = 3000;
$Mail::MIMEDefang::ML::Config{openai}{body_tail_chars} = 500;

The rest of the filter is the same as with the other backends, see How do I classify mail with a local machine-learning model?.

If you prefer the faster encoder models (Laya, GLiClass), which classify a message in a single forward pass, run their servers from the official mimedefang/laya and mimedefang/gliclass Docker images and set backend to laya or gliclass.