Skip to content

Commit

Permalink
Refactor calculate_cost function to include different cost calculatio…
Browse files Browse the repository at this point in the history
…ns based on model name (#17)

Co-authored-by: wangyuxin <[email protected]>
  • Loading branch information
wangyuxinwhy and wangyuxin authored Jan 23, 2024
1 parent 8b2ee93 commit 8e4c6c2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions generate/chat_completion/models/minimax_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def process(self, response: ResponseValue) -> ChatCompletionStreamOutput:
model_info=self.model_info,
message=self.message,
finish_reason=response['choices'][0]['finish_reason'],
cost=calculate_cost(response['usage']),
cost=calculate_cost(model_name=self.model_info.name , usage=response['usage']),
extra={
'input_sensitive': response['input_sensitive'],
'output_sensitive': response['output_sensitive'],
Expand Down Expand Up @@ -244,8 +244,16 @@ def update_existing_message(self, response: ResponseValue) -> str:
return delta


def calculate_cost(usage: dict[str, int], num_web_search: int = 0) -> float:
return 0.015 * (usage['total_tokens'] / 1000) + (0.03 * num_web_search)
def calculate_cost(model_name: str, usage: dict[str, int], num_web_search: int = 0) -> float:
if model_name == 'abab6-chat':
model_cost = 0.1 * (usage['total_tokens'] / 1000)
elif model_name == 'abab5.5-chat':
model_cost = 0.015 * (usage['total_tokens'] / 1000)
elif model_name == 'abab5.5s-chat':
model_cost = 0.005 * (usage['total_tokens'] / 1000)
else:
return 0.0
return model_cost + (0.03 * num_web_search)


class MinimaxProChat(ChatCompletionModel):
Expand Down Expand Up @@ -316,7 +324,7 @@ def _parse_reponse(self, response: ResponseValue) -> ChatCompletionOutput:
model_info=self.model_info,
message=message,
finish_reason=finish_reason,
cost=calculate_cost(response['usage'], num_web_search),
cost=calculate_cost(model_name=self.name, usage=response['usage'], num_web_search=num_web_search),
extra={
'input_sensitive': response['input_sensitive'],
'output_sensitive': response['output_sensitive'],
Expand Down

0 comments on commit 8e4c6c2

Please sign in to comment.