forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_memory.py
350 lines (328 loc) · 15.4 KB
/
complex_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Copyright (c) Microsoft. All rights reserved.
import argparse
import asyncio
from collections.abc import Callable
from dataclasses import dataclass, field
from typing import Annotated, Literal
from uuid import uuid4
import numpy as np
from samples.concepts.memory.utils import print_record
from samples.concepts.resources.utils import Colors, print_with_color
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import (
AzureTextEmbedding,
OpenAIEmbeddingPromptExecutionSettings,
OpenAITextEmbedding,
)
from semantic_kernel.connectors.memory.azure_ai_search import AzureAISearchCollection
from semantic_kernel.connectors.memory.azure_cosmos_db import (
AzureCosmosDBforMongoDBCollection,
AzureCosmosDBNoSQLCollection,
)
from semantic_kernel.connectors.memory.chroma import ChromaCollection
from semantic_kernel.connectors.memory.faiss import FaissCollection
from semantic_kernel.connectors.memory.in_memory import InMemoryVectorCollection
from semantic_kernel.connectors.memory.pinecone import PineconeCollection
from semantic_kernel.connectors.memory.postgres import PostgresCollection
from semantic_kernel.connectors.memory.qdrant import QdrantCollection
from semantic_kernel.connectors.memory.redis import RedisHashsetCollection, RedisJsonCollection
from semantic_kernel.connectors.memory.sql_server import SqlServerCollection
from semantic_kernel.connectors.memory.weaviate import WeaviateCollection
from semantic_kernel.data import (
VectorizableTextSearchMixin,
VectorizedSearchMixin,
VectorSearchFilter,
VectorSearchOptions,
VectorStoreRecordCollection,
VectorStoreRecordDataField,
VectorStoreRecordKeyField,
VectorStoreRecordVectorField,
VectorTextSearchMixin,
vectorstoremodel,
)
from semantic_kernel.data.const import DISTANCE_FUNCTION_DIRECTION_HELPER, DistanceFunction, IndexKind
from semantic_kernel.data.vector_search import add_vector_to_records
# This is a rather complex sample, showing how to use the vector store
# with a number of different collections.
# It also shows how to use the vector store with a number of different data models.
# It also uses all the types of search available in the vector store.
# For a simpler example, see "simple_memory.py"
def get_data_model(type: Literal["array", "list"], index_kind: IndexKind, distance_function: DistanceFunction) -> type:
if type == "array":
@vectorstoremodel
@dataclass
class DataModelArray:
vector: Annotated[
np.ndarray | None,
VectorStoreRecordVectorField(
embedding_settings={"embedding": OpenAIEmbeddingPromptExecutionSettings(dimensions=1536)},
index_kind=index_kind,
dimensions=1536,
distance_function=distance_function,
property_type="float",
serialize_function=np.ndarray.tolist,
deserialize_function=np.array,
),
] = None
id: Annotated[str, VectorStoreRecordKeyField()] = field(default_factory=lambda: str(uuid4()))
content: Annotated[
str,
VectorStoreRecordDataField(
has_embedding=True,
embedding_property_name="vector",
property_type="str",
is_full_text_searchable=True,
),
] = "content1"
title: Annotated[str, VectorStoreRecordDataField(property_type="str", is_full_text_searchable=True)] = (
"title"
)
tag: Annotated[str, VectorStoreRecordDataField(property_type="str", is_filterable=True)] = "tag"
return DataModelArray
@vectorstoremodel
@dataclass
class DataModelList:
vector: Annotated[
list[float] | None,
VectorStoreRecordVectorField(
embedding_settings={"embedding": OpenAIEmbeddingPromptExecutionSettings(dimensions=1536)},
index_kind=index_kind,
dimensions=1536,
distance_function=distance_function,
property_type="float",
),
] = None
id: Annotated[str, VectorStoreRecordKeyField()] = field(default_factory=lambda: str(uuid4()))
content: Annotated[
str,
VectorStoreRecordDataField(
has_embedding=True,
embedding_property_name="vector",
property_type="str",
is_full_text_searchable=True,
),
] = "content1"
title: Annotated[str, VectorStoreRecordDataField(property_type="str", is_full_text_searchable=True)] = "title"
tag: Annotated[str, VectorStoreRecordDataField(property_type="str", is_filterable=True)] = "tag"
return DataModelList
collection_name = "test"
# Depending on the vector database, the index kind and distance function may need to be adjusted
# since not all combinations are supported by all databases.
# The values below might need to be changed for your collection to work.
distance_function = DistanceFunction.COSINE_DISTANCE
index_kind = IndexKind.FLAT
DataModel = get_data_model("array", index_kind, distance_function)
# A list of VectorStoreRecordCollection that can be used.
# Available collections are:
# - ai_search: Azure AI Search
# - postgres: PostgreSQL
# - redis_json: Redis JSON
# - redis_hashset: Redis Hashset
# - qdrant: Qdrant
# - in_memory: In-memory store
# - weaviate: Weaviate
# Please either configure the weaviate settings via environment variables or provide them through the constructor.
# Note that embed mode is not supported on Windows: https://github.com/weaviate/weaviate/issues/3315
# - azure_cosmos_nosql: Azure Cosmos NoSQL
# https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-create-account?tabs=azure-portal
# Please see the link above to learn how to set up an Azure Cosmos NoSQL account.
# https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-develop-emulator?tabs=windows%2Cpython&pivots=api-nosql
# Please see the link above to learn how to set up the Azure Cosmos NoSQL emulator on your machine.
# For this sample to work with Azure Cosmos NoSQL, please adjust the index_kind of the data model to QUANTIZED_FLAT.
# - azure_cosmos_mongodb: Azure Cosmos MongoDB
# https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/introduction
# - chroma: Chroma
# The chroma collection is currently only available for in-memory versions
# Client-Server mode and Chroma Cloud are not yet supported.
# More info on Chroma here: https://docs.trychroma.com/docs/overview/introduction
# - faiss: Faiss - in-memory with optimized indexes.
# - pinecone: Pinecone
# - sql_server: SQL Server, can connect to any SQL Server compatible database, like Azure SQL.
# This is represented as a mapping from the collection name to a
# function which returns the collection.
# Using a function allows for lazy initialization of the collection,
# so that settings for unused collections do not cause validation errors.
collections: dict[str, Callable[[], VectorStoreRecordCollection]] = {
"ai_search": lambda: AzureAISearchCollection[str, DataModel](
data_model_type=DataModel,
),
"postgres": lambda: PostgresCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
"redis_json": lambda: RedisJsonCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
prefix_collection_name_to_key_names=True,
),
"redis_hash": lambda: RedisHashsetCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
prefix_collection_name_to_key_names=True,
),
"qdrant": lambda: QdrantCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
prefer_grpc=True,
named_vectors=False,
),
"in_memory": lambda: InMemoryVectorCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
"weaviate": lambda: WeaviateCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
"azure_cosmos_nosql": lambda: AzureCosmosDBNoSQLCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
create_database=True,
),
"azure_cosmos_mongodb": lambda: AzureCosmosDBforMongoDBCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
"faiss": lambda: FaissCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
"chroma": lambda: ChromaCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
"pinecone": lambda: PineconeCollection[str, DataModel](
collection_name=collection_name,
data_model_type=DataModel,
),
"sql_server": lambda: SqlServerCollection[str, DataModel](
data_model_type=DataModel,
collection_name=collection_name,
),
}
async def cleanup(record_collection):
print("-" * 30)
delete = input("Do you want to delete the collection? (y/n): ")
if delete.lower() != "y":
print("Skipping deletion.")
return
print_with_color("Deleting collection!", Colors.CBLUE)
await record_collection.delete_collection()
print_with_color("Done!", Colors.CGREY)
async def main(collection: str, use_azure_openai: bool):
print("-" * 30)
kernel = Kernel()
embedder = (
AzureTextEmbedding(service_id="embedding") if use_azure_openai else OpenAITextEmbedding(service_id="embedding")
)
kernel.add_service(embedder)
async with collections[collection]() as record_collection:
print_with_color(f"Creating {collection} collection!", Colors.CGREY)
# cleanup any existing collection
await record_collection.delete_collection()
# create a new collection
await record_collection.create_collection()
record1 = DataModel(
content="Semantic Kernel is awesome",
id="e6103c03-487f-4d7d-9c23-4723651c17f4",
title="Overview",
tag="general",
)
record2 = DataModel(
content="Semantic Kernel is available in dotnet, python and Java.",
id="09caec77-f7e1-466a-bcec-f1d51c5b15be",
title="Semantic Kernel Languages",
tag="general",
)
record3 = DataModel(
content="```python\nfrom semantic_kernel import Kernel\nkernel = Kernel()\n```",
id="d5c9913a-e015-4944-b960-5d4a84bca002",
title="Code sample",
tag="code",
)
print_with_color("Adding records!", Colors.CBLUE)
records = await add_vector_to_records(kernel, [record1, record2, record3], data_model_type=DataModel)
records = [record1, record2, record3]
keys = await record_collection.upsert_batch(records)
print(f" Upserted {keys=}")
print_with_color("Getting records!", Colors.CBLUE)
results = await record_collection.get([record1.id, record2.id, record3.id])
if results:
[print_record(record=result) for result in results]
else:
print("Nothing found...")
options = VectorSearchOptions(
vector_field_name="vector",
include_vectors=True,
filter=VectorSearchFilter.equal_to("tag", "general"),
)
print("-" * 30)
print_with_color("Now we can start searching.", Colors.CBLUE)
print_with_color(" For each type of search, enter a search term, for instance `python`.", Colors.CBLUE)
print_with_color(" Enter exit to exit, and skip or nothing to skip this search.", Colors.CBLUE)
if isinstance(record_collection, VectorTextSearchMixin):
search_text = input("Enter search text for text search: ")
if search_text.lower() == "exit":
await cleanup(record_collection)
return
if not search_text or search_text.lower() != "skip":
print_with_color(f"Searching for '{search_text}', with filter 'tag == general'", Colors.CBLUE)
print("-" * 30)
print_with_color("Using text search", Colors.CBLUE)
search_results = await record_collection.text_search(search_text, options)
if search_results.total_count == 0:
print("\nNothing found...\n")
else:
[print_record(result) async for result in search_results.results]
if isinstance(record_collection, VectorizedSearchMixin):
search_text = input("Enter search text for vector search: ")
if search_text.lower() == "exit":
await cleanup(record_collection)
return
if not search_text or search_text.lower() != "skip":
vector = (await embedder.generate_raw_embeddings([search_text]))[0]
print_with_color(f"Vector of search text (first five): {vector[:5]}", Colors.CBLUE)
print("-" * 30)
print_with_color(
f"Using vectorized search, for {distance_function.value}, "
f"the {'higher' if DISTANCE_FUNCTION_DIRECTION_HELPER[distance_function](1, 0) else 'lower'} the score the better" # noqa: E501
f"",
Colors.CBLUE,
)
search_results = await record_collection.vectorized_search(
vector=vector,
options=options,
)
if search_results.total_count == 0:
print("\nNothing found...\n")
else:
[print_record(result) async for result in search_results.results]
if isinstance(record_collection, VectorizableTextSearchMixin):
search_text = input("Enter search text for vectorizable text search: ")
if search_text.lower() == "exit":
await cleanup(record_collection)
return
if not search_text or search_text.lower() != "skip":
print("-" * 30)
print_with_color(
f"Using vectorizable text search, for {distance_function.value}, "
f"the {'higher' if DISTANCE_FUNCTION_DIRECTION_HELPER[distance_function](1, 0) else 'lower'} the score the better", # noqa: E501
Colors.CBLUE,
)
try:
search_results = await record_collection.vectorizable_text_search(search_text, options)
if search_results.total_count == 0:
print("\nNothing found...\n")
else:
[print_record(result) async for result in search_results.results]
except Exception as e:
print(f"Error: {e}")
await cleanup(record_collection)
if __name__ == "__main__":
argparse.ArgumentParser()
parser = argparse.ArgumentParser()
parser.add_argument("--collection", default="in_memory", choices=collections.keys(), help="What collection to use.")
# Option of whether to use OpenAI or Azure OpenAI.
parser.add_argument("--use-azure-openai", action="store_true", help="Use Azure OpenAI instead of OpenAI.")
args = parser.parse_args()
asyncio.run(main(collection=args.collection, use_azure_openai=args.use_azure_openai))