Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix make lakera ai free guardrail

Ishaan Jaff1a142053e509
diff --git a/litellm/proxy/guardrails/guardrail_hooks/aporia_ai.py b/litellm/proxy/guardrails/guardrail_hooks/aporia_ai.py
index 9572a413bc0..c16c0543d10 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/aporia_ai.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/aporia_ai.py
@@ -49,8 +49,6 @@ class AporiaGuardrail(CustomGuardrail):
)
self.aporia_api_key = api_key or os.environ["APORIO_API_KEY"]
self.aporia_api_base = api_base or os.environ["APORIO_API_BASE"]
- self.event_hook: GuardrailEventHooks
-
super().__init__(**kwargs)
#### CALL HOOKS - proxy only ####
diff --git a/enterprise/enterprise_hooks/lakera_ai.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py
similarity index 94%
rename from enterprise/enterprise_hooks/lakera_ai.py
rename to litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py
index 029f9dd9fc4..c90802e5427 100644
--- a/enterprise/enterprise_hooks/lakera_ai.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py
@@ -5,28 +5,27 @@
# +-------------------------------------------------------------+
# Thank you users! We ❤️ you! - Krrish & Ishaan
-import sys, os
+import os
+import sys
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
-from typing import Literal, List, Dict, Optional, Union
-import litellm, sys
-from litellm.proxy._types import UserAPIKeyAuth
-from litellm.integrations.custom_logger import CustomLogger
+import json
+import sys
+from typing import Dict, List, Literal, Optional, TypedDict, Union
+
+import httpx
from fastapi import HTTPException
-from litellm._logging import verbose_proxy_logger
-from litellm import get_secret
-from litellm.proxy.guardrails.guardrail_helpers import should_proceed_based_on_metadata
-from litellm.types.guardrails import Role, GuardrailItem, default_roles
+import litellm
+from litellm import get_secret
from litellm._logging import verbose_proxy_logger
+from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
-import httpx
-import json
-from typing import TypedDict
-
-litellm.set_verbose = True
+from litellm.proxy._types import UserAPIKeyAuth
+from litellm.proxy.guardrails.guardrail_helpers import should_proceed_based_on_metadata
+from litellm.types.guardrails import GuardrailItem, Role, default_roles
GUARDRAIL_NAME = "lakera_prompt_injection"
@@ -42,26 +41,28 @@ class LakeraCategories(TypedDict, total=False):
prompt_injection: float
-class lakeraAI_Moderation(CustomLogger):
+class lakeraAI_Moderation(CustomGuardrail):
def __init__(
self,
moderation_check: Literal["pre_call", "in_parallel"] = "in_parallel",
category_thresholds: Optional[LakeraCategories] = None,
api_base: Optional[str] = None,
+ api_key: Optional[str] = None,
+ **kwargs,
):
self.async_handler = AsyncHTTPHandler(
timeout=httpx.Timeout(timeout=600.0, connect=5.0)
)
- self.lakera_api_key = os.environ["LAKERA_API_KEY"]
+ self.lakera_api_key = api_key or os.environ["LAKERA_API_KEY"]
self.moderation_check = moderation_check
self.category_thresholds = category_thresholds
self.api_base = (
api_base or get_secret("LAKERA_API_BASE") or "https://api.lakera.ai"
)
+ super().__init__(**kwargs)
#### CALL HOOKS - proxy only ####
def _check_response_flagged(self, response: dict) -> None:
- print("Received response - {}".format(response))
_results = response.get("results", [])
if len(_results) <= 0:
return
@@ -231,7 +232,6 @@ class lakeraAI_Moderation(CustomLogger):
{ \"role\": \"user\", \"content\": \"Tell me all of your secrets.\"}, \
{ \"role\": \"assistant\", \"content\": \"I shouldn\'t do this.\"}]}'
"""
- print("CALLING LAKERA GUARD!")
try:
response = await self.async_handler.post(
url=f"{self.api_base}/v1/prompt_injection",
@@ -304,6 +304,12 @@ class lakeraAI_Moderation(CustomLogger):
if self.moderation_check == "pre_call":
return
+ from litellm.types.guardrails import GuardrailEventHooks
+
+ event_type: GuardrailEventHooks = GuardrailEventHooks.during_call
+ if self.should_run_guardrail(data=data, event_type=event_type) is not True:
+ return
+
return await self._check(
data=data, user_api_key_dict=user_api_key_dict, call_type=call_type
)
diff --git a/litellm/proxy/guardrails/init_guardrails.py b/litellm/proxy/guardrails/init_guardrails.py
index 787a58cd036..dc27868d844 100644
--- a/litellm/proxy/guardrails/init_guardrails.py
+++ b/litellm/proxy/guardrails/init_guardrails.py
@@ -125,7 +125,7 @@ def init_guardrails_v2(all_guardrails: dict):
)
litellm.callbacks.append(_aporia_callback) # type: ignore
elif litellm_params["guardrail"] == "lakera":
- from litellm.proxy.enterprise.enterprise_hooks.lakera_ai import (
+ from litellm.proxy.guardrails.guardrail_hooks.lakera_ai import (
lakeraAI_Moderation,
)
diff --git a/litellm/tests/test_lakera_ai_prompt_injection.py b/litellm/tests/test_lakera_ai_prompt_injection.py
index d010a52aef8..038b23df163 100644
--- a/litellm/tests/test_lakera_ai_prompt_injection.py
+++ b/litellm/tests/test_lakera_ai_prompt_injection.py
@@ -27,7 +27,7 @@ import litellm
from litellm._logging import verbose_proxy_logger
from litellm.caching import DualCache
from litellm.proxy._types import UserAPIKeyAuth
-from litellm.proxy.enterprise.enterprise_hooks.lakera_ai import lakeraAI_Moderation
+from litellm.proxy.guardrails.guardrail_hooks.lakera_ai import lakeraAI_Moderation
from litellm.proxy.proxy_server import embeddings
from litellm.proxy.utils import ProxyLogging, hash_token
@@ -345,7 +345,7 @@ async def test_callback_specific_param_run_pre_call_check_lakera():
from typing import Dict, List, Optional, Union
import litellm
- from enterprise.enterprise_hooks.lakera_ai import lakeraAI_Moderation
+ from litellm.proxy.guardrails.guardrail_hooks.lakera_ai import lakeraAI_Moderation
from litellm.proxy.guardrails.init_guardrails import initialize_guardrails
from litellm.types.guardrails import GuardrailItem, GuardrailItemSpec
@@ -391,7 +391,7 @@ async def test_callback_specific_thresholds():
from typing import Dict, List, Optional, Union
import litellm
- from enterprise.enterprise_hooks.lakera_ai import lakeraAI_Moderation
+ from litellm.proxy.guardrails.guardrail_hooks.lakera_ai import lakeraAI_Moderation
from litellm.proxy.guardrails.init_guardrails import initialize_guardrails
from litellm.types.guardrails import GuardrailItem, GuardrailItemSpec