mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
(feat) Allow enabling logging message / response for specific virtual keys (#7071)
* redact_message_input_output_from_logging * initialize_standard_callback_dynamic_params * allow dynamically opting out of redaction * test_redact_msgs_from_logs_with_dynamic_params * fix AddTeamCallback * _get_turn_off_message_logging_from_dynamic_params * test_global_redaction_with_dynamic_params * test_dynamic_turn_off_message_logging * docs Disable/Enable Message redaction * fix doe qual check * _get_turn_off_message_logging_from_dynamic_params
This commit is contained in:
parent
4d90e8ad62
commit
d43aa6f472
9 changed files with 409 additions and 4 deletions
|
@ -6,6 +6,8 @@ from unittest import mock
|
|||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from litellm.types.utils import StandardCallbackDynamicParams
|
||||
|
||||
load_dotenv()
|
||||
import os
|
||||
|
||||
|
@ -545,6 +547,92 @@ def test_redact_msgs_from_logs():
|
|||
print("Test passed")
|
||||
|
||||
|
||||
def test_redact_msgs_from_logs_with_dynamic_params():
|
||||
"""
|
||||
Tests redaction behavior based on standard_callback_dynamic_params setting:
|
||||
In all tests litellm.turn_off_message_logging is True
|
||||
|
||||
|
||||
1. When standard_callback_dynamic_params.turn_off_message_logging is False (or not set): No redaction should occur. User has opted out of redaction.
|
||||
2. When standard_callback_dynamic_params.turn_off_message_logging is True: Redaction should occur. User has opted in to redaction.
|
||||
3. standard_callback_dynamic_params.turn_off_message_logging not set, litellm.turn_off_message_logging is True: Redaction should occur.
|
||||
"""
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging
|
||||
from litellm.litellm_core_utils.redact_messages import (
|
||||
redact_message_input_output_from_logging,
|
||||
)
|
||||
|
||||
litellm.turn_off_message_logging = True
|
||||
test_content = "I'm LLaMA, an AI assistant developed by Meta AI that can understand and respond to human input in a conversational manner."
|
||||
response_obj = litellm.ModelResponse(
|
||||
choices=[
|
||||
{
|
||||
"finish_reason": "stop",
|
||||
"index": 0,
|
||||
"message": {
|
||||
"content": test_content,
|
||||
"role": "assistant",
|
||||
},
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
litellm_logging_obj = Logging(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
stream=False,
|
||||
call_type="acompletion",
|
||||
litellm_call_id="1234",
|
||||
start_time=datetime.now(),
|
||||
function_id="1234",
|
||||
)
|
||||
|
||||
# Test Case 1: standard_callback_dynamic_params = False (or not set)
|
||||
standard_callback_dynamic_params = StandardCallbackDynamicParams(
|
||||
turn_off_message_logging=False
|
||||
)
|
||||
litellm_logging_obj.model_call_details["standard_callback_dynamic_params"] = (
|
||||
standard_callback_dynamic_params
|
||||
)
|
||||
_redacted_response_obj = redact_message_input_output_from_logging(
|
||||
result=response_obj,
|
||||
model_call_details=litellm_logging_obj.model_call_details,
|
||||
)
|
||||
# Assert no redaction occurred
|
||||
assert _redacted_response_obj.choices[0].message.content == test_content
|
||||
|
||||
# Test Case 2: standard_callback_dynamic_params = True
|
||||
standard_callback_dynamic_params = StandardCallbackDynamicParams(
|
||||
turn_off_message_logging=True
|
||||
)
|
||||
litellm_logging_obj.model_call_details["standard_callback_dynamic_params"] = (
|
||||
standard_callback_dynamic_params
|
||||
)
|
||||
_redacted_response_obj = redact_message_input_output_from_logging(
|
||||
result=response_obj,
|
||||
model_call_details=litellm_logging_obj.model_call_details,
|
||||
)
|
||||
# Assert redaction occurred
|
||||
assert _redacted_response_obj.choices[0].message.content == "redacted-by-litellm"
|
||||
|
||||
# Test Case 3: standard_callback_dynamic_params does not override litellm.turn_off_message_logging
|
||||
# since litellm.turn_off_message_logging is True redaction should occur
|
||||
standard_callback_dynamic_params = StandardCallbackDynamicParams()
|
||||
litellm_logging_obj.model_call_details["standard_callback_dynamic_params"] = (
|
||||
standard_callback_dynamic_params
|
||||
)
|
||||
_redacted_response_obj = redact_message_input_output_from_logging(
|
||||
result=response_obj,
|
||||
model_call_details=litellm_logging_obj.model_call_details,
|
||||
)
|
||||
# Assert no redaction occurred
|
||||
assert _redacted_response_obj.choices[0].message.content == "redacted-by-litellm"
|
||||
|
||||
# Reset settings
|
||||
litellm.turn_off_message_logging = False
|
||||
print("Test passed")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"duration, unit",
|
||||
[("7s", "s"), ("7m", "m"), ("7h", "h"), ("7d", "d"), ("7mo", "mo")],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue