# Code

Code Guardrail is used for scanning if the Markdown output of the language model includes code snippets in specific programming languages. It can be configured to allow or block specific programming languages.

**Parameters**:

`data:`

* `languages` (Sequence\[str]): The list of programming languages to allow or deny.
* `prompt` (str): The prompt to scan for code snippets.

`arguments:`

* `is_blocked` (bool, optional): Whether the languages are blocked or allowed. Default is True, blocked.
* `threshold` (float, optional): The threshold for the risk score. Default is 0.5.
* `use_onnx` (bool, optional): Whether to use ONNX for inference. Default is False.

**Interpretation**:

Failed if blocked language is detected, Passed if no blocked language is detected. If blocked language is detected, the code snippet is removed.

**Example:**

Passed Scenario -

```
evaluator.add_test(
test_names=["code_guardrail"],
data={
"prompt": """
Give me reasons why I should use Python over Java.

def calculate_risk_score(score: float, threshold: float) -> float:
    if score > threshold:
        return 1.0

    risk_score = round(abs(score - threshold) / threshold, 1)
    # Ensure risk score is between 0 and 1
    return min(max(risk_score, 0), 1)

Java has better OOD and OOP concepts.
Python is more readable and has a simpler syntax.
""",
},
arguments={"languages": ["Python"], "is_blocked": True, "threshold": 0.5},
).run()

Result = "No markdown code snippets found in the text"
```

Failed Scenario -

````
evaluator.add_test(
test_names=["code_guardrail"],
data={
"prompt": """
Give me reasons why I should use Python over Java.
        ```
        def calculate_risk_score(score: float, threshold: float) -> float:
            if score > threshold:
                return 1.0

            risk_score = round(abs(score - threshold) / threshold, 1)
            # Ensure risk score is between 0 and 1
            return min(max(risk_score, 0), 1)
        ```
Java has better OOD and OOP concepts. Python is more readable and has a simpler syntax.
""",
},
arguments={"languages": ["Python"], "is_blocked": True, "threshold": 0.5},
).run()

````

#### Code Example:

````python
evaluator.add_test(
test_names=["code_guardrail"],
data={
"prompt": """
Give me reasons why I should use Python over Java.

        ```def calculate_risk_score(score: float, threshold: float) -> float:
    if score > threshold:
        return 1.0

    risk_score = round(abs(score - threshold) / threshold, 1)
    # Ensure risk score is between 0 and 1
    return min(max(risk_score, 0), 1)
        ```
    Java has better OOD and OOP concepts. Python is more readable and has a simpler syntax.

        """,
},
arguments={"languages": ["Python"], "is_blocked": True, "threshold": 0.5},
).run()
````


---

# 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/code.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.
