Elffuss BraidHub

kikocis/llama-stack public

174d429e7af6 · 1.9 KiBRawPermalink
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
7from enum import Enum
8from typing import List, Optional
9
10from llama_models.sku_list import CoreModelId, safety_models
11
12from pydantic import BaseModel, validator
13
14
15class MetaReferenceShieldType(Enum):
16 llama_guard = "llama_guard"
17 code_scanner_guard = "code_scanner_guard"
18 injection_shield = "injection_shield"
19 jailbreak_shield = "jailbreak_shield"
20
21
22class LlamaGuardShieldConfig(BaseModel):
23 model: str = "Llama-Guard-3-8B"
24 excluded_categories: List[str] = []
25 disable_input_check: bool = False
26 disable_output_check: bool = False
27
28 @validator("model")
29 @classmethod
30 def validate_model(cls, model: str) -> str:
31 permitted_models = [
32 m.descriptor()
33 for m in safety_models()
34 if (
35 m.core_model_id
36 in {CoreModelId.llama_guard_3_8b, CoreModelId.llama_guard_3_11b_vision}
37 )
38 ]
39 if model not in permitted_models:
40 raise ValueError(
41 f"Invalid model: {model}. Must be one of {permitted_models}"
42 )
43 return model
44
45
46class PromptGuardShieldConfig(BaseModel):
47 model: str = "Prompt-Guard-86M"
48
49 @validator("model")
50 @classmethod
51 def validate_model(cls, model: str) -> str:
52 permitted_models = [
53 m.descriptor()
54 for m in safety_models()
55 if m.core_model_id == CoreModelId.prompt_guard_86m
56 ]
57 if model not in permitted_models:
58 raise ValueError(
59 f"Invalid model: {model}. Must be one of {permitted_models}"
60 )
61 return model
62
63
64class SafetyConfig(BaseModel):
65 llama_guard_shield: Optional[LlamaGuardShieldConfig] = None
66 prompt_guard_shield: Optional[PromptGuardShieldConfig] = None