File size: 18,023 Bytes
e18c8b0
a6b53fb
 
e18c8b0
 
 
 
61173ea
727d1ca
5bfd036
3373091
10cef3f
5bfd036
 
e18c8b0
a6b53fb
e18c8b0
7d317a5
e18c8b0
 
 
 
05bf37a
5bfd036
e18c8b0
 
 
 
 
ca9549b
 
 
e18c8b0
 
05bf37a
a6b53fb
10cef3f
e18c8b0
 
 
 
 
5bfd036
e18c8b0
 
 
 
 
 
d4ce695
e18c8b0
 
 
 
05bf37a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bfd036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
 
5bfd036
 
 
 
 
 
 
 
e18c8b0
 
 
 
 
91df4d6
ca9549b
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
ca9549b
e18c8b0
 
 
 
 
 
 
ca9549b
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bfd036
 
 
 
 
 
 
 
e18c8b0
 
 
 
 
 
 
 
 
 
61173ea
 
e18c8b0
 
5bfd036
727d1ca
5bfd036
 
 
 
e18c8b0
3373091
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca9549b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bfd036
 
 
a6b53fb
 
 
 
 
ca9549b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727d1ca
ca9549b
 
 
 
5bfd036
 
 
 
ca9549b
 
5bfd036
ca9549b
 
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05bf37a
 
 
5bfd036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b53fb
 
 
e18c8b0
 
 
a6b53fb
 
 
e18c8b0
 
 
 
a6b53fb
e18c8b0
 
a6b53fb
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
b3e5cf7
e18c8b0
5bfd036
 
 
 
 
e6b76e7
 
 
5bfd036
 
 
e6b76e7
e18c8b0
 
 
 
 
 
 
 
 
 
 
ca9549b
e18c8b0
 
 
 
 
10cef3f
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b53fb
e18c8b0
ac95e09
ca9549b
 
ac95e09
ca9549b
 
 
 
 
e18c8b0
ca9549b
 
 
 
 
e18c8b0
 
 
 
 
 
 
 
 
 
05bf37a
e18c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bfd036
 
e18c8b0
 
 
 
 
5bfd036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e18c8b0
10cef3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e18c8b0
 
 
 
 
 
 
727d1ca
e18c8b0
727d1ca
05bf37a
727d1ca
 
 
10cef3f
727d1ca
 
 
 
 
 
 
 
 
 
e18c8b0
727d1ca
e18c8b0
 
 
7bcd3fa
 
 
 
 
 
a6b53fb
e18c8b0
a6b53fb
e18c8b0
7bcd3fa
 
 
 
 
e18c8b0
 
 
 
 
a6b53fb
 
e18c8b0
a6b53fb
 
 
 
 
 
 
 
 
 
7bcd3fa
 
 
 
e18c8b0
 
a6b53fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import logging
import time
from datetime import timedelta
from typing import Dict, List

import streamlit as st
from llm_guard.input_scanners.anonymize import default_entity_types
from llm_guard.input_scanners.code import SUPPORTED_LANGUAGES as SUPPORTED_CODE_LANGUAGES
from llm_guard.output_scanners import get_scanner_by_name
from llm_guard.output_scanners.bias import MatchType as BiasMatchType
from llm_guard.output_scanners.deanonymize import MatchingStrategy as DeanonymizeMatchingStrategy
from llm_guard.output_scanners.gibberish import MatchType as GibberishMatchType
from llm_guard.output_scanners.language import MatchType as LanguageMatchType
from llm_guard.output_scanners.toxicity import MatchType as ToxicityMatchType
from llm_guard.vault import Vault
from streamlit_tags import st_tags

logger = logging.getLogger("llm-guard-playground")


def init_settings() -> (List, Dict):
    all_scanners = [
        "BanCode",
        "BanCompetitors",
        "BanSubstrings",
        "BanTopics",
        "Bias",
        "Code",
        "Deanonymize",
        "JSON",
        "Language",
        "LanguageSame",
        "MaliciousURLs",
        "NoRefusal",
        "NoRefusalLight" "ReadingTime",
        "FactualConsistency",
        "Gibberish",
        "Regex",
        "Relevance",
        "Sensitive",
        "Sentiment",
        "Toxicity",
        "URLReachability",
    ]

    st_enabled_scanners = st.sidebar.multiselect(
        "Select scanners",
        options=all_scanners,
        default=all_scanners,
        help="The list can be found here: https://llm-guard.com/output_scanners/bias/",
    )

    settings = {}

    if "BanCode" in st_enabled_scanners:
        st_bc_expander = st.sidebar.expander(
            "Ban Code",
            expanded=False,
        )

        with st_bc_expander:
            st_bc_threshold = st.slider(
                label="Threshold",
                value=0.95,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="ban_code_threshold",
            )

        settings["BanCode"] = {"threshold": st_bc_threshold}

    if "BanCompetitors" in st_enabled_scanners:
        st_bc_expander = st.sidebar.expander(
            "Ban Competitors",
            expanded=False,
        )

        with st_bc_expander:
            st_bc_competitors = st_tags(
                label="List of competitors",
                text="Type and press enter",
                value=["openai", "anthropic", "deepmind", "google"],
                suggestions=[],
                maxtags=30,
                key="bc_competitors",
            )

            st_bc_threshold = st.slider(
                label="Threshold",
                value=0.5,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="ban_competitors_threshold",
            )

        settings["BanCompetitors"] = {
            "competitors": st_bc_competitors,
            "threshold": st_bc_threshold,
        }

    if "BanSubstrings" in st_enabled_scanners:
        st_bs_expander = st.sidebar.expander(
            "Ban Substrings",
            expanded=False,
        )

        with st_bs_expander:
            st_bs_substrings = st.text_area(
                "Enter substrings to ban (one per line)",
                value="test\nhello\nworld\n",
                height=200,
            ).split("\n")

            st_bs_match_type = st.selectbox(
                "Match type", ["str", "word"], index=0, key="bs_match_type"
            )
            st_bs_case_sensitive = st.checkbox(
                "Case sensitive", value=False, key="bs_case_sensitive"
            )
            st_bs_redact = st.checkbox("Redact", value=False, key="bs_redact")
            st_bs_contains_all = st.checkbox("Contains all", value=False, key="bs_contains_all")

        settings["BanSubstrings"] = {
            "substrings": st_bs_substrings,
            "match_type": st_bs_match_type,
            "case_sensitive": st_bs_case_sensitive,
            "redact": st_bs_redact,
            "contains_all": st_bs_contains_all,
        }

    if "BanTopics" in st_enabled_scanners:
        st_bt_expander = st.sidebar.expander(
            "Ban Topics",
            expanded=False,
        )

        with st_bt_expander:
            st_bt_topics = st_tags(
                label="List of topics",
                text="Type and press enter",
                value=["violence"],
                suggestions=[],
                maxtags=30,
                key="bt_topics",
            )

            st_bt_threshold = st.slider(
                label="Threshold",
                value=0.6,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="ban_topics_threshold",
            )

        settings["BanTopics"] = {"topics": st_bt_topics, "threshold": st_bt_threshold}

    if "Bias" in st_enabled_scanners:
        st_bias_expander = st.sidebar.expander(
            "Bias",
            expanded=False,
        )

        with st_bias_expander:
            st_bias_threshold = st.slider(
                label="Threshold",
                value=0.75,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="bias_threshold",
            )

            st_bias_match_type = st.selectbox(
                "Match type", [e.value for e in BiasMatchType], index=1, key="bias_match_type"
            )

        settings["Bias"] = {
            "threshold": st_bias_threshold,
            "match_type": BiasMatchType(st_bias_match_type),
        }

    if "Code" in st_enabled_scanners:
        st_cd_expander = st.sidebar.expander(
            "Code",
            expanded=False,
        )

        with st_cd_expander:
            st_cd_languages = st.multiselect(
                "Programming languages",
                options=SUPPORTED_CODE_LANGUAGES,
                default=["Python"],
            )

            st_cd_is_blocked = st.checkbox("Is blocked", value=False, key="cd_is_blocked")

        settings["Code"] = {
            "languages": st_cd_languages,
            "is_blocked": st_cd_is_blocked,
        }

    if "Deanonymize" in st_enabled_scanners:
        st_de_expander = st.sidebar.expander(
            "Deanonymize",
            expanded=False,
        )

        with st_de_expander:
            st_de_matching_strategy = st.selectbox(
                "Matching strategy", [e.value for e in DeanonymizeMatchingStrategy], index=0
            )

        settings["Deanonymize"] = {
            "matching_strategy": DeanonymizeMatchingStrategy(st_de_matching_strategy)
        }

    if "JSON" in st_enabled_scanners:
        st_json_expander = st.sidebar.expander(
            "JSON",
            expanded=False,
        )

        with st_json_expander:
            st_json_required_elements = st.slider(
                label="Required elements",
                value=0,
                min_value=0,
                max_value=10,
                step=1,
                key="json_required_elements",
                help="The minimum number of JSON elements that should be present",
            )

            st_json_repair = st.checkbox(
                "Repair", value=False, help="Attempt to repair the JSON", key="json_repair"
            )

        settings["JSON"] = {
            "required_elements": st_json_required_elements,
            "repair": st_json_repair,
        }

    if "Language" in st_enabled_scanners:
        st_lan_expander = st.sidebar.expander(
            "Language",
            expanded=False,
        )

        with st_lan_expander:
            st_lan_valid_language = st.multiselect(
                "Languages",
                [
                    "ar",
                    "bg",
                    "de",
                    "el",
                    "en",
                    "es",
                    "fr",
                    "hi",
                    "it",
                    "ja",
                    "nl",
                    "pl",
                    "pt",
                    "ru",
                    "sw",
                    "th",
                    "tr",
                    "ur",
                    "vi",
                    "zh",
                ],
                default=["en"],
            )

            st_lan_match_type = st.selectbox(
                "Match type", [e.value for e in LanguageMatchType], index=1, key="lan_match_type"
            )

        settings["Language"] = {
            "valid_languages": st_lan_valid_language,
            "match_type": LanguageMatchType(st_lan_match_type),
        }

    if "MaliciousURLs" in st_enabled_scanners:
        st_murls_expander = st.sidebar.expander(
            "Malicious URLs",
            expanded=False,
        )

        with st_murls_expander:
            st_murls_threshold = st.slider(
                label="Threshold",
                value=0.75,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="murls_threshold",
            )

        settings["MaliciousURLs"] = {"threshold": st_murls_threshold}

    if "NoRefusal" in st_enabled_scanners:
        st_no_ref_expander = st.sidebar.expander(
            "No refusal",
            expanded=False,
        )

        with st_no_ref_expander:
            st_no_ref_threshold = st.slider(
                label="Threshold",
                value=0.5,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="no_ref_threshold",
            )

        settings["NoRefusal"] = {"threshold": st_no_ref_threshold}

    if "NoRefusalLight" in st_enabled_scanners:
        settings["NoRefusalLight"] = {}

    if "ReadingTime" in st_enabled_scanners:
        st_rt_expander = st.sidebar.expander(
            "Reading Time",
            expanded=False,
        )

        with st_rt_expander:
            st_rt_max_reading_time = st.slider(
                label="Max reading time (in minutes)",
                value=5,
                min_value=0,
                max_value=3600,
                step=5,
                key="rt_max_reading_time",
            )

            st_rt_truncate = st.checkbox(
                "Truncate",
                value=False,
                help="Truncate the text to the max reading time",
                key="rt_truncate",
            )

        settings["ReadingTime"] = {"max_time": st_rt_max_reading_time, "truncate": st_rt_truncate}

    if "FactualConsistency" in st_enabled_scanners:
        st_fc_expander = st.sidebar.expander(
            "FactualConsistency",
            expanded=False,
        )

        with st_fc_expander:
            st_fc_minimum_score = st.slider(
                label="Minimum score",
                value=0.5,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="fc_threshold",
            )

        settings["FactualConsistency"] = {"minimum_score": st_fc_minimum_score}

    if "Regex" in st_enabled_scanners:
        st_regex_expander = st.sidebar.expander(
            "Regex",
            expanded=False,
        )

        with st_regex_expander:
            st_regex_patterns = st.text_area(
                "Enter patterns to ban (one per line)",
                value="Bearer [A-Za-z0-9-._~+/]+",
                height=200,
            ).split("\n")

            st_regex_is_blocked = st.checkbox("Is blocked", value=True, key="regex_is_blocked")

            st_regex_redact = st.checkbox(
                "Redact",
                value=False,
                help="Replace the matched bad patterns with [REDACTED]",
                key="regex_redact",
            )

        settings["Regex"] = {
            "patterns": st_regex_patterns,
            "is_blocked": st_regex_is_blocked,
            "redact": st_regex_redact,
        }

    if "Relevance" in st_enabled_scanners:
        st_rele_expander = st.sidebar.expander(
            "Relevance",
            expanded=False,
        )

        with st_rele_expander:
            st_rele_threshold = st.slider(
                label="Threshold",
                value=0.5,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="rele_threshold",
            )

        settings["Relevance"] = {"threshold": st_rele_threshold}

    if "Sensitive" in st_enabled_scanners:
        st_sens_expander = st.sidebar.expander(
            "Sensitive",
            expanded=False,
        )

        with st_sens_expander:
            st_sens_entity_types = st_tags(
                label="Sensitive entities",
                text="Type and press enter",
                value=default_entity_types,
                suggestions=default_entity_types
                + ["DATE_TIME", "NRP", "LOCATION", "MEDICAL_LICENSE", "US_PASSPORT"],
                maxtags=30,
                key="sensitive_entity_types",
            )
            st.caption(
                "Check all supported entities: https://llm-guard.com/input_scanners/anonymize/"
            )
            st_sens_redact = st.checkbox("Redact", value=False, key="sens_redact")
            st_sens_threshold = st.slider(
                label="Threshold",
                value=0.0,
                min_value=0.0,
                max_value=1.0,
                step=0.1,
                key="sens_threshold",
            )

        settings["Sensitive"] = {
            "entity_types": st_sens_entity_types,
            "redact": st_sens_redact,
            "threshold": st_sens_threshold,
        }

    if "Sentiment" in st_enabled_scanners:
        st_sent_expander = st.sidebar.expander(
            "Sentiment",
            expanded=False,
        )

        with st_sent_expander:
            st_sent_threshold = st.slider(
                label="Threshold",
                value=-0.5,
                min_value=-1.0,
                max_value=1.0,
                step=0.1,
                key="sentiment_threshold",
                help="Negative values are negative sentiment, positive values are positive sentiment",
            )

        settings["Sentiment"] = {"threshold": st_sent_threshold}

    if "Toxicity" in st_enabled_scanners:
        st_tox_expander = st.sidebar.expander(
            "Toxicity",
            expanded=False,
        )

        with st_tox_expander:
            st_tox_threshold = st.slider(
                label="Threshold",
                value=0.5,
                min_value=0.0,
                max_value=1.0,
                step=0.05,
                key="toxicity_threshold",
            )

            st_tox_match_type = st.selectbox(
                "Match type",
                [e.value for e in ToxicityMatchType],
                index=1,
                key="toxicity_match_type",
            )

        settings["Toxicity"] = {
            "threshold": st_tox_threshold,
            "match_type": ToxicityMatchType(st_tox_match_type),
        }

    if "URLReachability" in st_enabled_scanners:
        st_url_expander = st.sidebar.expander(
            "URL Reachability",
            expanded=False,
        )

        if st_url_expander:
            settings["URLReachability"] = {}

    if "Gibberish" in st_enabled_scanners:
        st_gib_expander = st.sidebar.expander(
            "Gibberish",
            expanded=False,
        )

        with st_gib_expander:
            st_gib_threshold = st.slider(
                label="Threshold",
                value=0.7,
                min_value=0.0,
                max_value=1.0,
                step=0.1,
                key="gib_threshold",
            )

            st_gib_match_type = st.selectbox(
                "Match type", [e.value for e in GibberishMatchType], index=1, key="gib_match_type"
            )

        settings["Gibberish"] = {"match_type": st_gib_match_type, "threshold": st_gib_threshold}

    return st_enabled_scanners, settings


def get_scanner(scanner_name: str, vault: Vault, settings: Dict):
    logger.debug(f"Initializing {scanner_name} scanner")

    if scanner_name == "Deanonymize":
        settings["vault"] = vault

    if scanner_name in [
        "BanCode",
        "BanTopics",
        "Bias",
        "Code",
        "Gibberish",
        "Language",
        "LanguageSame",
        "MaliciousURLs",
        "NoRefusal",
        "FactualConsistency",
        "Relevance",
        "Sensitive",
        "Toxicity",
    ]:
        settings["use_onnx"] = True

    return get_scanner_by_name(scanner_name, settings)


def scan(
    vault: Vault,
    enabled_scanners: List[str],
    settings: Dict,
    prompt: str,
    text: str,
    fail_fast: bool = False,
) -> (str, List[Dict[str, any]]):
    sanitized_output = text
    results = []

    status_text = "Scanning prompt..."
    if fail_fast:
        status_text = "Scanning prompt (fail fast mode)..."

    with st.status(status_text, expanded=True) as status:
        for scanner_name in enabled_scanners:
            st.write(f"{scanner_name} scanner...")
            scanner = get_scanner(
                scanner_name, vault, settings[scanner_name] if scanner_name in settings else {}
            )

            start_time = time.monotonic()
            sanitized_output, is_valid, risk_score = scanner.scan(prompt, sanitized_output)
            end_time = time.monotonic()

            results.append(
                {
                    "scanner": scanner_name,
                    "is_valid": is_valid,
                    "risk_score": risk_score,
                    "took_sec": round(timedelta(seconds=end_time - start_time).total_seconds(), 2),
                }
            )

            if fail_fast and not is_valid:
                break

        status.update(label="Scanning complete", state="complete", expanded=False)

    return sanitized_output, results