Metadata-Version: 2.4
Name: walledai
Version: 4.3.0
Summary: A Python SDK for interacting with Walled AI
Author-email: Walled AI <rishabh@walled.ai>
License: MIT License
        
        Copyright (c) 2025 Walled AI
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aiohttp>=3.8.0
Requires-Dist: typing_extensions>=4.0.0
Requires-Dist: pandas>=1.3.0
Requires-Dist: scikit-learn>=1.0.0
Dynamic: license-file

# Walled AI SDK

A Python SDK for interacting with Walled AI.

## Installation
```sh
pip install walledai
```

## Usage

```python
from walledai import WalledProtect, WalledRedact
# Initialize the client 
client = WalledProtect("your_api_key", retries=3)  # retries is optional
redact_client = WalledRedact("your_api_key", retries=3)  # for redaction
```

## Walled Protect

```python
response = client.guard(
    text="Hello, How are you", 
    greetings_list=["generalgreetings"], 
    text_type="prompt", 
    generic_safety_check=True,
    compliance_list=[],
    pii_list=[]
)
print(response)
```

Processes the text using Walled AI's protection mechanisms.

#### Parameters:
- **`text`** (*str*, required): The input text to be processed.
- **`greetings_list`** (*list of str*, optional): A list of predefined greetings categories. ex: ["Casual & Friendly", "Formal", "Professional"]. Defaults to ["Casual & Friendly"]
- **`text_type`** (*str*, optional): Type of text being processed. Defaults to `"prompt"`.
- **`generic_safety_check`** (*bool*, optional): Whether to apply a general safety filter. Defaults to `True`.
- **`compliance_list`** (*list of str*, optional): A list of compliances.
- **`pii_list`** (*list of str*, optional): Must be empty or contain only the following values: `"Person's Name"`, `"Address"`, `"Email Id"`, `"Contact No"`, `"Date Of Birth"`, `"Unique Id"`, `"Financial Data"`.

#### Example Usage:
```python
response = client.guard(
    text="Hello, How are you", 
    greetings_list=["generalgreetings"], 
    text_type="prompt", 
    generic_safety_check=True,
    pii_list=[],
    compliance_list=["Medical", "Finance"]
)
print(response)
```

### Example Responses
The response returned by the `guard` method is a dictionary.

#### Successful Response
```python
{
    "success": true,
    "data": {
        "safety": [{ "safety": "generic", "isSafe": true, "score": 5 }],
        "compliance": [],
        "pii": [],
        "greetings": [{ "greeting_type": "generalgreetings", "isPresent": true }]
    }
}
```

#### Error Response
If an error occurs, the SDK will retry the request up to the specified number of retries (`retries` parameter in `WalledProtect`) or default retry number. If the retries are exhausted, it will return an error response.
```python
{
    "success": false,
    "error": "Invalid API key provided."
}
```

## Walled Redact

Processes the text using Walled AI's redaction mechanisms.

#### Parameters:
- **`text`** (*str*, required): The input text to be processed.

#### Example Usage:
```python
response = redact_client.guard(
    text="Hello, How are you Henry", 
)
print(response)
```

### Example Responses
The response returned by the `guard` method is a dictionary.

#### Successful Response
```python
{
    "success": true,
    "data": {
        "success": true,
        "remark": "Success! one attempt",
        "input": "Hi my name is Henry",
        "masked_text": "Hello my name is PN1",
        "mapping": {
            "PNA1": "indranil"
        }
    }
}
```

#### Error Response
If an error occurs, the SDK will retry the request up to the specified number of retries (`retries` parameter in `WalledRedact`) or default retry number. If the retries are exhausted, it will return an error response.
```python
{
    "success": false,
    "error": "Invalid API key provided."
}
```

## Evaluation

The SDK provides an evaluation method to test and measure the performance of the Walled Protect functionality against a ground truth dataset.

#### Parameters:
- **`ground_truth_file_path`** (*str*, required): Path to the CSV file containing test cases with expected results.
- **`model_output_file_path`** (*str*, required): Path where the model's output results will be saved.
- **`metrics_output_file_path`** (*str*, required): Path where the evaluation metrics will be saved.
- **`concurrency_limit`** (*int*, optional): Maximum number of concurrent requests. Defaults to 20.

#### Example Usage:
```python
# Run evaluation
import asyncio
from walledai import WalledProtect

client = WalledProtect("your_api_key", retries=3)  # retries is optional
asyncio.run(await client.eval(
    ground_truth_file_path="./unit_test_cases.csv",
    model_output_file_path="./model_results.csv",
    metrics_output_file_path="./metrics.csv",
    concurrency_limit=20
))
```

### Ground Truth CSV Format
The ground truth CSV file has flexible column requirements:

#### Required Columns (must be present in this order):
- `test_input`: The input text to be processed.
- `compliance_topic`: The compliance topic for the test case.
- `compliance_isOnTopic`: Whether the input is on the specified compliance topic (`TRUE` or `FALSE`).

#### Optional Columns (can be included as needed):
- `Person's Name`: Whether a person's name is present (`TRUE` or `FALSE`).
- `Address`: Whether an address is present (`TRUE` or `FALSE`).
- `Email Id`: Whether an email ID is present (`TRUE` or `FALSE`).
- `Contact No`: Whether a contact number is present (`TRUE` or `FALSE`).
- `Date Of Birth`: Whether a date of birth is present (`TRUE` or `FALSE`).
- `Unique Id`: Whether a unique ID is present (`TRUE` or `FALSE`).
- `Financial Data`: Whether financial data is present (`TRUE` or `FALSE`).
- `Casual & Friendly`: Whether the greeting is casual & friendly (`TRUE` or `FALSE`).
- `Professional & Polite`: Whether the greeting is professional & polite (`TRUE` or `FALSE`).

**Notes:**
- Only the first 3 columns are mandatory and must be present in the exact order specified above.
- Optional columns can be included in any order after the required columns.
- The values for boolean columns should be `TRUE` or `FALSE` (case-insensitive).
- Missing optional columns will not result in an error during evaluation.

#### Example of a valid ground truth file
See [`example_unit_test_file`](https://docs.google.com/spreadsheets/d/136QaJQJr5KACXjuTPr86a2-XIFq8APy8XKVg6J00X9U/edit?usp=sharing) for a sample ground_truth_file.

### Output Files
1. **Model Results CSV**: Contains the actual model predictions for each test case. This file will include:
   - All columns present in the ground truth file
   - An additional `is_safe` column with `TRUE` or `FALSE` values indicating whether the input passed the safety evaluation
   
2. **Metrics CSV**: Contains evaluation metrics including:
   - Accuracy scores
   - Precision and recall
   - F1 scores
   - Confusion matrices
