Skip to content

Commit c819576

Browse files
authored
fix(openai-callback): completion count logic (#12383)
The changes introduced in #12267 and #12190 broke the cost computation of the `completion` tokens for fine-tuned models because of the early return. This PR aims at fixing this. @baskaryan.
1 parent b22da81 commit c819576

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

libs/langchain/langchain/callbacks/openai_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ def standardize_model_name(
9999
"""
100100
model_name = model_name.lower()
101101
if ".ft-" in model_name:
102-
return model_name.split(".ft-")[0] + "-azure-finetuned"
102+
model_name = model_name.split(".ft-")[0] + "-azure-finetuned"
103103
if ":ft-" in model_name:
104-
return model_name.split(":")[0] + "-finetuned-legacy"
104+
model_name = model_name.split(":")[0] + "-finetuned-legacy"
105105
if "ft:" in model_name:
106-
return model_name.split(":")[1] + "-finetuned"
107-
elif is_completion and (
106+
model_name = model_name.split(":")[1] + "-finetuned"
107+
if is_completion and (
108108
model_name.startswith("gpt-4")
109109
or model_name.startswith("gpt-3.5")
110110
or model_name.startswith("gpt-35")

libs/langchain/tests/unit_tests/callbacks/test_openai_info.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,36 @@ def test_on_llm_end_custom_model(handler: OpenAICallbackHandler) -> None:
5050

5151

5252
@pytest.mark.parametrize(
53-
"model_name",
53+
"model_name, expected_cost",
5454
[
55-
"ada:ft-your-org:custom-model-name-2022-02-15-04-21-04",
56-
"babbage:ft-your-org:custom-model-name-2022-02-15-04-21-04",
57-
"curie:ft-your-org:custom-model-name-2022-02-15-04-21-04",
58-
"davinci:ft-your-org:custom-model-name-2022-02-15-04-21-04",
59-
"ft:babbage-002:your-org:custom-model-name:1abcdefg",
60-
"ft:davinci-002:your-org:custom-model-name:1abcdefg",
61-
"ft:gpt-3.5-turbo-0613:your-org:custom-model-name:1abcdefg",
62-
"babbage-002.ft-0123456789abcdefghijklmnopqrstuv",
63-
"davinci-002.ft-0123456789abcdefghijklmnopqrstuv",
64-
"gpt-35-turbo-0613.ft-0123456789abcdefghijklmnopqrstuv",
55+
("ada:ft-your-org:custom-model-name-2022-02-15-04-21-04", 0.0032),
56+
("babbage:ft-your-org:custom-model-name-2022-02-15-04-21-04", 0.0048),
57+
("curie:ft-your-org:custom-model-name-2022-02-15-04-21-04", 0.024),
58+
("davinci:ft-your-org:custom-model-name-2022-02-15-04-21-04", 0.24),
59+
("ft:babbage-002:your-org:custom-model-name:1abcdefg", 0.0032),
60+
("ft:davinci-002:your-org:custom-model-name:1abcdefg", 0.024),
61+
("ft:gpt-3.5-turbo-0613:your-org:custom-model-name:1abcdefg", 0.028),
62+
("babbage-002.ft-0123456789abcdefghijklmnopqrstuv", 0.0008),
63+
("davinci-002.ft-0123456789abcdefghijklmnopqrstuv", 0.004),
64+
("gpt-35-turbo-0613.ft-0123456789abcdefghijklmnopqrstuv", 0.0035),
6565
],
6666
)
6767
def test_on_llm_end_finetuned_model(
68-
handler: OpenAICallbackHandler, model_name: str
68+
handler: OpenAICallbackHandler, model_name: str, expected_cost: float
6969
) -> None:
7070
response = LLMResult(
7171
generations=[],
7272
llm_output={
7373
"token_usage": {
74-
"prompt_tokens": 2,
75-
"completion_tokens": 1,
76-
"total_tokens": 3,
74+
"prompt_tokens": 1000,
75+
"completion_tokens": 1000,
76+
"total_tokens": 2000,
7777
},
7878
"model_name": model_name,
7979
},
8080
)
8181
handler.on_llm_end(response)
82-
assert handler.total_cost > 0
82+
assert handler.total_cost == expected_cost
8383

8484

8585
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)