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: Integrate Azure OpenAI API. (new) #499

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

L4zyy
Copy link

@L4zyy L4zyy commented Apr 4, 2024

Description

This PR integrate the Azure OpenAI API to CAMEL.
(older PR: #402)

Motivation and Context

Integrate Azure OpenAI API, close #231

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of example)

Implemented Tasks

  • add Azure OpenAI Model class with test
  • add example
  • add new section on README

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.

Copy link

coderabbitai bot commented Apr 4, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@L4zyy L4zyy force-pushed the feature/azure-openai-support branch from 35a974b to e9bcc7a Compare April 4, 2024 16:02
Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution and sorry for the late review, left some comments as below

Comment on lines +171 to +172
export AZURE_MODEL_TYPE=<insert the ModelType enum name that match your Azure OpenAI API model type>
```
Copy link
Member

Choose a reason for hiding this comment

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

Seems for Azure, AZURE_MODEL_TYPE is not a must, but AZURE_ API_VERSION is required, could you pls check this?

Copy link
Author

Choose a reason for hiding this comment

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

Currently the model type is used by token counter. I still think we should keep this variable for two reason: Firstly, Azure OpenAI API is basically an OpenAI API, I want other future function needs the model type developed on OpenAI Model class can be used directly by this class. Secondly, the deployed model type is a very important attribute for Azure OpenAI API, therefore I want it to be specified when we create this class.

Copy link
Member

Choose a reason for hiding this comment

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

Hey @L4zyy , I got your point, ModelType is required for counting token. We can pass ModelType as parameter rather than using export to set it into environment, just like how we did for OpenAI service. But AZURE_ API_VERSION is indeed required when we call Azure service, here in the README if you don't ask user set the AZURE_ API_VERSION then it will always be the default value you set in your code 2023-10-01-preview, it shouldn't be the case

Comment on lines 46 to 47
backend_config_dict (Dict[str, Any]): A dictionary that contains
the backend configs.(default : {})
Copy link
Member

Choose a reason for hiding this comment

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

if we are going to use this parameter pass all args including deployment name, endpoint, api version, I would suggest make the docstring more clear here.

Copy link
Author

Choose a reason for hiding this comment

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

I’ve updated this in the new commit.

Comment on lines +51 to +52
self.model_type = backend_config_dict.get(
"model_type", os.environ.get("AZURE_MODEL_TYPE", None))
Copy link
Member

Choose a reason for hiding this comment

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

again, model type is not necessary for azure

Copy link
Author

Choose a reason for hiding this comment

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

Explained my concerns above.

Copy link
Member

Choose a reason for hiding this comment

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

how about let user set ModelType as argument rather than set in environment?

camel/models/azure_openai_model.py Show resolved Hide resolved
Comment on lines 62 to 66
assert model_type is not None, "Azure model type is not provided."
assert (self.deployment_name
is not None), "Azure model deployment name is not provided."
assert (self.azure_endpoint
is not None), "Azure endpoint is not provided."
Copy link
Member

Choose a reason for hiding this comment

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

For expected error conditions, explicit error handling using try-except blocks is preferred.

Copy link
Author

Choose a reason for hiding this comment

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

I’ve updated this in new commit.

Comment on lines 114 to 134
def check_model_config(self):
r"""Check whether the model configuration contains any
unexpected arguments to OpenAI API.

Raises:
ValueError: If the model configuration dictionary contains any
unexpected arguments to OpenAI API.
"""
for param in self.model_config_dict:
if param not in OPENAI_API_PARAMS_WITH_FUNCTIONS:
raise ValueError(f"Unexpected argument `{param}` is "
"input into OpenAI model backend.")

@property
def stream(self) -> bool:
r"""Returns whether the model is in stream mode,
which sends partial results each time.
Returns:
bool: Whether the model is in stream mode.
"""
return self.model_config_dict.get("stream", False)
Copy link
Member

Choose a reason for hiding this comment

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

we also need to check the back-end config as we checked the model config

Copy link
Author

Choose a reason for hiding this comment

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

I’ve updated this in new commit.

Comment on lines +74 to +98
def azure_openai_api_key_required(func: F) -> F:
r"""Decorator that checks if the Azure OpenAI API key is available in the
environment variables.

Args:
func (callable): The function to be wrapped.

Returns:
callable: The decorated function.

Raises:
ValueError: If the Azure OpenAI API key is not found in the environment
variables.
"""

@wraps(func)
def wrapper(self, *args, **kwargs):
if 'AZURE_OPENAI_API_KEY' in os.environ:
return func(self, *args, **kwargs)
else:
raise ValueError('Azure OpenAI API key not found.')

return cast(F, wrapper)


Copy link
Member

Choose a reason for hiding this comment

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

for azure openai, I think not only api is required, the endpoint and api version also need to be checked

Copy link
Author

Choose a reason for hiding this comment

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

I don’t think we need to check these variables here since they will be checked when initializing the AzureOpenAIModel instance. When running the commands, we should only check the AZURE_OPENAI_API_KEY

Copy link
Member

Choose a reason for hiding this comment

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

AZURE_OPENAI_API_KEY , AZURE_ENDPOINT , AZURE_DEPLOYMENT_NAME , AZURE_ API_VERSION are all required by Azure, if we add AZURE_OPENAI_API_KEY as decorator checking item, why not follow the same logic add others? Can we move the checking in initializing to this decorator? If not why we not also also check AZURE_OPENAI_API_KEYwhen initializing the AzureOpenAIModel instance? WDYT?

@Wendong-Fan Wendong-Fan requested a review from a team April 6, 2024 14:07
@Wendong-Fan Wendong-Fan mentioned this pull request Apr 6, 2024
13 tasks
@Wendong-Fan Wendong-Fan added this to the Sprint 2 milestone Apr 22, 2024
@Wendong-Fan
Copy link
Member

Dear @camel-ai/camel-maintainers
A kind reminder, we should complete the review of this PR before the end of this sprint, which is in two days.

@L4zyy
Copy link
Author

L4zyy commented May 5, 2024

Apologies for the delay in updating. I've addressed all your valuable feedback, responded accordingly, and pushed a new commit. If the new commit looks good, I will merge this branch to the HEAD of master branch ASAP.

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

Thanks @L4zyy and sorry for the late reply~

Comment on lines +171 to +172
export AZURE_MODEL_TYPE=<insert the ModelType enum name that match your Azure OpenAI API model type>
```
Copy link
Member

Choose a reason for hiding this comment

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

Hey @L4zyy , I got your point, ModelType is required for counting token. We can pass ModelType as parameter rather than using export to set it into environment, just like how we did for OpenAI service. But AZURE_ API_VERSION is indeed required when we call Azure service, here in the README if you don't ask user set the AZURE_ API_VERSION then it will always be the default value you set in your code 2023-10-01-preview, it shouldn't be the case

Comment on lines +51 to +52
self.model_type = backend_config_dict.get(
"model_type", os.environ.get("AZURE_MODEL_TYPE", None))
Copy link
Member

Choose a reason for hiding this comment

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

how about let user set ModelType as argument rather than set in environment?

Comment on lines +74 to +98
def azure_openai_api_key_required(func: F) -> F:
r"""Decorator that checks if the Azure OpenAI API key is available in the
environment variables.

Args:
func (callable): The function to be wrapped.

Returns:
callable: The decorated function.

Raises:
ValueError: If the Azure OpenAI API key is not found in the environment
variables.
"""

@wraps(func)
def wrapper(self, *args, **kwargs):
if 'AZURE_OPENAI_API_KEY' in os.environ:
return func(self, *args, **kwargs)
else:
raise ValueError('Azure OpenAI API key not found.')

return cast(F, wrapper)


Copy link
Member

Choose a reason for hiding this comment

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

AZURE_OPENAI_API_KEY , AZURE_ENDPOINT , AZURE_DEPLOYMENT_NAME , AZURE_ API_VERSION are all required by Azure, if we add AZURE_OPENAI_API_KEY as decorator checking item, why not follow the same logic add others? Can we move the checking in initializing to this decorator? If not why we not also also check AZURE_OPENAI_API_KEYwhen initializing the AzureOpenAIModel instance? WDYT?

@Wendong-Fan Wendong-Fan requested a review from a team May 22, 2024 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Reviewing
Development

Successfully merging this pull request may close these issues.

[Feature Request] Support Azure OpenAI models
2 participants