4141
4242load_dotenv ()
4343
44+
4445def calculate_cost (model : str , usage : Dict [str , int ]) -> float :
4546 if model not in pricing :
4647 # raise ValueError(f"Unknown model: {model}")
@@ -240,6 +241,7 @@ def _handle_non_streaming(self, *args, **kwargs):
240241 cost = calculate_cost (response .model , formatted_response ["usage" ])
241242 formatted_response ["cost" ] = cost
242243
244+ # TODO: models, latency, cost shouldn't be logged as inputs. Should have separate fields for each.
243245 self ._capture_generation (inputs = kwargs , outputs = formatted_response )
244246 return response
245247
@@ -275,11 +277,11 @@ def parse(self, *args, **kwargs):
275277 # context manager to suppress duplicate logging
276278 with LoggingSuppressor (self ._worker ):
277279 response = self .original .parse (* args , ** kwargs )
278-
280+
279281 end_time = time .time ()
280282 latency = end_time - start_time
281283 latency = round (latency , 6 )
282-
284+
283285 formatted_response = {
284286 "id" : response .id ,
285287 "choices" : [choice .model_dump () for choice in response .choices ],
@@ -291,11 +293,12 @@ def parse(self, *args, **kwargs):
291293 "system_fingerprint" : response .system_fingerprint ,
292294 "latency" : latency ,
293295 }
294-
296+
295297 # Calculate and add cost
296298 cost = calculate_cost (response .model , formatted_response ["usage" ])
297299 formatted_response ["cost" ] = cost
298-
300+
301+ # TODO: models, latency, cost shouldn't be logged as inputs. Should have separate fields for each.
299302 self ._worker .capture_generation (
300303 observation_id = str (uuid4 ()),
301304 created_at = datetime .now (timezone .utc ),
@@ -304,7 +307,7 @@ def parse(self, *args, **kwargs):
304307 inputs = kwargs ,
305308 outputs = formatted_response ,
306309 )
307-
310+
308311 return response
309312
310313 self .chat .completions = CustomChatCompletions (
@@ -442,7 +445,7 @@ async def _handle_streaming(self, *args, **kwargs):
442445 usage = accumulated_response ["usage" ]
443446 cost = calculate_cost (model , usage )
444447 accumulated_response ["cost" ] = cost
445-
448+
446449 if self ._worker .capture_generation is not None :
447450 await self ._capture_generation (
448451 inputs = kwargs , outputs = accumulated_response
@@ -478,7 +481,7 @@ async def _handle_non_streaming(self, *args, **kwargs):
478481 inputs = kwargs , outputs = formatted_response
479482 )
480483 return response
481-
484+
482485 async def _capture_generation (
483486 self ,
484487 inputs ,
@@ -507,14 +510,14 @@ def __init__(self, original, weavel_api_key: str, base_url: Optional[str]):
507510 async def parse (self , * args , ** kwargs ):
508511 header = kwargs .pop ("headers" , {})
509512 start_time = time .time ()
510-
513+
511514 async with AsyncLoggingSuppressor (self ._worker ):
512515 response = await self .original .parse (* args , ** kwargs )
513-
516+
514517 end_time = time .time ()
515518 latency = end_time - start_time
516519 latency = round (latency , 6 )
517-
520+
518521 formatted_response = {
519522 "id" : response .id ,
520523 "choices" : [choice .model_dump () for choice in response .choices ],
@@ -526,7 +529,7 @@ async def parse(self, *args, **kwargs):
526529 "system_fingerprint" : response .system_fingerprint ,
527530 "latency" : latency ,
528531 }
529-
532+
530533 # Calculate and add cost
531534 cost = calculate_cost (response .model , formatted_response ["usage" ])
532535 formatted_response ["cost" ] = cost
0 commit comments