|
21 | 21 | from tests.utils import TestChat |
22 | 22 |
|
23 | 23 | # We detect if the environment is set up correct for SDD (presidio + downloaded spacy model) |
| 24 | +# poetry install -e "sdd" --with dev |
| 25 | +# python -m spacy download en_core_web_lg |
24 | 26 | try: |
25 | 27 | import presidio_analyzer |
26 | 28 | import presidio_anonymizer |
@@ -196,3 +198,115 @@ def retrieve_relevant_chunks(): |
196 | 198 |
|
197 | 199 | chat >> "Hi!" |
198 | 200 | chat << "Hello there!" |
| 201 | + |
| 202 | + |
| 203 | +@pytest.mark.skipif( |
| 204 | + not SDD_SETUP_PRESENT, reason="Sensitive Data Detection setup is not present." |
| 205 | +) |
| 206 | +@pytest.mark.unit |
| 207 | +def test_score_threshold(): |
| 208 | + config = RailsConfig.from_content( |
| 209 | + yaml_content=""" |
| 210 | + models: [] |
| 211 | + rails: |
| 212 | + config: |
| 213 | + sensitive_data_detection: |
| 214 | +
|
| 215 | + input: |
| 216 | + score_threshold: 0.4 |
| 217 | + entities: |
| 218 | + - PERSON |
| 219 | + output: |
| 220 | +
|
| 221 | + score_threshold: 0.4 |
| 222 | + entities: |
| 223 | + - PERSON |
| 224 | + input: |
| 225 | + flows: |
| 226 | + - detect sensitive data on input |
| 227 | + output: |
| 228 | + flows: |
| 229 | + - detect sensitive data on output |
| 230 | + """, |
| 231 | + colang_content=""" |
| 232 | + define user express greeting |
| 233 | + "hi" |
| 234 | +
|
| 235 | + define flow |
| 236 | + user express greeting |
| 237 | + bot express greeting |
| 238 | +
|
| 239 | + define bot inform answer unknown |
| 240 | + "I can't answer that." |
| 241 | + """, |
| 242 | + ) |
| 243 | + |
| 244 | + chat = TestChat( |
| 245 | + config, |
| 246 | + llm_completions=[" express greeting", ' "Hi! My name is John as well."'], |
| 247 | + ) |
| 248 | + |
| 249 | + # This will trigger the input rail |
| 250 | + chat >> "Hi! I am Mr. John!" |
| 251 | + chat << "I can't answer that." |
| 252 | + |
| 253 | + # This will trigger only the output one |
| 254 | + chat >> "Hi!" |
| 255 | + chat << "I can't answer that." |
| 256 | + |
| 257 | + |
| 258 | +@pytest.mark.skipif( |
| 259 | + not SDD_SETUP_PRESENT, reason="Sensitive Data Detection setup is not present." |
| 260 | +) |
| 261 | +@pytest.mark.unit |
| 262 | +def test_invalid_score_threshold(caplog): |
| 263 | + config = RailsConfig.from_content( |
| 264 | + yaml_content=""" |
| 265 | + models: [] |
| 266 | + rails: |
| 267 | + config: |
| 268 | + sensitive_data_detection: |
| 269 | +
|
| 270 | + input: |
| 271 | + score_threshold: -0.4 |
| 272 | + entities: |
| 273 | + - PERSON |
| 274 | + output: |
| 275 | +
|
| 276 | + score_threshold: -0.4 |
| 277 | + entities: |
| 278 | + - PERSON |
| 279 | + input: |
| 280 | + flows: |
| 281 | + - detect sensitive data on input |
| 282 | + output: |
| 283 | + flows: |
| 284 | + - detect sensitive data on output |
| 285 | + """, |
| 286 | + colang_content=""" |
| 287 | + define user express greeting |
| 288 | + "hi" |
| 289 | +
|
| 290 | + define flow |
| 291 | + user express greeting |
| 292 | + bot express greeting |
| 293 | +
|
| 294 | + define bot inform answer unknown |
| 295 | + "I can't answer that." |
| 296 | + """, |
| 297 | + ) |
| 298 | + |
| 299 | + chat = TestChat( |
| 300 | + config, |
| 301 | + llm_completions=[" express greeting", ' "Hi! My name is John as well."'], |
| 302 | + ) |
| 303 | + |
| 304 | + rails = chat.app |
| 305 | + |
| 306 | + messages = [ |
| 307 | + {"role": "user", "content": "Hi! I am Mr. John!"}, |
| 308 | + ] |
| 309 | + |
| 310 | + _ = rails.generate(messages=messages) |
| 311 | + |
| 312 | + assert "score_threshold must be a float between 0 and 1 (inclusive)." in caplog.text |
0 commit comments