Zum Hauptinhalt springen

Bedrock Guardrails

LiteLLM unterstĂĽtzt Bedrock Guardrails ĂĽber die Bedrock ApplyGuardrail API.

Schnellstart​

1. Definieren Sie Guardrails in Ihrer LiteLLM config.yaml​

Definieren Sie Ihre Guardrails unter dem Abschnitt guardrails

model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: openai/gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY

guardrails:
- guardrail_name: "bedrock-pre-guard"
litellm_params:
guardrail: bedrock # supported values: "aporia", "bedrock", "lakera"
mode: "during_call"
guardrailIdentifier: ff6ujrregl1q # your guardrail ID on bedrock
guardrailVersion: "DRAFT" # your guardrail version on bedrock

Unterstützte Werte für mode​

  • pre_call Vor dem LLM-Aufruf, auf der Eingabe ausfĂĽhren
  • post_call Nach dem LLM-Aufruf, auf der Eingabe & Ausgabe ausfĂĽhren
  • during_call Während des LLM-Aufrufs, auf der Eingabe ausfĂĽhren. Gleicht pre_call, läuft aber parallel zum LLM-Aufruf. Die Antwort wird erst zurĂĽckgegeben, wenn die Guardrail-PrĂĽfung abgeschlossen ist.

2. LiteLLM Gateway starten​

litellm --config config.yaml --detailed_debug

3. Anfrage testen​

Langchain, OpenAI SDK Anwendungsbeispiele

Dies sollte fehlschlagen, da ishaan@berri.ai in der Anfrage PII ist.

curl -i https://:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-npnwjPQciVRok5yNZgKmFQ" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "hi my email is ishaan@berri.ai"}
],
"guardrails": ["bedrock-pre-guard"]
}'

Erwartete Antwort bei Fehler

{
"error": {
"message": {
"error": "Violated guardrail policy",
"bedrock_guardrail_response": {
"action": "GUARDRAIL_INTERVENED",
"assessments": [
{
"topicPolicy": {
"topics": [
{
"action": "BLOCKED",
"name": "Coffee",
"type": "DENY"
}
]
}
}
],
"blockedResponse": "Sorry, the model cannot answer this question. coffee guardrail applied ",
"output": [
{
"text": "Sorry, the model cannot answer this question. coffee guardrail applied "
}
],
"outputs": [
{
"text": "Sorry, the model cannot answer this question. coffee guardrail applied "
}
],
"usage": {
"contentPolicyUnits": 0,
"contextualGroundingPolicyUnits": 0,
"sensitiveInformationPolicyFreeUnits": 0,
"sensitiveInformationPolicyUnits": 0,
"topicPolicyUnits": 1,
"wordPolicyUnits": 0
}
}
},
"type": "None",
"param": "None",
"code": "400"
}
}

PII-Maskierung mit Bedrock Guardrails​

Bedrock Guardrails unterstĂĽtzen PII-Erkennungs- und Maskierungsfunktionen. Um diese Funktion zu aktivieren, mĂĽssen Sie

  1. Setzen Sie mode auf pre_call, um die Guardrail-PrĂĽfung vor dem LLM-Aufruf durchzufĂĽhren
  2. Aktivieren Sie die Maskierung, indem Sie mask_request_content und/oder mask_response_content auf true setzen

So konfigurieren Sie dies in Ihrer config.yaml

litellm proxy config.yaml
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: openai/gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY

guardrails:
- guardrail_name: "bedrock-pre-guard"
litellm_params:
guardrail: bedrock
mode: "pre_call" # Important: must use pre_call mode for masking
guardrailIdentifier: wf0hkdb5x07f
guardrailVersion: "DRAFT"
mask_request_content: true # Enable masking in user requests
mask_response_content: true # Enable masking in model responses

Mit dieser Konfiguration liest LiteLLM, wenn die Bedrock Guardrail eingreift, die maskierte Ausgabe der Guardrail und sendet sie an das Modell.

Beispielverwendung​

Wenn aktiviert, werden PII automatisch im Text maskiert. Wenn ein Benutzer beispielsweise Folgendes sendet:

My email is john.doe@example.com and my phone number is 555-123-4567

Der an das Modell gesendete Text könnte wie folgt maskiert sein:

My email is [EMAIL] and my phone number is [PHONE_NUMBER]

Dies schützt sensible Informationen und ermöglicht es dem Modell dennoch, den Kontext der Anfrage zu verstehen.