mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
Merge pull request #5303 from BerriAI/litellm_make_lakera_free
[Feat-Proxy] Make Guardrails Free / OSS - Lakera AI, Aporia AI 🛡️
This commit is contained in:
commit
befe041f00
9 changed files with 81 additions and 76 deletions
|
|
@ -36,7 +36,7 @@ litellm.set_verbose = True
|
|||
GUARDRAIL_NAME = "aporia"
|
||||
|
||||
|
||||
class _ENTERPRISE_Aporia(CustomGuardrail):
|
||||
class AporiaGuardrail(CustomGuardrail):
|
||||
def __init__(
|
||||
self, api_key: Optional[str] = None, api_base: Optional[str] = None, **kwargs
|
||||
):
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Literal
|
||||
from typing import Literal, Optional
|
||||
|
||||
from litellm._logging import verbose_logger
|
||||
from litellm.integrations.custom_logger import CustomLogger
|
||||
|
|
@ -7,9 +7,14 @@ from litellm.types.guardrails import GuardrailEventHooks
|
|||
|
||||
class CustomGuardrail(CustomLogger):
|
||||
|
||||
def __init__(self, guardrail_name: str, event_hook: GuardrailEventHooks, **kwargs):
|
||||
def __init__(
|
||||
self,
|
||||
guardrail_name: Optional[str] = None,
|
||||
event_hook: Optional[GuardrailEventHooks] = None,
|
||||
**kwargs
|
||||
):
|
||||
self.guardrail_name = guardrail_name
|
||||
self.event_hook: GuardrailEventHooks = event_hook
|
||||
self.event_hook: Optional[GuardrailEventHooks] = event_hook
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def should_run_guardrail(self, data, event_type: GuardrailEventHooks) -> bool:
|
||||
|
|
|
|||
|
|
@ -101,35 +101,21 @@ def initialize_callbacks_on_proxy(
|
|||
openai_moderations_object = _ENTERPRISE_OpenAI_Moderation()
|
||||
imported_list.append(openai_moderations_object)
|
||||
elif isinstance(callback, str) and callback == "lakera_prompt_injection":
|
||||
from enterprise.enterprise_hooks.lakera_ai import (
|
||||
_ENTERPRISE_lakeraAI_Moderation,
|
||||
from litellm.proxy.guardrails.guardrail_hooks.lakera_ai import (
|
||||
lakeraAI_Moderation,
|
||||
)
|
||||
|
||||
if premium_user != True:
|
||||
raise Exception(
|
||||
"Trying to use LakeraAI Prompt Injection"
|
||||
+ CommonProxyErrors.not_premium_user.value
|
||||
)
|
||||
|
||||
init_params = {}
|
||||
if "lakera_prompt_injection" in callback_specific_params:
|
||||
init_params = callback_specific_params["lakera_prompt_injection"]
|
||||
lakera_moderations_object = _ENTERPRISE_lakeraAI_Moderation(
|
||||
**init_params
|
||||
)
|
||||
lakera_moderations_object = lakeraAI_Moderation(**init_params)
|
||||
imported_list.append(lakera_moderations_object)
|
||||
elif isinstance(callback, str) and callback == "aporia_prompt_injection":
|
||||
from litellm.proxy.guardrails.guardrail_hooks.aporia_ai import (
|
||||
_ENTERPRISE_Aporia,
|
||||
AporiaGuardrail,
|
||||
)
|
||||
|
||||
if premium_user is not True:
|
||||
raise Exception(
|
||||
"Trying to use Aporia AI Guardrail"
|
||||
+ CommonProxyErrors.not_premium_user.value
|
||||
)
|
||||
|
||||
aporia_guardrail_object = _ENTERPRISE_Aporia()
|
||||
aporia_guardrail_object = AporiaGuardrail()
|
||||
imported_list.append(aporia_guardrail_object)
|
||||
elif isinstance(callback, str) and callback == "google_text_moderation":
|
||||
from enterprise.enterprise_hooks.google_text_moderation import (
|
||||
|
|
@ -309,7 +295,11 @@ def get_applied_guardrails_header(request_data: Dict) -> Optional[Dict]:
|
|||
return None
|
||||
|
||||
|
||||
def add_guardrail_to_applied_guardrails_header(request_data: Dict, guardrail_name: str):
|
||||
def add_guardrail_to_applied_guardrails_header(
|
||||
request_data: Dict, guardrail_name: Optional[str]
|
||||
):
|
||||
if guardrail_name is None:
|
||||
return
|
||||
_metadata = request_data.get("metadata", None) or {}
|
||||
if "applied_guardrails" in _metadata:
|
||||
_metadata["applied_guardrails"].append(guardrail_name)
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ litellm.set_verbose = True
|
|||
GUARDRAIL_NAME = "aporia"
|
||||
|
||||
|
||||
class _ENTERPRISE_Aporia(CustomGuardrail):
|
||||
class AporiaGuardrail(CustomGuardrail):
|
||||
def __init__(
|
||||
self, api_key: Optional[str] = None, api_base: Optional[str] = None, **kwargs
|
||||
):
|
||||
|
|
@ -49,8 +49,6 @@ class _ENTERPRISE_Aporia(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 ####
|
||||
|
|
|
|||
|
|
@ -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
|
||||
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
|
||||
|
||||
from litellm._logging import verbose_proxy_logger
|
||||
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
||||
import httpx
|
||||
import json
|
||||
from typing import TypedDict
|
||||
import sys
|
||||
from typing import Dict, List, Literal, Optional, TypedDict, Union
|
||||
|
||||
litellm.set_verbose = True
|
||||
import httpx
|
||||
from fastapi import HTTPException
|
||||
|
||||
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
|
||||
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 _ENTERPRISE_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 _ENTERPRISE_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",
|
||||
|
|
@ -301,7 +301,14 @@ class _ENTERPRISE_lakeraAI_Moderation(CustomLogger):
|
|||
user_api_key_dict: UserAPIKeyAuth,
|
||||
call_type: Literal["completion", "embeddings", "image_generation"],
|
||||
):
|
||||
if self.moderation_check == "pre_call":
|
||||
if self.event_hook is None:
|
||||
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(
|
||||
|
|
@ -114,10 +114,10 @@ def init_guardrails_v2(all_guardrails: dict):
|
|||
# Init guardrail CustomLoggerClass
|
||||
if litellm_params["guardrail"] == "aporia":
|
||||
from litellm.proxy.guardrails.guardrail_hooks.aporia_ai import (
|
||||
_ENTERPRISE_Aporia,
|
||||
AporiaGuardrail,
|
||||
)
|
||||
|
||||
_aporia_callback = _ENTERPRISE_Aporia(
|
||||
_aporia_callback = AporiaGuardrail(
|
||||
api_base=litellm_params["api_base"],
|
||||
api_key=litellm_params["api_key"],
|
||||
guardrail_name=guardrail["guardrail_name"],
|
||||
|
|
@ -125,15 +125,21 @@ 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 (
|
||||
_ENTERPRISE_lakeraAI_Moderation,
|
||||
from litellm.proxy.guardrails.guardrail_hooks.lakera_ai import (
|
||||
lakeraAI_Moderation,
|
||||
)
|
||||
|
||||
_lakera_callback = _ENTERPRISE_lakeraAI_Moderation()
|
||||
_lakera_callback = lakeraAI_Moderation(
|
||||
api_base=litellm_params["api_base"],
|
||||
api_key=litellm_params["api_key"],
|
||||
guardrail_name=guardrail["guardrail_name"],
|
||||
event_hook=litellm_params["mode"],
|
||||
)
|
||||
litellm.callbacks.append(_lakera_callback) # type: ignore
|
||||
|
||||
parsed_guardrail = Guardrail(
|
||||
guardrail_name=guardrail["guardrail_name"], litellm_params=litellm_params
|
||||
guardrail_name=guardrail["guardrail_name"],
|
||||
litellm_params=litellm_params,
|
||||
)
|
||||
|
||||
guardrail_list.append(parsed_guardrail)
|
||||
|
|
|
|||
|
|
@ -12,9 +12,10 @@ guardrails:
|
|||
mode: "post_call"
|
||||
api_key: os.environ/APORIA_API_KEY_1
|
||||
api_base: os.environ/APORIA_API_BASE_1
|
||||
- guardrail_name: "aporia-post-guard"
|
||||
- guardrail_name: "lakera-pre-guard"
|
||||
litellm_params:
|
||||
guardrail: aporia # supported values: "aporia", "bedrock", "lakera"
|
||||
mode: "post_call"
|
||||
api_key: os.environ/APORIA_API_KEY_2
|
||||
api_base: os.environ/APORIA_API_BASE_2
|
||||
guardrail: lakera # supported values: "aporia", "bedrock", "lakera"
|
||||
mode: "during_call"
|
||||
api_key: os.environ/LAKERA_API_KEY
|
||||
api_base: os.environ/LAKERA_API_BASE
|
||||
|
||||
|
|
@ -27,9 +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 (
|
||||
_ENTERPRISE_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
|
||||
|
||||
|
|
@ -62,7 +60,7 @@ async def test_lakera_prompt_injection_detection():
|
|||
Tests to see OpenAI Moderation raises an error for a flagged response
|
||||
"""
|
||||
|
||||
lakera_ai = _ENTERPRISE_lakeraAI_Moderation()
|
||||
lakera_ai = lakeraAI_Moderation()
|
||||
_api_key = "sk-12345"
|
||||
_api_key = hash_token("sk-12345")
|
||||
user_api_key_dict = UserAPIKeyAuth(api_key=_api_key)
|
||||
|
|
@ -106,7 +104,7 @@ async def test_lakera_safe_prompt():
|
|||
Nothing should get raised here
|
||||
"""
|
||||
|
||||
lakera_ai = _ENTERPRISE_lakeraAI_Moderation()
|
||||
lakera_ai = lakeraAI_Moderation()
|
||||
_api_key = "sk-12345"
|
||||
_api_key = hash_token("sk-12345")
|
||||
user_api_key_dict = UserAPIKeyAuth(api_key=_api_key)
|
||||
|
|
@ -144,7 +142,7 @@ async def test_moderations_on_embeddings():
|
|||
setattr(litellm.proxy.proxy_server, "llm_router", temp_router)
|
||||
|
||||
api_route = APIRoute(path="/embeddings", endpoint=embeddings)
|
||||
litellm.callbacks = [_ENTERPRISE_lakeraAI_Moderation()]
|
||||
litellm.callbacks = [lakeraAI_Moderation()]
|
||||
request = Request(
|
||||
{
|
||||
"type": "http",
|
||||
|
|
@ -189,7 +187,7 @@ async def test_moderations_on_embeddings():
|
|||
),
|
||||
)
|
||||
async def test_messages_for_disabled_role(spy_post):
|
||||
moderation = _ENTERPRISE_lakeraAI_Moderation()
|
||||
moderation = lakeraAI_Moderation()
|
||||
data = {
|
||||
"messages": [
|
||||
{"role": "assistant", "content": "This should be ignored."},
|
||||
|
|
@ -227,7 +225,7 @@ async def test_messages_for_disabled_role(spy_post):
|
|||
)
|
||||
@patch("litellm.add_function_to_prompt", False)
|
||||
async def test_system_message_with_function_input(spy_post):
|
||||
moderation = _ENTERPRISE_lakeraAI_Moderation()
|
||||
moderation = lakeraAI_Moderation()
|
||||
data = {
|
||||
"messages": [
|
||||
{"role": "system", "content": "Initial content."},
|
||||
|
|
@ -271,7 +269,7 @@ async def test_system_message_with_function_input(spy_post):
|
|||
)
|
||||
@patch("litellm.add_function_to_prompt", False)
|
||||
async def test_multi_message_with_function_input(spy_post):
|
||||
moderation = _ENTERPRISE_lakeraAI_Moderation()
|
||||
moderation = lakeraAI_Moderation()
|
||||
data = {
|
||||
"messages": [
|
||||
{
|
||||
|
|
@ -318,7 +316,7 @@ async def test_multi_message_with_function_input(spy_post):
|
|||
),
|
||||
)
|
||||
async def test_message_ordering(spy_post):
|
||||
moderation = _ENTERPRISE_lakeraAI_Moderation()
|
||||
moderation = lakeraAI_Moderation()
|
||||
data = {
|
||||
"messages": [
|
||||
{"role": "assistant", "content": "Assistant message."},
|
||||
|
|
@ -347,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 _ENTERPRISE_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
|
||||
|
||||
|
|
@ -374,10 +372,10 @@ async def test_callback_specific_param_run_pre_call_check_lakera():
|
|||
|
||||
assert len(litellm.guardrail_name_config_map) == 1
|
||||
|
||||
prompt_injection_obj: Optional[_ENTERPRISE_lakeraAI_Moderation] = None
|
||||
prompt_injection_obj: Optional[lakeraAI_Moderation] = None
|
||||
print("litellm callbacks={}".format(litellm.callbacks))
|
||||
for callback in litellm.callbacks:
|
||||
if isinstance(callback, _ENTERPRISE_lakeraAI_Moderation):
|
||||
if isinstance(callback, lakeraAI_Moderation):
|
||||
prompt_injection_obj = callback
|
||||
else:
|
||||
print("Type of callback={}".format(type(callback)))
|
||||
|
|
@ -393,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 _ENTERPRISE_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
|
||||
|
||||
|
|
@ -426,10 +424,10 @@ async def test_callback_specific_thresholds():
|
|||
|
||||
assert len(litellm.guardrail_name_config_map) == 1
|
||||
|
||||
prompt_injection_obj: Optional[_ENTERPRISE_lakeraAI_Moderation] = None
|
||||
prompt_injection_obj: Optional[lakeraAI_Moderation] = None
|
||||
print("litellm callbacks={}".format(litellm.callbacks))
|
||||
for callback in litellm.callbacks:
|
||||
if isinstance(callback, _ENTERPRISE_lakeraAI_Moderation):
|
||||
if isinstance(callback, lakeraAI_Moderation):
|
||||
prompt_injection_obj = callback
|
||||
else:
|
||||
print("Type of callback={}".format(type(callback)))
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ def test_active_callbacks(client):
|
|||
_active_callbacks = json_response["litellm.callbacks"]
|
||||
|
||||
expected_callback_names = [
|
||||
"_ENTERPRISE_lakeraAI_Moderation",
|
||||
"lakeraAI_Moderation",
|
||||
"_OPTIONAL_PromptInjectionDetectio",
|
||||
"_ENTERPRISE_SecretDetection",
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue