Skip to content

Commit 35e69fc

Browse files
Python: Naming updates (#12233)
### 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. --> This pull request introduces several updates across multiple files to standardize naming conventions, improve consistency in method calls, and modernize data model definitions. The primary changes include renaming key classes and methods, replacing deprecated fields with updated ones, and ensuring uniformity in collection management operations. ### Standardization of Data Model Fields: * Replaced `VectorStoreRecordDataField`, `VectorStoreRecordKeyField`, and `VectorStoreRecordVectorField` with `VectorStoreDataField`, `VectorStoreKeyField`, and `VectorStoreVectorField` respectively across all relevant files. [[1]](diffhunk://#diff-df5224582cd487ed94306045bd09dcd32ccbd9b3f82a63184d037ebe02d34b17R16-R19) [[2]](diffhunk://#diff-bff31c838fdfdb680d4dbdd3a24764dfd8a316b89b7d2ea466234edb98b68e9dL10-L18) [[3]](diffhunk://#diff-af84e00363a79d8bd2f3b72976ecc95c0744fcdf89930ac9b058bfd27b27b83eL18-R18) [[4]](diffhunk://#diff-c7a1b7cdeb95d9cdf398adc0cead91e30fcee1c6abff046f2933f0ac361d3982L30-R36) [[5]](diffhunk://#diff-4c45e261749985abb60b9bd147b2bd2cb91fdfda869190da1f56c89032667eefL11-R14) * Updated data models in `semantic_caching.py`, `store_chat_history_in_cosmosdb.py`, and other files to use the new field types for consistency. [[1]](diffhunk://#diff-df5224582cd487ed94306045bd09dcd32ccbd9b3f82a63184d037ebe02d34b17L35-R37) [[2]](diffhunk://#diff-bff31c838fdfdb680d4dbdd3a24764dfd8a316b89b7d2ea466234edb98b68e9dL42-R44) [[3]](diffhunk://#diff-af84e00363a79d8bd2f3b72976ecc95c0744fcdf89930ac9b058bfd27b27b83eL63-R80) ### Renaming and Refactoring Collection Management: * Renamed `create_collection_if_not_exists` to `ensure_collection_exists` and `delete_collection` to `ensure_collection_deleted` for clarity and consistency. [[1]](diffhunk://#diff-df5224582cd487ed94306045bd09dcd32ccbd9b3f82a63184d037ebe02d34b17L68-R68) [[2]](diffhunk://#diff-bff31c838fdfdb680d4dbdd3a24764dfd8a316b89b7d2ea466234edb98b68e9dL194-R194) [[3]](diffhunk://#diff-3ad17b6faefe0a6ac2fff5b863169e5aac7ab99c49d4f376ba46f2d0d181b642L67-R67) * Updated method calls in various files to reflect these changes. [[1]](diffhunk://#diff-df5224582cd487ed94306045bd09dcd32ccbd9b3f82a63184d037ebe02d34b17L89-R89) [[2]](diffhunk://#diff-c7a1b7cdeb95d9cdf398adc0cead91e30fcee1c6abff046f2933f0ac361d3982L150-R149) ### Renaming Classes for Improved Clarity: * Renamed `AzureCosmosDBNoSQLStore` to `CosmosNoSqlStore` and `AzureCosmosDBNoSQLCollection` to `CosmosNoSqlCollection`. Similar updates were made to MongoDB-related classes. [[1]](diffhunk://#diff-bff31c838fdfdb680d4dbdd3a24764dfd8a316b89b7d2ea466234edb98b68e9dL178-R178) [[2]](diffhunk://#diff-c7a1b7cdeb95d9cdf398adc0cead91e30fcee1c6abff046f2933f0ac361d3982L16-R18) ### Consistency in Record Type Definitions: * Replaced `data_model_type` with `record_type` in collection initialization across multiple files for uniformity. [[1]](diffhunk://#diff-df5224582cd487ed94306045bd09dcd32ccbd9b3f82a63184d037ebe02d34b17L53-R53) [[2]](diffhunk://#diff-bff31c838fdfdb680d4dbdd3a24764dfd8a316b89b7d2ea466234edb98b68e9dL67-R69) [[3]](diffhunk://#diff-c7a1b7cdeb95d9cdf398adc0cead91e30fcee1c6abff046f2933f0ac361d3982L99-R123) ### Miscellaneous Updates: * Consolidated imports for improved readability and maintainability. [[1]](diffhunk://#diff-df5224582cd487ed94306045bd09dcd32ccbd9b3f82a63184d037ebe02d34b17R16-R19) [[2]](diffhunk://#diff-af84e00363a79d8bd2f3b72976ecc95c0744fcdf89930ac9b058bfd27b27b83eL18-R18) * Adjusted default values and annotations in data models to align with the updated field types. [[1]](diffhunk://#diff-4c45e261749985abb60b9bd147b2bd2cb91fdfda869190da1f56c89032667eefL47-R58) [[2]](diffhunk://#diff-4c45e261749985abb60b9bd147b2bd2cb91fdfda869190da1f56c89032667eefL72-R71) These changes collectively enhance the maintainability and readability of the codebase while ensuring alignment with modern conventions. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Closes: #11938 ### 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 😄
1 parent fcc2d3e commit 35e69fc

File tree

69 files changed

+5604
-5823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+5604
-5823
lines changed

python/pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ ollama = [
117117
"ollama ~= 0.4"
118118
]
119119
onnx = [
120-
"onnxruntime-genai ~= 0.5; python_version < '3.13' and platform_system != 'Windows'",
121-
"onnxruntime == 1.22.0; platform_system == 'Windows'"
120+
"onnxruntime-genai ~= 0.7"
122121
]
123122
pandas = [
124123
"pandas ~= 2.2"

python/samples/concepts/caching/semantic_caching.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from semantic_kernel.data import (
1414
VectorSearchOptions,
1515
VectorStore,
16+
VectorStoreDataField,
17+
VectorStoreKeyField,
1618
VectorStoreRecordCollection,
17-
VectorStoreRecordDataField,
18-
VectorStoreRecordKeyField,
19-
VectorStoreRecordVectorField,
19+
VectorStoreVectorField,
2020
vectorstoremodel,
2121
)
2222
from semantic_kernel.filters import FilterTypes, FunctionInvocationContext, PromptRenderContext
@@ -32,9 +32,9 @@
3232
@vectorstoremodel(collection_name=COLLECTION_NAME)
3333
@dataclass
3434
class CacheRecord:
35-
result: Annotated[str, VectorStoreRecordDataField(is_full_text_indexed=True)]
36-
prompt: Annotated[str | None, VectorStoreRecordVectorField(dimensions=1536)] = None
37-
id: Annotated[str, VectorStoreRecordKeyField] = field(default_factory=lambda: str(uuid4()))
35+
result: Annotated[str, VectorStoreDataField(is_full_text_indexed=True)]
36+
prompt: Annotated[str | None, VectorStoreVectorField(dimensions=1536)] = None
37+
id: Annotated[str, VectorStoreKeyField] = field(default_factory=lambda: str(uuid4()))
3838

3939

4040
# Define the filters, one for caching the results and one for using the cache.
@@ -50,7 +50,7 @@ def __init__(
5050
raise ValueError("The vector store must have an embedding generator.")
5151
self.vector_store = vector_store
5252
self.collection: VectorStoreRecordCollection[str, CacheRecord] = vector_store.get_collection(
53-
data_model_type=CacheRecord
53+
record_type=CacheRecord
5454
)
5555
self.score_threshold = score_threshold
5656

@@ -65,7 +65,7 @@ async def on_prompt_render(
6565
closer the match.
6666
"""
6767
await next(context)
68-
await self.collection.create_collection_if_not_exists()
68+
await self.collection.ensure_collection_exists()
6969
results = await self.collection.search(
7070
context.rendered_prompt, options=VectorSearchOptions(vector_property_name="prompt", top=1)
7171
)
@@ -86,7 +86,7 @@ async def on_function_invocation(
8686
result = context.result
8787
if result and result.rendered_prompt and RECORD_ID_KEY not in result.metadata:
8888
cache_record = CacheRecord(prompt=result.rendered_prompt, result=str(result))
89-
await self.collection.create_collection_if_not_exists()
89+
await self.collection.ensure_collection_exists()
9090
await self.collection.upsert(cache_record)
9191

9292

python/samples/concepts/chat_history/store_chat_history_in_cosmosdb.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
from samples.concepts.setup.chat_completion_services import Services, get_chat_completion_service_and_request_settings
88
from semantic_kernel import Kernel
99
from semantic_kernel.connectors.ai import FunctionChoiceBehavior
10-
from semantic_kernel.connectors.memory.azure_cosmos_db import AzureCosmosDBNoSQLStore
10+
from semantic_kernel.connectors.memory.azure_cosmos_db import CosmosNoSqlStore
1111
from semantic_kernel.contents import ChatHistory, ChatMessageContent
1212
from semantic_kernel.core_plugins.math_plugin import MathPlugin
1313
from semantic_kernel.core_plugins.time_plugin import TimePlugin
1414
from semantic_kernel.data import (
1515
VectorStore,
16+
VectorStoreDataField,
17+
VectorStoreKeyField,
1618
VectorStoreRecordCollection,
17-
VectorStoreRecordDataField,
18-
VectorStoreRecordKeyField,
1919
vectorstoremodel,
2020
)
2121

@@ -39,9 +39,9 @@
3939
@vectorstoremodel
4040
@dataclass
4141
class ChatHistoryModel:
42-
session_id: Annotated[str, VectorStoreRecordKeyField]
43-
user_id: Annotated[str, VectorStoreRecordDataField(is_indexed=True)]
44-
messages: Annotated[list[dict[str, str]], VectorStoreRecordDataField(is_indexed=True)]
42+
session_id: Annotated[str, VectorStoreKeyField]
43+
user_id: Annotated[str, VectorStoreDataField(is_indexed=True)]
44+
messages: Annotated[list[dict[str, str]], VectorStoreDataField(is_indexed=True)]
4545

4646

4747
# 2. We then create a class that extends the ChatHistory class
@@ -64,9 +64,9 @@ async def create_collection(self, collection_name: str) -> None:
6464
"""
6565
self.collection = self.store.get_collection(
6666
collection_name=collection_name,
67-
data_model_type=ChatHistoryModel,
67+
record_type=ChatHistoryModel,
6868
)
69-
await self.collection.create_collection_if_not_exists()
69+
await self.collection.ensure_collection_exists()
7070

7171
async def store_messages(self) -> None:
7272
"""Store the chat history in the Cosmos DB.
@@ -175,7 +175,7 @@ async def main() -> None:
175175

176176
# First we enter the store context manager to connect.
177177
# The create_database flag will create the database if it does not exist.
178-
async with AzureCosmosDBNoSQLStore(create_database=True) as store:
178+
async with CosmosNoSqlStore(create_database=True) as store:
179179
# Then we create the chat history in CosmosDB.
180180
history = ChatHistoryInCosmosDB(store=store, session_id=session_id, user_id="user")
181181
# Finally we create the collection.
@@ -191,7 +191,7 @@ async def main() -> None:
191191
except Exception:
192192
print("Closing chat...")
193193
if delete_when_done and history.collection:
194-
await history.collection.delete_collection()
194+
await history.collection.ensure_collection_deleted()
195195

196196

197197
if __name__ == "__main__":

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
@@ -25,7 +25,7 @@ async def main(query: str):
2525
records = load_records()
2626
# Create the Azure AI Search collection
2727
async with AzureAISearchCollection[str, HotelSampleClass](
28-
data_model_type=HotelSampleClass, embedding_generator=OpenAITextEmbedding()
28+
record_type=HotelSampleClass, embedding_generator=OpenAITextEmbedding()
2929
) as collection:
3030
# Check if the collection exists.
3131
if not await collection.does_collection_exist():
@@ -64,7 +64,7 @@ async def main(query: str):
6464
f"{result.record.Address.Country}): {result.record.Description} (score: {result.score})"
6565
)
6666

67-
await collection.delete_collection()
67+
await collection.ensure_collection_deleted()
6868

6969

7070
if __name__ == "__main__":

python/samples/concepts/memory/azure_ai_search_hotel_samples/2_use_as_a_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
# Create an Azure AI Search collection.
3737
collection = AzureAISearchCollection[str, HotelSampleClass](
38-
data_model_type=HotelSampleClass, embedding_generator=OpenAITextEmbedding()
38+
record_type=HotelSampleClass, embedding_generator=OpenAITextEmbedding()
3939
)
4040
# load the records
4141
records = load_records()
@@ -210,7 +210,7 @@ async def chat():
210210

211211
delete_collection = input("Do you want to delete the collection? (y/n): ")
212212
if delete_collection.lower() == "y":
213-
await collection.delete_collection()
213+
await collection.ensure_collection_deleted()
214214
print("Collection deleted.")
215215
else:
216216
print("Collection not deleted.")

python/samples/concepts/memory/azure_ai_search_hotel_samples/data_model.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
)
1616
from pydantic import BaseModel, ConfigDict
1717

18-
from semantic_kernel.data import (
19-
VectorStoreRecordDataField,
20-
VectorStoreRecordKeyField,
21-
VectorStoreRecordVectorField,
22-
vectorstoremodel,
23-
)
18+
from semantic_kernel.data import VectorStoreDataField, VectorStoreKeyField, VectorStoreVectorField, vectorstoremodel
2419

2520
"""
2621
The data model used for this sample is based on the hotel data model from the Azure AI Search samples.
@@ -60,31 +55,29 @@ class Address(BaseModel):
6055

6156
@vectorstoremodel(collection_name="hotel-index")
6257
class HotelSampleClass(BaseModel):
63-
HotelId: Annotated[str, VectorStoreRecordKeyField]
64-
HotelName: Annotated[str | None, VectorStoreRecordDataField()] = None
58+
HotelId: Annotated[str, VectorStoreKeyField]
59+
HotelName: Annotated[str | None, VectorStoreDataField()] = None
6560
Description: Annotated[
6661
str,
67-
VectorStoreRecordDataField(is_full_text_indexed=True),
62+
VectorStoreDataField(is_full_text_indexed=True),
6863
]
6964
DescriptionVector: Annotated[
7065
list[float] | str | None,
71-
VectorStoreRecordVectorField(dimensions=1536),
66+
VectorStoreVectorField(dimensions=1536),
7267
] = None
73-
Description_fr: Annotated[str, VectorStoreRecordDataField(is_full_text_indexed=True)]
68+
Description_fr: Annotated[str, VectorStoreDataField(is_full_text_indexed=True)]
7469
DescriptionFrVector: Annotated[
7570
list[float] | str | None,
76-
VectorStoreRecordVectorField(dimensions=1536),
77-
] = None
78-
Category: Annotated[str, VectorStoreRecordDataField()]
79-
Tags: Annotated[list[str], VectorStoreRecordDataField(is_indexed=True)]
80-
ParkingIncluded: Annotated[bool | None, VectorStoreRecordDataField()] = None
81-
LastRenovationDate: Annotated[
82-
str | None, VectorStoreRecordDataField(property_type=SearchFieldDataType.DateTimeOffset)
71+
VectorStoreVectorField(dimensions=1536),
8372
] = None
84-
Rating: Annotated[float, VectorStoreRecordDataField()]
85-
Location: Annotated[dict[str, Any], VectorStoreRecordDataField(property_type=SearchFieldDataType.GeographyPoint)]
86-
Address: Annotated[Address, VectorStoreRecordDataField()]
87-
Rooms: Annotated[list[Rooms], VectorStoreRecordDataField()]
73+
Category: Annotated[str, VectorStoreDataField()]
74+
Tags: Annotated[list[str], VectorStoreDataField(is_indexed=True)]
75+
ParkingIncluded: Annotated[bool | None, VectorStoreDataField()] = None
76+
LastRenovationDate: Annotated[str | None, VectorStoreDataField(type=SearchFieldDataType.DateTimeOffset)] = None
77+
Rating: Annotated[float, VectorStoreDataField()]
78+
Location: Annotated[dict[str, Any], VectorStoreDataField(type=SearchFieldDataType.GeographyPoint)]
79+
Address: Annotated[Address, VectorStoreDataField()]
80+
Rooms: Annotated[list[Rooms], VectorStoreDataField()]
8881

8982
model_config = ConfigDict(extra="ignore")
9083

python/samples/concepts/memory/complex_memory.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from semantic_kernel.connectors.ai.open_ai import AzureTextEmbedding, OpenAITextEmbedding
1414
from semantic_kernel.connectors.memory import (
1515
AzureAISearchCollection,
16-
AzureCosmosDBforMongoDBCollection,
17-
AzureCosmosDBNoSQLCollection,
1816
ChromaCollection,
17+
CosmosMongoCollection,
18+
CosmosNoSqlCollection,
1919
FaissCollection,
2020
InMemoryCollection,
2121
PineconeCollection,
@@ -27,14 +27,13 @@
2727
WeaviateCollection,
2828
)
2929
from semantic_kernel.data import (
30-
VectorSearch,
30+
VectorStoreDataField,
31+
VectorStoreKeyField,
3132
VectorStoreRecordCollection,
32-
VectorStoreRecordDataField,
33-
VectorStoreRecordKeyField,
34-
VectorStoreRecordVectorField,
33+
VectorStoreVectorField,
3534
vectorstoremodel,
3635
)
37-
from semantic_kernel.data.vector_search import SearchType
36+
from semantic_kernel.data.vectors import SearchType, VectorSearch
3837

3938
# This is a rather complex sample, showing how to use the vector store
4039
# with a number of different collections.
@@ -49,14 +48,14 @@
4948
@vectorstoremodel(collection_name="test")
5049
@dataclass
5150
class DataModel:
52-
title: Annotated[str, VectorStoreRecordDataField(is_full_text_indexed=True)]
53-
content: Annotated[str, VectorStoreRecordDataField(is_full_text_indexed=True)]
51+
title: Annotated[str, VectorStoreDataField(is_full_text_indexed=True)]
52+
content: Annotated[str, VectorStoreDataField(is_full_text_indexed=True)]
5453
embedding: Annotated[
5554
str | None,
56-
VectorStoreRecordVectorField(dimensions=1536, property_type="float"),
55+
VectorStoreVectorField(dimensions=1536, type_="float"),
5756
] = None
58-
id: Annotated[str, VectorStoreRecordKeyField()] = field(default_factory=lambda: str(uuid4()))
59-
tag: Annotated[str | None, VectorStoreRecordDataField(property_type="str", is_indexed=True)] = None
57+
id: Annotated[str, VectorStoreKeyField()] = field(default_factory=lambda: str(uuid4()))
58+
tag: Annotated[str | None, VectorStoreDataField(type_="str", is_indexed=True)] = None
6059

6160
def __post_init__(self, **kwargs):
6261
if self.embedding is None:
@@ -96,32 +95,32 @@ def __post_init__(self, **kwargs):
9695
# Using a function allows for lazy initialization of the collection,
9796
# so that settings for unused collections do not cause validation errors.
9897
collections: dict[str, Callable[[], VectorStoreRecordCollection]] = {
99-
"ai_search": lambda: AzureAISearchCollection[str, DataModel](data_model_type=DataModel),
100-
"postgres": lambda: PostgresCollection[str, DataModel](data_model_type=DataModel),
98+
"ai_search": lambda: AzureAISearchCollection[str, DataModel](record_type=DataModel),
99+
"postgres": lambda: PostgresCollection[str, DataModel](record_type=DataModel),
101100
"redis_json": lambda: RedisJsonCollection[str, DataModel](
102-
data_model_type=DataModel,
101+
record_type=DataModel,
103102
prefix_collection_name_to_key_names=True,
104103
),
105104
"redis_hash": lambda: RedisHashsetCollection[str, DataModel](
106-
data_model_type=DataModel,
105+
record_type=DataModel,
107106
prefix_collection_name_to_key_names=True,
108107
),
109108
"qdrant": lambda: QdrantCollection[str, DataModel](
110-
data_model_type=DataModel,
109+
record_type=DataModel,
111110
prefer_grpc=True,
112111
named_vectors=False,
113112
),
114-
"in_memory": lambda: InMemoryCollection[str, DataModel](data_model_type=DataModel),
115-
"weaviate": lambda: WeaviateCollection[str, DataModel](data_model_type=DataModel),
116-
"azure_cosmos_nosql": lambda: AzureCosmosDBNoSQLCollection[str, DataModel](
117-
data_model_type=DataModel,
113+
"in_memory": lambda: InMemoryCollection[str, DataModel](record_type=DataModel),
114+
"weaviate": lambda: WeaviateCollection[str, DataModel](record_type=DataModel),
115+
"azure_cosmos_nosql": lambda: CosmosNoSqlCollection[str, DataModel](
116+
record_type=DataModel,
118117
create_database=True,
119118
),
120-
"azure_cosmos_mongodb": lambda: AzureCosmosDBforMongoDBCollection[str, DataModel](data_model_type=DataModel),
121-
"faiss": lambda: FaissCollection[str, DataModel](data_model_type=DataModel),
122-
"chroma": lambda: ChromaCollection[str, DataModel](data_model_type=DataModel),
123-
"pinecone": lambda: PineconeCollection[str, DataModel](data_model_type=DataModel),
124-
"sql_server": lambda: SqlServerCollection[str, DataModel](data_model_type=DataModel),
119+
"azure_cosmos_mongodb": lambda: CosmosMongoCollection[str, DataModel](record_type=DataModel),
120+
"faiss": lambda: FaissCollection[str, DataModel](record_type=DataModel),
121+
"chroma": lambda: ChromaCollection[str, DataModel](record_type=DataModel),
122+
"pinecone": lambda: PineconeCollection[str, DataModel](record_type=DataModel),
123+
"sql_server": lambda: SqlServerCollection[str, DataModel](record_type=DataModel),
125124
}
126125

127126

@@ -132,7 +131,7 @@ async def cleanup(record_collection):
132131
print("Skipping deletion.")
133132
return
134133
print_with_color("Deleting collection!", Colors.CBLUE)
135-
await record_collection.delete_collection()
134+
await record_collection.ensure_collection_deleted()
136135
print_with_color("Done!", Colors.CGREY)
137136

138137

@@ -147,7 +146,7 @@ async def main(collection: str, use_azure_openai: bool):
147146
record_collection.embedding_generator = embedder
148147
print_with_color(f"Creating {collection} collection!", Colors.CGREY)
149148
# cleanup any existing collection
150-
await record_collection.delete_collection()
149+
await record_collection.ensure_collection_deleted()
151150
# create a new collection
152151
await record_collection.create_collection()
153152

0 commit comments

Comments
 (0)