Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openai-callback): add Azure OpenAI fine-tuned models #694

Merged
merged 1 commit into from
Oct 27, 2023

Conversation

mspronesti
Copy link
Contributor

@mspronesti mspronesti commented Oct 27, 2023

Completion of #682, plus some tests covering the cost computation for both OpenAI and Azure OpenAI - including fine-tuned models.

Summary by CodeRabbit

  • New Feature: Introduced a function to standardize model names, enhancing the consistency of interactions with the OpenAI API.
  • Improvement: Updated the cost calculation and checking process to use the new standardized model names, improving the accuracy of cost estimations.
  • Test: Added comprehensive test cases for the OpenAI Callback Handler, increasing the reliability of the system by ensuring it handles various model names and calculates costs correctly.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 27, 2023

Walkthrough

The changes introduce a new function standardize_model_name() in the openai_info.py file to standardize model names for OpenAI API usage. This function is now used in calculating costs and checking costs. The test_openai_info.py file has been updated with new test cases to enhance the test coverage for different model names and their costs.

Changes

File Path Summary
.../helpers/openai_info.py Introduced standardize_model_name() function to standardize model names. This function is now used in get_openai_token_cost_for_model() and __call__() methods. Two new model names have been added to the MODEL_COST_PER_1K_TOKENS dictionary.
.../helpers/test_openai_info.py Added new test cases to OpenAICallbackHandler class. Introduced test_handler_azure_openai and test_handler_finetuned_model methods to test the behavior of the handler with different model names and verify the total_cost attribute.

🐇💻

In the land of code, where the functions play,

A rabbit hopped in, with changes to convey.

Standardizing names, with a function so neat,

Making the code cleaner, isn't that sweet?

Tests are now richer, covering more ground,

In the land of code, improvements abound! 🎉


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

@mspronesti mspronesti force-pushed the feat/azure-finetune-cost branch from 5d8ae62 to 72695fa Compare October 27, 2023 23:21
@codecov-commenter
Copy link

codecov-commenter commented Oct 27, 2023

Codecov Report

Merging #694 (72695fa) into main (139e80a) will increase coverage by 0.35%.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##             main     #694      +/-   ##
==========================================
+ Coverage   84.80%   85.16%   +0.35%     
==========================================
  Files          70       70              
  Lines        3547     3552       +5     
==========================================
+ Hits         3008     3025      +17     
+ Misses        539      527      -12     
Files Coverage Δ
pandasai/helpers/openai_info.py 91.48% <100.00%> (+12.91%) ⬆️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 139e80a and 72695fa.
Files selected for processing (2)
  • pandasai/helpers/openai_info.py (3 hunks)
  • tests/helpers/test_openai_info.py (1 hunks)
Additional comments: 5
tests/helpers/test_openai_info.py (2)
  • 60-81: The new test cases for the test_handler_openai function are well-structured and cover a variety of model names, including variations with different token counts and Azure OpenAI models. This will help ensure that the OpenAICallbackHandler correctly calculates the cost for these models.

  • 136-142: The test_handler_finetuned_model function tests the handling of fine-tuned model names, which is a good addition to the test coverage. However, the expected cost for the first model name seems to be significantly higher than for the other models. Please verify if this is correct.

pandasai/helpers/openai_info.py (3)
  • 42-52: The addition of new model names to the MODEL_COST_PER_1K_TOKENS dictionary is a good step towards supporting more models. However, ensure that the cost values are accurate and up-to-date.

  • 106-106: The use of the standardize_model_name() function in get_openai_token_cost_for_model() is a good practice as it ensures that the model name is standardized before it is used to calculate the cost. This improves code consistency and readability.

  • 137-137: The use of the standardize_model_name() function in the __call__() method of the OpenAICallbackHandler class is a good practice as it ensures that the model name is standardized before it is used to calculate the cost. This improves code consistency and readability.

Comment on lines +98 to +119
@pytest.mark.parametrize(
"model_name,expected_cost",
[
("gpt-35-turbo", 0.0035),
(
"gpt-35-turbo-0613",
0.0035,
),
(
"gpt-35-turbo-16k-0613",
0.007,
),
(
"gpt-35-turbo-16k",
0.007,
),
("gpt-4", 0.09),
("gpt-4-0613", 0.09),
("gpt-4-32k", 0.18),
("gpt-4-32k-0613", 0.18),
],
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test_handler_azure_openai function is almost identical to the test_handler_openai function, and they test the same logic with slightly different model names. To avoid code duplication, consider combining these two functions into one and extending the parameterized inputs to include all the model names.

Comment on lines +55 to +87
def standardize_model_name(
model_name: str,
is_completion: bool = False,
) -> str:
"""
Standardize the model name to a format that can be used in the OpenAI API.

Args:
model_name: Model name to standardize.
is_completion: Whether the model is used for completion or not.
Defaults to False.

Returns:
Standardized model name.

"""
model_name = model_name.lower()
if ".ft-" in model_name:
model_name = model_name.split(".ft-")[0] + "-azure-finetuned"
if "ft:" in model_name:
model_name = model_name.split(":")[1] + "-finetuned"
if is_completion and (
model_name.startswith("gpt-4")
or model_name.startswith("gpt-3.5")
or model_name.startswith("gpt-35")
or "finetuned" in model_name
):
# The cost of completion token is different from
# the cost of prompt tokens.
return model_name + "-completion"
else:
return model_name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standardize_model_name() function is a good addition to the codebase. It improves code consistency and readability by standardizing the model name for use in the OpenAI API. However, the function could be simplified by using a dictionary to map the model prefixes to their standardized names, which would make the code more maintainable and easier to extend in the future.

@gventuri
Copy link
Collaborator

gventuri commented Oct 27, 2023

@mspronesti thanks a lot, great to also support Azure finetuned models! Merging :)

@gventuri gventuri merged commit 30a8cca into Sinaptik-AI:main Oct 27, 2023
9 checks passed
@mspronesti
Copy link
Contributor Author

mspronesti commented Oct 27, 2023

@gventuri Thanks for merging! Just be careful that you are no longer squashing the commits before merging them (check the commit history). If this was not intentional, I suppose you might want to rebase, otherwise just ignore this comment :)

@gventuri
Copy link
Collaborator

@mspronesti thanks a lot for reporting. That was actually unintentional, thanks for mentioning :)

@mspronesti
Copy link
Contributor Author

mspronesti commented Oct 28, 2023

I think it was also the case for the previous one or two PRs (which are still in the commit history), but I suppose it's no big deal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants