LlmGuard.Config (LlmGuard v0.3.1)
View SourceConfiguration management for LlmGuard.
This module provides a centralized configuration system for all LlmGuard security features, including detection toggles, thresholds, rate limiting, and audit logging.
Configuration Options
Detection Toggles
:prompt_injection_detection- Enable/disable prompt injection detection (default:true):jailbreak_detection- Enable/disable jailbreak detection (default:true):data_leakage_prevention- Enable/disable data leakage prevention (default:true):content_moderation- Enable/disable content moderation (default:true)
Thresholds
:confidence_threshold- Minimum confidence for detection (0.0-1.0, default:0.7):max_input_length- Maximum input length in characters (default:10_000):max_output_length- Maximum output length in characters (default:10_000)
Custom Detectors
:enabled_detectors- List of custom detector modules to enable (default:[])
Rate Limiting
:rate_limiting- Map with rate limiting configuration::requests_per_minute- Maximum requests per minute:tokens_per_minute- Maximum tokens per minute:strategy- Rate limiting strategy (:token_bucket,:sliding_window)
Audit Logging
:audit_logging- Map with audit configuration::enabled- Enable audit logging (default:false):backend- Backend to use (:ets,:database,:external):log_safe_inputs- Whether to log inputs that pass validation (default:false)
Caching
:caching- Map with caching configuration::enabled- Enable pattern and result caching (default:false):pattern_cache- Enable compiled pattern caching (default:true):result_cache- Enable detection result caching (default:true):result_ttl_seconds- TTL for cached results (default:300):max_cache_entries- Maximum cached results (default:10_000)
Examples
# Create default configuration
config = LlmGuard.Config.new()
# Create custom configuration
config = LlmGuard.Config.new(
confidence_threshold: 0.85,
max_input_length: 5000,
prompt_injection_detection: true,
rate_limiting: %{
requests_per_minute: 100,
tokens_per_minute: 200_000
}
)
# Update existing configuration
updated = LlmGuard.Config.update(config, confidence_threshold: 0.9)
# Get configuration value
threshold = LlmGuard.Config.get(config, :confidence_threshold)
# Get list of enabled detectors
detectors = LlmGuard.Config.enabled_detectors(config)
Summary
Functions
Returns the audit logging configuration if configured.
Returns the caching configuration if configured.
Checks if caching is enabled.
Returns a list of enabled detector types based on configuration.
Creates a configuration from a map.
Gets a configuration value by key.
Creates a new configuration with the given options.
Returns the rate limiting configuration if configured.
Converts a configuration struct to a map.
Updates an existing configuration with new values.
Types
@type t() :: %LlmGuard.Config{ audit_logging: map() | nil, caching: map() | nil, confidence_threshold: float(), content_moderation: boolean(), custom_options: map(), data_leakage_prevention: boolean(), enabled_detectors: [atom()], jailbreak_detection: boolean(), max_input_length: pos_integer(), max_output_length: pos_integer(), prompt_injection_detection: boolean(), rate_limiting: map() | nil }
Functions
Returns the audit logging configuration if configured.
Parameters
config- Configuration struct
Returns
Audit configuration map or nil if not configured.
Returns the caching configuration if configured.
Parameters
config- Configuration struct
Returns
Caching configuration map or nil if not configured.
Checks if caching is enabled.
Parameters
config- Configuration struct
Returns
true if caching is enabled, false otherwise.
Returns a list of enabled detector types based on configuration.
Combines built-in detector flags with custom enabled detectors.
Parameters
config- Configuration struct
Returns
List of detector type atoms.
Examples
iex> config = LlmGuard.Config.new()
iex> detectors = LlmGuard.Config.enabled_detectors(config)
iex> :prompt_injection in detectors
true
Creates a configuration from a map.
Parameters
map- Map with configuration values
Returns
A validated Config struct.
Gets a configuration value by key.
Returns the value if the key exists, otherwise returns the default value.
Parameters
config- Configuration structkey- Key to retrievedefault- Default value if key doesn't exist (default:nil)
Examples
iex> config = LlmGuard.Config.new(confidence_threshold: 0.8)
iex> LlmGuard.Config.get(config, :confidence_threshold)
0.8
iex> LlmGuard.Config.get(config, :missing_key, :default)
:default
Creates a new configuration with the given options.
Merges provided options with defaults and validates all values.
Parameters
opts- Keyword list of configuration options
Returns
A validated Config struct.
Raises
ArgumentError if any configuration value is invalid.
Examples
iex> config = LlmGuard.Config.new()
iex> config.confidence_threshold
0.7
iex> config = LlmGuard.Config.new(confidence_threshold: 0.85)
iex> config.confidence_threshold
0.85
Returns the rate limiting configuration if configured.
Parameters
config- Configuration struct
Returns
Rate limiting configuration map or nil if not configured.
Converts a configuration struct to a map.
Parameters
config- Configuration struct
Returns
Map representation of the configuration.
Updates an existing configuration with new values.
Returns a new configuration struct with the updates applied. The original configuration is not modified.
Parameters
config- Existing configuration structupdates- Keyword list of updates to apply
Returns
A new validated Config struct with updates applied.
Examples
iex> config = LlmGuard.Config.new()
iex> updated = LlmGuard.Config.update(config, confidence_threshold: 0.9)
iex> updated.confidence_threshold
0.9