š« CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Azure OpenAI API (#733)
maoquanfd944b7a8274
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.ymlindex 7d9b60f91..2469951d2 100644--- a/.github/workflows/build_package.yml+++ b/.github/workflows/build_package.yml@@ -35,4 +35,8 @@ jobs:OPENWEATHERMAP_API_KEY: "${{ secrets.OPENWEATHERMAP_API_KEY }}"ANTHROPIC_API_KEY: "${{ secrets.ANTHROPIC_API_KEY }}"COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"+ AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}"+ AZURE_API_VERSION: ${{ secrets.AZURE_API_VERSION }}"+ AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"+ AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}"run: pytest --fast-test-mode ./testdiff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.ymlindex 65a879661..825fadd6b 100644--- a/.github/workflows/pytest_package.yml+++ b/.github/workflows/pytest_package.yml@@ -33,6 +33,10 @@ jobs:NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"+ AZURE_OPENAI_API_KEY: "to-be-filled"+ AZURE_API_VERSION: "to-be-filled"+ AZURE_DEPLOYMENT_NAME: "to-be-filled"+ AZURE_OPENAI_ENDPOINT: "https://camel.openai.azure.com/"run: poetry run pytest --fast-test-mode test/pytest_package_llm_test:@@ -55,6 +59,10 @@ jobs:NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"+ AZURE_OPENAI_API_KEY: "to-be-filled"+ AZURE_API_VERSION: "to-be-filled"+ AZURE_DEPLOYMENT_NAME: "to-be-filled"+ AZURE_OPENAI_ENDPOINT: "https://camel.openai.azure.com/"run: poetry run pytest --llm-test-only test/pytest_package_very_slow_test:@@ -77,4 +85,8 @@ jobs:NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"+ AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}"+ AZURE_API_VERSION: ${{ secrets.AZURE_API_VERSION }}"+ AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"+ AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}"run: poetry run pytest --very-slow-test-only test/diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 42fca4415..b57158fab 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -12,6 +12,7 @@# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from .anthropic_model import AnthropicModel+from .azure_openai_model import AzureOpenAIModelfrom .base_model import BaseModelBackendfrom .gemini_model import GeminiModelfrom .litellm_model import LiteLLMModel@@ -28,6 +29,7 @@ from .zhipuai_model import ZhipuAIModel__all__ = ['BaseModelBackend','OpenAIModel',+ 'AzureOpenAIModel','AnthropicModel','StubModel','ZhipuAIModel',diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.pynew file mode 100644index 000000000..a4b59a0ef--- /dev/null+++ b/camel/models/azure_openai_model.py@@ -0,0 +1,152 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+from typing import Any, Dict, List, Optional, Union++from openai import AzureOpenAI, Stream++from camel.configs import OPENAI_API_PARAMS+from camel.messages import OpenAIMessage+from camel.models.base_model import BaseModelBackend+from camel.types import ChatCompletion, ChatCompletionChunk, ModelType+from camel.utils import BaseTokenCounter, OpenAITokenCounter, api_keys_required+++class AzureOpenAIModel(BaseModelBackend):+ r"""Azure OpenAI API in a unified BaseModelBackend interface.+ Doc: https://learn.microsoft.com/en-us/azure/ai-services/openai/+ """++ def __init__(+ self,+ model_type: ModelType,+ model_config_dict: Dict[str, Any],+ api_key: Optional[str] = None,+ url: Optional[str] = None,+ api_version: Optional[str] = None,+ azure_deployment_name: Optional[str] = None,+ ) -> None:+ r"""Constructor for OpenAI backend.++ Args:+ model_type (ModelType): Model for which a backend is created,+ one of GPT_* series.+ model_config_dict (Dict[str, Any]): A dictionary that will+ be fed into openai.ChatCompletion.create().+ api_key (Optional[str]): The API key for authenticating with the+ OpenAI service. (default: :obj:`None`)+ url (Optional[str]): The url to the OpenAI service. (default:+ :obj:`None`)+ api_version (Optional[str]): The api version for the model.+ azure_deployment_name (Optional[str]): The deployment name you+ chose when you deployed an azure model. (default: :obj:`None`)+ """+ super().__init__(model_type, model_config_dict, api_key, url)+ self._url = url or os.environ.get("AZURE_OPENAI_ENDPOINT")+ self._api_key = api_key or os.environ.get("AZURE_OPENAI_API_KEY")+ self.api_version = api_version or os.environ.get("AZURE_API_VERSION")+ self.azure_deployment_name = azure_deployment_name or os.environ.get(+ "AZURE_DEPLOYMENT_NAME"+ )++ if self._url is None:+ raise ValueError(+ "Must provide either the `url` argument "+ "or `AZURE_OPENAI_ENDPOINT` environment variable."+ )+ if self._api_key is None:+ raise ValueError(+ "Must provide either the `api_key` argument "+ "or `AZURE_OPENAI_API_KEY` environment variable."+ )+ if self.api_version is None:+ raise ValueError(+ "Must provide either the `api_version` argument "+ "or `AZURE_API_VERSION` environment variable."+ )+ if self.azure_deployment_name is None:+ raise ValueError(+ "Must provide either the `azure_deployment_name` argument "+ "or `AZURE_DEPLOYMENT_NAME` environment variable."+ )+ self.model = str(self.azure_deployment_name)++ self._client = AzureOpenAI(+ azure_endpoint=str(self._url),+ azure_deployment=self.azure_deployment_name,+ api_version=self.api_version,+ api_key=self._api_key,+ timeout=60,+ max_retries=3,+ )+ self._token_counter: Optional[BaseTokenCounter] = None++ @property+ def token_counter(self) -> BaseTokenCounter:+ r"""Initialize the token counter for the model backend.++ Returns:+ BaseTokenCounter: The token counter following the model's+ tokenization style.+ """+ if not self._token_counter:+ self._token_counter = OpenAITokenCounter(self.model_type)+ return self._token_counter++ @api_keys_required("AZURE_OPENAI_API_KEY", "AZURE_API_VERSION")+ def run(+ self,+ messages: List[OpenAIMessage],+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ r"""Runs inference of Azure OpenAI chat completion.++ Args:+ messages (List[OpenAIMessage]): Message list with the chat history+ in OpenAI API format.++ Returns:+ Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ `ChatCompletion` in the non-stream mode, or+ `Stream[ChatCompletionChunk]` in the stream mode.+ """+ response = self._client.chat.completions.create(+ messages=messages,+ model=self.model,+ **self.model_config_dict,+ )+ return response++ def check_model_config(self):+ r"""Check whether the model configuration contains any+ unexpected arguments to Azure OpenAI API.++ Raises:+ ValueError: If the model configuration dictionary contains any+ unexpected arguments to Azure OpenAI API.+ """+ for param in self.model_config_dict:+ if param not in OPENAI_API_PARAMS:+ raise ValueError(+ f"Unexpected argument `{param}` is "+ "input into Azure 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)diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex bc99c7d0f..a3daded23 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -14,6 +14,7 @@from typing import Any, Dict, Optional, Unionfrom camel.models.anthropic_model import AnthropicModel+from camel.models.azure_openai_model import AzureOpenAIModelfrom camel.models.base_model import BaseModelBackendfrom camel.models.gemini_model import GeminiModelfrom camel.models.litellm_model import LiteLLMModel@@ -75,6 +76,8 @@ class ModelFactory:)if model_platform.is_openai and model_type.is_openai:model_class = OpenAIModel+ elif model_platform.is_azure and model_type.is_azure_openai:+ model_class = AzureOpenAIModelelif model_platform.is_anthropic and model_type.is_anthropic:model_class = AnthropicModelelif model_platform.is_zhipuai and model_type.is_zhipuai:diff --git a/camel/types/enums.py b/camel/types/enums.pyindex f5bb19d29..9cfeedee5 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -84,6 +84,19 @@ class ModelType(Enum):ModelType.GPT_4O_MINI,}+ @property+ def is_azure_openai(self) -> bool:+ r"""Returns whether this type of models is an OpenAI-released model+ from Azure.+ """+ return self in {+ ModelType.GPT_3_5_TURBO,+ ModelType.GPT_4,+ ModelType.GPT_4_32K,+ ModelType.GPT_4_TURBO,+ ModelType.GPT_4O,+ }+@propertydef is_zhipuai(self) -> bool:r"""Returns whether this type of models is an ZhipuAI model."""diff --git a/examples/models/azure_openai_model_example.py b/examples/models/azure_openai_model_example.pynew file mode 100644index 000000000..3369fbc79--- /dev/null+++ b/examples/models/azure_openai_model_example.py@@ -0,0 +1,58 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+from camel.agents import ChatAgent+from camel.configs import ChatGPTConfig+from camel.messages import BaseMessage+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++"""+please set the below os environment:+export AZURE_OPENAI_ENDPOINT=""+export AZURE_API_VERSION=""+export AZURE_OPENAI_API_KEY=""+export AZURE_DEPLOYMENT_NAME=""+"""++model = ModelFactory.create(+ model_platform=ModelPlatformType.AZURE,+ model_type=ModelType.GPT_3_5_TURBO,+ model_config_dict=ChatGPTConfig(temperature=0.2).__dict__,+)++# Define system message+sys_msg = BaseMessage.make_assistant_message(+ role_name="Assistant",+ content="You are a helpful assistant.",+)++# Set agent+camel_agent = ChatAgent(system_message=sys_msg, model=model)++user_msg = BaseMessage.make_user_message(+ role_name="User",+ content="""Say hi to CAMEL AI, one open-source community dedicated to the+ study of autonomous and communicative agents.""",+)++# Get response information+response = camel_agent.step(user_msg)+print(response.msgs[0].content)+'''+===============================================================================+Hello CAMEL AI! It's great to hear about your open-source community dedicated+to the study of autonomous and communicative agents. If you have any+questions or need assistance, feel free to ask!+===============================================================================+'''diff --git a/test/models/test_azure_openai_model.py b/test/models/test_azure_openai_model.pynew file mode 100644index 000000000..e9b5043d6--- /dev/null+++ b/test/models/test_azure_openai_model.py@@ -0,0 +1,93 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+"""+please set the below os environment:+export AZURE_OPENAI_ENDPOINT=""++# if `AZURE_API_VERSION` is not set, `OPENAI_API_VERSION` will be used as api version+export AZURE_API_VERSION=""+export AZURE_OPENAI_API_KEY=""+export AZURE_DEPLOYMENT_NAME=""+"""++import re++import pytest++from camel.configs import ChatGPTConfig, OpenSourceConfig+from camel.models import AzureOpenAIModel, ModelFactory+from camel.types import ModelPlatformType, ModelType+from camel.utils import OpenAITokenCounter++[email protected]_backend+ "model_type",+ [+ ModelType.GPT_3_5_TURBO,+ ModelType.GPT_4,+ ModelType.GPT_4_32K,+ ModelType.GPT_4_TURBO,+ ModelType.GPT_4O,+ ],+)+def test_openai_model(model_type):+ model_config_dict = ChatGPTConfig().__dict__+ model = AzureOpenAIModel(model_type, model_config_dict)+ assert model.model_type == model_type+ assert model.model_config_dict == model_config_dict+ assert isinstance(model.token_counter, OpenAITokenCounter)+ assert isinstance(model.model_type.value_for_tiktoken, str)+ assert isinstance(model.model_type.token_limit, int)++[email protected]_backend+ "model_type",+ [+ ModelType.GPT_3_5_TURBO,+ ModelType.GPT_4,+ ModelType.GPT_4_32K,+ ModelType.GPT_4_TURBO,+ ModelType.GPT_4O,+ ],+)+def test_openai_model_create(model_type):+ model = ModelFactory.create(+ model_platform=ModelPlatformType.AZURE,+ model_type=model_type,+ model_config_dict=ChatGPTConfig(temperature=0.8, n=3).__dict__,+ )+ assert model.model_type == model_type++[email protected]_backend+def test_openai_model_unexpected_argument():+ model_type = ModelType.GPT_4+ model_config = OpenSourceConfig(+ model_path="vicuna-7b-v1.5",+ server_url="http://localhost:8000/v1",+ )+ model_config_dict = model_config.__dict__++ with pytest.raises(+ ValueError,+ # ruff: noqa: E501+ match=re.escape(+ (+ "Unexpected argument `model_path` is input into Azure OpenAI model backend."+ )+ ),+ ):+ _ = AzureOpenAIModel(model_type, model_config_dict)