From 8852fc115cab11246806715326d5da09727ac84a Mon Sep 17 00:00:00 2001 From: reidliu Date: Sat, 8 Mar 2025 17:09:08 +0800 Subject: [PATCH] docs: improve safety doc Signed-off-by: reidliu --- docs/source/building_applications/safety.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/source/building_applications/safety.md b/docs/source/building_applications/safety.md index 30afe7ad2..16e85bbe0 100644 --- a/docs/source/building_applications/safety.md +++ b/docs/source/building_applications/safety.md @@ -3,15 +3,28 @@ Safety is a critical component of any AI application. Llama Stack provides a Shield system that can be applied at multiple touchpoints: ```python +from llama_stack_client import LlamaStackClient + +# Replace host and port +client = LlamaStackClient(base_url=f"http://{HOST}:{PORT}") + # Register a safety shield shield_id = "content_safety" -client.shields.register(shield_id=shield_id, provider_shield_id="llama-guard-basic") +SHEILD_NAME = "meta-llama/Llama-Guard-3-1B" +# If no provider specified and multiple providers available, need specify provider_id, e.g. provider_id="llama-guard" +# Check with `llama model list` for the supported Llama Guard type models +client.shields.register(shield_id=shield_id, provider_shield_id=SHEILD_NAME) # Run content through shield +# To trigger a violation result, try inputting some sensitive content in response = client.safety.run_shield( - shield_id=shield_id, messages=[{"role": "user", "content": "User message here"}] + shield_id=SHEILD_NAME, + messages=[{"role": "user", "content": "User message here"}], + params={}, ) if response.violation: print(f"Safety violation detected: {response.violation.user_message}") +else: + print("The input content does not trigger any violations.") ```