# Language

A class for scanning the prompt for different languages detection.

The Guardrail supports the 22 languages:

```
arabic (ar), bulgarian (bg), german (de), modern greek (el), english (en), spanish (es), french (fr), hindi (hi), italian (it), japanese (ja), dutch (nl), polish (pl), portuguese (pt), russian (ru), swahili (sw), thai (th), turkish (tr), urdu (ur), vietnamese (vi), and chinese (zh)
```

**Parameters**:

`data:`

* `prompt` (str): The prompt to check for language.
* `valid_languages` (Sequence\[str]): A list of valid language codes in ISO 639-1.

`arguments:`

* `threshold` (float, optional): Minimum confidence score. Default is 0.6.
* `match_type` (str, optional): Whether to match the full text or individual sentences. Default is `full`. Options are `sentence` or `full`
* `use_onnx` (bool, optional): Whether to use ONNX for inference. Default is False.

**Interpretation**:

Passed if valid languages are found in the text, Fails if invalid language is detected above the threshold.

**Example:**

Passed Scenario -

```
evaluator.add_test(
    test_names=["language_guardrail"],
    data={
        "prompt": """Craft a poem that captures the breathtaking beauty and serenity of this underwater world.
        Describe the gentle swaying of the coral, the playful dance of the fish, and the mesmerising underwater soundscape.
        Use vivid imagery and metaphors to evoke a sense of awe and wonder for this natural treasure.""",
    },
    arguments={"threshold": 0.6, "valid_languages":["en"]},
).run()

Result = "Only valid languages are found in the text".
```

Failed Scenario -

```
evaluator.add_test(
    test_names=["language_guardrail"],
    data={
        "prompt": ["""Siempre he querido aprender a tocar el piano. Me encanta la música clásica y me gustaría poder tocar algunas piezas de Beethoven y Chopin. ¿Podrías recomendarme un buen profesor de piano?
                   Bonjour, je m'appelle Jean. J'ai 25 ans et je suis un grand fan de musique classique. J'aimerais apprendre à jouer du piano. Pourriez-vous me recommander un bon professeur de piano?
                   """,],
    },
    arguments={"threshold": 0.4, "valid_languages": ['es'], 'match_type': 'sentence'},
).run()

Result = "Invalid Language [fr] is found in the text."
```

#### Code Example:

```python
evaluator.add_test(
    test_names=["language_guardrail"],
    data={
        "prompt": """Craft a poem that captures the breathtaking beauty and serenity of this underwater world.
        Describe the gentle swaying of the coral, the playful dance of the fish, and the mesmerising underwater soundscape.
        Use vivid imagery and metaphors to evoke a sense of awe and wonder for this natural treasure.""",
    },
    arguments={"threshold": 0.6, "valid_languages":["en"]},
).run()
evaluator.print_results()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.raga.ai/ragaai-catalyst/ragaai-metric-library/additional-metrics/guardrails/language.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
