mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
Python: preprend 'Semantic-Kernel' to User-Agent, and add version key-value pair to headers (#5565)
### Motivation and Context
The SK Python custom headers to track the USER_AGENT and the Python
version were getting stuck into the default headers under the USER_AGENT
key. Instead, they need to be represented like:
```
{
"UserAgent": HTTP_USER_AGENT,
"Semantic-Kernel-Version": f"python-{version_info}",
}
```
Trying to extract the Python version from telemetry data was not working
as expected.
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
1. Why is this change required?
2. What problem does it solve?
3. What scenario does it contribute to?
4. If it fixes an open issue, please link to the issue here.
-->
### Description
Use the APP_INFO dict as-is, and stick the key-value pairs into the
default_headers.
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Awaitable, Callable, Dict, Mapping, Optional, Union
|
||||
|
||||
@@ -15,7 +14,7 @@ from semantic_kernel.connectors.ai.open_ai.services.open_ai_handler import (
|
||||
OpenAIHandler,
|
||||
OpenAIModelTypes,
|
||||
)
|
||||
from semantic_kernel.connectors.telemetry import APP_INFO
|
||||
from semantic_kernel.connectors.telemetry import APP_INFO, prepend_semantic_kernel_to_user_agent
|
||||
from semantic_kernel.exceptions import ServiceInitializationError
|
||||
from semantic_kernel.kernel_pydantic import HttpsUrl
|
||||
|
||||
@@ -61,7 +60,8 @@ class AzureOpenAIConfigBase(OpenAIHandler):
|
||||
# Merge APP_INFO into the headers if it exists
|
||||
merged_headers = default_headers.copy() if default_headers else {}
|
||||
if APP_INFO:
|
||||
merged_headers[USER_AGENT] = json.dumps(APP_INFO)
|
||||
merged_headers.update(APP_INFO)
|
||||
merged_headers = prepend_semantic_kernel_to_user_agent(merged_headers)
|
||||
|
||||
if not async_client:
|
||||
if not api_key and not ad_token and not ad_token_provider:
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Dict, Mapping, Optional
|
||||
|
||||
@@ -10,7 +9,7 @@ from pydantic import Field, validate_call
|
||||
from semantic_kernel.connectors.ai.open_ai.const import USER_AGENT
|
||||
from semantic_kernel.connectors.ai.open_ai.services.open_ai_handler import OpenAIHandler
|
||||
from semantic_kernel.connectors.ai.open_ai.services.open_ai_model_types import OpenAIModelTypes
|
||||
from semantic_kernel.connectors.telemetry import APP_INFO
|
||||
from semantic_kernel.connectors.telemetry import APP_INFO, prepend_semantic_kernel_to_user_agent
|
||||
from semantic_kernel.exceptions import ServiceInitializationError
|
||||
|
||||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
@@ -49,7 +48,8 @@ class OpenAIConfigBase(OpenAIHandler):
|
||||
# Merge APP_INFO into the headers if it exists
|
||||
merged_headers = default_headers.copy() if default_headers else {}
|
||||
if APP_INFO:
|
||||
merged_headers[USER_AGENT] = json.dumps(APP_INFO)
|
||||
merged_headers.update(APP_INFO)
|
||||
merged_headers = prepend_semantic_kernel_to_user_agent(merged_headers)
|
||||
|
||||
if not async_client:
|
||||
if not api_key:
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
import os
|
||||
from importlib.metadata import PackageNotFoundError, version
|
||||
from typing import Any, Dict
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai.const import USER_AGENT
|
||||
|
||||
TELEMETRY_DISABLED_ENV_VAR = "AZURE_TELEMETRY_DISABLED"
|
||||
|
||||
@@ -16,9 +19,24 @@ except PackageNotFoundError:
|
||||
|
||||
APP_INFO = (
|
||||
{
|
||||
"UserAgent": HTTP_USER_AGENT,
|
||||
"Semantic-Kernel-Version": f"python-{version_info}",
|
||||
}
|
||||
if IS_TELEMETRY_ENABLED
|
||||
else None
|
||||
)
|
||||
|
||||
|
||||
def prepend_semantic_kernel_to_user_agent(headers: Dict[str, Any]):
|
||||
"""
|
||||
Prepend "Semantic-Kernel" to the User-Agent in the headers.
|
||||
|
||||
Args:
|
||||
headers: The existing headers dictionary.
|
||||
|
||||
Returns:
|
||||
The modified headers dictionary with "Semantic-Kernel" prepended to the User-Agent.
|
||||
"""
|
||||
|
||||
headers[USER_AGENT] = f"{HTTP_USER_AGENT} {headers[USER_AGENT]}" if USER_AGENT in headers else f"{HTTP_USER_AGENT}"
|
||||
|
||||
return headers
|
||||
|
||||
Reference in New Issue
Block a user