kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | 9fefaaef1108e26035a7aa734305a164d981aae552ddbcdb13a01341decc881d |
|---|---|
| Author | Ashwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-23 19:06 -07:00 |
| Committer | Ashwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-23 19:06 -07:00 |
| Parents | a2f7f2d716e2 |
2 files changed
modified llama_stack/providers/impls/meta_reference/agents/safety.py +15 −3
| @@ -32,14 +32,26 @@ | ||
| 32 | 32 | self.output_shields = output_shields |
| 33 | 33 | |
| 34 | 34 | async def run_multiple_shields( |
| 35 | - self, messages: List[Message], shields: List[str] | |
| 35 | + self, messages: List[Message], shield_types: List[str] | |
| 36 | 36 | ) -> None: |
| 37 | - await asyncio.gather( | |
| 37 | + responses = await asyncio.gather( | |
| 38 | 38 | *[ |
| 39 | 39 | self.safety_api.run_shield( |
| 40 | 40 | shield_type=shield_type, |
| 41 | 41 | messages=messages, |
| 42 | 42 | ) |
| 43 | - for shield_type in shields | |
| 43 | + for shield_type in shield_types | |
| 44 | 44 | ] |
| 45 | 45 | ) |
| 46 | + for shield_type, response in zip(shields, responses): | |
| 47 | + if not response.violation: | |
| 48 | + continue | |
| 49 | + | |
| 50 | + violation = response.violation | |
| 51 | + if violation.violation_level == ViolationLevel.ERROR: | |
| 52 | + raise SafetyException(violation) | |
| 53 | + elif violation.violation_level == ViolationLevel.WARN: | |
| 54 | + cprint( | |
| 55 | + f"[Warn]{shield_type} raised a warning", | |
| 56 | + color="red", | |
| 57 | + ) |
modified llama_stack/providers/impls/meta_reference/safety/safety.py +6 −11
| @@ -10,7 +10,6 @@ | ||
| 10 | 10 | from llama_stack.apis.safety import * # noqa: F403 |
| 11 | 11 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 12 | 12 | |
| 13 | -from llama_stack.providers.impls.meta_reference.agents.safety import SafetyException | |
| 14 | 13 | from llama_stack.providers.impls.meta_reference.safety.shields.base import ( |
| 15 | 14 | OnViolationAction, |
| 16 | 15 | ) |
| @@ -74,23 +73,19 @@ | ||
| 74 | 73 | # TODO: we can refactor ShieldBase, etc. to be inline with the API types |
| 75 | 74 | res = await shield.run(messages) |
| 76 | 75 | violation = None |
| 77 | - if res.is_violation: | |
| 76 | + if res.is_violation and shield.on_violation_action != OnViolationAction.IGNORE: | |
| 78 | 77 | violation = SafetyViolation( |
| 79 | - violation_level=ViolationLevel.ERROR, | |
| 78 | + violation_level=( | |
| 79 | + ViolationLevel.ERROR | |
| 80 | + if shield.on_violation_action == OnViolationAction.RAISE | |
| 81 | + else ViolationLevel.WARN | |
| 82 | + ), | |
| 80 | 83 | user_message=res.violation_return_message, |
| 81 | 84 | metadata={ |
| 82 | 85 | "violation_type": res.violation_type, |
| 83 | 86 | }, |
| 84 | 87 | ) |
| 85 | 88 | |
| 86 | - if shield.on_violation_action == OnViolationAction.RAISE: | |
| 87 | - raise SafetyException(violation) | |
| 88 | - elif shield.on_violation_action == OnViolationAction.WARN: | |
| 89 | - cprint( | |
| 90 | - f"[Warn]{shield.__class__.__name__} raised a warning", | |
| 91 | - color="red", | |
| 92 | - ) | |
| 93 | - | |
| 94 | 89 | return RunShieldResponse(violation=violation) |
| 95 | 90 | |
| 96 | 91 | def get_shield_impl(self, typ: MetaReferenceShieldType) -> ShieldBase: |