From 0119ca1ce1679590ce7b02d45b176650e3f18be4 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 17 Jul 2024 10:04:36 -0700 Subject: [PATCH] fix langsmith - don't log api_key --- litellm/integrations/langsmith.py | 35 +++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/litellm/integrations/langsmith.py b/litellm/integrations/langsmith.py index 48185afee..817c0357e 100644 --- a/litellm/integrations/langsmith.py +++ b/litellm/integrations/langsmith.py @@ -1,14 +1,33 @@ #### What this does #### # On success, logs events to Langsmith -import dotenv, os # type: ignore -import requests # type: ignore -from datetime import datetime -import traceback import asyncio +import os +import traceback 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 +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): non_serializable_types = ( types.CoroutineType, @@ -52,10 +71,11 @@ class LangsmithLogger: print_verbose( f"Langsmith Logging - Enters logging function for model {kwargs}" ) - import requests import datetime from datetime import timezone + import requests + try: start_time = kwargs["start_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() # 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 = {} for key in kwargs: value = kwargs[key] @@ -103,7 +126,7 @@ class LangsmithLogger: else: print_verbose("Run successfully created") 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: print_verbose(f"Langsmith Layer Error - {traceback.format_exc()}")