|
from litellm.integrations.custom_logger import CustomLogger |
|
import litellm |
|
|
|
|
|
|
|
class MyCustomHandler(CustomLogger): |
|
def log_pre_api_call(self, model, messages, kwargs): |
|
print(f"Pre-API Call") |
|
|
|
def log_post_api_call(self, kwargs, response_obj, start_time, end_time): |
|
print(f"Post-API Call") |
|
|
|
def log_stream_event(self, kwargs, response_obj, start_time, end_time): |
|
print(f"On Stream") |
|
|
|
def log_success_event(self, kwargs, response_obj, start_time, end_time): |
|
|
|
print("\nOn Success") |
|
|
|
model = kwargs.get("model", None) |
|
messages = kwargs.get("messages", None) |
|
user = kwargs.get("user", None) |
|
|
|
|
|
litellm_params = kwargs.get("litellm_params", {}) |
|
metadata = litellm_params.get("metadata", {}) |
|
|
|
|
|
|
|
cost = litellm.completion_cost(completion_response=response_obj) |
|
response = response_obj |
|
|
|
usage = response_obj["usage"] |
|
|
|
print( |
|
f""" |
|
Model: {model}, |
|
Messages: {messages}, |
|
User: {user}, |
|
Usage: {usage}, |
|
Cost: {cost}, |
|
Response: {response} |
|
Proxy Metadata: {metadata} |
|
""" |
|
) |
|
return |
|
|
|
def log_failure_event(self, kwargs, response_obj, start_time, end_time): |
|
print(f"On Failure") |
|
|
|
proxy_handler_instance = MyCustomHandler() |
|
|
|
|
|
|