kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | a2f7f2d716e2626861c4b88a10d35d3239e6baebe5d7bbcd3030d29869a5376d |
|---|---|
| Author | Xi Yan <xiyan@meta.com> · 2024-09-23 18:17 -07:00 |
| Committer | Xi Yan <xiyan@meta.com> · 2024-09-23 18:17 -07:00 |
| Parents | cd503abf3090 |
3 files changed
modified llama_stack/providers/impls/meta_reference/agents/agent_instance.py +1 −1
| @@ -130,7 +130,7 @@ | ||
| 130 | 130 | # CompletionMessage itself in the ShieldResponse |
| 131 | 131 | messages.append( |
| 132 | 132 | CompletionMessage( |
| 133 | - content=violation.user_message, | |
| 133 | + content=step.violation.user_message, | |
| 134 | 134 | stop_reason=StopReason.end_of_turn, |
| 135 | 135 | ) |
| 136 | 136 | ) |
modified llama_stack/providers/impls/meta_reference/agents/safety.py +1 −11
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | async def run_multiple_shields( |
| 35 | 35 | self, messages: List[Message], shields: List[str] |
| 36 | 36 | ) -> None: |
| 37 | - responses = await asyncio.gather( | |
| 37 | + await asyncio.gather( | |
| 38 | 38 | *[ |
| 39 | 39 | self.safety_api.run_shield( |
| 40 | 40 | shield_type=shield_type, |
| @@ -43,13 +43,3 @@ | ||
| 43 | 43 | for shield_type in shields |
| 44 | 44 | ] |
| 45 | 45 | ) |
| 46 | - | |
| 47 | - for shield, r in zip(shields, responses): | |
| 48 | - if r.violation: | |
| 49 | - if shield.on_violation_action == OnViolationAction.RAISE: | |
| 50 | - raise SafetyException(r) | |
| 51 | - elif shield.on_violation_action == OnViolationAction.WARN: | |
| 52 | - cprint( | |
| 53 | - f"[Warn]{shield.__class__.__name__} raised a warning", | |
| 54 | - color="red", | |
| 55 | - ) |
modified llama_stack/providers/impls/meta_reference/safety/safety.py +13 −0
| @@ -10,6 +10,11 @@ | ||
| 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 | +from llama_stack.providers.impls.meta_reference.safety.shields.base import ( | |
| 15 | + OnViolationAction, | |
| 16 | +) | |
| 17 | + | |
| 13 | 18 | from .config import MetaReferenceShieldType, SafetyConfig |
| 14 | 19 | |
| 15 | 20 | from .shields import ( |
| @@ -78,6 +83,14 @@ | ||
| 78 | 83 | }, |
| 79 | 84 | ) |
| 80 | 85 | |
| 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 | + | |
| 81 | 94 | return RunShieldResponse(violation=violation) |
| 82 | 95 | |
| 83 | 96 | def get_shield_impl(self, typ: MetaReferenceShieldType) -> ShieldBase: |