From efe0d39c6a67e65517c1ad9aecb5c6e3f4736fa9 Mon Sep 17 00:00:00 2001 From: Neil Neuwirth Date: Tue, 27 Jun 2023 19:53:06 -0400 Subject: [PATCH] Adjusted OpenAI cost calculation (#6798) Added parentheses to ensure the division operation is performed before multiplication. This now correctly calculates the cost by dividing the number of tokens by 1000 first (to get the cost per token), and then multiplies it with the model's cost per 1k tokens @agola11 --- langchain/callbacks/openai_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain/callbacks/openai_info.py b/langchain/callbacks/openai_info.py index 549720e7c4d..37c0ee10da9 100644 --- a/langchain/callbacks/openai_info.py +++ b/langchain/callbacks/openai_info.py @@ -96,7 +96,7 @@ def get_openai_token_cost_for_model( f"Unknown model: {model_name}. Please provide a valid OpenAI model name." "Known models are: " + ", ".join(MODEL_COST_PER_1K_TOKENS.keys()) ) - return MODEL_COST_PER_1K_TOKENS[model_name] * num_tokens / 1000 + return MODEL_COST_PER_1K_TOKENS[model_name] * (num_tokens / 1000) class OpenAICallbackHandler(BaseCallbackHandler):