Garak's Ollama module provides two generator classes — OllamaGenerator for non-chat text generation and OllamaGeneratorChat for multi-turn conversations — both wrapping the local ollama Python client with configurable timeouts, parameter mapping, and optional API authentication. The generators support parameter suppression, Fibonacci backoff with automatic retries on empty responses, and fast-fail on missing-model errors (404), while treating the ollama dependency as optional at load time.
garak/generators/ollama.py provides two generator classes: OllamaGenerator (uses client.generate — non-chat mode) and OllamaGeneratorChat (uses client.chat — chat mode). The module-level DEFAULT_CLASS is "OllamaGeneratorChat". OllamaGenerator declares extra_dependency_names = ["ollama"], so the ollama Python package must be installed for this generator to load. OllamaGenerator sets parallel_capable = False, indicating it does not support concurrent generation requests.
OllamaGenerator.DEFAULT_PARAMS sets timeout=30 (seconds), host="127.0.0.1:11434", verify_ssl=None, extra_params=None, and suppressed_params=set(). The 30-second default timeout exists because Ollama can hang indefinitely on failures without it. OllamaGenerator passes extra_params as additional kwargs to ollama.Client(), which are treated as httpx.Client kwargs; this allows configuration such as custom SSL settings or proxy options.
OllamaGenerator._PARAM_MAP translates garak attribute names to Ollama options field names: max_tokens → num_predict (int), temperature → temperature (float), top_k → top_k (int), and seed → seed (int). Only these four parameters are forwarded to Ollama's options. OllamaGenerator._build_options() returns None when no generation parameters are set, which the Ollama client treats identically to omitting the options argument. Individual parameters can be excluded from Ollama requests via suppressed_params. OllamaGenerator.__init__() warns (but does not error) when an entry in suppressed_params is not a known key in _PARAM_MAP, and prints the valid keys.
A garak.site.yaml snippet to suppress top_k from Ollama requests illustrates how suppressed_params is configured per-generator — see Config loading and precedence for how site config is loaded.
plugins:
generators:
ollama:
OllamaGenerator:
suppressed_params:
- top_k
OllamaGenerator._validate_env_var() silently swallows APIKeyMissingError, making the API key optional for Ollama (no key is needed for local servers). If self.api_key is set, OllamaGenerator.__init__() adds an Authorization: Bearer <key> header to the Ollama client, enabling authenticated endpoints.
OllamaGeneratorChat._call_model() calls client.chat() with the full conversation history converted via _conversation_to_list(), while OllamaGenerator._call_model() calls client.generate() with only prompt.last_message().text. OllamaGeneratorChat._call_model() extracts the response text from response["message"]["content"] and wraps it in Message, returning [None] if the key is absent. OllamaGenerator._call_model() uses Fibonacci backoff (max 70 s) on GeneratorBackoffTrigger, and additionally retries up to 3 times (via @backoff.on_predicate) when the response is [None] or empty, because Ollama sometimes returns empty responses. The empty-response retry fix in garak/generators/ollama.py may change observed latency and output distributions in production Ollama-backed probing pipelines, as transient empty responses are now retried rather than returned immediately. HTTP 404 errors are re-raised immediately rather than triggering backoff, because 404 typically means the requested model is not found on the server. The _give_up predicate aborts Fibonacci backoff on 404 only when the cause is not a TimeoutException, preventing endless retries for genuinely missing models.