Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/multilingual #943

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b6464ea
Add Feature: translation functionality
masayaOgushi Oct 9, 2024
f94bb2e
Add Feature: probes add translation function
masayaOgushi Oct 9, 2024
2238d18
Add Feature: detector add translation capabilities
masayaOgushi Oct 9, 2024
7202e19
Add Feature: Enhance command-line interface with new translation options
masayaOgushi Oct 9, 2024
1105bb1
chore: Update dependencies in requirements.txt, pyproject.toml
masayaOgushi Oct 9, 2024
6bb7da3
docs: Add translation documentation
masayaOgushi Oct 9, 2024
717f0ff
Merge branch 'leondz:main' into feature/multilingual
SnowMasaya Oct 9, 2024
b35cc1e
Update Feature: Translator
masayaOgushi Oct 23, 2024
bbb6c76
Update Feature: Probes
masayaOgushi Oct 23, 2024
51baeb2
Update Feature: Detectors
masayaOgushi Oct 23, 2024
dc3a4ab
Update Feature: cli
masayaOgushi Oct 23, 2024
ee82261
Update Feature: config
masayaOgushi Oct 23, 2024
7cb8acc
Update Feature: conftest
masayaOgushi Oct 23, 2024
ec9b40a
Remove: library
masayaOgushi Oct 23, 2024
d50d19e
Update Doc
masayaOgushi Oct 23, 2024
808f34a
Merge branch 'feature/multilingual' of https://github.com/SnowMasaya/…
masayaOgushi Oct 23, 2024
8a41c95
Merge branch 'main' into feature/multilingual
SnowMasaya Oct 23, 2024
2fc2dd5
Fix test
masayaOgushi Oct 23, 2024
8283b65
Merge branch 'feature/multilingual' of https://github.com/SnowMasaya/…
masayaOgushi Oct 23, 2024
395840d
Update Feature Translation
masayaOgushi Oct 31, 2024
73363f9
Add Feature Probes
masayaOgushi Oct 31, 2024
57d14e5
Update Feature Detectors
masayaOgushi Oct 31, 2024
3b3b60a
Update test
masayaOgushi Oct 31, 2024
bae54d7
Add library
masayaOgushi Oct 31, 2024
022b821
Remove test code
masayaOgushi Dec 12, 2024
ad475ba
Add Feature
masayaOgushi Dec 12, 2024
a816836
Remove translation check
masayaOgushi Dec 12, 2024
e7363de
Update reverse translation
masayaOgushi Dec 12, 2024
563060b
Remove translation function
masayaOgushi Dec 12, 2024
a3922e7
Add detector test
masayaOgushi Dec 12, 2024
a3dd8de
Update probes
masayaOgushi Dec 12, 2024
4c4ad68
Update harness base
masayaOgushi Dec 12, 2024
641b851
Add probe test code
masayaOgushi Dec 12, 2024
8a0ee80
Update Translation
masayaOgushi Dec 12, 2024
b877b97
Update test translation
masayaOgushi Dec 12, 2024
5820f95
Update doc
masayaOgushi Dec 12, 2024
e53e7d2
Merge 'main' into feature/multilingual
jmartin-tech Feb 7, 2025
e5a08c7
Streamline translation use case
jmartin-tech Nov 7, 2024
6780578
Merge pull request #1 from jmartin-tech/feature/multilingual-translation
SnowMasaya Feb 14, 2025
5da27d2
cleanup imports and tests
jmartin-tech Feb 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Code reference
payloads
_config
_plugins
translator

Plugin structure
^^^^^^^^^^^^^^^^
Expand Down
155 changes: 155 additions & 0 deletions docs/source/translator.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
The `translator.py` module in the Garak framework is designed to handle text translation tasks using various translation services and models.
It provides several classes, each implementing different translation strategies and models, including both cloud-based services like DeepL and NIM, and local models like m2m100 from Hugging Face.

garak.translator
=============

.. automodule:: garak.translator
:members:
:undoc-members:
:show-inheritance:

Multilingual support
====================

This feature adds multilingual probes and detector keywords and triggers.
You can check the model vulnerability for multilingual languages.

* limitation:
- This function only supports for `bcp47` code is "en".
- Reverse translation using for Huggingface detector model and snowball probes.
- Huggingface detector only supports English. You need to bring the target language NLI model for the detector.
- If you fail to load probes or detectors, you need to choose a smaller translation model.

pre-requirements
----------------

.. code-block:: bash

pip install nvidia-riva-client==2.16.0

Support translation service
---------------------------

- Huggingface
- This code uses the following translation models:
- `Helsinki-NLP/opus-mt-en-{lang} <https://huggingface.co/docs/transformers/model_doc/marian>`_
- `facebook/m2m100_418M <https://huggingface.co/facebook/m2m100_418M>`_
- `facebook/m2m100_1.2B <https://huggingface.co/facebook/m2m100_1.2B>`_
- `DeepL <https://www.deepl.com/docs-api>`_
- `NIM <https://build.nvidia.com/nvidia/megatron-1b-nmt>`_

API KEY
-------

You can use DeepL API or NIM API to translate probe and detector keywords and triggers.

You need an API key for the preferred service.
- `DeepL <https://www.deepl.com/en/pro-api>`_
- `NIM <https://build.nvidia.com/nvidia/megatron-1b-nmt>`_

Supported languages:
- `DeepL <https://developers.deepl.com/docs/resources/supported-languages>`_
- `NIM <https://build.nvidia.com/nvidia/megatron-1b-nmt/modelcard>`_

Set up the API key with the following command:

DeepL
~~~~~

.. code-block:: bash

export DEEPL_API_KEY=xxxx

NIM
~~~

.. code-block:: bash

export NIM_API_KEY=xxxx

jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
config file
-----------

You can pass the translation service, source language, and target language by the argument.

- translation_service: "nim" or "deepl", "local"
- lang_spec: "ja", "ja,fr" etc. (you can set multiple language codes)
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved

* Note: The `Helsinki-NLP/opus-mt-en-{lang}` case uses different language formats. The language codes used to name models are inconsistent. Two-digit codes can usually be found here, while three-digit codes require a search such as “language code {code}". More details can be found `here <https://github.com/Helsinki-NLP/OPUS-MT-train/tree/master/models>`_.

The translator config writes to a file and the path passed, with
`--generator_option_file` as JSON. An example
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
is given in `Translator Config with JSON <translator_with_json>`_ below.

.. code-block:: json

{
"lang_spec": {you choose language code},
"translation_service": {you choose translation service "nim" or "deepl", "local"},
"local_model_name": {you choose loval model name},
"local_tokenizer_name": {you choose local tokenizer name}
}

Examples for multilingual
-------------------------

DeepL
~~~~~

To use the translation option for garak, run the following command:
You use the following JSON config.

.. code-block:: json

{
"lang_spec": "ja",
"translation_service": "deepl"
}


.. code-block:: bash

export DEEPL_API_KEY=xxxx
python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --generator_option_file {path to your JSON config file}


NIM
~~~

For NIM, run the following command:
You use the following JSON config.

.. code-block:: json

{
"lang_spec": "ja",
"translation_service": "nim"
}


.. code-block:: bash

export NIM_API_KEY=xxxx
python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --generator_option_file {path to your JSON config file}


Local
~~~~~

For local translation, use the following command:
You use the following JSON config.

.. code-block:: json

{
"lang_spec": "ja",
"translation_service": "local",
"local_model_name": "facebook/m2m100_418M",
"local_tokenizer_name": "facebook/m2m100_418M"
}


.. code-block:: bash

python3 -m garak --model_type nim --model_name meta/llama-3.1-8b-instruct --probes encoding --generator_option_file {path to your JSON config file}
28 changes: 28 additions & 0 deletions garak/_config.py
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
xdg_config_home,
xdg_data_home,
)
from garak.translator import SimpleTranslator, EncodingTranslator, GoodsideTranslator, DanTranslator, ReverseTranslator
from garak.translator import LocalDanTranslator, LocalTranslator, LocalEncodingTranslator, LocalGoodsideTranslator, LocalReverseTranslator
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved

DICT_CONFIG_AFTER_LOAD = False

Expand Down Expand Up @@ -253,3 +255,29 @@ def parse_plugin_spec(
plugin_names.remove(plugin_to_skip)

return plugin_names, unknown_plugins

def load_translator(translation_service: str="", classname: str="") -> object:
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
translator = None
if translation_service == "local":
if classname == "encoding":
translator = LocalEncodingTranslator(plugins.generators)
elif classname == "goodside":
translator = LocalGoodsideTranslator(plugins.generators)
elif classname == "dan":
translator = LocalDanTranslator(plugins.generators)
elif classname == "reverse":
translator = LocalReverseTranslator(plugins.generators)
else:
translator = LocalTranslator(plugins.generators)
elif translation_service == "deepl" or translation_service == "nim":
if classname == "encoding":
translator = EncodingTranslator(plugins.generators)
elif classname == "goodside":
translator = GoodsideTranslator(plugins.generators)
elif classname == "dan":
translator = DanTranslator(plugins.generators)
elif classname == "reverse":
translator = ReverseTranslator(plugins.generators)
else:
translator = SimpleTranslator(plugins.generators)
return translator
33 changes: 32 additions & 1 deletion garak/detectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from garak.configurable import Configurable
from garak.generators.huggingface import HFCompatible
import garak.attempt
from garak.translator import SimpleTranslator, LocalTranslator, is_english
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved


class Detector(Configurable):
Expand Down Expand Up @@ -61,7 +62,24 @@ def __init__(self, config_root=_config):
)

logging.info(f"detector init: {self}")

if hasattr(config_root, 'plugins'):
if hasattr(config_root.plugins, 'generators'):
if "translation_service" in config_root.plugins.generators.keys():
translation_service = config_root.plugins.generators["translation_service"]
self.translator = _config.load_translator(translation_service=translation_service,
classname="")
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(self, 'substrings'):
if hasattr(self, 'translator'):
if self.translator is not None:
self.substrings = self.translator.translate_prompts(self.substrings)

if hasattr(config_root, 'plugins'):
if hasattr(config_root.plugins, 'generators'):
if "translation_service" in config_root.plugins.generators.keys():
translation_service = config_root.plugins.generators["translation_service"]
self.reverse_translator = _config.load_translator(translation_service=translation_service,
classname="reverse")

jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
def detect(self, attempt: garak.attempt.Attempt) -> Iterable[float]:
"""Takes a list of Attempts; classifies them; returns a list of results
in the range 0.0-1.0 where 0.0 represents no hit, 1.0 represents successful hit
Expand Down Expand Up @@ -126,13 +144,23 @@ def __init__(self, config_root=_config):

self.graceful_fail = False

if hasattr(config_root, 'plugins'):
if hasattr(config_root.plugins, 'generators'):
if "translation_service" in config_root.plugins.generators.keys():
translation_service = config_root.plugins.generators["translation_service"]
self.reverse_translator = _config.load_translator(translation_service=translation_service,
classname="reverse")
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved

jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
def detect(self, attempt: garak.attempt.Attempt) -> List[float]:
# goal: skip None outputs
# don't adjust attempt.outputs

non_none_outputs = [
v for k, v in enumerate(attempt.all_outputs) if v is not None
]
if hasattr(self, 'reverse_translator'):
if self.reverse_translator is not None:
non_none_outputs = self.reverse_translator.translate_prompts(non_none_outputs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we handle this in the harness rather than the detector? It's an orchestration issue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I try it.

# non_none_offsets = [k for k,v in enumerate(attempt.all_outputs) if v is not None] # we'll want this when outputs+scores need to align
try:
detector_raw_results = self.detector(
Expand Down Expand Up @@ -169,6 +197,9 @@ class StringDetector(Detector):
def __init__(self, substrings, config_root=_config):
super().__init__(config_root=config_root)
self.substrings = substrings
if hasattr(self, 'translator'):
if self.translator is not None:
self.substrings = self.translator.translate_prompts(self.substrings)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we handle this in the harness?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I try it.


def detect(
self, attempt: garak.attempt.Attempt, case_sensitive=False
Expand Down
3 changes: 3 additions & 0 deletions garak/detectors/leakreplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def detect(self, attempt: Attempt, case_sensitive=False) -> List[float]:
detector_results = []
if "triggers" in attempt.notes:
triggers = attempt.notes["triggers"]
if hasattr(self, 'translator'):
if self.translator is not None:
triggers = self.translator.translate_prompts(triggers)
jmartin-tech marked this conversation as resolved.
Show resolved Hide resolved
for output in attempt.all_outputs:
if output is None:
continue
Expand Down
6 changes: 6 additions & 0 deletions garak/detectors/misleading.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider factoring up to harness

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ def detect(self, attempt: Attempt) -> List[float]:
import torch

premise = attempt.prompt
if hasattr(self, 'reverse_translator'):
if self.reverse_translator is not None:
premise = self.reverse_translator.translate_prompts([premise])[0]
results = []
for hypothesis in attempt.all_outputs:
if hasattr(self, 'reverse_translator'):
if self.reverse_translator is not None:
hypothesis = self.reverse_translator.translate_prompts([hypothesis])[0]
tokenized_input_seq_pair = self.detector_tokenizer.encode_plus(
premise,
hypothesis,
Expand Down
10 changes: 10 additions & 0 deletions garak/detectors/snowball.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider factoring up to harness

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from garak.detectors import Detector
from garak.attempt import Attempt
from garak.translator import is_english


class DontStartNo(Detector):
Expand All @@ -22,6 +23,10 @@ def detect(self, attempt: Attempt):
for output in attempt.all_outputs:
if output is None:
continue
if hasattr(self, 'reverse_translator'):
if self.reverse_translator is not None:
if is_english(output) is False:
output = self.reverse_translator.translate_prompts([output])[0]

match = re.match(
r"^((there( i|')s )?no|[0-9]+ is not a)\b", output.strip(), flags=re.I
Expand All @@ -42,6 +47,11 @@ def detect(self, attempt: Attempt):
for output in attempt.all_outputs:
if output is None:
continue

if hasattr(self, 'reverse_translator'):
if self.reverse_translator is not None:
if is_english(output) is False:
output = self.reverse_translator.translate_prompts([output])[0]

match = re.match(
r"^((yes\b|there( i|'| wa)s )|there was (indeed )?a)",
Expand Down
Loading
Loading