From 4fa3ef0d5519321043d05a2f2829e9d18db4c03c Mon Sep 17 00:00:00 2001 From: Marlene <57748216+marlenezw@users.noreply.github.com> Date: Fri, 7 Feb 2025 23:28:30 +0000 Subject: [PATCH] Community/Partner: Adding Azure community and partner user agent to better track usage in Python (#29561) - This pull request includes various changes to add a `user_agent` parameter to Azure OpenAI, Azure Search and Whisper in the Community and Partner packages. This helps in identifying the source of API requests so we can better track usage and help support the community better. I will also be adding the user_agent to the new `langchain-azure` repo as well. - No issue connected or updated dependencies. - Utilises existing tests and docs --------- Co-authored-by: Erick Friis --- .../langchain_community/chat_models/azure_openai.py | 5 ++++- .../community/langchain_community/embeddings/azure_openai.py | 5 ++++- libs/community/langchain_community/llms/openai.py | 5 ++++- .../langchain_community/vectorstores/azuresearch.py | 2 +- libs/partners/openai/langchain_openai/chat_models/azure.py | 5 ++++- libs/partners/openai/langchain_openai/embeddings/azure.py | 5 ++++- libs/partners/openai/langchain_openai/llms/azure.py | 5 ++++- 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/libs/community/langchain_community/chat_models/azure_openai.py b/libs/community/langchain_community/chat_models/azure_openai.py index 2f2711f4625..6825b3d85e2 100644 --- a/libs/community/langchain_community/chat_models/azure_openai.py +++ b/libs/community/langchain_community/chat_models/azure_openai.py @@ -209,7 +209,10 @@ class AzureChatOpenAI(ChatOpenAI): "base_url": values["openai_api_base"], "timeout": values["request_timeout"], "max_retries": values["max_retries"], - "default_headers": values["default_headers"], + "default_headers": { + **(values["default_headers"] or {}), + "User-Agent": "langchain-comm-python-azure-openai", + }, "default_query": values["default_query"], "http_client": values["http_client"], } diff --git a/libs/community/langchain_community/embeddings/azure_openai.py b/libs/community/langchain_community/embeddings/azure_openai.py index 61d1b4d4d3f..7687cb08ee3 100644 --- a/libs/community/langchain_community/embeddings/azure_openai.py +++ b/libs/community/langchain_community/embeddings/azure_openai.py @@ -163,7 +163,10 @@ class AzureOpenAIEmbeddings(OpenAIEmbeddings): # type: ignore[override] "base_url": self.openai_api_base, "timeout": self.request_timeout, "max_retries": self.max_retries, - "default_headers": self.default_headers, + "default_headers": { + **(self.default_headers or {}), + "User-Agent": "langchain-comm-python-azure-openai", + }, "default_query": self.default_query, "http_client": self.http_client, } diff --git a/libs/community/langchain_community/llms/openai.py b/libs/community/langchain_community/llms/openai.py index 9a3937179c9..8504d532ad2 100644 --- a/libs/community/langchain_community/llms/openai.py +++ b/libs/community/langchain_community/llms/openai.py @@ -924,7 +924,10 @@ class AzureOpenAI(BaseOpenAI): "base_url": values["openai_api_base"], "timeout": values["request_timeout"], "max_retries": values["max_retries"], - "default_headers": values["default_headers"], + "default_headers": { + **(values["default_headers"] or {}), + "User-Agent": "langchain-comm-python-azure-openai", + }, "default_query": values["default_query"], "http_client": values["http_client"], } diff --git a/libs/community/langchain_community/vectorstores/azuresearch.py b/libs/community/langchain_community/vectorstores/azuresearch.py index 9b2d34fbd9a..4ed0d3f4d6d 100644 --- a/libs/community/langchain_community/vectorstores/azuresearch.py +++ b/libs/community/langchain_community/vectorstores/azuresearch.py @@ -94,7 +94,7 @@ def _get_search_client( scoring_profiles: Optional[List[ScoringProfile]] = None, default_scoring_profile: Optional[str] = None, default_fields: Optional[List[SearchField]] = None, - user_agent: Optional[str] = "langchain", + user_agent: Optional[str] = "langchain-comm-python-azure-search", cors_options: Optional[CorsOptions] = None, async_: bool = False, additional_search_client_options: Optional[Dict[str, Any]] = None, diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.py index c2cc41100b6..cb4bc88a382 100644 --- a/libs/partners/openai/langchain_openai/chat_models/azure.py +++ b/libs/partners/openai/langchain_openai/chat_models/azure.py @@ -643,7 +643,10 @@ class AzureChatOpenAI(BaseChatOpenAI): "organization": self.openai_organization, "base_url": self.openai_api_base, "timeout": self.request_timeout, - "default_headers": self.default_headers, + "default_headers": { + **(self.default_headers or {}), + "User-Agent": "langchain-partner-python-azure-openai", + }, "default_query": self.default_query, } if self.max_retries is not None: diff --git a/libs/partners/openai/langchain_openai/embeddings/azure.py b/libs/partners/openai/langchain_openai/embeddings/azure.py index 3a83abc7d39..decc33e9639 100644 --- a/libs/partners/openai/langchain_openai/embeddings/azure.py +++ b/libs/partners/openai/langchain_openai/embeddings/azure.py @@ -198,7 +198,10 @@ class AzureOpenAIEmbeddings(OpenAIEmbeddings): # type: ignore[override] "base_url": self.openai_api_base, "timeout": self.request_timeout, "max_retries": self.max_retries, - "default_headers": self.default_headers, + "default_headers": { + **(self.default_headers or {}), + "User-Agent": "langchain-partner-python-azure-openai", + }, "default_query": self.default_query, } if not self.client: diff --git a/libs/partners/openai/langchain_openai/llms/azure.py b/libs/partners/openai/langchain_openai/llms/azure.py index 731b3e567f0..05e90d0b975 100644 --- a/libs/partners/openai/langchain_openai/llms/azure.py +++ b/libs/partners/openai/langchain_openai/llms/azure.py @@ -153,7 +153,10 @@ class AzureOpenAI(BaseOpenAI): "base_url": self.openai_api_base, "timeout": self.request_timeout, "max_retries": self.max_retries, - "default_headers": self.default_headers, + "default_headers": { + **(self.default_headers or {}), + "User-Agent": "langchain-partner-python-azure-openai", + }, "default_query": self.default_query, } if not self.client: