Files
semantic-kernel/python/semantic_kernel/connectors/telemetry.py
T
Eduard van Valkenburg 81cdde24fc Python: upgraded all files to 310 plus format and removed from future (#6353)
### Motivation and Context

<!-- 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.
-->
Now that we only support 310 and up, we could unify all files, to use
the new style typing and annotations.

### Description

<!-- 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 😄
2024-05-21 16:33:08 +00:00

43 lines
1.1 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import os
from importlib.metadata import PackageNotFoundError, version
from typing import Any
from semantic_kernel.connectors.ai.open_ai.const import USER_AGENT
TELEMETRY_DISABLED_ENV_VAR = "AZURE_TELEMETRY_DISABLED"
IS_TELEMETRY_ENABLED = os.environ.get(TELEMETRY_DISABLED_ENV_VAR, "false").lower() not in ["true", "1"]
HTTP_USER_AGENT = "Semantic-Kernel"
try:
version_info = version("semantic-kernel")
except PackageNotFoundError:
version_info = "dev"
APP_INFO = (
{
"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