Repository navigation
Expand file tree
/
Copy pathmemory_store_base.py
More file actions
203 lines (161 loc) · 7 KB
/
Copy pathmemory_store_base.py
File metadata and controls
203 lines (161 loc) · 7 KB
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
# Copyright (c) Microsoft. All rights reserved.
import sys
from abc import ABC, abstractmethod
from numpy import ndarray
from semantic_kernel.memory.memory_record import MemoryRecord
if sys.version_info >= (3, 13):
from warnings import deprecated
else:
from typing_extensions import deprecated
@deprecated("This class will be removed in a future version.")
class MemoryStoreBase(ABC):
"""Base class for memory store."""
async def __aenter__(self):
"""Enter the context manager."""
return self
async def __aexit__(self, *args):
"""Exit the context manager."""
await self.close()
async def close(self):
"""Close the connection."""
pass
@abstractmethod
async def create_collection(self, collection_name: str) -> None:
"""Creates a new collection in the data store.
Args:
collection_name (str): The name associated with a collection of embeddings.
"""
pass
@abstractmethod
async def get_collections(
self,
) -> list[str]:
"""Gets all collection names in the data store.
Returns:
List[str]: A group of collection names.
"""
pass
@abstractmethod
async def delete_collection(self, collection_name: str) -> None:
"""Deletes a collection from the data store.
Args:
collection_name (str): The name associated with a collection of embeddings.
"""
pass
@abstractmethod
async def does_collection_exist(self, collection_name: str) -> bool:
"""Determines if a collection exists in the data store.
Args:
collection_name (str): The name associated with a collection of embeddings.
Returns:
bool: True if given collection exists, False if not.
"""
pass
@abstractmethod
async def upsert(self, collection_name: str, record: MemoryRecord) -> str:
"""Upserts a memory record into the data store.
Does not guarantee that the collection exists.
If the record already exists, it will be updated.
If the record does not exist, it will be created.
Args:
collection_name (str): The name associated with a collection of embeddings.
record (MemoryRecord): The memory record to upsert.
Returns:
str: The unique identifier for the memory record.
"""
pass
@abstractmethod
async def upsert_batch(self, collection_name: str, records: list[MemoryRecord]) -> list[str]:
"""Upserts a group of memory records into the data store.
Does not guarantee that the collection exists.
If the record already exists, it will be updated.
If the record does not exist, it will be created.
Args:
collection_name (str): The name associated with a collection of embeddings.
records (MemoryRecord): The memory records to upsert.
Returns:
List[str]: The unique identifiers for the memory records.
"""
pass
@abstractmethod
async def get(self, collection_name: str, key: str, with_embedding: bool) -> MemoryRecord:
"""Gets a memory record from the data store. Does not guarantee that the collection exists.
Args:
collection_name (str): The name associated with a collection of embeddings.
key (str): The unique id associated with the memory record to get.
with_embedding (bool): If true, the embedding will be returned in the memory record.
Returns:
MemoryRecord: The memory record if found
"""
pass
@abstractmethod
async def get_batch(
self,
collection_name: str,
keys: list[str],
with_embeddings: bool,
) -> list[MemoryRecord]:
"""Gets a batch of memory records from the data store. Does not guarantee that the collection exists.
Args:
collection_name (str): The name associated with a collection of embeddings.
keys (List[str]): The unique ids associated with the memory records to get.
with_embeddings (bool): If true, the embedding will be returned in the memory records.
Returns:
List[MemoryRecord]: The memory records associated with the unique keys provided.
"""
pass
@abstractmethod
async def remove(self, collection_name: str, key: str) -> None:
"""Removes a memory record from the data store. Does not guarantee that the collection exists.
Args:
collection_name (str): The name associated with a collection of embeddings.
key (str): The unique id associated with the memory record to remove.
"""
pass
@abstractmethod
async def remove_batch(self, collection_name: str, keys: list[str]) -> None:
"""Removes a batch of memory records from the data store. Does not guarantee that the collection exists.
Args:
collection_name (str): The name associated with a collection of embeddings.
keys (List[str]): The unique ids associated with the memory records to remove.
"""
pass
@abstractmethod
async def get_nearest_matches(
self,
collection_name: str,
embedding: ndarray,
limit: int,
min_relevance_score: float,
with_embeddings: bool,
) -> list[tuple[MemoryRecord, float]]:
"""Gets the nearest matches to an embedding of type float. Does not guarantee that the collection exists.
Args:
collection_name (str): The name associated with a collection of embeddings.
embedding (ndarray): The embedding to compare the collection's embeddings with.
limit (int): The maximum number of similarity results to return.
min_relevance_score (float): The minimum relevance threshold for returned results.
with_embeddings (bool): If true, the embeddings will be returned in the memory records.
Returns:
List[Tuple[MemoryRecord, float]]: A list of tuples where item1 is a MemoryRecord and item2
is its similarity score as a float.
"""
pass
@abstractmethod
async def get_nearest_match(
self,
collection_name: str,
embedding: ndarray,
min_relevance_score: float,
with_embedding: bool,
) -> tuple[MemoryRecord, float]:
"""Gets the nearest match to an embedding of type float. Does not guarantee that the collection exists.
Args:
collection_name (str): The name associated with a collection of embeddings.
embedding (ndarray): The embedding to compare the collection's embeddings with.
min_relevance_score (float): The minimum relevance threshold for returned result.
with_embedding (bool): If true, the embeddings will be returned in the memory record.
Returns:
Tuple[MemoryRecord, float]: A tuple consisting of the MemoryRecord and the similarity score as a float.
"""
pass