mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
feat- control lakera ai per llm call
This commit is contained in:
parent
caf9ac4311
commit
da2be30aa0
3 changed files with 62 additions and 22 deletions
46
litellm/proxy/guardrails/guardrail_helpers.py
Normal file
46
litellm/proxy/guardrails/guardrail_helpers.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
from litellm._logging import verbose_proxy_logger
|
||||
from litellm.proxy.guardrails.init_guardrails import guardrail_name_config_map
|
||||
from litellm.types.guardrails import *
|
||||
|
||||
|
||||
async def should_proceed_based_on_metadata(data: dict, guardrail_name: str) -> 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"]:
|
||||
# expect users to pass
|
||||
# guardrails: { prompt_injection: true, rail_2: false }
|
||||
request_guardrails = data["metadata"]["guardrails"]
|
||||
verbose_proxy_logger.debug(
|
||||
"Guardrails %s passed in request - checking which to apply",
|
||||
request_guardrails,
|
||||
)
|
||||
|
||||
requested_callback_names = []
|
||||
|
||||
# get guardrail configs from `init_guardrails.py`
|
||||
# for all requested guardrails -> get their associated callbacks
|
||||
for _guardrail_name, should_run in request_guardrails.items():
|
||||
if should_run is False:
|
||||
verbose_proxy_logger.debug(
|
||||
"Guardrail %s skipped because request set to False",
|
||||
_guardrail_name,
|
||||
)
|
||||
continue
|
||||
|
||||
# lookup the guardrail in guardrail_name_config_map
|
||||
guardrail_item: GuardrailItem = guardrail_name_config_map[
|
||||
_guardrail_name
|
||||
]
|
||||
|
||||
guardrail_callbacks = guardrail_item.callbacks
|
||||
requested_callback_names.extend(guardrail_callbacks)
|
||||
|
||||
verbose_proxy_logger.debug(
|
||||
"requested_callback_names %s", requested_callback_names
|
||||
)
|
||||
if guardrail_name in requested_callback_names:
|
||||
return True
|
||||
|
||||
return False
|
Loading…
Add table
Add a link
Reference in a new issue