Skip to content

Commit ddb0059

Browse files
authored
chore: unify Azure OpenAI model type interface (#3353)
1 parent 42ff55f commit ddb0059

File tree

9 files changed

+946
-939
lines changed

9 files changed

+946
-939
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ body:
2626
attributes:
2727
label: What version of camel are you using?
2828
description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.
29-
placeholder: E.g., 0.2.79a0
29+
placeholder: E.g., 0.2.79a1
3030
validations:
3131
required: true
3232

.github/workflows/pytest_package.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ jobs:
209209
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
210210
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
211211
HF_TOKEN: "${{ secrets.HF_TOKEN }}"
212-
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}"
213-
AZURE_API_VERSION: ${{ secrets.AZURE_API_VERSION }}"
214-
AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"
215-
AZURE_OPENAI_BASE_URL: ${{ secrets.AZURE_OPENAI_BASE_URL }}"
212+
AZURE_OPENAI_API_KEY: "${{ secrets.AZURE_OPENAI_API_KEY }}"
213+
AZURE_API_VERSION: "${{ secrets.AZURE_API_VERSION }}"
214+
AZURE_DEPLOYMENT_NAME: "${{ secrets.AZURE_DEPLOYMENT_NAME }}"
215+
AZURE_OPENAI_BASE_URL: "${{ secrets.AZURE_OPENAI_BASE_URL }}"
216216
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
217217
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
218218
NEO4J_URI: "${{ secrets.NEO4J_URI }}"

camel/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from camel.logger import disable_logging, enable_logging, set_log_level
1616

17-
__version__ = '0.2.79a0'
17+
__version__ = '0.2.79a1'
1818

1919
__all__ = [
2020
'__version__',

camel/models/azure_openai_model.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414
import copy
1515
import os
16+
import warnings
1617
from typing import Any, Callable, Dict, List, Optional, Type, Union
1718

1819
from openai import AsyncAzureOpenAI, AsyncStream, AzureOpenAI, Stream
@@ -60,7 +61,8 @@ class AzureOpenAIModel(BaseModelBackend):
6061
6162
Args:
6263
model_type (Union[ModelType, str]): Model for which a backend is
63-
created, one of GPT_* series.
64+
created, Should be the deployment name you chose when you deployed
65+
an azure model.
6466
model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
6567
that will be fed into:obj:`openai.ChatCompletion.create()`. If
6668
:obj:`None`, :obj:`ChatGPTConfig().as_dict()` will be used.
@@ -71,8 +73,6 @@ class AzureOpenAIModel(BaseModelBackend):
7173
(default: :obj:`None`)
7274
api_version (Optional[str], optional): The api version for the model.
7375
(default: :obj:`None`)
74-
azure_deployment_name (Optional[str], optional): The deployment name
75-
you chose when you deployed an azure model. (default: :obj:`None`)
7676
azure_ad_token (Optional[str], optional): Your Azure Active Directory
7777
token, https://www.microsoft.com/en-us/security/business/
7878
identity-access/microsoft-entra-id. (default: :obj:`None`)
@@ -99,6 +99,10 @@ class AzureOpenAIModel(BaseModelBackend):
9999
AzureOpenAI client instance. If provided, this client will be
100100
used instead of creating a new one. The client should implement
101101
the AsyncAzureOpenAI client interface. (default: :obj:`None`)
102+
azure_deployment_name (Optional[str], optional): **Deprecated**.
103+
Use `model_type` parameter instead. This parameter is kept for
104+
backward compatibility and will be removed in a future version.
105+
(default: :obj:`None`)
102106
**kwargs (Any): Additional arguments to pass to the client
103107
initialization. Ignored if custom clients are provided.
104108
@@ -115,14 +119,35 @@ def __init__(
115119
timeout: Optional[float] = None,
116120
token_counter: Optional[BaseTokenCounter] = None,
117121
api_version: Optional[str] = None,
118-
azure_deployment_name: Optional[str] = None,
119122
azure_ad_token_provider: Optional["AzureADTokenProvider"] = None,
120123
azure_ad_token: Optional[str] = None,
121124
max_retries: int = 3,
122125
client: Optional[Any] = None,
123126
async_client: Optional[Any] = None,
127+
azure_deployment_name: Optional[str] = None,
124128
**kwargs: Any,
125129
) -> None:
130+
# Handle deprecated azure_deployment_name parameter
131+
if azure_deployment_name is not None:
132+
warnings.warn(
133+
"The 'azure_deployment_name' parameter is deprecated. "
134+
"Please use 'model_type' parameter instead. "
135+
"The 'azure_deployment_name' parameter is being ignored.",
136+
DeprecationWarning,
137+
stacklevel=2,
138+
)
139+
140+
# Handle deprecated AZURE_DEPLOYMENT_NAME environment variable
141+
if os.environ.get("AZURE_DEPLOYMENT_NAME") is not None:
142+
warnings.warn(
143+
"The 'AZURE_DEPLOYMENT_NAME' environment variable is "
144+
"deprecated. Please use the 'model_type' parameter "
145+
"instead. The 'AZURE_DEPLOYMENT_NAME' environment "
146+
"variable is being ignored.",
147+
DeprecationWarning,
148+
stacklevel=2,
149+
)
150+
126151
if model_config_dict is None:
127152
model_config_dict = ChatGPTConfig().as_dict()
128153
api_key = api_key or os.environ.get("AZURE_OPENAI_API_KEY")
@@ -133,9 +158,6 @@ def __init__(
133158
)
134159

135160
self.api_version = api_version or os.environ.get("AZURE_API_VERSION")
136-
self._azure_deployment_name = azure_deployment_name or os.environ.get(
137-
"AZURE_DEPLOYMENT_NAME"
138-
)
139161
self._azure_ad_token = azure_ad_token or os.environ.get(
140162
"AZURE_AD_TOKEN"
141163
)
@@ -145,11 +167,6 @@ def __init__(
145167
"Must provide either the `api_version` argument "
146168
"or `AZURE_API_VERSION` environment variable."
147169
)
148-
if self._azure_deployment_name is None:
149-
raise ValueError(
150-
"Must provide either the `azure_deployment_name` argument "
151-
"or `AZURE_DEPLOYMENT_NAME` environment variable."
152-
)
153170

154171
# Use custom clients if provided, otherwise create new ones
155172
if client is not None:
@@ -162,7 +179,7 @@ def __init__(
162179

163180
self._client = LangfuseOpenAI(
164181
azure_endpoint=str(self._url),
165-
azure_deployment=self._azure_deployment_name,
182+
azure_deployment=str(self.model_type),
166183
api_version=self.api_version,
167184
api_key=self._api_key,
168185
azure_ad_token=self._azure_ad_token,
@@ -174,7 +191,7 @@ def __init__(
174191
else:
175192
self._client = AzureOpenAI(
176193
azure_endpoint=str(self._url),
177-
azure_deployment=self._azure_deployment_name,
194+
azure_deployment=str(self.model_type),
178195
api_version=self.api_version,
179196
api_key=self._api_key,
180197
azure_ad_token=self._azure_ad_token,
@@ -196,7 +213,7 @@ def __init__(
196213

197214
self._async_client = LangfuseAsyncOpenAI(
198215
azure_endpoint=str(self._url),
199-
azure_deployment=self._azure_deployment_name,
216+
azure_deployment=str(self.model_type),
200217
api_version=self.api_version,
201218
api_key=self._api_key,
202219
azure_ad_token=self._azure_ad_token,
@@ -208,7 +225,7 @@ def __init__(
208225
else:
209226
self._async_client = AsyncAzureOpenAI(
210227
azure_endpoint=str(self._url),
211-
azure_deployment=self._azure_deployment_name,
228+
azure_deployment=str(self.model_type),
212229
api_version=self.api_version,
213230
api_key=self._api_key,
214231
azure_ad_token=self._azure_ad_token,
@@ -359,7 +376,7 @@ def _request_chat_completion(
359376

360377
return self._client.chat.completions.create(
361378
messages=messages,
362-
model=self._azure_deployment_name, # type:ignore[arg-type]
379+
model=str(self.model_type),
363380
**request_config,
364381
)
365382

@@ -375,7 +392,7 @@ async def _arequest_chat_completion(
375392

376393
return await self._async_client.chat.completions.create(
377394
messages=messages,
378-
model=self._azure_deployment_name, # type:ignore[arg-type]
395+
model=str(self.model_type),
379396
**request_config,
380397
)
381398

@@ -396,7 +413,7 @@ def _request_parse(
396413

397414
return self._client.beta.chat.completions.parse(
398415
messages=messages,
399-
model=self._azure_deployment_name, # type:ignore[arg-type]
416+
model=str(self.model_type),
400417
**request_config,
401418
)
402419

@@ -417,7 +434,7 @@ async def _arequest_parse(
417434

418435
return await self._async_client.beta.chat.completions.parse(
419436
messages=messages,
420-
model=self._azure_deployment_name, # type:ignore[arg-type]
437+
model=str(self.model_type),
421438
**request_config,
422439
)
423440

@@ -443,7 +460,7 @@ def _request_stream_parse(
443460
# Use the beta streaming API for structured outputs
444461
return self._client.beta.chat.completions.stream(
445462
messages=messages,
446-
model=self.model_type,
463+
model=str(self.model_type),
447464
response_format=response_format,
448465
**request_config,
449466
)
@@ -470,7 +487,7 @@ async def _arequest_stream_parse(
470487
# Use the beta streaming API for structured outputs
471488
return self._async_client.beta.chat.completions.stream(
472489
messages=messages,
473-
model=self.model_type,
490+
model=str(self.model_type),
474491
response_format=response_format,
475492
**request_config,
476493
)

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
project = 'CAMEL'
2828
copyright = '2024, CAMEL-AI.org'
2929
author = 'CAMEL-AI.org'
30-
release = '0.2.79a0'
30+
release = '0.2.79a1'
3131

3232
html_favicon = (
3333
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'

examples/models/azure_openai_model_example.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14+
import os
15+
1416
from camel.agents import ChatAgent
15-
from camel.configs import ChatGPTConfig
1617
from camel.models import ModelFactory
17-
from camel.types import ModelPlatformType, ModelType
18+
from camel.types import ModelPlatformType
1819

1920
"""
2021
please set the below os environment:
2122
export AZURE_OPENAI_BASE_URL=""
2223
export AZURE_API_VERSION=""
2324
export AZURE_OPENAI_API_KEY=""
24-
export AZURE_DEPLOYMENT_NAME=""
2525
"""
2626

2727
model = ModelFactory.create(
2828
model_platform=ModelPlatformType.AZURE,
29-
model_type=ModelType.GPT_4O_MINI,
30-
model_config_dict=ChatGPTConfig(temperature=0.2).as_dict(),
29+
model_type="gpt-4.1",
30+
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
31+
url=os.getenv("AZURE_OPENAI_BASE_URL"),
32+
api_version="2024-12-01-preview",
3133
)
3234

3335
# Define system message

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "camel-ai"
7-
version = "0.2.79a0"
7+
version = "0.2.79a1"
88
description = "Communicative Agents for AI Society Study"
99
authors = [{ name = "CAMEL-AI.org" }]
1010
requires-python = ">=3.10,<3.15"

test/models/test_azure_openai_model.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
please set the below os environment:
1616
export AZURE_OPENAI_BASE_URL=""
1717
18-
# if `AZURE_API_VERSION` is not set, `OPENAI_API_VERSION` will be used as api
19-
# version
18+
# if `AZURE_API_VERSION` is not set, the api_version parameter must be provided
2019
export AZURE_API_VERSION=""
2120
export AZURE_OPENAI_API_KEY=""
22-
export AZURE_DEPLOYMENT_NAME=""
2321
"""
2422

2523
import pytest
@@ -43,7 +41,13 @@
4341
)
4442
def test_openai_model(model_type):
4543
model_config_dict = ChatGPTConfig().as_dict()
46-
model = AzureOpenAIModel(model_type, model_config_dict)
44+
model = AzureOpenAIModel(
45+
model_type,
46+
model_config_dict,
47+
api_version="2024-02-15-preview",
48+
api_key="test-key",
49+
url="https://test.openai.azure.com/",
50+
)
4751
assert model.model_type == model_type
4852
assert model.model_config_dict == model_config_dict
4953
assert isinstance(model.token_counter, OpenAITokenCounter)
@@ -67,5 +71,8 @@ def test_openai_model_create(model_type: ModelType):
6771
model_platform=ModelPlatformType.AZURE,
6872
model_type=model_type,
6973
model_config_dict=ChatGPTConfig(temperature=0.8, n=3).as_dict(),
74+
api_version="2024-02-15-preview",
75+
api_key="test-key",
76+
url="https://test.openai.azure.com/",
7077
)
7178
assert model.model_type == model_type

0 commit comments

Comments
 (0)