Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
10338cc
feat(litellm_pre_call_utils.py): support forwarding request headers t…
Oct 15, 2024
ed0dfca
fix(litellm_pre_call_utils.py): handle custom litellm key header
Oct 15, 2024
93701ef
test(router_code_coverage.py): check if all router functions are dire…
Oct 15, 2024
bc07dae
bump: version 1.49.3 → 1.49.4
Oct 15, 2024
39faecb
(refactor) use helper function `_assemble_complete_response_from_stre…
ishaan-jaff Oct 15, 2024
dd091aa
(refactor) OTEL - use safe_set_attribute for setting attributes (#6226)
ishaan-jaff Oct 15, 2024
89a7cff
(fix) prompt caching cost calculation OpenAI, Azure OpenAI (#6231)
ishaan-jaff Oct 15, 2024
8d64602
fix(allowed_model_region): allow us as allowed region (#6234)
MarcusElwin Oct 15, 2024
01dd22b
fix(litellm_pre_call_utils.py): support 'us' region routing + fix hea…
Oct 15, 2024
1295669
docs(customer_routing.md): fix region-based routing example
Oct 15, 2024
350c5cd
feat(azure.py): handle empty arguments function call - azure
Oct 15, 2024
28cfee4
feat(guardrails_ai.py): support guardrails ai integration
Oct 15, 2024
70678d9
fix(proxy/utils.py): prevent sql injection attack
Oct 15, 2024
f428b07
fix: fix linting errors
Oct 15, 2024
392dde8
fix(litellm_pre_call_utils.py): don't log litellm api key in proxy se…
Oct 16, 2024
14083ef
fix(litellm_pre_call_utils.py): don't forward stainless headers
Oct 16, 2024
7d984fa
docs(guardrails_ai.md): add guardrails ai quick start to docs
Oct 16, 2024
06c9bcd
test: handle flaky test
Oct 16, 2024
7d843ee
Merge branch 'main' into litellm_10_15_2024
Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions docs/my-website/docs/proxy/customer_routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ curl -X POST --location 'http://0.0.0.0:4000/end_user/new' \

### 2. Add eu models to model-group

Add eu models to a model group. For azure models, litellm can automatically infer the region (no need to set it).
Add eu models to a model group. Use the 'region_name' param to specify the region for each model.

Supported regions are 'eu' and 'us'.

```yaml
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/gpt-35-turbo-eu # 👈 EU azure model
api_base: https://my-endpoint-europe-berri-992.openai.azure.com/
api_key: os.environ/AZURE_EUROPE_API_KEY
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/chatgpt-v-2
api_base: https://openai-gpt-4-test-v-1.openai.azure.com/
api_version: "2023-05-15"
api_key: os.environ/AZURE_API_KEY
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/gpt-35-turbo # 👈 EU azure model
api_base: https://my-endpoint-europe-berri-992.openai.azure.com/
api_key: os.environ/AZURE_EUROPE_API_KEY
region_name: "eu"
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/chatgpt-v-2
api_base: https://openai-gpt-4-test-v-1.openai.azure.com/
api_version: "2023-05-15"
api_key: os.environ/AZURE_API_KEY
region_name: "us"

router_settings:
enable_pre_call_checks: true # 👈 IMPORTANT
Expand Down Expand Up @@ -74,10 +78,11 @@ Expected API Base in response headers

```
x-litellm-api-base: "https://my-endpoint-europe-berri-992.openai.azure.com/"
x-litellm-model-region: "eu" # 👈 CONFIRMS REGION-BASED ROUTING WORKED
```

### FAQ

**What happens if there are no available models for that region?**

Since the router filters out models not in the specified region, it will return back as an error to the user, if no models in that region are available.
Since the router filters out models not in the specified region, it will return back as an error to the user, if no models in that region are available.
118 changes: 118 additions & 0 deletions docs/my-website/docs/proxy/guardrails/guardrails_ai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import Image from '@theme/IdealImage';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Guardrails.ai

Use [Guardrails.ai](https://www.guardrailsai.com/) to add checks to LLM output.

## Pre-requisites

- Setup Guardrails AI Server. [quick start](https://www.guardrailsai.com/docs/getting_started/guardrails_server)

## Usage

1. Setup config.yaml

```yaml
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY

guardrails:
- guardrail_name: "guardrails_ai-guard"
litellm_params:
guardrail: guardrails_ai
guard_name: "gibberish_guard" # 👈 Guardrail AI guard name
mode: "post_call"
api_base: os.environ/GUARDRAILS_AI_API_BASE # 👈 Guardrails AI API Base. Defaults to "http://0.0.0.0:8000"
```

2. Start LiteLLM Gateway

```shell
litellm --config config.yaml --detailed_debug
```

3. Test request

**[Langchain, OpenAI SDK Usage Examples](../proxy/user_keys#request-format)**

```shell
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-npnwjPQciVRok5yNZgKmFQ" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "hi my email is ishaan@berri.ai"}
],
"guardrails": ["guardrails_ai-guard"]
}'
```


## ✨ Control Guardrails per Project (API Key)

:::info

✨ This is an Enterprise only feature [Contact us to get a free trial](https://calendly.com/d/4mp-gd3-k5k/litellm-1-1-onboarding-chat)

:::

Use this to control what guardrails run per project. In this tutorial we only want the following guardrails to run for 1 project (API Key)
- `guardrails`: ["aporia-pre-guard", "aporia-post-guard"]

**Step 1** Create Key with guardrail settings

<Tabs>
<TabItem value="/key/generate" label="/key/generate">

```shell
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-D '{
"guardrails": ["guardrails_ai-guard"]
}
}'
```

</TabItem>
<TabItem value="/key/update" label="/key/update">

```shell
curl --location 'http://0.0.0.0:4000/key/update' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"key": "sk-jNm1Zar7XfNdZXp49Z1kSQ",
"guardrails": ["guardrails_ai-guard"]
}
}'
```

</TabItem>
</Tabs>

**Step 2** Test it with new key

```shell
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-jNm1Zar7XfNdZXp49Z1kSQ' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "my email is ishaan@berri.ai"
}
]
}'
```



1 change: 1 addition & 0 deletions docs/my-website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const sidebars = {
items: [
"proxy/guardrails/quick_start",
"proxy/guardrails/aporia_api",
"proxy/guardrails/guardrails_ai",
"proxy/guardrails/lakera_ai",
"proxy/guardrails/bedrock",
"proxy/guardrails/pii_masking_v2",
Expand Down
3 changes: 2 additions & 1 deletion litellm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,11 @@ class LlmProviders(str, Enum):
from .llms.volcengine import VolcEngineConfig
from .llms.text_completion_codestral import MistralTextCompletionConfig
from .llms.AzureOpenAI.azure import (
AzureOpenAIConfig,
AzureOpenAIError,
AzureOpenAIAssistantsAPIConfig,
)

from .llms.AzureOpenAI.chat.gpt_transformation import AzureOpenAIConfig
from .llms.hosted_vllm.chat.transformation import HostedVLLMChatConfig
from .llms.AzureOpenAI.chat.o1_transformation import AzureOpenAIO1Config
from .llms.watsonx import IBMWatsonXAIConfig
Expand Down
11 changes: 10 additions & 1 deletion litellm/integrations/custom_guardrail.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, Optional
from typing import List, Literal, Optional

from litellm._logging import verbose_logger
from litellm.integrations.custom_logger import CustomLogger
Expand All @@ -10,11 +10,20 @@ class CustomGuardrail(CustomLogger):
def __init__(
self,
guardrail_name: Optional[str] = None,
supported_event_hooks: Optional[List[GuardrailEventHooks]] = None,
event_hook: Optional[GuardrailEventHooks] = None,
**kwargs,
):
self.guardrail_name = guardrail_name
self.supported_event_hooks = supported_event_hooks
self.event_hook: Optional[GuardrailEventHooks] = event_hook

if supported_event_hooks:
## validate event_hook is in supported_event_hooks
if event_hook and event_hook not in supported_event_hooks:
raise ValueError(
f"Event hook {event_hook} is not in the supported event hooks {supported_event_hooks}"
)
super().__init__(**kwargs)

def should_run_guardrail(self, data, event_type: GuardrailEventHooks) -> bool:
Expand Down
Loading