Merge pull request #4464 from BerriAI/litellm_return_lakera_exception_in_response

[Enterprise] Return Raw response from Lakera in failed responses
This commit is contained in:
Ishaan Jaff
2024-06-28 16:56:59 -07:00
committed by GitHub
2 changed files with 26 additions and 12 deletions
+5 -1
View File
@@ -114,7 +114,11 @@ class _ENTERPRISE_lakeraAI_Moderation(CustomLogger):
if flagged == True:
raise HTTPException(
status_code=400, detail={"error": "Violated content safety policy"}
status_code=400,
detail={
"error": "Violated content safety policy",
"lakera_ai_response": _json_response,
},
)
pass
@@ -1,10 +1,16 @@
# What is this?
## This tests the Lakera AI integration
import sys, os, asyncio, time, random
from datetime import datetime
import asyncio
import os
import random
import sys
import time
import traceback
from datetime import datetime
from dotenv import load_dotenv
from fastapi import HTTPException
load_dotenv()
import os
@@ -12,17 +18,19 @@ import os
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
import logging
import pytest
import litellm
from litellm import Router, mock_completion
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 import Router, mock_completion
from litellm.proxy.utils import ProxyLogging, hash_token
from litellm.proxy._types import UserAPIKeyAuth
from litellm.caching import DualCache
from litellm._logging import verbose_proxy_logger
import logging
verbose_proxy_logger.setLevel(logging.DEBUG)
@@ -55,10 +63,12 @@ async def test_lakera_prompt_injection_detection():
call_type="completion",
)
pytest.fail(f"Should have failed")
except Exception as e:
print("Got exception: ", e)
assert "Violated content safety policy" in str(e)
pass
except HTTPException as http_exception:
print("http exception details=", http_exception.detail)
# Assert that the laker ai response is in the exception raise
assert "lakera_ai_response" in http_exception.detail
assert "Violated content safety policy" in str(http_exception)
@pytest.mark.asyncio