-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
user_agent.py
46 lines (32 loc) · 1.25 KB
/
user_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright (c) Microsoft. All rights reserved.
import os
from importlib.metadata import PackageNotFoundError, version
from typing import Any
from semantic_kernel.const import USER_AGENT
# Note that if this environment variable does not exist, telemetry is enabled.
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-python"
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
)
SEMANTIC_KERNEL_USER_AGENT = f"{HTTP_USER_AGENT}/{version_info}"
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-python/{version}" prepended to the User-Agent.
"""
headers[USER_AGENT] = (
f"{SEMANTIC_KERNEL_USER_AGENT} {headers[USER_AGENT]}" if USER_AGENT in headers else SEMANTIC_KERNEL_USER_AGENT
)
return headers