From 9c505006b3a9ab5eecddaaf76e3f8bacc3a49a58 Mon Sep 17 00:00:00 2001 From: alex cai Date: Wed, 13 Aug 2025 03:11:02 +0000 Subject: [PATCH 1/3] feat(vec-db/qdrant): add optional api_key support --- src/memos/configs/vec_db.py | 1 + src/memos/vec_dbs/qdrant.py | 12 +++++++++--- tests/configs/test_vec_db.py | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/memos/configs/vec_db.py b/src/memos/configs/vec_db.py index b43298d9..ac06197a 100644 --- a/src/memos/configs/vec_db.py +++ b/src/memos/configs/vec_db.py @@ -27,6 +27,7 @@ class QdrantVecDBConfig(BaseVecDBConfig): host: str | None = Field(default=None, description="Host for Qdrant") port: int | None = Field(default=None, description="Port for Qdrant") path: str | None = Field(default=None, description="Path for Qdrant") + api_key: str | None = Field(default=None, description="Optional API key for Qdrant authentication") @model_validator(mode="after") def set_default_path(self): diff --git a/src/memos/vec_dbs/qdrant.py b/src/memos/vec_dbs/qdrant.py index a0ebf1d8..b8ca7c8d 100644 --- a/src/memos/vec_dbs/qdrant.py +++ b/src/memos/vec_dbs/qdrant.py @@ -33,9 +33,15 @@ def __init__(self, config: QdrantVecDBConfig): "(e.g., via Docker: https://qdrant.tech/documentation/quickstart/)." ) - self.client = QdrantClient( - host=self.config.host, port=self.config.port, path=self.config.path - ) + client_kwargs = { + "host": self.config.host, + "port": self.config.port, + "path": self.config.path, + } + if self.config.api_key: + client_kwargs["api_key"] = self.config.api_key + + self.client = QdrantClient(**client_kwargs) self.create_collection() def create_collection(self) -> None: diff --git a/tests/configs/test_vec_db.py b/tests/configs/test_vec_db.py index b41e775a..ec739460 100644 --- a/tests/configs/test_vec_db.py +++ b/tests/configs/test_vec_db.py @@ -40,7 +40,7 @@ def test_qdrant_vec_db_config(): required_fields=[ "collection_name", ], - optional_fields=["vector_dimension", "distance_metric", "host", "port", "path"], + optional_fields=["vector_dimension", "distance_metric", "host", "port", "path", "api_key"], ) check_config_instantiation_valid( From bdfb73c767838bd1350c47219cae60183c8a213e Mon Sep 17 00:00:00 2001 From: alex cai Date: Wed, 13 Aug 2025 03:34:40 +0000 Subject: [PATCH 2/3] feat(vec-db/qdrant): add 'test_qdrant.py::test_client_receives_api_key' --- tests/vec_dbs/test_qdrant.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/vec_dbs/test_qdrant.py b/tests/vec_dbs/test_qdrant.py index 828240ae..3f9a540b 100644 --- a/tests/vec_dbs/test_qdrant.py +++ b/tests/vec_dbs/test_qdrant.py @@ -113,3 +113,31 @@ def test_get_all(vec_db): results = vec_db.get_all() assert len(results) == 1 assert isinstance(results[0], VecDBItem) + + +def test_client_receives_api_key(): + from unittest.mock import patch + from memos import settings + from memos.configs.vec_db import VectorDBConfigFactory + from memos.vec_dbs.factory import VecDBFactory + + api_key = "your_secure_api_key_here_change_in_production" + with patch("qdrant_client.QdrantClient") as mock_client: + cfg = VectorDBConfigFactory.model_validate( + { + "backend": "qdrant", + "config": { + "collection_name": "test_collection", + "vector_dimension": 4, + "distance_metric": "cosine", + "path": str(settings.MEMOS_DIR / "qdrant"), + "api_key": api_key, + }, + } + ) + _ = VecDBFactory.from_config(cfg) + + # 断言 QdrantClient 被以 api_key 关键字参数调用 + assert mock_client.called + kwargs = mock_client.call_args.kwargs + assert kwargs.get("api_key") == api_key From aeb2fd93bfc8a42a1a70976ae5d59c8b54cc58a1 Mon Sep 17 00:00:00 2001 From: alex-16rd Date: Mon, 1 Sep 2025 15:11:19 +0800 Subject: [PATCH 3/3] Update test_qdrant.py Use English for All Comments --- tests/vec_dbs/test_qdrant.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/vec_dbs/test_qdrant.py b/tests/vec_dbs/test_qdrant.py index 3f9a540b..519298c8 100644 --- a/tests/vec_dbs/test_qdrant.py +++ b/tests/vec_dbs/test_qdrant.py @@ -137,7 +137,7 @@ def test_client_receives_api_key(): ) _ = VecDBFactory.from_config(cfg) - # 断言 QdrantClient 被以 api_key 关键字参数调用 + # Assert that QdrantClient was called with api_key keyword argument assert mock_client.called kwargs = mock_client.call_args.kwargs assert kwargs.get("api_key") == api_key