fix timestamps and user

This commit is contained in:
Vince Loewe 2024-02-27 22:30:32 -08:00
parent a9648613dc
commit 4e74582b6f
3 changed files with 34 additions and 23 deletions

View file

@ -1,15 +1,12 @@
#### What this does #### #### What this does ####
# On success + failure, log events to aispend.io # On success + failure, log events to lunary.ai
import datetime from datetime import datetime, timezone
import traceback import traceback
import dotenv import dotenv
import os import subprocess
import requests import sys
dotenv.load_dotenv() # Loading env variables using dotenv dotenv.load_dotenv()
import traceback
import datetime, subprocess, sys
import litellm
# convert to {completion: xx, tokens: xx} # convert to {completion: xx, tokens: xx}
def parse_usage(usage): def parse_usage(usage):
@ -86,8 +83,8 @@ class LunaryLogger:
input=None, input=None,
user_id=None, user_id=None,
response_obj=None, response_obj=None,
start_time=datetime.datetime.now(), start_time=datetime.now(timezone.utc),
end_time=datetime.datetime.now(), end_time=datetime.now(timezone.utc),
error=None, error=None,
): ):
# Method definition # Method definition
@ -102,23 +99,38 @@ class LunaryLogger:
tags = litellm_params.pop("tags", None) or [] tags = litellm_params.pop("tags", None) or []
template_id = extra.pop("templateId", None), if extra:
template_id = extra.pop("templateId", None)
extra.pop("extra_body", None)
extra.pop("user", None)
# keep only serializable types
for param, value in extra.items(): for param, value in extra.items():
if not isinstance(value, (str, int, bool, float)): if not isinstance(value, (str, int, bool, float)):
try: try:
extra[param] = str(value) extra[param] = str(value)
except: except:
# if casting value to str fails don't block logging
pass pass
for param, value in metadata.items():
if not isinstance(value, (str, int, bool, float)):
try:
extra[param] = str(value)
except:
pass
if response_obj: if response_obj:
usage = ( usage = (
parse_usage(response_obj["usage"]) parse_usage(response_obj["usage"])
if "usage" in response_obj if "usage" in response_obj
else None else None
) )
output = response_obj["choices"] if "choices" in response_obj else None output = response_obj["choices"] if "choices" in response_obj else None
print(output)
else: else:
usage = None usage = None
output = None output = None
@ -128,7 +140,7 @@ class LunaryLogger:
else: else:
error_obj = None error_obj = None
print(start_time.isoformat())
self.lunary_client.track_event( self.lunary_client.track_event(
type, type,
@ -137,8 +149,8 @@ class LunaryLogger:
user_id=user_id, user_id=user_id,
name=model, name=model,
input=parse_messages(input), input=parse_messages(input),
timestamp=start_time.isoformat(), timestamp=start_time.astimezone(timezone.utc).isoformat(),
# template_id=template_id, template_id=template_id,
metadata=metadata, metadata=metadata,
runtime="litellm", runtime="litellm",
tags=tags, tags=tags,
@ -150,14 +162,11 @@ class LunaryLogger:
type, type,
event, event,
run_id, run_id,
timestamp=end_time.isoformat(), timestamp=end_time.astimezone(timezone.utc).isoformat(),
runtime="litellm", runtime="litellm",
error=error_obj, error=error_obj,
output=parse_messages(output), output=parse_messages(output),
token_usage={ token_usage=usage
"prompt": usage.get("prompt_tokens"),
"completion": usage.get("completion_tokens"),
}
) )

View file

@ -9,7 +9,7 @@ import litellm
litellm.success_callback = ["lunary"] litellm.success_callback = ["lunary"]
litellm.set_verbose = True litellm.set_verbose = True
import time
def test_lunary_logging(): def test_lunary_logging():
@ -19,6 +19,7 @@ def test_lunary_logging():
messages=[{"role": "user", "content": "what llm are u"}], messages=[{"role": "user", "content": "what llm are u"}],
max_tokens=10, max_tokens=10,
temperature=0.2, temperature=0.2,
user="test-user",
) )
print(response) print(response)
except Exception as e: except Exception as e:
@ -67,4 +68,4 @@ def test_lunary_logging_with_streaming_and_metadata():
print(e) print(e)
# test_lunary_logging_with_streaming_and_metadata() test_lunary_logging_with_streaming_and_metadata()

View file

@ -1347,7 +1347,8 @@ class Logging:
event="end", event="end",
model=model, model=model,
input=input, input=input,
user_id=self.model_call_details.get("user", "default"), user_id=self.model_call_details.get("user_id", self.model_call_details.get("user", None)),
#user_props=self.model_call_details.get("user_props", None),
extra=self.model_call_details.get("optional_params", {}), extra=self.model_call_details.get("optional_params", {}),
response_obj=result, response_obj=result,
start_time=start_time, start_time=start_time,