mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 02:34:29 +00:00
* fix(generic_api_callback.py): remove requests lib usage * fix(budget_manager.py): remove requests lib usgae * fix(main.py): cleanup requests lib usage * fix(utils.py): remove requests lib usage * fix(argilla.py): fix argilla test * fix(athina.py): replace 'requests' lib usage with litellm module * fix(greenscale.py): replace 'requests' lib usage with httpx * fix: remove unused 'requests' lib import + replace usage in some places * fix(prompt_layer.py): remove 'requests' lib usage from prompt layer * fix(ollama_chat.py): remove 'requests' lib usage * fix(baseten.py): replace 'requests' lib usage * fix(codestral/): replace 'requests' lib usage * fix(predibase/): replace 'requests' lib usage * refactor: cleanup unused 'requests' lib imports * fix(oobabooga.py): cleanup 'requests' lib usage * fix(invoke_handler.py): remove unused 'requests' lib usage * refactor: cleanup unused 'requests' lib import * fix: fix linting errors * refactor(ollama/): move ollama to using base llm http handler removes 'requests' lib dep for ollama integration * fix(ollama_chat.py): fix linting errors * fix(ollama/completion/transformation.py): convert non-jpeg/png image to jpeg/png before passing to ollama
92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
#### What this does ####
|
|
# On success + failure, log events to Supabase
|
|
|
|
import datetime
|
|
import os
|
|
import traceback
|
|
import uuid
|
|
from typing import Any
|
|
|
|
import dotenv
|
|
|
|
import litellm
|
|
|
|
|
|
class DyanmoDBLogger:
|
|
# Class variables or attributes
|
|
|
|
def __init__(self):
|
|
# Instance variables
|
|
import boto3
|
|
|
|
self.dynamodb: Any = boto3.resource(
|
|
"dynamodb", region_name=os.environ["AWS_REGION_NAME"]
|
|
)
|
|
if litellm.dynamodb_table_name is None:
|
|
raise ValueError(
|
|
"LiteLLM Error, trying to use DynamoDB but not table name passed. Create a table and set `litellm.dynamodb_table_name=<your-table>`"
|
|
)
|
|
self.table_name = litellm.dynamodb_table_name
|
|
|
|
async def _async_log_event(
|
|
self, kwargs, response_obj, start_time, end_time, print_verbose
|
|
):
|
|
self.log_event(kwargs, response_obj, start_time, end_time, print_verbose)
|
|
|
|
def log_event(self, kwargs, response_obj, start_time, end_time, print_verbose):
|
|
try:
|
|
print_verbose(
|
|
f"DynamoDB Logging - Enters logging function for model {kwargs}"
|
|
)
|
|
|
|
# construct payload to send to DynamoDB
|
|
# follows the same params as langfuse.py
|
|
litellm_params = kwargs.get("litellm_params", {})
|
|
metadata = (
|
|
litellm_params.get("metadata", {}) or {}
|
|
) # if litellm_params['metadata'] == None
|
|
messages = kwargs.get("messages")
|
|
optional_params = kwargs.get("optional_params", {})
|
|
call_type = kwargs.get("call_type", "litellm.completion")
|
|
usage = response_obj["usage"]
|
|
id = response_obj.get("id", str(uuid.uuid4()))
|
|
|
|
# Build the initial payload
|
|
payload = {
|
|
"id": id,
|
|
"call_type": call_type,
|
|
"startTime": start_time,
|
|
"endTime": end_time,
|
|
"model": kwargs.get("model", ""),
|
|
"user": kwargs.get("user", ""),
|
|
"modelParameters": optional_params,
|
|
"messages": messages,
|
|
"response": response_obj,
|
|
"usage": usage,
|
|
"metadata": metadata,
|
|
}
|
|
|
|
# Ensure everything in the payload is converted to str
|
|
for key, value in payload.items():
|
|
try:
|
|
payload[key] = str(value)
|
|
except Exception:
|
|
# non blocking if it can't cast to a str
|
|
pass
|
|
|
|
print_verbose(f"\nDynamoDB Logger - Logging payload = {payload}")
|
|
|
|
# put data in dyanmo DB
|
|
table = self.dynamodb.Table(self.table_name)
|
|
# Assuming log_data is a dictionary with log information
|
|
response = table.put_item(Item=payload)
|
|
|
|
print_verbose(f"Response from DynamoDB:{str(response)}")
|
|
|
|
print_verbose(
|
|
f"DynamoDB Layer Logging - final response object: {response_obj}"
|
|
)
|
|
return response
|
|
except Exception:
|
|
print_verbose(f"DynamoDB Layer Error - {traceback.format_exc()}")
|
|
pass
|