Skip to content

Commit 121de59

Browse files
marlenezwErick Friis
authored andcommitted
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 <[email protected]>
1 parent 0577fe0 commit 121de59

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

libs/community/langchain_community/chat_models/azure_openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ def validate_environment(cls, values: Dict) -> Dict:
209209
"base_url": values["openai_api_base"],
210210
"timeout": values["request_timeout"],
211211
"max_retries": values["max_retries"],
212-
"default_headers": values["default_headers"],
212+
"default_headers": {
213+
**(values["default_headers"] or {}),
214+
"User-Agent": "langchain-comm-python-azure-openai",
215+
},
213216
"default_query": values["default_query"],
214217
"http_client": values["http_client"],
215218
}

libs/community/langchain_community/embeddings/azure_openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ def post_init_validator(self) -> Self:
163163
"base_url": self.openai_api_base,
164164
"timeout": self.request_timeout,
165165
"max_retries": self.max_retries,
166-
"default_headers": self.default_headers,
166+
"default_headers": {
167+
**(self.default_headers or {}),
168+
"User-Agent": "langchain-comm-python-azure-openai",
169+
},
167170
"default_query": self.default_query,
168171
"http_client": self.http_client,
169172
}

libs/community/langchain_community/llms/openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,10 @@ def validate_environment(cls, values: Dict) -> Dict:
924924
"base_url": values["openai_api_base"],
925925
"timeout": values["request_timeout"],
926926
"max_retries": values["max_retries"],
927-
"default_headers": values["default_headers"],
927+
"default_headers": {
928+
**(values["default_headers"] or {}),
929+
"User-Agent": "langchain-comm-python-azure-openai",
930+
},
928931
"default_query": values["default_query"],
929932
"http_client": values["http_client"],
930933
}

libs/community/langchain_community/vectorstores/azuresearch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _get_search_client(
9494
scoring_profiles: Optional[List[ScoringProfile]] = None,
9595
default_scoring_profile: Optional[str] = None,
9696
default_fields: Optional[List[SearchField]] = None,
97-
user_agent: Optional[str] = "langchain",
97+
user_agent: Optional[str] = "langchain-comm-python-azure-search",
9898
cors_options: Optional[CorsOptions] = None,
9999
async_: bool = False,
100100
additional_search_client_options: Optional[Dict[str, Any]] = None,

libs/partners/openai/langchain_openai/chat_models/azure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,10 @@ def validate_environment(self) -> Self:
643643
"organization": self.openai_organization,
644644
"base_url": self.openai_api_base,
645645
"timeout": self.request_timeout,
646-
"default_headers": self.default_headers,
646+
"default_headers": {
647+
**(self.default_headers or {}),
648+
"User-Agent": "langchain-partner-python-azure-openai",
649+
},
647650
"default_query": self.default_query,
648651
}
649652
if self.max_retries is not None:

libs/partners/openai/langchain_openai/embeddings/azure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ def validate_environment(self) -> Self:
198198
"base_url": self.openai_api_base,
199199
"timeout": self.request_timeout,
200200
"max_retries": self.max_retries,
201-
"default_headers": self.default_headers,
201+
"default_headers": {
202+
**(self.default_headers or {}),
203+
"User-Agent": "langchain-partner-python-azure-openai",
204+
},
202205
"default_query": self.default_query,
203206
}
204207
if not self.client:

libs/partners/openai/langchain_openai/llms/azure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def validate_environment(self) -> Self:
153153
"base_url": self.openai_api_base,
154154
"timeout": self.request_timeout,
155155
"max_retries": self.max_retries,
156-
"default_headers": self.default_headers,
156+
"default_headers": {
157+
**(self.default_headers or {}),
158+
"User-Agent": "langchain-partner-python-azure-openai",
159+
},
157160
"default_query": self.default_query,
158161
}
159162
if not self.client:

0 commit comments

Comments
 (0)