@@ -48,6 +48,12 @@ def __init__(
4848 )
4949
5050 def _extract_data (self , kwargs , response_obj , start_time , end_time ):
51+ api_key = kwargs .get ("api_key" )
52+ if api_key and len (api_key ) > 8 :
53+ api_key = api_key [:4 ] + "****" + api_key [- 4 :]
54+ else :
55+ api_key = "****"
56+
5157 data = {
5258 "model" : kwargs .get ("model" ),
5359 "messages" : kwargs .get ("messages" ),
@@ -58,77 +64,80 @@ def _extract_data(self, kwargs, response_obj, start_time, end_time):
5864 "stream" : kwargs .get ("stream" , False ),
5965 "call_type" : kwargs .get ("call_type" , "completion" ),
6066 "provider" : kwargs .get ("custom_llm_provider" ),
61- "api_key" : kwargs .get ("api_key" ),
6267 "log_event_type" : kwargs .get ("log_event_type" ),
6368 "llm_api_duration_ms" : kwargs .get ("llm_api_duration_ms" ),
6469 "response_headers" : kwargs .get ("response_headers" , {}),
6570 "cache_hit" : kwargs .get ("cache_hit" , False ),
66- "standard_logging_object_id" : kwargs .get ("standard_logging_object" , {}).get ("id" ),
67- "standard_logging_object_status" : kwargs .get ("standard_logging_object" , {}).get ("status" ),
68- "standard_logging_object_response_time" : kwargs .get ("standard_logging_object" , {}).get
69- ("response_time" ),
70- "standard_logging_object_saved_cache_cost" : kwargs .get ("standard_logging_object" , {}).get ("saved_cache_cost" ),
71- "standard_logging_object_hidden_params" : kwargs .get ("standard_logging_object" , {}).get ("status" ),
71+ "standard_logging_object_id" : kwargs .get ("standard_logging_object" , {}).get (
72+ "id"
73+ ),
74+ "standard_logging_object_status" : kwargs .get (
75+ "standard_logging_object" , {}
76+ ).get ("status" ),
77+ "standard_logging_object_response_time" : kwargs .get (
78+ "standard_logging_object" , {}
79+ ).get ("response_time" ),
80+ "standard_logging_object_saved_cache_cost" : kwargs .get (
81+ "standard_logging_object" , {}
82+ ).get ("saved_cache_cost" ),
83+ "standard_logging_object_hidden_params" : kwargs .get (
84+ "standard_logging_object" , {}
85+ ).get ("status" ),
86+ "api_key" : api_key ,
7287 }
73-
88+
7489 # response = litellm.completion(model="gpt-3.5-turbo", messages=messages, metadata={"hello": "world"})
7590 litellm_params = kwargs .get ("litellm_params" , {})
7691 data ["proxy_metadata" ] = litellm_params .get ("metadata" , {})
7792 data ["response" ] = response_obj .json ()
78- data ["cost" ] = litellm .completion_cost (completion_response = response_obj )
93+ data ["cost" ] = litellm .completion_cost (
94+ completion_response = response_obj ,
95+ custom_llm_provider = kwargs .get ("custom_llm_provider" ),
96+ )
7997
8098 data ["exception" ] = kwargs .get ("exception" , None )
8199 data ["traceback" ] = kwargs .get ("traceback_exception" , None )
82100 if isinstance (start_time , datetime ) and isinstance (end_time , datetime ):
83101 duration = (end_time - start_time ).total_seconds ()
84102 else :
85- duration = ( end_time - start_time )
103+ duration = end_time - start_time
86104 data ["duration" ] = duration
87105 return safe_json_dumps (data )
88-
106+
107+
89108class TinybirdLitellmSyncHandler (TinybirdLitellmHandler ):
90109 def __init__ (self , * args , ** kwargs ):
91110 super ().__init__ (* args , ** kwargs )
92111
93112 def log_success_event (self , kwargs , response_obj , start_time , end_time ):
94113 try :
95114 data = self ._extract_data (kwargs , response_obj , start_time , end_time )
96- self .api .send (
97- f"events?name={ self .datasource_name } " ,
98- data = data
99- )
115+ self .api .send (f"events?name={ self .datasource_name } " , data = data )
100116 except Exception as e :
101117 logging .error (f"Error logging success event: { e } " )
118+
102119 def log_failure_event (self , kwargs , response_obj , start_time , end_time ):
103120 try :
104121 data = self ._extract_data (kwargs , response_obj , start_time , end_time )
105- self .api .send (
106- f"events?name={ self .datasource_name } " ,
107- data = data
108- )
122+ self .api .send (f"events?name={ self .datasource_name } " , data = data )
109123 except Exception as e :
110124 logging .error (f"Error logging failure event: { e } " )
111125
126+
112127class TinybirdLitellmAsyncHandler (TinybirdLitellmHandler ):
113128 def __init__ (self , * args , ** kwargs ):
114129 super ().__init__ (* args , ** kwargs )
115130
116131 async def async_log_success_event (self , kwargs , response_obj , start_time , end_time ):
117132 try :
118133 data = self ._extract_data (kwargs , response_obj , start_time , end_time )
119- await self .async_api .send (
120- f"events?name={ self .datasource_name } " ,
121- data = data
122- )
134+ await self .async_api .send (f"events?name={ self .datasource_name } " , data = data )
123135 except Exception as e :
124136 logging .error (f"Error logging success event: { e } " )
125137
126138 async def async_log_failure_event (self , kwargs , response_obj , start_time , end_time ):
127139 try :
128140 data = self ._extract_data (kwargs , response_obj , start_time , end_time )
129- await self .async_api .send (
130- f"events?name={ self .datasource_name } " ,
131- data = data
132- )
141+ await self .async_api .send (f"events?name={ self .datasource_name } " , data = data )
133142 except Exception as e :
134143 logging .error (f"Error logging failure event: { e } " )
0 commit comments