Aporia Guardrails mit LiteLLM Gateway
In diesem Tutorial verwenden wir das LiteLLM AI Gateway mit Aporia, um PII in Anfragen und anstößige Sprache in Antworten zu erkennen.
1. Guardrails auf Aporia einrichten
Aporia-Projekte erstellen
Erstellen Sie zwei Projekte auf Aporia
- Pre LLM API Call - Legen Sie alle Richtlinien fest, die Sie vor dem LLM API Call ausführen möchten.
- Post LLM API Call - Legen Sie alle Richtlinien fest, die Sie nach dem LLM API Call ausführen möchten.
Pre-Call: PII erkennen
Fügen Sie den PII - Prompt zu Ihrem Pre LLM API Call Projekt hinzu.
Post-Call: Anstößige Sprache in Antworten erkennen
Fügen Sie den Toxicity - Response zu Ihrem Post LLM API Call Projekt hinzu.
2. Guardrails in Ihrer LiteLLM config.yaml definieren
- Definieren Sie Ihre Guardrails im Abschnitt
guardrailsund setzen Siepre_call_guardrailsundpost_call_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: "aporia-pre-guard"
litellm_params:
guardrail: aporia # supported values: "aporia", "lakera"
mode: "during_call"
api_key: os.environ/APORIA_API_KEY_1
api_base: os.environ/APORIA_API_BASE_1
- guardrail_name: "aporia-post-guard"
litellm_params:
guardrail: aporia # supported values: "aporia", "lakera"
mode: "post_call"
api_key: os.environ/APORIA_API_KEY_2
api_base: os.environ/APORIA_API_BASE_2
Unterstützte Werte für mode
pre_callVor dem LLM-Aufruf, auf der Eingabe ausführenpost_callNach dem LLM-Aufruf, auf der Eingabe & Ausgabe ausführenduring_callWährend des LLM-Aufrufs, auf der Eingabe ausführen. Gleichtpre_call, läuft aber parallel zum LLM-Aufruf. Die Antwort wird erst zurückgegeben, wenn die Guardrail-Prüfung abgeschlossen ist.
3. Starten Sie das LiteLLM Gateway
litellm --config config.yaml --detailed_debug
4. Testanfrage
Langchain, OpenAI SDK Anwendungsbeispiele
- Fehlgeschlagener Aufruf
- Erfolgreicher Aufruf
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": ["aporia-pre-guard", "aporia-post-guard"]
}'
Erwartete Antwort bei Fehler
{
"error": {
"message": {
"error": "Violated guardrail policy",
"aporia_ai_response": {
"action": "block",
"revised_prompt": null,
"revised_response": "Aporia detected and blocked PII",
"explain_log": null
}
},
"type": "None",
"param": "None",
"code": "400"
}
}
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 what is the weather"}
],
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}'
5. Guardrails pro Projekt steuern (API-Schlüssel)
Verwenden Sie dies, um zu steuern, welche Guardrails pro Projekt ausgeführt werden. In diesem Tutorial möchten wir nur die folgenden Guardrails für 1 Projekt (API-Schlüssel) ausführen
guardrails: ["aporia-pre-guard", "aporia-post-guard"]
Schritt 1 Schlüssel mit Guardrail-Einstellungen erstellen
- /key/generate
- /key/update
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-D '{
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}
}'
curl --location 'http://0.0.0.0:4000/key/update' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"key": "sk-jNm1Zar7XfNdZXp49Z1kSQ",
"guardrails": ["aporia-pre-guard", "aporia-post-guard"]
}
}'
Schritt 2 Mit neuem Schlüssel testen
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-jNm1Zar7XfNdZXp49Z1kSQ' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "my email is ishaan@berri.ai"
}
]
}'