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

Juliagomes/arize default response #28

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ We defer readers to the arxiv paper to see the effectiveness of these prompts ag
## Installation

```bash
$ guardrails hub install hub://guardrails/validator_template
$ guardrails hub install hub://arize/dataset_embeddings
```

## Usage Examples
Expand Down Expand Up @@ -97,6 +97,7 @@ Initializes a new instance of the ValidatorTemplate class.
**Parameters**
- **`threshold`** *(str)*: When the Guard is called on an input message, the Guard checks the cosine distance between the embedded incoming message and the embedded chunks from the source dataset. If any of the chunks are within the `threshold` (defaults to 0.2), then the Guard will take the `on_fail` action.
- **`on_fail`** *(str, Callable)*: The policy to enact when a validator fails. If `str`, must be one of `reask`, `fix`, `filter`, `refrain`, `noop`, `exception` or `fix_reask`. Otherwise, must be a function that is called when the validator fails.
- **`default_response`** *(str)*: The default response to use if the Validator fails. This will override the `on_fail` action and be used as a default response if the Guard fails.
- **`sources`** *List[str]*: Specifies a source dataset with examples of either user input messages or LLM output messages that we would like to Guard against. We recommend including 10 examples.
- **`embed_function`** *(Callable)*: The embedding function used to embed both the text chunks from `sources` and the input messages.
- **`chunk_strategy`** *(EmbeddingChunkStrategy)*: The strategy to use for chunking in the Guardrails AI helper `get_chunks_from_text`.
Expand Down
15 changes: 11 additions & 4 deletions validator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import numpy as np
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from enum import Enum
import pandas as pd
import os
import logging

import numpy as np
Expand All @@ -15,6 +13,7 @@
ValidationResult,
Validator,
register_validator,
OnFailAction
)
from llama_index.embeddings.openai import OpenAIEmbedding

Expand Down Expand Up @@ -44,6 +43,9 @@ class EmbeddingChunkStrategy(Enum):
TOKEN = 3


ARIZE_DEFAULT_RESPONSE = "I'm sorry, I cannot respond to that. Please try rephrasing or let me know if I can help you with something else."


@register_validator(name="arize/dataset_embeddings", data_type="string")
class ArizeDatasetEmbeddings(Validator):
"""Validates that user-generated input does not match dataset of jailbreak
Expand All @@ -52,11 +54,15 @@ class ArizeDatasetEmbeddings(Validator):
def __init__(
self,
threshold: float = 0.2,
on_fail: Optional[Callable] = None,
on_fail: Optional[Union[Callable, OnFailAction]] = None,
**kwargs,
):
if kwargs.get("default_response") is not None:
on_fail = "fix"
self.fix_value = kwargs.get("default_response", ARIZE_DEFAULT_RESPONSE)

super().__init__(
on_fail, threshold=threshold, **kwargs
on_fail=on_fail, threshold=threshold, **kwargs
)
self._threshold = float(threshold)
if kwargs.get("sources") is None:
Expand Down Expand Up @@ -114,6 +120,7 @@ def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
error_message=(
f"The following message triggered the ArizeDatasetEmbeddings Guard:\n\t{user_message}"
),
fix_value=self.fix_value
)
# All chunks exceeded the cosine distance threshold
return PassResult(metadata=metadata)
Expand Down
Loading