|
| 1 | += ollama_moderation |
| 2 | +// tag::single-source[] |
| 3 | +:type: processor |
| 4 | +:page-beta: true |
| 5 | +:categories: ["AI"] |
| 6 | + |
| 7 | +component_type_dropdown::[] |
| 8 | + |
| 9 | +include::components:partial$ollama_warning.adoc[] |
| 10 | + |
| 11 | +Generates responses to messages in a chat conversation using the Ollama API, and checks the responses to make sure they do not violate https://mlcommons.org/2024/04/mlc-aisafety-v0-5-poc/[safety or security standards^]. |
| 12 | + |
| 13 | +ifndef::env-cloud[] |
| 14 | +Introduced in version 4.45.0. |
| 15 | +endif::[] |
| 16 | + |
| 17 | +[tabs] |
| 18 | +====== |
| 19 | +Common:: |
| 20 | ++ |
| 21 | +-- |
| 22 | +
|
| 23 | +```yml |
| 24 | +# Common configuration fields, showing default values |
| 25 | +label: "" |
| 26 | +ollama_moderation: |
| 27 | + model: llama-guard3 # No default (required) |
| 28 | + prompt: "" # No default (required) |
| 29 | + response: "" # No default (required) |
| 30 | + runner: |
| 31 | + context_size: 0 # No default (optional) |
| 32 | + batch_size: 0 # No default (optional) |
| 33 | + server_address: http://127.0.0.1:11434 # No default (optional) |
| 34 | +``` |
| 35 | +
|
| 36 | +-- |
| 37 | +Advanced:: |
| 38 | ++ |
| 39 | +-- |
| 40 | +
|
| 41 | +```yml |
| 42 | +# All configuration fields, showing default values |
| 43 | +label: "" |
| 44 | +ollama_moderation: |
| 45 | + model: llama-guard3 # No default (required) |
| 46 | + prompt: "" # No default (required) |
| 47 | + response: "" # No default (required) |
| 48 | + runner: |
| 49 | + context_size: 0 # No default (optional) |
| 50 | + batch_size: 0 # No default (optional) |
| 51 | + gpu_layers: 0 # No default (optional) |
| 52 | + threads: 0 # No default (optional) |
| 53 | + use_mmap: false # No default (optional) |
| 54 | + use_mlock: false # No default (optional) |
| 55 | + server_address: http://127.0.0.1:11434 # No default (optional) |
| 56 | + cache_directory: /opt/cache/connect/ollama # No default (optional) |
| 57 | + download_url: "" # No default (optional) |
| 58 | +``` |
| 59 | +
|
| 60 | +-- |
| 61 | +====== |
| 62 | + |
| 63 | +This processor checks the safety of responses from your chosen large language model (LLM) using either https://ollama.com/library/llama-guard3[Llama Guard 3^] or https://ollama.com/library/shieldgemma[ShieldGemma^]. |
| 64 | + |
| 65 | +By default, the processor starts and runs a locally-installed Ollama server. Alternatively, to use an already running Ollama server, add your server details to the `server_address` field. You can https://ollama.com/download[download and install Ollama from the Ollama website^]. |
| 66 | + |
| 67 | +For more information, see the https://github.com/ollama/ollama/tree/main/docs[Ollama documentation^] and <<Examples, Examples>>. |
| 68 | + |
| 69 | +To check the safety of your prompts, see the xref:components:processors/ollama_chat.adoc#examples[`ollama_chat` processor] documentation. |
| 70 | + |
| 71 | + |
| 72 | +== Fields |
| 73 | + |
| 74 | +=== `model` |
| 75 | + |
| 76 | +The name of the Ollama LLM to use. |
| 77 | + |
| 78 | +*Type*: `string` |
| 79 | + |
| 80 | +Options: |
| 81 | + |
| 82 | +|=== |
| 83 | +| Option | Description |
| 84 | +| `llama-guard3` |
| 85 | +a| The Llama Guard 3 model writes the following metadata to each processed message: |
| 86 | + |
| 87 | +* The `@safe` field: If the message content is unsafe, the value is set to either `yes`. Otherwise, it's set to `no`. |
| 88 | +* The `@category` field: When the `@safe` field is set to `yes`, this field returns the category of safety violation. |
| 89 | + |
| 90 | +For more information, see the https://ollama.com/library/llama-guard3[Llama Guard 3 model description^]. |
| 91 | +| `shieldgemma` |
| 92 | +| The ShieldGemma model writes an `@safe` metadata field to each processed message, with the value of `yes` if the message content is unsafe or `no` if it's not. |
| 93 | + |
| 94 | +For more information, see the https://ollama.com/library/shieldgemma[ShieldGemma model description^]. |
| 95 | + |
| 96 | +|=== |
| 97 | + |
| 98 | +```yml |
| 99 | +# Examples |
| 100 | + |
| 101 | +model: llama-guard3 |
| 102 | + |
| 103 | +model: shieldgemma |
| 104 | +``` |
| 105 | + |
| 106 | +=== `prompt` |
| 107 | + |
| 108 | +The prompt you used to generate a response from an LLM. |
| 109 | + |
| 110 | +If you're using the `ollama_chat` processor, you can set the `save_prompt_metadata` field to save the contents of your prompts. You can then run them through `ollama_moderation` processor to check the model responses for safety. For more details, see <<Examples, Examples>>. |
| 111 | + |
| 112 | +You can also check the safety of your prompts. For more information, see the xref:components:processors/ollama_chat.adoc#examples[`ollama_chat` processor] documentation. |
| 113 | + |
| 114 | +This field supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions]. |
| 115 | + |
| 116 | +*Type*: `string` |
| 117 | + |
| 118 | + |
| 119 | +=== `response` |
| 120 | + |
| 121 | +The LLM's response that you want to check for safety. |
| 122 | + |
| 123 | +This field supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions]. |
| 124 | + |
| 125 | +*Type*: `string` |
| 126 | + |
| 127 | + |
| 128 | +=== `runner` |
| 129 | + |
| 130 | +Options for the model runner that are used when the model is first loaded into memory. |
| 131 | + |
| 132 | + |
| 133 | +*Type*: `object` |
| 134 | + |
| 135 | + |
| 136 | +=== `runner.context_size` |
| 137 | + |
| 138 | +Sets the size of the context window used to generate the next token. Using a larger context window uses more memory and takes longer to process. |
| 139 | + |
| 140 | + |
| 141 | +*Type*: `int` |
| 142 | + |
| 143 | + |
| 144 | +=== `runner.batch_size` |
| 145 | + |
| 146 | +The maximum number of requests to process in parallel. |
| 147 | + |
| 148 | + |
| 149 | +*Type*: `int` |
| 150 | + |
| 151 | + |
| 152 | +=== `runner.gpu_layers` |
| 153 | + |
| 154 | +Sets the number of layers to offload to the GPU for computation. This generally results in increased performance. By default, the runtime decides the number of layers dynamically. |
| 155 | + |
| 156 | + |
| 157 | +*Type*: `int` |
| 158 | + |
| 159 | + |
| 160 | +=== `runner.threads` |
| 161 | + |
| 162 | +Sets the number of threads to use during response generation. For optimal performance, set this value to the number of physical CPU cores your system has. By default, the runtime decides the optimal number of threads. |
| 163 | + |
| 164 | + |
| 165 | +*Type*: `int` |
| 166 | + |
| 167 | + |
| 168 | +=== `runner.use_mmap` |
| 169 | + |
| 170 | +Map the model into memory. Set to `true` to load only the necessary parts of the model into memory. This setting is only supported on Unix systems. |
| 171 | + |
| 172 | + |
| 173 | +*Type*: `bool` |
| 174 | + |
| 175 | + |
| 176 | +=== `runner.use_mlock` |
| 177 | + |
| 178 | +Set to `true` to lock the model in memory, preventing it from being swapped out when it's mapped into memory. This option can improve performance but reduces the benefits of memory-mapping by increasing RAM usage and slowing model load times. |
| 179 | + |
| 180 | +*Type*: `bool` |
| 181 | + |
| 182 | + |
| 183 | +=== `server_address` |
| 184 | + |
| 185 | +The address of the Ollama server to use. Leave this field blank and the processor starts and runs a local Ollama server, or specify the address of your own local or remote server. |
| 186 | + |
| 187 | + |
| 188 | +*Type*: `string` |
| 189 | + |
| 190 | + |
| 191 | +```yml |
| 192 | +# Examples |
| 193 | + |
| 194 | +server_address: http://127.0.0.1:11434 |
| 195 | +``` |
| 196 | + |
| 197 | +=== `cache_directory` |
| 198 | + |
| 199 | +If the `server_address` is not set, download the Ollama binary to this directory and use it as a model cache. |
| 200 | + |
| 201 | +*Type*: `string` |
| 202 | + |
| 203 | + |
| 204 | +```yml |
| 205 | +# Examples |
| 206 | + |
| 207 | +cache_directory: /opt/cache/connect/ollama |
| 208 | +``` |
| 209 | + |
| 210 | +=== `download_url` |
| 211 | + |
| 212 | +If `server_address` is not set, download the Ollama binary from this URL. The default value is the official Ollama GitHub release for this platform. |
| 213 | + |
| 214 | +*Type*: `string` |
| 215 | + |
| 216 | +== Examples |
| 217 | + |
| 218 | +This example uses Llama Guard 3 to check if another model (LLaVA) responded with a safe or unsafe content. |
| 219 | + |
| 220 | +```yaml |
| 221 | +input: |
| 222 | + stdin: |
| 223 | + scanner: |
| 224 | + lines: {} |
| 225 | +pipeline: |
| 226 | + processors: |
| 227 | + - ollama_chat: |
| 228 | + model: llava |
| 229 | + prompt: "${!content().string()}" |
| 230 | + save_prompt_metadata: true |
| 231 | + - ollama_moderation: |
| 232 | + model: llama-guard3 |
| 233 | + prompt: "${!@prompt}" |
| 234 | + response: "${!content().string()}" |
| 235 | + - mapping: | |
| 236 | + root.response = content().string() |
| 237 | + root.is_safe = @safe |
| 238 | +output: |
| 239 | + stdout: |
| 240 | + codec: lines |
| 241 | +``` |
| 242 | + |
| 243 | +// end::single-source[] |
0 commit comments