fix langsmith - don't log api_key

This commit is contained in:
Ishaan Jaff 2024-07-17 10:04:36 -07:00
parent ee33a80486
commit 0119ca1ce1

View file

@ -1,14 +1,33 @@
#### What this does #### #### What this does ####
# On success, logs events to Langsmith # On success, logs events to Langsmith
import dotenv, os # type: ignore
import requests # type: ignore
from datetime import datetime
import traceback
import asyncio import asyncio
import os
import traceback
import types import types
from datetime import datetime
from typing import Any, List, Optional
import dotenv # type: ignore
import requests # type: ignore
from pydantic import BaseModel # type: ignore from pydantic import BaseModel # type: ignore
class LangsmithInputs(BaseModel):
model: Optional[str] = None
messages: Optional[List[Any]] = None
stream: Optional[bool] = None
call_type: Optional[str] = None
litellm_call_id: Optional[str] = None
completion_start_time: Optional[datetime] = None
temperature: Optional[float] = None
max_tokens: Optional[int] = None
custom_llm_provider: Optional[str] = None
input: Optional[List[Any]] = None
log_event_type: Optional[str] = None
original_response: Optional[str] = None
response_cost: Optional[float] = None
def is_serializable(value): def is_serializable(value):
non_serializable_types = ( non_serializable_types = (
types.CoroutineType, types.CoroutineType,
@ -52,10 +71,11 @@ class LangsmithLogger:
print_verbose( print_verbose(
f"Langsmith Logging - Enters logging function for model {kwargs}" f"Langsmith Logging - Enters logging function for model {kwargs}"
) )
import requests
import datetime import datetime
from datetime import timezone from datetime import timezone
import requests
try: try:
start_time = kwargs["start_time"].astimezone(timezone.utc).isoformat() start_time = kwargs["start_time"].astimezone(timezone.utc).isoformat()
end_time = kwargs["end_time"].astimezone(timezone.utc).isoformat() end_time = kwargs["end_time"].astimezone(timezone.utc).isoformat()
@ -64,6 +84,9 @@ class LangsmithLogger:
end_time = datetime.datetime.utcnow().isoformat() end_time = datetime.datetime.utcnow().isoformat()
# filter out kwargs to not include any dicts, langsmith throws an erros when trying to log kwargs # filter out kwargs to not include any dicts, langsmith throws an erros when trying to log kwargs
logged_kwargs = LangsmithInputs(**kwargs)
kwargs = logged_kwargs.model_dump()
new_kwargs = {} new_kwargs = {}
for key in kwargs: for key in kwargs:
value = kwargs[key] value = kwargs[key]
@ -103,7 +126,7 @@ class LangsmithLogger:
else: else:
print_verbose("Run successfully created") print_verbose("Run successfully created")
print_verbose( print_verbose(
f"Langsmith Layer Logging - final response object: {response_obj}" f"Langsmith Layer Logging - final response object: {response_obj}. Response text from langsmith={response.text}"
) )
except: except:
print_verbose(f"Langsmith Layer Error - {traceback.format_exc()}") print_verbose(f"Langsmith Layer Error - {traceback.format_exc()}")