|
| 1 | +The ``langservice`` module in garak is designed to handle text language tasks using various translation services and models. |
| 2 | +It provides an entry point to translation support backed by translators, each implementing different translation strategies and models, including both cloud-based services, |
| 3 | +like `DeepL<https://www.deepl.com/>`_ and `NVIDIA Riva<https://build.nvidia.com/nvidia/megatron-1b-nmt>`_, and local models like facebook/m2m100 available on `Hugging Face<https://huggingface.co/>`_. |
| 4 | + |
| 5 | +garak.langservice |
| 6 | +================= |
| 7 | + |
| 8 | +.. automodule:: garak.langservice |
| 9 | + :members: |
| 10 | + :undoc-members: |
| 11 | + :show-inheritance: |
| 12 | + |
| 13 | +Translation support |
| 14 | +=================== |
| 15 | + |
| 16 | +This module provides translation support for probe and detector keywords and triggers. |
| 17 | +Allowing testing of models that accept and produce text in languages other than the language the plugin was written for. |
| 18 | + |
| 19 | +* limitations: |
| 20 | + - This functionality is strongly coupled to ``bcp47`` code "en" for sentence detection and structure at this time. |
| 21 | + - Reverse translation is required for snowball probes, and Huggingface detectors due to model load formats. |
| 22 | + - Huggingface detectors primarily load English models. Requiring a target language NLI model for the detector. |
| 23 | + - If probes or detectors fail to load, you need may need to choose a smaller local translation model or utilize a remote service. |
| 24 | + - Translation may add significant execution time to the run depending on resources available. |
| 25 | + |
| 26 | +Supported translation services |
| 27 | +------------------------------ |
| 28 | + |
| 29 | +- Huggingface |
| 30 | + - This project supports usage of the following translation models: |
| 31 | + - `Helsinki-NLP/opus-mt-{<source_lang>-<target_lang>} <https://huggingface.co/docs/transformers/model_doc/marian>`_ |
| 32 | + - `facebook/m2m100_418M <https://huggingface.co/facebook/m2m100_418M>`_ |
| 33 | + - `facebook/m2m100_1.2B <https://huggingface.co/facebook/m2m100_1.2B>`_ |
| 34 | +- `DeepL <https://www.deepl.com/docs-api>`_ |
| 35 | +- `NVIDIA Riva <https://build.nvidia.com/nvidia/megatron-1b-nmt>`_ |
| 36 | + |
| 37 | +API KEY Requirements |
| 38 | +-------------------- |
| 39 | + |
| 40 | +To use use DeepL API or Riva API to translate probe and detector keywords and triggers from cloud services an API key must be supplied. |
| 41 | + |
| 42 | +API keys for the preferred service can be obtained in following locations: |
| 43 | +- `DeepL <https://www.deepl.com/en/pro-api>`_ |
| 44 | +- `Riva <https://build.nvidia.com/nvidia/megatron-1b-nmt>`_ |
| 45 | + |
| 46 | +Supported languages for remote services: |
| 47 | +- `DeepL <https://developers.deepl.com/docs/resources/supported-languages>`_ |
| 48 | +- `Riva <https://docs.nvidia.com/nim/riva/nmt/latest/getting-started.html#supported-languages>`_ |
| 49 | + |
| 50 | +API keys can be stored in environment variables with the following commands: |
| 51 | + |
| 52 | +DeepL |
| 53 | +~~~~~ |
| 54 | + |
| 55 | +.. code-block:: bash |
| 56 | +
|
| 57 | + export DEEPL_API_KEY=xxxx |
| 58 | +
|
| 59 | +RIVA |
| 60 | +~~~ |
| 61 | + |
| 62 | +.. code-block:: bash |
| 63 | +
|
| 64 | + export RIVA_API_KEY=xxxx |
| 65 | +
|
| 66 | +Configuration file |
| 67 | +------------------ |
| 68 | + |
| 69 | +Translation function is configured in the ``run`` section of a configuration with the following keys: |
| 70 | + |
| 71 | +target_lang - A single ``bcp47`` entry designating the language of the target under test. "ja", "fr", "jap" etc. |
| 72 | +translators - A list of language pair designated translator configurations. |
| 73 | + |
| 74 | +* Note: The `Helsinki-NLP/opus-mt-{source},{target}` case uses different language formats. The language codes used to name models are inconsistent. |
| 75 | +Two-digit codes can usually be found `here<https://developers.google.com/admin-sdk/directory/v1/languages>`_, while three-digit codes require |
| 76 | +a search such as “language code {code}". More details can be found `here <https://github.com/Helsinki-NLP/OPUS-MT-train/tree/master/models>`_. |
| 77 | + |
| 78 | +A translator configuration is provided using the project's configurable pattern with the following required keys: |
| 79 | + |
| 80 | +* ``language`` - A ``,`` separated pair of ``bcp47`` entires describing translation format provided by the configuration |
| 81 | +* ``model_type`` - the module and optional instance class to be instantiated. local, remote, remote.DeeplTranslator etc. |
| 82 | +* ``model_name`` - (optional) the model name loaded for translation, required for ``local`` translator model_type |
| 83 | + |
| 84 | +(Optional) Model specific parameters defined by the translator model type may exist. |
| 85 | + |
| 86 | +* Note: local translation support loads a model and is not designed to support crossing the multi-processing boundary. |
| 87 | + |
| 88 | +The translator configuration can be written to a file and the path passed, with the ``--config`` cli option. |
| 89 | + |
| 90 | +An example template is provided below. |
| 91 | + |
| 92 | +.. code-block:: yaml |
| 93 | +run: |
| 94 | + target_lang: {target language code} |
| 95 | + translators: |
| 96 | + - language: {source language code},{target language code} |
| 97 | + api_key: {your API key} |
| 98 | + model_type: {translator module or module.classname} |
| 99 | + model_name: {huggingface model name} |
| 100 | + - language: {target language code},{source language code} |
| 101 | + api_key: {your API key} |
| 102 | + model_type: {translator module or module.classname} |
| 103 | + model_name: {huggingface model name} |
| 104 | + |
| 105 | +* Note: each translator is configured for a single translation pair and specification is required in each direction for a run to proceed. |
| 106 | + |
| 107 | +Examples for translation configuration |
| 108 | +-------------------------------------- |
| 109 | + |
| 110 | +DeepL |
| 111 | +~~~~~ |
| 112 | + |
| 113 | +To use DeepL translation in garak, run the following command: |
| 114 | +You use the following yaml config. |
| 115 | + |
| 116 | +.. code-block:: yaml |
| 117 | +run: |
| 118 | + target_lang: {target language code} |
| 119 | + translators: |
| 120 | + - language: {source language code},{target language code} |
| 121 | + model_type: remote.DeeplTranslator |
| 122 | + - language: {target language code},{source language code} |
| 123 | + model_type: remote.DeeplTranslator |
| 124 | + |
| 125 | + |
| 126 | +.. code-block:: bash |
| 127 | +
|
| 128 | + export DEEPL_API_KEY=xxxx |
| 129 | + python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --config {path to your yaml config file} |
| 130 | +
|
| 131 | +
|
| 132 | +Riva |
| 133 | +~~~~ |
| 134 | + |
| 135 | +For Riva, run the following command: |
| 136 | +You use the following yaml config. |
| 137 | + |
| 138 | +.. code-block:: yaml |
| 139 | +
|
| 140 | +run: |
| 141 | + target_lang: {target language code} |
| 142 | + translators: |
| 143 | + - language: {source language code},{target language code} |
| 144 | + model_type: remote |
| 145 | + - language: {target language code},{source language code} |
| 146 | + model_type: remote |
| 147 | + |
| 148 | + |
| 149 | +.. code-block:: bash |
| 150 | +
|
| 151 | + export RIVA_API_KEY=xxxx |
| 152 | + python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --config {path to your yaml config file} |
| 153 | +
|
| 154 | +
|
| 155 | +Local |
| 156 | +~~~~~ |
| 157 | + |
| 158 | +For local translation, use the following command: |
| 159 | +You use the following yaml config. |
| 160 | + |
| 161 | +.. code-block:: yaml |
| 162 | +run: |
| 163 | + target_lang: jap |
| 164 | + translators: |
| 165 | + - language: en,jap |
| 166 | + model_type: local |
| 167 | + - language: jap,en |
| 168 | + model_type: local |
| 169 | + |
| 170 | +.. code-block:: bash |
| 171 | +
|
| 172 | + python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --config {path to your yaml config file} |
| 173 | +
|
| 174 | +The default configuration will load `Helsinki-NLP MarianMT <https://huggingface.co/docs/transformers/model_doc/marian>`_ models for local translation. |
| 175 | + |
| 176 | +Additional support for Huggingface ``M2M100Model`` type only is enabled by providing ``model_name`` for local translators. The model name provided must |
| 177 | +contain ``m2m100`` to be loaded by garak. |
| 178 | + |
| 179 | +.. code-block:: yaml |
| 180 | +run: |
| 181 | + target_lang: ja |
| 182 | + translators: |
| 183 | + - language: en,ja |
| 184 | + model_type: local |
| 185 | + model_name: facebook/m2m100_418M |
| 186 | + - language: jap,en |
| 187 | + model_type: local |
| 188 | + model_name: facebook/m2m100_418M |
| 189 | + |
| 190 | + |
| 191 | +.. code-block:: bash |
| 192 | +
|
| 193 | + python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --config {path to your yaml config file} |
0 commit comments