| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import asyncio |
| 8 | |
| 9 | from llama_models.sku_list import resolve_model |
| 10 | |
| 11 | from llama_stack.distribution.utils.model_utils import model_local_dir |
| 12 | from llama_stack.apis.safety import * # noqa |
| 13 | |
| 14 | from .config import SafetyConfig |
| 15 | from .shields import ( |
| 16 | CodeScannerShield, |
| 17 | InjectionShield, |
| 18 | JailbreakShield, |
| 19 | LlamaGuardShield, |
| 20 | PromptGuardShield, |
| 21 | ShieldBase, |
| 22 | ThirdPartyShield, |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | def resolve_and_get_path(model_name: str) -> str: |
| 27 | model = resolve_model(model_name) |
| 28 | assert model is not None, f"Could not resolve model {model_name}" |
| 29 | model_dir = model_local_dir(model.descriptor()) |
| 30 | return model_dir |
| 31 | |
| 32 | |
| 33 | class MetaReferenceSafetyImpl(Safety): |
| 34 | def __init__(self, config: SafetyConfig) -> None: |
| 35 | self.config = config |
| 36 | |
| 37 | async def initialize(self) -> None: |
| 38 | shield_cfg = self.config.llama_guard_shield |
| 39 | if shield_cfg is not None: |
| 40 | model_dir = resolve_and_get_path(shield_cfg.model) |
| 41 | _ = LlamaGuardShield.instance( |
| 42 | model_dir=model_dir, |
| 43 | excluded_categories=shield_cfg.excluded_categories, |
| 44 | disable_input_check=shield_cfg.disable_input_check, |
| 45 | disable_output_check=shield_cfg.disable_output_check, |
| 46 | ) |
| 47 | |
| 48 | shield_cfg = self.config.prompt_guard_shield |
| 49 | if shield_cfg is not None: |
| 50 | model_dir = resolve_and_get_path(shield_cfg.model) |
| 51 | _ = PromptGuardShield.instance(model_dir) |
| 52 | |
| 53 | async def run_shields( |
| 54 | self, |
| 55 | messages: List[Message], |
| 56 | shields: List[ShieldDefinition], |
| 57 | ) -> RunShieldResponse: |
| 58 | shields = [shield_config_to_shield(c, self.config) for c in shields] |
| 59 | |
| 60 | responses = await asyncio.gather(*[shield.run(messages) for shield in shields]) |
| 61 | |
| 62 | return RunShieldResponse(responses=responses) |
| 63 | |
| 64 | |
| 65 | def shield_type_equals(a: ShieldType, b: ShieldType): |
| 66 | return a == b or a == b.value |
| 67 | |
| 68 | |
| 69 | def shield_config_to_shield( |
| 70 | sc: ShieldDefinition, safety_config: SafetyConfig |
| 71 | ) -> ShieldBase: |
| 72 | if shield_type_equals(sc.shield_type, BuiltinShield.llama_guard): |
| 73 | assert ( |
| 74 | safety_config.llama_guard_shield is not None |
| 75 | ), "Cannot use LlamaGuardShield since not present in config" |
| 76 | model_dir = resolve_and_get_path(safety_config.llama_guard_shield.model) |
| 77 | return LlamaGuardShield.instance(model_dir=model_dir) |
| 78 | elif shield_type_equals(sc.shield_type, BuiltinShield.jailbreak_shield): |
| 79 | assert ( |
| 80 | safety_config.prompt_guard_shield is not None |
| 81 | ), "Cannot use Jailbreak Shield since Prompt Guard not present in config" |
| 82 | model_dir = resolve_and_get_path(safety_config.prompt_guard_shield.model) |
| 83 | return JailbreakShield.instance(model_dir) |
| 84 | elif shield_type_equals(sc.shield_type, BuiltinShield.injection_shield): |
| 85 | assert ( |
| 86 | safety_config.prompt_guard_shield is not None |
| 87 | ), "Cannot use PromptGuardShield since not present in config" |
| 88 | model_dir = resolve_and_get_path(safety_config.prompt_guard_shield.model) |
| 89 | return InjectionShield.instance(model_dir) |
| 90 | elif shield_type_equals(sc.shield_type, BuiltinShield.code_scanner_guard): |
| 91 | return CodeScannerShield.instance() |
| 92 | elif shield_type_equals(sc.shield_type, BuiltinShield.third_party_shield): |
| 93 | return ThirdPartyShield.instance() |
| 94 | else: |
| 95 | raise ValueError(f"Unknown shield type: {sc.shield_type}") |