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(llm): add support for fine-tuned OpenAI models #682

Merged
merged 1 commit into from
Oct 24, 2023

Conversation

mspronesti
Copy link
Contributor

@mspronesti mspronesti commented Oct 24, 2023

Hi @gventuri,
this PR aims to support OpenAI's newest fine-tuned models. Please notice that I haven't added any dual porting for Azure OpenAI yet as I don't have any access to those (yet).

Summary by CodeRabbit

  • New Feature: Added support for fine-tuned models in our AI services. This update allows the system to correctly identify and handle fine-tuned models, ensuring the appropriate methods are called based on the model name.
  • Improvement: Updated the cost calculation for fine-tuned models, providing more accurate cost estimations.
  • Test: Introduced a new test case to verify the correct handling and response of fine-tuned models, enhancing the reliability of our system.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2023

Walkthrough

The changes primarily focus on enhancing the handling of fine-tuned models in the OpenAI API. They introduce a mechanism to extract the "standard" model name from a fine-tuned model, ensuring the correct method is called based on the model name. Additionally, a new test case is added to verify this functionality.

Changes

File Path Summary
.../helpers/openai_info.py Introduced two new entries in a dictionary for fine-tuned models and modified the get_openai_token_cost_for_model function to handle fine-tuned models.
.../llm/openai.py Updated the call method to extract the "standard" model name from a fine-tuned model and determine the appropriate method to call.
tests/llms/test_openai.py Added a new test case test_call_finetuned_model to verify the handling of fine-tuned models.

🐇💻

In the land of code, where the brackets lie,

Changes were made, under the AI's eye.

Fine-tuned models, now handled with care,

Ensuring the right methods, are called fair and square.

Tests were written, to keep bugs at bay,

Celebrating the changes, the CodeRabbit way! 🎉


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.

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

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 0d56e93 and b538cf1.
Files selected for processing (3)
  • pandasai/helpers/openai_info.py (2 hunks)
  • pandasai/llm/openai.py (1 hunks)
  • tests/llms/test_openai.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • pandasai/helpers/openai_info.py

Comment on lines +105 to +109
if "ft:" in self.model:
# extract "standard" model name from fine-tuned model
model_name = self.model.split(":")[1]
else:
model_name = self.model
Copy link
Contributor

Choose a reason for hiding this comment

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

The code checks if the model name contains "ft:" and extracts the base model name accordingly. This is a good approach to handle fine-tuned models. However, it assumes that the model name after "ft:" is always valid. It would be better to add a check to ensure that the extracted model name is not empty or invalid. This will prevent potential errors when calling the OpenAI API.

        if "ft:" in self.model:
            # extract "standard" model name from fine-tuned model
            model_name = self.model.split(":")[1]
+            if not model_name:
+                raise ValueError("Invalid fine-tuned model name.")
        else:
            model_name = self.model
Committable suggestion (Beta)
Suggested change
if "ft:" in self.model:
# extract "standard" model name from fine-tuned model
model_name = self.model.split(":")[1]
else:
model_name = self.model
if "ft:" in self.model:
# extract "standard" model name from fine-tuned model
model_name = self.model.split(":")[1]
if not model_name:
raise ValueError("Invalid fine-tuned model name.")
else:
model_name = self.model

Comment on lines +134 to +139
def test_call_finetuned_model(self, mocker, prompt):
openai = OpenAI(api_token="test", model="ft:gpt-3.5-turbo:my-org:custom_suffix:id")
mocker.patch.object(openai, "chat_completion", return_value="response")

result = openai.call(instruction=prompt)
assert result == "response"
Copy link
Contributor

Choose a reason for hiding this comment

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

The new test case test_call_finetuned_model is testing the call method with a fine-tuned model. It's good to see that the test is mocking the chat_completion method and asserting the expected response. However, it would be beneficial to also verify that the chat_completion method is called with the correct arguments, especially the model name. This will ensure that the call method is correctly extracting the "standard" model name from the fine-tuned model and passing it to the chat_completion method.

-        mocker.patch.object(openai, "chat_completion", return_value="response")
+        chat_completion_mock = mocker.patch.object(openai, "chat_completion", return_value="response")
-        assert result == "response"
+        assert result == "response"
+        chat_completion_mock.assert_called_once_with(instruction=prompt, model="gpt-3.5-turbo")
Committable suggestion (Beta)
Suggested change
def test_call_finetuned_model(self, mocker, prompt):
openai = OpenAI(api_token="test", model="ft:gpt-3.5-turbo:my-org:custom_suffix:id")
mocker.patch.object(openai, "chat_completion", return_value="response")
result = openai.call(instruction=prompt)
assert result == "response"
def test_call_finetuned_model(self, mocker, prompt):
openai = OpenAI(api_token="test", model="ft:gpt-3.5-turbo:my-org:custom_suffix:id")
chat_completion_mock = mocker.patch.object(openai, "chat_completion", return_value="response")
result = openai.call(instruction=prompt)
assert result == "response"
chat_completion_mock.assert_called_once_with(instruction=prompt, model="gpt-3.5-turbo")

@codecov-commenter
Copy link

Codecov Report

Merging #682 (b538cf1) into main (0d56e93) will decrease coverage by 0.01%.
The diff coverage is 85.71%.

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

@@            Coverage Diff             @@
##             main     #682      +/-   ##
==========================================
- Coverage   83.84%   83.84%   -0.01%     
==========================================
  Files          67       67              
  Lines        3201     3206       +5     
==========================================
+ Hits         2684     2688       +4     
- Misses        517      518       +1     
Files Coverage Δ
pandasai/llm/openai.py 100.00% <100.00%> (ø)
pandasai/helpers/openai_info.py 78.57% <50.00%> (-1.43%) ⬇️

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

@mspronesti mspronesti changed the title feat: add support for finetuned OpenAI models feat(llm): add support for finet-uned OpenAI models Oct 24, 2023
@mspronesti mspronesti changed the title feat(llm): add support for finet-uned OpenAI models feat(llm): add support for fine-tuned OpenAI models Oct 24, 2023
@gventuri
Copy link
Collaborator

@mspronesti thanks a lot for the addition, can't wait to play around with it! Merging

@gventuri gventuri merged commit fe45cca into Sinaptik-AI:main Oct 24, 2023
9 checks passed
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