Skip to content

Commit 57b55af

Browse files
Python: (Astra DB) Include caller identity as user-agent when issuing HTTP request to Astra DB's Data API (microsoft#5921)
### Motivation and Context This PR enriches the HTTP requests to the Astra DB Data API (occurring in the AstraClient) with a User-Agent header specifying the identity of the caller (specifically, "semantic_kernel"). While not mandatory, this is good practice with Astra DB and helps collecting aggregate usage data on the API side. ### Description Using the `importlib.metadata` library the package version is extracted (in a fail-safe way) and used, if present, to enrich the caller information (variable `ASTRA_CALLER_IDENTITY`, later set as the user-agent header for all HTTP requests to the Data API). ### Contribution Checklist - [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 😄 --------- Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
1 parent c84258a commit 57b55af

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎python/.env.example‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ AZCOSMOS_API = "" // should be mongo-vcore for now, as CosmosDB only supports ve
4141
AZCOSMOS_CONNSTR = ""
4242
AZCOSMOS_DATABASE_NAME = ""
4343
AZCOSMOS_CONTAINER_NAME = ""
44-
ASTRADB_APP_TOKEN="" // Starts with AstraCS:
44+
# Starts with AstraCS:
45+
ASTRADB_APP_TOKEN=""
4546
ASTRADB_ID=""
4647
ASTRADB_REGION=""
4748
ASTRADB_KEYSPACE=""

‎python/semantic_kernel/connectors/memory/astradb/astra_client.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
import aiohttp
55

66
from semantic_kernel.connectors.memory.astradb.utils import AsyncSession
7+
from semantic_kernel.connectors.telemetry import APP_INFO
78
from semantic_kernel.exceptions import ServiceResponseException
89

10+
ASTRA_CALLER_IDENTITY: str
11+
SEMANTIC_KERNEL_VERSION = APP_INFO.get("Semantic-Kernel-Version")
12+
if SEMANTIC_KERNEL_VERSION:
13+
ASTRA_CALLER_IDENTITY = f"semantic-kernel/{SEMANTIC_KERNEL_VERSION}"
14+
else:
15+
ASTRA_CALLER_IDENTITY = "semantic-kernel"
16+
917

1018
class AstraClient:
1119
def __init__(
@@ -31,6 +39,7 @@ def __init__(
3139
self.request_header = {
3240
"x-cassandra-token": self.astra_application_token,
3341
"Content-Type": "application/json",
42+
"User-Agent": ASTRA_CALLER_IDENTITY,
3443
}
3544
self._session = session
3645

‎python/tests/integration/connectors/memory/test_astradb.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import pytest
77

8-
import semantic_kernel as sk
98
from semantic_kernel.connectors.memory.astradb import AstraDBMemoryStore
9+
from semantic_kernel.utils.settings import astradb_settings_from_dot_env
1010

1111
astradb_installed: bool
1212
try:
@@ -43,7 +43,7 @@ def get_astradb_config():
4343
keyspace = os.environ["ASTRADB_KEYSPACE"]
4444
else:
4545
# Load credentials from .env file
46-
app_token, db_id, region, keyspace = sk.astradb_settings_from_dot_env()
46+
app_token, db_id, region, keyspace = astradb_settings_from_dot_env()
4747

4848
return app_token, db_id, region, keyspace
4949

0 commit comments

Comments
 (0)