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

chore: update docs and examples for Azure OpenAI v1 #761

Merged
merged 2 commits into from
Nov 19, 2023

Conversation

mspronesti
Copy link
Contributor

@mspronesti mspronesti commented Nov 18, 2023

This short PR updates the docs and the examples related to AzureOpenAI (plus some ruff formatting).

Summary by CodeRabbit

  • Documentation

    • Updated environment variable names in the documentation to align with the new Azure OpenAI API configuration.
  • Refactor

    • Renamed api_base parameter to azure_endpoint in the AzureOpenAI object instantiation.
    • Changed is_chat_model parameter from True to False to reflect the model type usage in Azure OpenAI API.
    • Adjusted initialization logic and parameter handling in the AzureOpenAI and OpenAI classes to accommodate API updates and versioning.
  • Style

    • Improved indentation and spacing within functions for better code readability.

Copy link
Contributor

coderabbitai bot commented Nov 18, 2023

It looks like you've provided a detailed summary of changes and instructions for generating specific sections. I'll proceed to generate the requested sections based on the provided information.

Walkthrough

The updates across various files reflect a shift in the configuration and instantiation of the Azure OpenAI API client. The primary changes involve renaming environment variables and parameters to align with Azure's naming conventions, adjusting the instantiation logic based on the OpenAI API version, and setting the model type. These modifications suggest an effort to streamline the integration with Azure's services and possibly an API version upgrade.

Changes

File Path Change Summary
docs/LLMs/llms.md Renamed api_base to azure_endpoint and updated environment variable names.
examples/with_azure.py Updated environment variable names and changed is_chat_model parameter.
pandasai/llm/.../azure_openai.py (multiple entries) Reformatted __init__ method arguments, updated environment variable usage, and adjusted client initialization logic.
pandasai/llm/base.py Adjusted indentation and spacing within functions.
pandasai/llm/openai.py Swapped order of parameters in __init__, adjusted indentation, and conditional client assignment.

🐇 In the code's gentle flow,
🍂 As autumn leaves do blow,
Changes come with thoughtful hop,
Azure's keys now on top. 🌟


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 resolve resolve all the CodeRabbit review comments.
  • @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.v2.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: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 2ebe5a6 and 00f72c7.
Files selected for processing (5)
  • docs/LLMs/llms.md (1 hunks)
  • examples/with_azure.py (2 hunks)
  • pandasai/llm/azure_openai.py (5 hunks)
  • pandasai/llm/base.py (2 hunks)
  • pandasai/llm/openai.py (2 hunks)
Files skipped from review due to trivial changes (2)
  • examples/with_azure.py
  • pandasai/llm/base.py
Additional comments: 5
docs/LLMs/llms.md (1)
  • 153-160: The documentation correctly reflects the changes in the instantiation of the AzureOpenAI object, with the api_base parameter being renamed to azure_endpoint. It's important to ensure that all example code and documentation throughout the project are updated to use the new parameter and environment variable names to avoid confusion.
pandasai/llm/openai.py (1)
  • 76-92: The logic for setting self.client based on the model type is sound, but it's important to ensure that the is_openai_v1() function correctly identifies the API version and that the openai.OpenAI and openai.ChatCompletion classes, as well as the openai.Completion class, are correctly implemented and imported. If these classes or methods do not exist or are not imported, this will raise an AttributeError.
pandasai/llm/azure_openai.py (3)
  • 48-61: The __init__ method has been updated to include new parameters and environment variable handling. The renaming of api_base to azure_endpoint and the introduction of azure_ad_token and azure_ad_token_provider are consistent with Azure-specific naming conventions. The addition of is_chat_model parameter allows for specifying the type of model being used, which is important for the conditional assignment of the client attribute later in the code.

  • 135-143: The conditional assignment of the client attribute based on the OpenAI API version and the model type is a good approach to maintain compatibility with different versions and model types. However, ensure that the openai.AzureOpenAI and openai.ChatCompletion classes exist and are correctly imported from the openai library.

  • 151-160: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [146-157]

The _default_params property has been updated to include the engine attribute for OpenAI versions prior to v1. This ensures backward compatibility with earlier versions of the API that required an engine parameter. The use of a conditional expression to determine the correct key (model or engine) is a good practice.

docs/LLMs/llms.md Show resolved Hide resolved
pandasai/llm/openai.py Show resolved Hide resolved
Comment on lines 85 to 91
self.api_token = (
api_token
or os.getenv("OPENAI_API_KEY")
or os.getenv("AZURE_OPENAI_API_KEY")
api_token
or os.getenv("OPENAI_API_KEY")
or os.getenv("AZURE_OPENAI_API_KEY")
)
self.azure_endpoint = azure_endpoint or os.getenv("AZURE_OPENAI_ENDPOINT")
self.api_base = api_base or os.getenv("OPENAI_API_BASE")
Copy link
Contributor

Choose a reason for hiding this comment

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

The environment variable fallbacks have been updated to reflect the new Azure-specific names. However, the fallback for api_token still checks for OPENAI_API_KEY before AZURE_OPENAI_API_KEY. This should be reversed to prioritize the Azure-specific environment variable, as the context suggests a move towards Azure-specific naming conventions.

85        self.api_token = (
86            api_token
-           or os.getenv("OPENAI_API_KEY")
+           or os.getenv("AZURE_OPENAI_API_KEY")
+           or os.getenv("OPENAI_API_KEY")
88        )

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
self.api_token = (
api_token
or os.getenv("OPENAI_API_KEY")
or os.getenv("AZURE_OPENAI_API_KEY")
api_token
or os.getenv("OPENAI_API_KEY")
or os.getenv("AZURE_OPENAI_API_KEY")
)
self.azure_endpoint = azure_endpoint or os.getenv("AZURE_OPENAI_ENDPOINT")
self.api_base = api_base or os.getenv("OPENAI_API_BASE")
self.api_token = (
api_token
or os.getenv("AZURE_OPENAI_API_KEY")
or os.getenv("OPENAI_API_KEY")
)
self.azure_endpoint = azure_endpoint or os.getenv("AZURE_OPENAI_ENDPOINT")
self.api_base = api_base or os.getenv("OPENAI_API_BASE")

@codecov-commenter
Copy link

codecov-commenter commented Nov 18, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (2ebe5a6) 85.36% compared to head (abdb46f) 85.42%.

Files Patch % Lines
pandasai/llm/azure_openai.py 0.00% 2 Missing ⚠️

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #761      +/-   ##
==========================================
+ Coverage   85.36%   85.42%   +0.05%     
==========================================
  Files          74       74              
  Lines        3656     3650       -6     
==========================================
- Hits         3121     3118       -3     
+ Misses        535      532       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mspronesti mspronesti changed the title chore: update docs and examples for Azure OpenAI chore: update docs and examples for Azure OpenAI v1 Nov 18, 2023
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 00f72c7 and ecbc278.
Files selected for processing (1)
  • pandasai/llm/azure_openai.py (5 hunks)
Additional comments: 3
pandasai/llm/azure_openai.py (3)
  • 48-61: The __init__ method has been updated to include new parameters and to reflect the changes in the Azure OpenAI API. The inclusion of azure_endpoint, azure_ad_token, azure_ad_token_provider, and the renaming of api_base to azure_endpoint are consistent with the Azure branding and API updates. The default value for is_chat_model has been changed to True, which should be verified if it aligns with the intended default behavior for the class instances.

  • 135-143: The logic for setting the client attribute based on the is_openai_v1() function's result is a good approach to handle different versions of the OpenAI API. However, ensure that the openai.AzureOpenAI and openai.ChatCompletion classes exist and are correctly imported from the openai module, as this code assumes their presence.

  • 151-157: The _default_params property has been updated to use a conditional key based on the is_openai_v1() function's result. This is a good use of Python's conditional expressions to maintain compatibility with different API versions. Ensure that the model and engine keys are correctly used in the respective API versions.

pandasai/llm/azure_openai.py Show resolved Hide resolved
pandasai/llm/azure_openai.py Show resolved Hide resolved
This change prioritizes AZURE_OPENAI_API_KEY over OPENAI_API_KEY, so one can use 2 backend (OpenAI, AzureOpenAI) setting 2 different vars without conflicts
@mspronesti mspronesti force-pushed the chore/docs-examples-openai_v1 branch from ecbc278 to abdb46f Compare November 19, 2023 15:13
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 00f72c7 and abdb46f.
Files selected for processing (1)
  • pandasai/llm/azure_openai.py (5 hunks)
Additional comments: 3
pandasai/llm/azure_openai.py (3)
  • 135-143: The conditional logic for setting the client attribute based on the API version is correct. However, ensure that the openai.AzureOpenAI class and its methods chat.completions and completions are available in the OpenAI library for Azure as of version 1.0.0. If the library or methods have changed, this code will need to be updated.

  • 151-157: The _default_params property is correctly overridden to provide default parameters specific to the Azure OpenAI implementation. The use of is_openai_v1() to conditionally set the key to either "model" or "engine" is a good approach to maintain compatibility with different API versions.

  • 160-160: The _invocation_params property is correctly overridden to provide additional parameters when not using OpenAI version 1. The inclusion of api_type and api_version is consistent with the need to specify these when invoking the Azure OpenAI API.

pandasai/llm/azure_openai.py Show resolved Hide resolved
pandasai/llm/azure_openai.py Show resolved Hide resolved
@gventuri
Copy link
Collaborator

Thanks a lot @mspronesti!!

@gventuri gventuri merged commit b869df8 into sinaptik-ai:main Nov 19, 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