Elffuss BraidHub

kikocis/llama-stack public

bug fix for safety violation
Changeseta2f7f2d716e2626861c4b88a10d35d3239e6baebe5d7bbcd3030d29869a5376d
AuthorXi Yan <xiyan@meta.com> · 2024-09-23 18:17 -07:00
CommitterXi Yan <xiyan@meta.com> · 2024-09-23 18:17 -07:00
Parentscd503abf3090

Browse the files at this changeset

3 files changed

modified llama_stack/providers/impls/meta_reference/agents/agent_instance.py +1 −1
@@ -130,7 +130,7 @@
130130 # CompletionMessage itself in the ShieldResponse
131131 messages.append(
132132 CompletionMessage(
133- content=violation.user_message,
133+ content=step.violation.user_message,
134134 stop_reason=StopReason.end_of_turn,
135135 )
136136 )
modified llama_stack/providers/impls/meta_reference/agents/safety.py +1 −11
@@ -34,7 +34,7 @@
3434 async def run_multiple_shields(
3535 self, messages: List[Message], shields: List[str]
3636 ) -> None:
37- responses = await asyncio.gather(
37+ await asyncio.gather(
3838 *[
3939 self.safety_api.run_shield(
4040 shield_type=shield_type,
@@ -43,13 +43,3 @@
4343 for shield_type in shields
4444 ]
4545 )
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 @@
1010 from llama_stack.apis.safety import * # noqa: F403
1111 from llama_models.llama3.api.datatypes import * # noqa: F403
1212
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+
1318 from .config import MetaReferenceShieldType, SafetyConfig
1419
1520 from .shields import (
@@ -78,6 +83,14 @@
7883 },
7984 )
8085
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+
8194 return RunShieldResponse(violation=violation)
8295
8396 def get_shield_impl(self, typ: MetaReferenceShieldType) -> ShieldBase: