mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 10:44:24 +00:00
feat - control lakera per call
This commit is contained in:
parent
9f8572e427
commit
b6af67344c
1 changed files with 19 additions and 0 deletions
|
@ -32,6 +32,8 @@ import json
|
||||||
|
|
||||||
litellm.set_verbose = True
|
litellm.set_verbose = True
|
||||||
|
|
||||||
|
GUARDRAIL_NAME = "lakera_prompt_injection"
|
||||||
|
|
||||||
|
|
||||||
class _ENTERPRISE_lakeraAI_Moderation(CustomLogger):
|
class _ENTERPRISE_lakeraAI_Moderation(CustomLogger):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -41,6 +43,19 @@ class _ENTERPRISE_lakeraAI_Moderation(CustomLogger):
|
||||||
self.lakera_api_key = os.environ["LAKERA_API_KEY"]
|
self.lakera_api_key = os.environ["LAKERA_API_KEY"]
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def should_proceed(self, data: dict) -> bool:
|
||||||
|
"""
|
||||||
|
checks if this guardrail should be applied to this call
|
||||||
|
"""
|
||||||
|
if "metadata" in data and isinstance(data["metadata"], dict):
|
||||||
|
if "guardrails" in data["metadata"]:
|
||||||
|
# if guardrails passed in metadata -> this is a list of guardrails the user wants to run on the call
|
||||||
|
if GUARDRAIL_NAME not in data["metadata"]["guardrails"]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# in all other cases it should proceed
|
||||||
|
return True
|
||||||
|
|
||||||
#### CALL HOOKS - proxy only ####
|
#### CALL HOOKS - proxy only ####
|
||||||
|
|
||||||
async def async_moderation_hook( ### 👈 KEY CHANGE ###
|
async def async_moderation_hook( ### 👈 KEY CHANGE ###
|
||||||
|
@ -49,6 +64,10 @@ class _ENTERPRISE_lakeraAI_Moderation(CustomLogger):
|
||||||
user_api_key_dict: UserAPIKeyAuth,
|
user_api_key_dict: UserAPIKeyAuth,
|
||||||
call_type: Literal["completion", "embeddings", "image_generation"],
|
call_type: Literal["completion", "embeddings", "image_generation"],
|
||||||
):
|
):
|
||||||
|
|
||||||
|
if await self.should_proceed(data=data) is False:
|
||||||
|
return
|
||||||
|
|
||||||
if "messages" in data and isinstance(data["messages"], list):
|
if "messages" in data and isinstance(data["messages"], list):
|
||||||
text = ""
|
text = ""
|
||||||
for m in data["messages"]: # assume messages is a list
|
for m in data["messages"]: # assume messages is a list
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue