From b2ae4d1203b4dc095ab65df79b56b51b3dd8fd55 Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 25 Nov 2024 12:34:52 -0500 Subject: [PATCH] Add warnings for deprecated azure oai config changes (#4317) * Add warnings for deprecated azure oai config changes * Update docs and usages, simplify capabilities --- .../tutorial/models.ipynb | 8 +--- .../cookbook/azure-openai-with-aad-auth.md | 8 +--- .../cookbook/structured-output-agent.ipynb | 10 ++--- .../framework/model-clients.ipynb | 10 ++--- .../models/_openai/_openai_client.py | 38 ++++++++++++------- .../models/_openai/config/__init__.py | 6 +-- .../tests/models/test_openai_model_client.py | 1 + 7 files changed, 37 insertions(+), 44 deletions(-) diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb index 2adc862e5..5251e17c0 100644 --- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb +++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb @@ -137,16 +137,12 @@ "token_provider = get_bearer_token_provider(DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\")\n", "\n", "az_model_client = AzureOpenAIChatCompletionClient(\n", - " model=\"{your-azure-deployment}\",\n", + " azure_deployment=\"{your-azure-deployment}\",\n", + " model=\"{model-name, such as gpt-4o}\",\n", " api_version=\"2024-06-01\",\n", " azure_endpoint=\"https://{your-custom-endpoint}.openai.azure.com/\",\n", " azure_ad_token_provider=token_provider, # Optional if you choose key-based authentication.\n", " # api_key=\"sk-...\", # For key-based authentication.\n", - " model_capabilities={\n", - " \"vision\": True,\n", - " \"function_calling\": True,\n", - " \"json_output\": True,\n", - " },\n", ")" ] }, diff --git a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/azure-openai-with-aad-auth.md b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/azure-openai-with-aad-auth.md index c8e4b632b..b347ed7de 100644 --- a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/azure-openai-with-aad-auth.md +++ b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/azure-openai-with-aad-auth.md @@ -24,15 +24,11 @@ token_provider = get_bearer_token_provider( ) client = AzureOpenAIChatCompletionClient( - model="{your-azure-deployment}", + azure_deployment="{your-azure-deployment}", + model="{model-name, such as gpt-4o}", api_version="2024-02-01", azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/", azure_ad_token_provider=token_provider, - model_capabilities={ - "vision":True, - "function_calling":True, - "json_output":True, - } ) ``` diff --git a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/structured-output-agent.ipynb b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/structured-output-agent.ipynb index fa50d6da2..95edbfa0c 100644 --- a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/structured-output-agent.ipynb +++ b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/structured-output-agent.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -79,15 +79,11 @@ "\n", "# Create the client with type-checked environment variables\n", "client = AzureOpenAIChatCompletionClient(\n", - " model=get_env_variable(\"AZURE_OPENAI_DEPLOYMENT_NAME\"),\n", + " azure_deployment=get_env_variable(\"AZURE_OPENAI_DEPLOYMENT_NAME\"),\n", + " model=get_env_variable(\"AZURE_OPENAI_MODEL\"),\n", " api_version=get_env_variable(\"AZURE_OPENAI_API_VERSION\"),\n", " azure_endpoint=get_env_variable(\"AZURE_OPENAI_ENDPOINT\"),\n", " api_key=get_env_variable(\"AZURE_OPENAI_API_KEY\"),\n", - " model_capabilities={\n", - " \"vision\": False,\n", - " \"function_calling\": True,\n", - " \"json_output\": True,\n", - " },\n", ")" ] }, diff --git a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/framework/model-clients.ipynb b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/framework/model-clients.ipynb index cb17886b9..e074eb970 100644 --- a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/framework/model-clients.ipynb +++ b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/framework/model-clients.ipynb @@ -283,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -294,16 +294,12 @@ "token_provider = get_bearer_token_provider(DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\")\n", "\n", "az_model_client = AzureOpenAIChatCompletionClient(\n", - " model=\"{your-azure-deployment}\",\n", + " azure_deployment=\"{your-azure-deployment}\",\n", + " model=\"{model-name, such as gpt-4o}\",\n", " api_version=\"2024-06-01\",\n", " azure_endpoint=\"https://{your-custom-endpoint}.openai.azure.com/\",\n", " azure_ad_token_provider=token_provider, # Optional if you choose key-based authentication.\n", " # api_key=\"sk-...\", # For key-based authentication.\n", - " model_capabilities={\n", - " \"vision\": True,\n", - " \"function_calling\": True,\n", - " \"json_output\": True,\n", - " },\n", ")" ] }, diff --git a/python/packages/autogen-ext/src/autogen_ext/models/_openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/_openai/_openai_client.py index 999e5083f..28dc81a33 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/_openai/_openai_client.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/_openai/_openai_client.py @@ -86,10 +86,29 @@ def _azure_openai_client_from_config(config: Mapping[str, Any]) -> AsyncAzureOpe # Take a copy copied_config = dict(config).copy() + import warnings + + if "azure_deployment" not in copied_config and "model" in copied_config: + warnings.warn( + "Previous behavior of using the model name as the deployment name is deprecated and will be removed in 0.4. Please specify azure_deployment", + stacklevel=2, + ) + + if "azure_endpoint" not in copied_config and "base_url" in copied_config: + warnings.warn( + "Previous behavior of using the base_url as the endpoint is deprecated and will be removed in 0.4. Please specify azure_endpoint", + stacklevel=2, + ) + # Do some fixups copied_config["azure_deployment"] = copied_config.get("azure_deployment", config.get("model")) if copied_config["azure_deployment"] is not None: - copied_config["azure_deployment"] = copied_config["azure_deployment"].replace(".", "") + if "." in copied_config["azure_deployment"]: + warnings.warn( + "Previous behavior stripping '.' from the deployment name is deprecated and will be removed in 0.4", + stacklevel=2, + ) + copied_config["azure_deployment"] = copied_config["azure_deployment"].replace(".", "") copied_config["azure_endpoint"] = copied_config.get("azure_endpoint", copied_config.pop("base_url", None)) # Shave down the config to just the AzureOpenAIChatCompletionClient kwargs @@ -331,9 +350,7 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient): model_capabilities: Optional[ModelCapabilities] = None, ): self._client = client - if model_capabilities is None and isinstance(client, AsyncAzureOpenAI): - raise ValueError("AzureOpenAIChatCompletionClient requires explicit model capabilities") - elif model_capabilities is None: + if model_capabilities is None: self._model_capabilities = _model_info.get_capabilities(create_args["model"]) else: self._model_capabilities = model_capabilities @@ -963,7 +980,7 @@ class AzureOpenAIChatCompletionClient(BaseOpenAIChatCompletionClient): api_version (str): The API version to use. **Required for Azure models.** azure_ad_token (str): The Azure AD token to use. Provide this or `azure_ad_token_provider` for token-based authentication. azure_ad_token_provider (Callable[[], Awaitable[str]]): The Azure AD token provider to use. Provide this or `azure_ad_token` for token-based authentication. - model_capabilities (ModelCapabilities): The capabilities of the model. **Required for Azure models.** + model_capabilities (ModelCapabilities): The capabilities of the model if default resolved values are not correct. api_key (optional, str): The API key to use, use this if you are using key based authentication. It is optional if you are using Azure AD token based authentication or `AZURE_OPENAI_API_KEY` environment variable. timeout (optional, int): The timeout for the request in seconds. max_retries (optional, int): The maximum number of retries to attempt. @@ -990,16 +1007,12 @@ class AzureOpenAIChatCompletionClient(BaseOpenAIChatCompletionClient): token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") az_model_client = AzureOpenAIChatCompletionClient( - model="{your-azure-deployment}", + azure_deployment="{your-azure-deployment}", + model="{deployed-model, such as 'gpt-4o'}", api_version="2024-06-01", azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/", azure_ad_token_provider=token_provider, # Optional if you choose key-based authentication. # api_key="sk-...", # For key-based authentication. `AZURE_OPENAI_API_KEY` environment variable can also be used instead. - model_capabilities={ - "vision": True, - "function_calling": True, - "json_output": True, - }, ) See `here `_ for how to use the Azure client directly or for more info. @@ -1007,9 +1020,6 @@ class AzureOpenAIChatCompletionClient(BaseOpenAIChatCompletionClient): """ def __init__(self, **kwargs: Unpack[AzureOpenAIClientConfiguration]): - if "model" not in kwargs: - raise ValueError("model is required for OpenAIChatCompletionClient") - model_capabilities: Optional[ModelCapabilities] = None copied_args = dict(kwargs).copy() if "model_capabilities" in kwargs: diff --git a/python/packages/autogen-ext/src/autogen_ext/models/_openai/config/__init__.py b/python/packages/autogen-ext/src/autogen_ext/models/_openai/config/__init__.py index 53abfcc58..8afff8682 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/_openai/config/__init__.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/_openai/config/__init__.py @@ -30,14 +30,14 @@ class BaseOpenAIClientConfiguration(CreateArguments, total=False): api_key: str timeout: Union[float, None] max_retries: int + model_capabilities: ModelCapabilities + """What functionality the model supports, determined by default from model name but is overriden if value passed.""" # See OpenAI docs for explanation of these parameters class OpenAIClientConfiguration(BaseOpenAIClientConfiguration, total=False): organization: str base_url: str - # Not required - model_capabilities: ModelCapabilities class AzureOpenAIClientConfiguration(BaseOpenAIClientConfiguration, total=False): @@ -47,8 +47,6 @@ class AzureOpenAIClientConfiguration(BaseOpenAIClientConfiguration, total=False) api_version: Required[str] azure_ad_token: str azure_ad_token_provider: AsyncAzureADTokenProvider - # Must be provided - model_capabilities: Required[ModelCapabilities] __all__ = ["AzureOpenAIClientConfiguration", "OpenAIClientConfiguration"] diff --git a/python/packages/autogen-ext/tests/models/test_openai_model_client.py b/python/packages/autogen-ext/tests/models/test_openai_model_client.py index cee3be583..b2dc504ab 100644 --- a/python/packages/autogen-ext/tests/models/test_openai_model_client.py +++ b/python/packages/autogen-ext/tests/models/test_openai_model_client.py @@ -141,6 +141,7 @@ async def test_openai_chat_completion_client() -> None: @pytest.mark.asyncio async def test_azure_openai_chat_completion_client() -> None: client = AzureOpenAIChatCompletionClient( + azure_deployment="gpt-4o-1", model="gpt-4o", api_key="api_key", api_version="2020-08-04",