fix(utils.py): allow user to opt in to raw request logging to langfuse

This commit is contained in:
Krrish Dholakia 2024-06-11 13:35:22 -07:00
parent a8ea7c6d31
commit 7eae0ff7e3
5 changed files with 62 additions and 21 deletions

View file

@ -0,0 +1,38 @@
import Image from '@theme/IdealImage';
# Raw Request/Response Logging
See the raw request/response sent by LiteLLM in your logging provider (OTEL/Langfuse/etc.).
```python
# pip install langfuse
import litellm
import os
# log raw request/response
litellm.log_raw_request_response = True
# from https://cloud.langfuse.com/
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
# Optional, defaults to https://cloud.langfuse.com
os.environ["LANGFUSE_HOST"] # optional
# LLM API Keys
os.environ['OPENAI_API_KEY']=""
# set langfuse as a callback, litellm will send the data to langfuse
litellm.success_callback = ["langfuse"]
# openai call
response = litellm.completion(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Hi 👋 - i'm openai"}
]
)
```
**Expected Log**
<Image img={require('../../img/raw_request_log.png')}/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

View file

@ -182,6 +182,7 @@ const sidebars = {
label: "Logging & Observability", label: "Logging & Observability",
items: [ items: [
"debugging/local_debugging", "debugging/local_debugging",
"observability/raw_request_response",
"observability/callbacks", "observability/callbacks",
"observability/custom_callback", "observability/custom_callback",
"observability/langfuse_integration", "observability/langfuse_integration",

View file

@ -60,6 +60,7 @@ _async_failure_callback: List[Callable] = (
pre_call_rules: List[Callable] = [] pre_call_rules: List[Callable] = []
post_call_rules: List[Callable] = [] post_call_rules: List[Callable] = []
turn_off_message_logging: Optional[bool] = False turn_off_message_logging: Optional[bool] = False
log_raw_request_response: bool = False
redact_messages_in_exceptions: Optional[bool] = False redact_messages_in_exceptions: Optional[bool] = False
store_audit_logs = False # Enterprise feature, allow users to see audit logs store_audit_logs = False # Enterprise feature, allow users to see audit logs
## end of callbacks ############# ## end of callbacks #############

View file

@ -1347,28 +1347,29 @@ class Logging:
) )
else: else:
verbose_logger.debug(f"\033[92m{curl_command}\033[0m\n") verbose_logger.debug(f"\033[92m{curl_command}\033[0m\n")
# log raw request to provider (like LangFuse) # log raw request to provider (like LangFuse) -- if opted in.
try: if litellm.log_raw_request_response is True:
# [Non-blocking Extra Debug Information in metadata] try:
_litellm_params = self.model_call_details.get("litellm_params", {}) # [Non-blocking Extra Debug Information in metadata]
_metadata = _litellm_params.get("metadata", {}) or {} _litellm_params = self.model_call_details.get("litellm_params", {})
if ( _metadata = _litellm_params.get("metadata", {}) or {}
litellm.turn_off_message_logging is not None if (
and litellm.turn_off_message_logging is True litellm.turn_off_message_logging is not None
): and litellm.turn_off_message_logging is True
):
_metadata["raw_request"] = (
"redacted by litellm. \
'litellm.turn_off_message_logging=True'"
)
else:
_metadata["raw_request"] = str(curl_command)
except Exception as e:
_metadata["raw_request"] = ( _metadata["raw_request"] = (
"redacted by litellm. \ "Unable to Log \
'litellm.turn_off_message_logging=True'" raw request: {}".format(
str(e)
)
) )
else:
_metadata["raw_request"] = str(curl_command)
except Exception as e:
_metadata["raw_request"] = (
"Unable to Log \
raw request: {}".format(
str(e)
)
)
if self.logger_fn and callable(self.logger_fn): if self.logger_fn and callable(self.logger_fn):
try: try:
self.logger_fn( self.logger_fn(
@ -2735,7 +2736,7 @@ class Logging:
only redacts when litellm.turn_off_message_logging == True only redacts when litellm.turn_off_message_logging == True
""" """
# check if user opted out of logging message/response to callbacks # check if user opted out of logging message/response to callbacks
if litellm.turn_off_message_logging == True: if litellm.turn_off_message_logging is True:
# remove messages, prompts, input, response from logging # remove messages, prompts, input, response from logging
self.model_call_details["messages"] = [ self.model_call_details["messages"] = [
{"role": "user", "content": "redacted-by-litellm"} {"role": "user", "content": "redacted-by-litellm"}