BerriAI / litellm

Python SDK, Proxy Server (LLM Gateway) to call 100+ LLM APIs in OpenAI format - [Bedrock, Azure, OpenAI, VertexAI, Cohere, Anthropic, Sagemaker, HuggingFace, Replicate, Groq]
https://docs.litellm.ai/docs/
Other
13.15k stars 1.54k forks source link

[Feature]: Allow any python package to be allowed under guardrails in litellm_config.yaml #6252

Open abhishek9sharma opened 6 days ago

abhishek9sharma commented 6 days ago

The Feature

As per this example https://docs.litellm.ai/docs/proxy/guardrails/custom_guardrail, custom_guardrails can be hooked up with litellm_config by adding information in yaml like below


guardrails:
  - guardrail_name: "custom-pre-guard"
    litellm_params:
      guardrail: custom_guardrail.myCustomGuardrail  # 👈 Key change
      mode: "pre_call"                  # runs async_pre_call_hook
  - guardrail_name: "custom-during-guard"
    litellm_params:
      guardrail: custom_guardrail.myCustomGuardrail  
      mode: "during_call"               # runs async_moderation_hook
  - guardrail_name: "custom-post-guard"
    litellm_params:
      guardrail: custom_guardrail.myCustomGuardrail
      mode: "post_call" 

Add a feature to allow any class inside an installed package to be usable in the config above. A dummy example is below

guardrails:
  - guardrail_name: "custom-pre-guard"
    litellm_params:
      guardrail: abcpackage.custom_guardrail.myCustomGuardrail  # 👈 Key change
      mode: "pre_call"                  # runs async_pre_call_hook
  - guardrail_name: "custom-during-guard"
    litellm_params:
      guardrail: abcpackage.custom_guardrail.myCustomGuardrail  
      mode: "during_call"               # runs async_moderation_hook
  - guardrail_name: "custom-post-guard"
    litellm_params:
      guardrail: abcpackage.custom_guardrail.myCustomGuardrail
      mode: "post_call" 

I think the logic needs to be changed at below line

Motivation, pitch

Customers may have their own python packages with guardrail implementation which they may want to use

Twitter / LinkedIn details

No response

krrishdholakia commented 6 days ago

what's an e2e example of this? @abhishek9sharma

abhishek9sharma commented 5 days ago

@krrishdholakia Below should demonstrate what I am trying to achieve

e2example.zip

Can be run as below

unzip e2example.zip -d .
cd e2example/customguardpkg
python -m pip install .
cd ..
litellm --config litellm_config.yaml