Skip to content

A programming framework for agentic AI

Add autogen user agent to azure openai requests (#6124)

Jack Gerrits8a5ee3de6a4a
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/__init__.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/__init__.py
index 366ad8311..cd0689b8e 100644
--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/__init__.py
+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/__init__.py
@@ -1,4 +1,9 @@
-from ._openai_client import AzureOpenAIChatCompletionClient, BaseOpenAIChatCompletionClient, OpenAIChatCompletionClient
+from ._openai_client import (
+ AzureOpenAIChatCompletionClient,
+ BaseOpenAIChatCompletionClient,
+ OpenAIChatCompletionClient,
+ AZURE_OPENAI_USER_AGENT,
+)
from .config import (
AzureOpenAIClientConfigurationConfigModel,
BaseOpenAIClientConfigurationConfigModel,
@@ -14,4 +19,5 @@ __all__ = [
"OpenAIClientConfigurationConfigModel",
"BaseOpenAIClientConfigurationConfigModel",
"CreateArgumentsConfigModel",
+ "AZURE_OPENAI_USER_AGENT",
]
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 efdb22cd1..74a9caa44 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
@@ -87,6 +87,8 @@ from .config import (
OpenAIClientConfiguration,
OpenAIClientConfigurationConfigModel,
)
+from importlib.metadata import PackageNotFoundError, version
+
logger = logging.getLogger(EVENT_LOGGER_NAME)
trace_logger = logging.getLogger(TRACE_LOGGER_NAME)
@@ -101,12 +103,31 @@ create_kwargs = set(completion_create_params.CompletionCreateParamsBase.__annota
disallowed_create_args = set(["stream", "messages", "function_call", "functions", "n"])
required_create_args: Set[str] = set(["model"])
+USER_AGENT_HEADER_NAME = "User-Agent"
+
+try:
+ version_info = version("autogen-ext")
+except PackageNotFoundError:
+ version_info = "dev"
+AZURE_OPENAI_USER_AGENT = f"autogen-python/{version_info}"
+
def _azure_openai_client_from_config(config: Mapping[str, Any]) -> AsyncAzureOpenAI:
# Take a copy
copied_config = dict(config).copy()
# Shave down the config to just the AzureOpenAIChatCompletionClient kwargs
azure_config = {k: v for k, v in copied_config.items() if k in aopenai_init_kwargs}
+
+ DEFAULT_HEADERS_KEY = "default_headers"
+ if DEFAULT_HEADERS_KEY not in azure_config:
+ azure_config[DEFAULT_HEADERS_KEY] = {}
+
+ azure_config[DEFAULT_HEADERS_KEY][USER_AGENT_HEADER_NAME] = (
+ f"{AZURE_OPENAI_USER_AGENT} {azure_config[DEFAULT_HEADERS_KEY][USER_AGENT_HEADER_NAME]}"
+ if USER_AGENT_HEADER_NAME in azure_config[DEFAULT_HEADERS_KEY]
+ else AZURE_OPENAI_USER_AGENT
+ )
+
return AsyncAzureOpenAI(**azure_config)
@@ -1548,6 +1569,10 @@ class AzureOpenAIChatCompletionClient(
Right now only `DefaultAzureCredential` is supported with no additional args passed to it.
+ .. note::
+
+ The Azure OpenAI client by default sets the User-Agent header to `autogen-python/{version}`. To override this, you can set the variable `autogen_ext.models.openai.AZURE_OPENAI_USER_AGENT` environment variable to an empty string.
+
See `here <https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/managed-identity#chat-completions>`_ for how to use the Azure client directly or for more info.
"""