mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-08-11 20:40:40 +00:00
docs: improve safety doc
Signed-off-by: reidliu <reid201711@gmail.com>
This commit is contained in:
parent
0db3a2f511
commit
8852fc115c
1 changed files with 15 additions and 2 deletions
|
@ -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:
|
Safety is a critical component of any AI application. Llama Stack provides a Shield system that can be applied at multiple touchpoints:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from llama_stack_client import LlamaStackClient
|
||||||
|
|
||||||
|
# Replace host and port
|
||||||
|
client = LlamaStackClient(base_url=f"http://{HOST}:{PORT}")
|
||||||
|
|
||||||
# Register a safety shield
|
# Register a safety shield
|
||||||
shield_id = "content_safety"
|
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
|
# Run content through shield
|
||||||
|
# To trigger a violation result, try inputting some sensitive content in <User message here>
|
||||||
response = client.safety.run_shield(
|
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:
|
if response.violation:
|
||||||
print(f"Safety violation detected: {response.violation.user_message}")
|
print(f"Safety violation detected: {response.violation.user_message}")
|
||||||
|
else:
|
||||||
|
print("The input content does not trigger any violations.")
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue