@@ -126,9 +126,11 @@ def message_retrieval(
126
126
127
127
if TOOL_ENABLED :
128
128
return [ # type: ignore [return-value]
129
- choice .message # type: ignore [union-attr]
130
- if choice .message .function_call is not None or choice .message .tool_calls is not None # type: ignore [union-attr]
131
- else choice .message .content # type: ignore [union-attr]
129
+ (
130
+ choice .message # type: ignore [union-attr]
131
+ if choice .message .function_call is not None or choice .message .tool_calls is not None # type: ignore [union-attr]
132
+ else choice .message .content
133
+ ) # type: ignore [union-attr]
132
134
for choice in choices
133
135
]
134
136
else :
@@ -276,8 +278,8 @@ def cost(self, response: Union[ChatCompletion, Completion]) -> float:
276
278
logger .debug (f"Model { model } is not found. The cost will be 0." , exc_info = True )
277
279
return 0
278
280
279
- n_input_tokens = response .usage .prompt_tokens # type: ignore [union-attr]
280
- n_output_tokens = response .usage .completion_tokens # type: ignore [union-attr]
281
+ n_input_tokens = response .usage .prompt_tokens if response . usage is not None else 0 # type: ignore [union-attr]
282
+ n_output_tokens = response .usage .completion_tokens if response . usage is not None else 0 # type: ignore [union-attr]
281
283
tmp_price1K = OAI_PRICE1K [model ]
282
284
# First value is input token rate, second value is output token rate
283
285
if isinstance (tmp_price1K , tuple ):
@@ -287,10 +289,10 @@ def cost(self, response: Union[ChatCompletion, Completion]) -> float:
287
289
@staticmethod
288
290
def get_usage (response : Union [ChatCompletion , Completion ]) -> Dict :
289
291
return {
290
- "prompt_tokens" : response .usage .prompt_tokens ,
291
- "completion_tokens" : response .usage .completion_tokens ,
292
- "total_tokens" : response .usage .total_tokens ,
293
- "cost" : response .cost ,
292
+ "prompt_tokens" : response .usage .prompt_tokens if response . usage is not None else 0 ,
293
+ "completion_tokens" : response .usage .completion_tokens if response . usage is not None else 0 ,
294
+ "total_tokens" : response .usage .total_tokens if response . usage is not None else 0 ,
295
+ "cost" : response .cost if hasattr ( response , "cost" ) else 0 ,
294
296
"model" : response .model ,
295
297
}
296
298
@@ -471,12 +473,14 @@ def _construct_create_params(self, create_config: Dict[str, Any], extra_kwargs:
471
473
elif context :
472
474
# Instantiate the messages
473
475
params ["messages" ] = [
474
- {
475
- ** m ,
476
- "content" : self .instantiate (m ["content" ], context , allow_format_str_template ),
477
- }
478
- if m .get ("content" )
479
- else m
476
+ (
477
+ {
478
+ ** m ,
479
+ "content" : self .instantiate (m ["content" ], context , allow_format_str_template ),
480
+ }
481
+ if m .get ("content" )
482
+ else m
483
+ )
480
484
for m in messages # type: ignore [union-attr]
481
485
]
482
486
return params
0 commit comments