Skip to content

Commit fa752f3

Browse files
updated method names
1 parent 194d19b commit fa752f3

24 files changed

+55
-80
lines changed

python/samples/concepts/memory/azure_ai_search_hotel_samples/1_interact_with_the_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ async def main(query: str):
2828
record_type=HotelSampleClass, embedding_generator=OpenAITextEmbedding()
2929
) as collection:
3030
# Check if the collection exists.
31-
if not await collection.does_collection_exist():
32-
await collection.create_collection(index=custom_index)
31+
if not await collection.collection_exists():
32+
await collection.ensure_collection_exists(index=custom_index)
3333
await collection.upsert(records)
3434
# get the first five records to check the upsert worked.
3535
results = await collection.get(order_by="HotelName", top=5)

python/samples/concepts/memory/complex_memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def main(collection: str, use_azure_openai: bool):
153153
# cleanup any existing collection
154154
await record_collection.ensure_collection_deleted()
155155
# create a new collection
156-
await record_collection.create_collection()
156+
await record_collection.ensure_collection_exists()
157157

158158
record1 = DataModel(
159159
content="Semantic Kernel is awesome",

python/samples/concepts/rag/self_critique_rag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def main() -> None:
5555

5656
print("Creating index for memory...")
5757
await collection.ensure_collection_deleted()
58-
await collection.create_collection()
58+
await collection.ensure_collection_exists()
5959

6060
print("Populating memory...")
6161
# Add information to the collection

python/semantic_kernel/connectors/azure_ai_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def _deserialize_store_models_to_dicts(self, records: Sequence[Any], **kwargs: A
463463
return records
464464

465465
@override
466-
async def create_collection(self, **kwargs) -> None:
466+
async def ensure_collection_exists(self, **kwargs) -> None:
467467
"""Create a new collection in Azure AI Search.
468468
469469
Args:
@@ -489,7 +489,7 @@ async def create_collection(self, **kwargs) -> None:
489489
)
490490

491491
@override
492-
async def does_collection_exist(self, **kwargs) -> bool:
492+
async def collection_exists(self, **kwargs) -> bool:
493493
if "params" not in kwargs:
494494
kwargs["params"] = {"select": ["name"]}
495495
return self.collection_name in [

python/semantic_kernel/connectors/azure_cosmos_db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def __init__(
360360
)
361361

362362
@override
363-
async def create_collection(self, **kwargs) -> None:
363+
async def ensure_collection_exists(self, **kwargs) -> None:
364364
"""Create a new collection in Azure CosmosDB for MongoDB.
365365
366366
This first creates a collection, with the kwargs.
@@ -965,7 +965,7 @@ def _deserialize_store_models_to_dicts(self, records: Sequence[Any], **kwargs: A
965965
return deserialized_records
966966

967967
@override
968-
async def create_collection(self, **kwargs) -> None:
968+
async def ensure_collection_exists(self, **kwargs) -> None:
969969
indexing_policy = kwargs.pop("indexing_policy", _create_default_indexing_policy_nosql(self.definition))
970970
vector_embedding_policy = kwargs.pop(
971971
"vector_embedding_policy", _create_default_vector_embedding_policy(self.definition)
@@ -983,7 +983,7 @@ async def create_collection(self, **kwargs) -> None:
983983
raise VectorStoreOperationException("Failed to create container.") from e
984984

985985
@override
986-
async def does_collection_exist(self, **kwargs) -> bool:
986+
async def collection_exists(self, **kwargs) -> bool:
987987
container_proxy = await self._get_container_proxy(self.collection_name, **kwargs)
988988
try:
989989
await container_proxy.read(**kwargs)

python/semantic_kernel/connectors/chroma.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _get_collection(self) -> Collection:
127127
raise RuntimeError(f"Failed to get collection {self.collection_name}") from e
128128

129129
@override
130-
async def does_collection_exist(self, **kwargs: Any) -> bool:
130+
async def collection_exists(self, **kwargs: Any) -> bool:
131131
"""Check if the collection exists."""
132132
try:
133133
self.client.get_collection(name=self.collection_name, embedding_function=self.embedding_func)
@@ -136,7 +136,7 @@ async def does_collection_exist(self, **kwargs: Any) -> bool:
136136
return False
137137

138138
@override
139-
async def create_collection(self, **kwargs: Any) -> None:
139+
async def ensure_collection_exists(self, **kwargs: Any) -> None:
140140
"""Create the collection.
141141
142142
Will create a metadata object with the hnsw arguments.

python/semantic_kernel/connectors/faiss.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _create_indexes(self, index: faiss.Index | None = None, indexes: dict[str, f
143143
self.indexes_key_map.setdefault(vector_field.name, {})
144144

145145
@override
146-
async def create_collection(
146+
async def ensure_collection_exists(
147147
self, index: faiss.Index | None = None, indexes: dict[str, faiss.Index] | None = None, **kwargs: Any
148148
) -> None:
149149
"""Create a collection.
@@ -200,7 +200,7 @@ async def ensure_collection_deleted(self, **kwargs: Any) -> None:
200200
await super().ensure_collection_deleted(**kwargs)
201201

202202
@override
203-
async def does_collection_exist(self, **kwargs: Any) -> bool:
203+
async def collection_exists(self, **kwargs: Any) -> bool:
204204
return bool(self.indexes)
205205

206206
@override

python/semantic_kernel/connectors/in_memory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ def _serialize_dicts_to_store_models(self, records: Sequence[dict[str, Any]], **
146146
return records
147147

148148
@override
149-
async def create_collection(self, **kwargs: Any) -> None:
149+
async def ensure_collection_exists(self, **kwargs: Any) -> None:
150150
pass
151151

152152
@override
153153
async def ensure_collection_deleted(self, **kwargs: Any) -> None:
154154
self.inner_storage = {}
155155

156156
@override
157-
async def does_collection_exist(self, **kwargs: Any) -> bool:
157+
async def collection_exists(self, **kwargs: Any) -> bool:
158158
return True
159159

160160
@override

python/semantic_kernel/connectors/mongodb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def _deserialize_store_models_to_dicts(self, records: Sequence[Any], **kwargs: A
315315
return [self._reset_key_field(record) for record in records]
316316

317317
@override
318-
async def create_collection(self, **kwargs) -> None:
318+
async def ensure_collection_exists(self, **kwargs) -> None:
319319
"""Create a new collection in MongoDB.
320320
321321
This first creates a collection, with the kwargs.
@@ -328,7 +328,7 @@ async def create_collection(self, **kwargs) -> None:
328328
await collection.create_search_indexes(models=_create_index_definitions(self.definition, self.index_name))
329329

330330
@override
331-
async def does_collection_exist(self, **kwargs) -> bool:
331+
async def collection_exists(self, **kwargs) -> bool:
332332
return bool(await self._get_database().list_collection_names(filter={"name": self.collection_name}))
333333

334334
@override

python/semantic_kernel/connectors/pinecone.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _validate_data_model(self):
187187
)
188188

189189
@override
190-
async def create_collection(self, **kwargs: Any) -> None:
190+
async def ensure_collection_exists(self, **kwargs: Any) -> None:
191191
"""Create the Pinecone collection.
192192
193193
Args:
@@ -292,7 +292,7 @@ async def _load_index_client(self) -> None:
292292
)
293293

294294
@override
295-
async def does_collection_exist(self, **kwargs) -> bool:
295+
async def collection_exists(self, **kwargs) -> bool:
296296
"""Check if the Pinecone collection exists."""
297297
exists = (
298298
await self.client.has_index(self.collection_name)
@@ -306,7 +306,7 @@ async def does_collection_exist(self, **kwargs) -> bool:
306306
@override
307307
async def ensure_collection_deleted(self, **kwargs: Any) -> None:
308308
"""Delete the Pinecone collection."""
309-
if not await self.does_collection_exist():
309+
if not await self.collection_exists():
310310
if self.index or self.index_client:
311311
self.index = None
312312
self.index_client = None

python/semantic_kernel/connectors/postgres.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ def _deserialize_store_models_to_dicts(self, records: Sequence[Any], **kwargs: A
556556
return records
557557

558558
@override
559-
async def create_collection(self, **kwargs: Any) -> None:
559+
async def ensure_collection_exists(self, **kwargs: Any) -> None:
560560
"""Create a PostgreSQL table based on a dictionary of VectorStoreRecordField.
561561
562562
Args:
@@ -621,7 +621,7 @@ async def create_collection(self, **kwargs: Any) -> None:
621621
await self._create_index(table_name, vector_field)
622622

623623
@override
624-
async def does_collection_exist(self, **kwargs: Any) -> bool:
624+
async def collection_exists(self, **kwargs: Any) -> bool:
625625
"""Check if the collection exists."""
626626
if self.connection_pool is None:
627627
raise VectorStoreOperationException(

python/semantic_kernel/connectors/qdrant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def _deserialize_store_models_to_dicts(
478478
]
479479

480480
@override
481-
async def create_collection(self, **kwargs) -> None:
481+
async def ensure_collection_exists(self, **kwargs) -> None:
482482
"""Create a new collection in Qdrant.
483483
484484
Args:
@@ -521,7 +521,7 @@ async def create_collection(self, **kwargs) -> None:
521521
await self.qdrant_client.create_collection(**kwargs)
522522

523523
@override
524-
async def does_collection_exist(self, **kwargs) -> bool:
524+
async def collection_exists(self, **kwargs) -> bool:
525525
return await self.qdrant_client.collection_exists(self.collection_name, **kwargs)
526526

527527
@override

python/semantic_kernel/connectors/redis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def _unget_redis_key(self, key: TKey) -> TKey:
258258
return key
259259

260260
@override
261-
async def create_collection(self, **kwargs) -> None:
261+
async def ensure_collection_exists(self, **kwargs) -> None:
262262
"""Create a new index in Redis.
263263
264264
Args:
@@ -283,7 +283,7 @@ async def create_collection(self, **kwargs) -> None:
283283
await self.redis_database.ft(self.collection_name).create_index(fields, definition=index_definition, **kwargs)
284284

285285
@override
286-
async def does_collection_exist(self, **kwargs) -> bool:
286+
async def collection_exists(self, **kwargs) -> bool:
287287
try:
288288
await self.redis_database.ft(self.collection_name).info()
289289
return True
@@ -292,7 +292,7 @@ async def does_collection_exist(self, **kwargs) -> bool:
292292

293293
@override
294294
async def ensure_collection_deleted(self, **kwargs) -> None:
295-
exists = await self.does_collection_exist()
295+
exists = await self.collection_exists()
296296
if exists:
297297
await self.redis_database.ft(self.collection_name).dropindex(**kwargs)
298298
else:

python/semantic_kernel/connectors/sql_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def _deserialize_store_models_to_dicts(self, records: Sequence[Any], **kwargs: A
449449
return records
450450

451451
@override
452-
async def create_collection(
452+
async def ensure_collection_exists(
453453
self, *, create_if_not_exists: bool = True, queries: list[str] | None = None, **kwargs: Any
454454
) -> None:
455455
"""Create a SQL table based on the data model.
@@ -495,7 +495,7 @@ def _get_schema_and_table(self) -> tuple[str, str]:
495495
return schema, table
496496

497497
@override
498-
async def does_collection_exist(self, **kwargs: Any) -> bool:
498+
async def collection_exists(self, **kwargs: Any) -> bool:
499499
"""Check if the collection exists."""
500500
if self.connection is None:
501501
raise VectorStoreOperationException("connection is not available, use the collection as a context manager.")

python/semantic_kernel/connectors/weaviate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def _deserialize_store_models_to_dicts(self, records: Sequence[Any], **kwargs: A
551551
return records_in_dict
552552

553553
@override
554-
async def create_collection(self, **kwargs) -> None:
554+
async def ensure_collection_exists(self, **kwargs) -> None:
555555
"""Create the collection in Weaviate.
556556
557557
Args:
@@ -619,7 +619,7 @@ async def create_collection(self, **kwargs) -> None:
619619
raise VectorStoreOperationException(f"Failed to create collection: {ex}") from ex
620620

621621
@override
622-
async def does_collection_exist(self, **kwargs) -> bool:
622+
async def collection_exists(self, **kwargs) -> bool:
623623
"""Check if the collection exists in Weaviate.
624624
625625
Args:

python/semantic_kernel/data/vector.py

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,37 +1241,12 @@ async def _inner_delete(self, keys: Sequence[TKey], **kwargs: Any) -> None:
12411241
"""
12421242
... # pragma: no cover
12431243

1244-
async def delete_create_collection(self, **kwargs: Any) -> None:
1245-
"""Create the collection in the service, after first trying to delete it.
1246-
1247-
First uses does_collection_exist to check if it exists, if it does deletes it.
1248-
Then, creates the collection.
1249-
1250-
"""
1251-
if await self.does_collection_exist(**kwargs):
1252-
await self.ensure_collection_deleted(**kwargs)
1253-
await self.create_collection(**kwargs)
1254-
1255-
async def ensure_collection_exists(self, **kwargs: Any) -> bool:
1256-
"""Create the collection in the service if it does not exists.
1257-
1258-
First uses does_collection_exist to check if it exists, if it does returns False.
1259-
Otherwise, creates the collection and returns True.
1260-
1261-
Returns:
1262-
bool: True if the collection was created, False if it already exists.
1263-
1264-
"""
1265-
if await self.does_collection_exist(**kwargs):
1266-
return False
1267-
await self.create_collection(**kwargs)
1268-
return True
1269-
12701244
@abstractmethod
1271-
async def create_collection(self, **kwargs: Any) -> None:
1245+
async def ensure_collection_exists(self, **kwargs: Any) -> None:
12721246
"""Create the collection in the service.
12731247
1274-
This should be overridden by the child class.
1248+
This should be overridden by the child class. Should first check if the collection exists,
1249+
if it does not, it should create the collection.
12751250
12761251
Raises:
12771252
Make sure the implementation of this function raises relevant exceptions with good descriptions.
@@ -1281,7 +1256,7 @@ async def create_collection(self, **kwargs: Any) -> None:
12811256
... # pragma: no cover
12821257

12831258
@abstractmethod
1284-
async def does_collection_exist(self, **kwargs: Any) -> bool:
1259+
async def collection_exists(self, **kwargs: Any) -> bool:
12851260
"""Check if the collection exists.
12861261
12871262
This should be overridden by the child class.
@@ -1605,7 +1580,7 @@ async def does_collection_exist(self, collection_name: str) -> bool:
16051580
try:
16061581
data_model = VectorStoreCollectionDefinition(fields=[VectorStoreField("key", name="id")])
16071582
collection = self.get_collection(record_type=dict, definition=data_model, collection_name=collection_name)
1608-
return await collection.does_collection_exist()
1583+
return await collection.collection_exists()
16091584
except VectorStoreOperationException:
16101585
return False
16111586

python/tests/integration/memory/postgres/test_postgres_int.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async def create_simple_collection(
112112
collection_id = f"test_collection_{suffix}"
113113
collection = vector_store.get_collection(collection_name=collection_id, record_type=SimpleDataModel)
114114
assert isinstance(collection, PostgresCollection)
115-
await collection.create_collection()
115+
await collection.ensure_collection_exists()
116116
try:
117117
yield collection
118118
finally:

python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def test_create_collection_calls_database_methods(definition) -> None:
8484
)
8585

8686
# Act
87-
await collection.create_collection(customArg="customValue")
87+
await collection.ensure_collection_exists(customArg="customValue")
8888

8989
# Assert
9090
mock_database.create_collection.assert_awaited_once_with("test_collection", customArg="customValue")

python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_no_sql_collection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async def test_azure_cosmos_db_no_sql_collection_create_collection(
226226

227227
mock_database_proxy.create_container_if_not_exists = AsyncMock(return_value=None)
228228

229-
await vector_collection.create_collection()
229+
await vector_collection.ensure_collection_exists()
230230

231231
mock_database_proxy.create_container_if_not_exists.assert_called_once_with(
232232
id=collection_name,
@@ -256,7 +256,7 @@ async def test_azure_cosmos_db_no_sql_collection_create_collection_allow_custom_
256256

257257
mock_database_proxy.create_container_if_not_exists = AsyncMock(return_value=None)
258258

259-
await vector_collection.create_collection(indexing_policy={"automatic": False})
259+
await vector_collection.ensure_collection_exists(indexing_policy={"automatic": False})
260260

261261
mock_database_proxy.create_container_if_not_exists.assert_called_once_with(
262262
id=collection_name,
@@ -286,7 +286,7 @@ async def test_azure_cosmos_db_no_sql_collection_create_collection_allow_custom_
286286

287287
mock_database_proxy.create_container_if_not_exists = AsyncMock(return_value=None)
288288

289-
await vector_collection.create_collection(vector_embedding_policy={"vectorEmbeddings": []})
289+
await vector_collection.ensure_collection_exists(vector_embedding_policy={"vectorEmbeddings": []})
290290

291291
mock_database_proxy.create_container_if_not_exists.assert_called_once_with(
292292
id=collection_name,
@@ -323,7 +323,7 @@ async def test_azure_cosmos_db_no_sql_collection_create_collection_unsupported_v
323323
mock_database_proxy.create_container_if_not_exists = AsyncMock(return_value=None)
324324

325325
with pytest.raises(VectorStoreModelException):
326-
await vector_collection.create_collection()
326+
await vector_collection.ensure_collection_exists()
327327

328328

329329
@patch("azure.cosmos.aio.DatabaseProxy")

python/tests/unit/connectors/memory/mongodb_atlas/test_mongodb_atlas_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ async def test_mongodb_atlas_collection_collection_exists(mongodb_atlas_unit_tes
9090
)
9191
with patch.object(collection, "_get_database", new=mock_get_database) as mock_get:
9292
mock_get.return_value.list_collection_names.return_value = ["test_collection"]
93-
assert await collection.does_collection_exist()
93+
assert await collection.collection_exists()

0 commit comments

Comments
 (0)