2
2
3
3
import asyncio
4
4
5
+ from step_0_data_model import HotelSampleClass
6
+
7
+ from semantic_kernel import Kernel
8
+ from semantic_kernel .connectors .ai .open_ai import OpenAITextEmbedding
9
+ from semantic_kernel .connectors .memory .azure_ai_search import AzureAISearchCollection
10
+
5
11
###
6
12
# The data model used for this sample is based on the hotel data model from the Azure AI Search samples.
7
13
# When deploying a new index in Azure AI Search using the import wizard you can choose to deploy the 'hotel-samples'
13
19
# This sample assumes the index is deployed, the vector fields can be empty.
14
20
# If the vector fields are empty, change the first_run parameter to True to add the vectors.
15
21
###
16
- from step_0_data_model import HotelSampleClass
17
-
18
- from semantic_kernel import Kernel
19
- from semantic_kernel .connectors .ai .open_ai import OpenAITextEmbedding
20
- from semantic_kernel .connectors .memory .azure_ai_search import AzureAISearchCollection
21
22
from semantic_kernel .data import (
22
23
VectorSearchOptions ,
23
- VectorStoreRecordUtils ,
24
24
)
25
+ from semantic_kernel .data .vector_search import add_vector_to_records
25
26
26
27
first_run = False
27
28
28
29
# Note: you may need to update this `collection_name` depending upon how your index is named.
29
30
COLLECTION_NAME = "hotels-sample-index"
30
31
31
32
32
- async def add_vectors (collection : AzureAISearchCollection , vectorizer : VectorStoreRecordUtils ):
33
- """This is a simple function that uses the VectorStoreRecordUtils to add vectors to the records in the collection .
33
+ async def add_vectors (collection : AzureAISearchCollection , kernel : Kernel ):
34
+ """This is a simple function that uses the add_vector_to_records function to add vectors.
34
35
35
36
It first uses the search_client within the collection to get a list of ids.
36
37
and then uses the upsert to add the vectors to the records.
@@ -42,7 +43,7 @@ async def add_vectors(collection: AzureAISearchCollection, vectorizer: VectorSto
42
43
if hotels is not None and isinstance (hotels , list ):
43
44
for hotel in hotels :
44
45
if not hotel .description_vector or not hotel .description_fr_vector :
45
- hotel = await vectorizer . add_vector_to_records (hotel , HotelSampleClass )
46
+ hotel = await add_vector_to_records (kernel , hotel , HotelSampleClass )
46
47
await collection .upsert (hotel )
47
48
48
49
@@ -52,10 +53,8 @@ async def main(query: str, first_run: bool = False):
52
53
# Add the OpenAI text embedding service
53
54
embeddings = OpenAITextEmbedding (service_id = "embedding" , ai_model_id = "text-embedding-3-small" )
54
55
kernel .add_service (embeddings )
55
- # Create the VectorStoreRecordUtils object
56
- vectorizer = VectorStoreRecordUtils (kernel )
57
56
# Create the Azure AI Search collection
58
- collection = AzureAISearchCollection [HotelSampleClass ](
57
+ collection = AzureAISearchCollection [str , HotelSampleClass ](
59
58
collection_name = COLLECTION_NAME , data_model_type = HotelSampleClass
60
59
)
61
60
# Check if the collection exists.
@@ -71,7 +70,7 @@ async def main(query: str, first_run: bool = False):
71
70
72
71
# If it is the first run and there are no vectors, add them.
73
72
if first_run :
74
- await add_vectors (collection , vectorizer )
73
+ await add_vectors (collection , kernel )
75
74
76
75
# Search using just text, by default this will search all the searchable text fields in the index.
77
76
results = await collection .text_search (search_text = query )
0 commit comments