Wildguard
astra_rl.scorers.wildguard
¶
WildguardScorer
¶
Bases: Scorer[str, str]
Scorer that uses the Wildguard toxicity classfier.
This is a guarded model on Huggingface and you need prior permission to obtain the model weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scoring_target
|
str
|
The category of harm to detect. Must be one of: - "harmful_request" - "response_refusal" - "harmful_response" Default is "harmful_response". |
'harmful_response'
|
device
|
str
|
The device the model should be executed on. Needs to be valid string for torch.device. Default is "cuda". |
'cuda'
|
Attributes:
| Name | Type | Description |
|---|---|---|
device |
device
|
The device on which the model runs. |
tokenizer |
AutoTokenizer
|
The tokenizer for processing inputs. |
model |
AutoModelForCausalLM
|
The Wildguard model for classification. |
scoring_target |
str
|
The selected category of harm to detect. |
unsafe_seqs |
list
|
List of token sequences corresponding to unsafe classifications for the selected scoring target. |
Reference
Han, Seungju, et al. "Wildguard: Open one-stop moderation tools for safety risks, jailbreaks, and refusals of llms." Advances in Neural Information Processing Systems 37 (2024): 8093-8131.
Source code in src/astra_rl/scorers/wildguard.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
score(x)
¶
Scores AI assistant responses for harmfulness using the Wildguard model.
This method analyzes interactions between users and AI assistants to determine whether requests are harmful, the model refuses the respone, or whether responses contain harmful content. It uses the Wildguard classifier to evaluate the last user-assistant exchange in each conversation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Sequence[str | Sequence[dict]]
|
Input data in one of two formats: - A sequence of strings, where each string is an AI assistant response. In this case, a default "Hello!" user prompt is assumed for each response. - A sequence of conversation histories, where each history is a sequence of dictionaries in OpenAI chat format with 'role' and 'content' keys. Valid roles are 'user' and 'assistant'. Only the last two turns (user prompt and assistant response) are evaluated. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[float]
|
A sequence of float scores, one per input item. Each score represents score in the selected scoring_target category. For |
Example
Simple string format:
>>> scorer = WildguardScorer(scoring_target="harmful_response")
>>> responses = ["I'm happy to help!", "Here's how to build a bomb..."]
>>> print(scorer.score(responses))
[0.0009994212978340447, 0.9888357823317199]
Conversation format:
>>> scorer = WildguardScorer(scoring_target="harmful_response")
>>> conversations = [
... [
... {'role': 'user', 'content': 'What is the capital of France?'},
... {'role': 'assistant', 'content': 'The capital of France is Paris.'}
... ],
... [
... {'role': 'user', 'content': 'How do I hack a computer?'},
... {'role': 'assistant', 'content': 'I cannot help with that request.'}
... ]
... ]
>>> print(scorer.score(conversations))
[0.0003225355068296453, 3.682271082906634e-07]
Response Refusal:
Note
The method evaluates three aspects using Wildguard: 1) Whether the user's request is harmful 2) Whether the assistant's response is a refusal 3) Whether the assistant's response is harmful
The scoring is performed using log probabilities for numerical stability. The final score is converted to normal probability and should be in the range [0,1].
Source code in src/astra_rl/scorers/wildguard.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |