|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from typing import List, Tuple |
| 4 | + |
| 5 | +from numpy import ndarray |
| 6 | + |
| 7 | +from semantic_kernel.connectors.memory.azure_cosmosdb.azure_cosmos_db_store_api import ( |
| 8 | + AzureCosmosDBStoreApi, |
| 9 | +) |
| 10 | +from semantic_kernel.connectors.memory.azure_cosmosdb.cosmosdb_utils import ( |
| 11 | + get_mongodb_resources, |
| 12 | +) |
| 13 | +from semantic_kernel.connectors.memory.azure_cosmosdb.mongo_vcore_store_api import ( |
| 14 | + MongoStoreApi, |
| 15 | +) |
| 16 | +from semantic_kernel.memory.memory_record import MemoryRecord |
| 17 | +from semantic_kernel.memory.memory_store_base import MemoryStoreBase |
| 18 | +from semantic_kernel.utils.settings import azure_cosmos_db_settings_from_dot_env |
| 19 | + |
| 20 | +# Load environment variables |
| 21 | +(cosmos_api, cosmos_connstr) = azure_cosmos_db_settings_from_dot_env() |
| 22 | + |
| 23 | + |
| 24 | +class AzureCosmosDBMemoryStore(MemoryStoreBase): |
| 25 | + """A memory store that uses AzureCosmosDB for MongoDB vCore, to perform vector similarity search on a fully |
| 26 | + managed MongoDB compatible database service. |
| 27 | + https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/vector-search""" |
| 28 | + |
| 29 | + # Right now this only supports Mongo, but set up to support more later. |
| 30 | + apiStore: AzureCosmosDBStoreApi = None |
| 31 | + mongodb_client = None |
| 32 | + database = None |
| 33 | + index_name = None |
| 34 | + vector_dimensions = None |
| 35 | + num_lists = None |
| 36 | + similarity = None |
| 37 | + collection_name = None |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + cosmosStore: AzureCosmosDBStoreApi, |
| 42 | + database_name: str, |
| 43 | + index_name: str, |
| 44 | + vector_dimensions: int, |
| 45 | + num_lists: int, |
| 46 | + similarity: str, |
| 47 | + ): |
| 48 | + if vector_dimensions <= 0: |
| 49 | + raise ValueError("Vector dimensions must be a positive number.") |
| 50 | + # if connection_string is None: |
| 51 | + # raise ValueError("Connection String cannot be empty.") |
| 52 | + if database_name is None: |
| 53 | + raise ValueError("Database Name cannot be empty.") |
| 54 | + if index_name is None: |
| 55 | + raise ValueError("Index Name cannot be empty.") |
| 56 | + |
| 57 | + self.cosmosStore = cosmosStore |
| 58 | + self.index_name = index_name |
| 59 | + self.num_lists = num_lists |
| 60 | + self.similarity = similarity |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + async def create( |
| 64 | + database_name, |
| 65 | + collection_name, |
| 66 | + index_name, |
| 67 | + vector_dimensions, |
| 68 | + num_lists, |
| 69 | + similarity, |
| 70 | + ) -> MemoryStoreBase: |
| 71 | + """Creates the underlying data store based on the API definition""" |
| 72 | + # Right now this only supports Mongo, but set up to support more later. |
| 73 | + apiStore: AzureCosmosDBStoreApi = None |
| 74 | + if cosmos_api == "mongo-vcore": |
| 75 | + mongodb_client, database = get_mongodb_resources( |
| 76 | + cosmos_connstr, database_name |
| 77 | + ) |
| 78 | + apiStore = MongoStoreApi( |
| 79 | + collection_name, |
| 80 | + index_name, |
| 81 | + vector_dimensions, |
| 82 | + num_lists, |
| 83 | + similarity, |
| 84 | + database, |
| 85 | + ) |
| 86 | + else: |
| 87 | + raise NotImplementedError |
| 88 | + |
| 89 | + store = AzureCosmosDBMemoryStore( |
| 90 | + apiStore, |
| 91 | + database_name, |
| 92 | + index_name, |
| 93 | + vector_dimensions, |
| 94 | + num_lists, |
| 95 | + similarity, |
| 96 | + ) |
| 97 | + await store.create_collection_async(collection_name) |
| 98 | + return store |
| 99 | + |
| 100 | + async def create_collection_async(self, collection_name: str) -> None: |
| 101 | + """Creates a new collection in the data store. |
| 102 | +
|
| 103 | + Arguments: |
| 104 | + collection_name {str} -- The name associated with a collection of embeddings. |
| 105 | +
|
| 106 | + Returns: |
| 107 | + None |
| 108 | + """ |
| 109 | + return await self.cosmosStore.create_collection(collection_name) |
| 110 | + |
| 111 | + async def get_collections_async(self) -> List[str]: |
| 112 | + """Gets the list of collections. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + List[str] -- The list of collections. |
| 116 | + """ |
| 117 | + return await self.cosmosStore.get_collections_async() |
| 118 | + |
| 119 | + async def delete_collection_async(self, collection_name: str) -> None: |
| 120 | + """Deletes a collection. |
| 121 | +
|
| 122 | + Arguments: |
| 123 | + collection_name {str} -- The name of the collection to delete. |
| 124 | +
|
| 125 | + Returns: |
| 126 | + None |
| 127 | + """ |
| 128 | + return await self.cosmosStore.delete_collection(str()) |
| 129 | + |
| 130 | + async def does_collection_exist_async(self, collection_name: str) -> bool: |
| 131 | + """Checks if a collection exists. |
| 132 | +
|
| 133 | + Arguments: |
| 134 | + collection_name {str} -- The name of the collection to check. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + bool -- True if the collection exists; otherwise, False. |
| 138 | + """ |
| 139 | + return await self.cosmosStore.does_collection_exist(str()) |
| 140 | + |
| 141 | + async def upsert_async(self, collection_name: str, record: MemoryRecord) -> str: |
| 142 | + """Upsert a record. |
| 143 | +
|
| 144 | + Arguments: |
| 145 | + collection_name {str} -- The name of the collection to upsert the record into. |
| 146 | + record {MemoryRecord} -- The record to upsert. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + str -- The unique record id of the record. |
| 150 | + """ |
| 151 | + return await self.cosmosStore.upsert(str(), record) |
| 152 | + |
| 153 | + async def upsert_batch_async( |
| 154 | + self, collection_name: str, records: List[MemoryRecord] |
| 155 | + ) -> List[str]: |
| 156 | + """Upsert a batch of records. |
| 157 | +
|
| 158 | + Arguments: |
| 159 | + collection_name {str} -- The name of the collection to upsert the records into. |
| 160 | + records {List[MemoryRecord]} -- The records to upsert. |
| 161 | +
|
| 162 | + Returns: |
| 163 | + List[str] -- The unique database keys of the records. |
| 164 | + """ |
| 165 | + return await self.cosmosStore.upsert_batch(str(), records) |
| 166 | + |
| 167 | + async def get_async( |
| 168 | + self, collection_name: str, key: str, with_embedding: bool |
| 169 | + ) -> MemoryRecord: |
| 170 | + """Gets a record. |
| 171 | +
|
| 172 | + Arguments: |
| 173 | + collection_name {str} -- The name of the collection to get the record from. |
| 174 | + key {str} -- The unique database key of the record. |
| 175 | + with_embedding {bool} -- Whether to include the embedding in the result. (default: {False}) |
| 176 | +
|
| 177 | + Returns: |
| 178 | + MemoryRecord -- The record. |
| 179 | + """ |
| 180 | + return await self.cosmosStore.get(str(), key, with_embedding) |
| 181 | + |
| 182 | + async def get_batch_async( |
| 183 | + self, collection_name: str, keys: List[str], with_embeddings: bool |
| 184 | + ) -> List[MemoryRecord]: |
| 185 | + """Gets a batch of records. |
| 186 | +
|
| 187 | + Arguments: |
| 188 | + collection_name {str} -- The name of the collection to get the records from. |
| 189 | + keys {List[str]} -- The unique database keys of the records. |
| 190 | + with_embeddings {bool} -- Whether to include the embeddings in the results. (default: {False}) |
| 191 | +
|
| 192 | + Returns: |
| 193 | + List[MemoryRecord] -- The records. |
| 194 | + """ |
| 195 | + return await self.cosmosStore.get_batch(str(), keys, with_embeddings) |
| 196 | + |
| 197 | + async def remove_async(self, collection_name: str, key: str) -> None: |
| 198 | + """Removes a record. |
| 199 | +
|
| 200 | + Arguments: |
| 201 | + collection_name {str} -- The name of the collection to remove the record from. |
| 202 | + key {str} -- The unique database key of the record to remove. |
| 203 | +
|
| 204 | + Returns: |
| 205 | + None |
| 206 | + """ |
| 207 | + return await self.cosmosStore.remove(str(), key) |
| 208 | + |
| 209 | + async def remove_batch_async(self, collection_name: str, keys: List[str]) -> None: |
| 210 | + """Removes a batch of records. |
| 211 | +
|
| 212 | + Arguments: |
| 213 | + collection_name {str} -- The name of the collection to remove the records from. |
| 214 | + keys {List[str]} -- The unique database keys of the records to remove. |
| 215 | +
|
| 216 | + Returns: |
| 217 | + None |
| 218 | + """ |
| 219 | + return await self.cosmosStore.remove_batch(str(), keys) |
| 220 | + |
| 221 | + async def get_nearest_matches_async( |
| 222 | + self, |
| 223 | + collection_name: str, |
| 224 | + embedding: ndarray, |
| 225 | + limit: int, |
| 226 | + min_relevance_score: float, |
| 227 | + with_embeddings: bool, |
| 228 | + ) -> List[Tuple[MemoryRecord, float]]: |
| 229 | + """Gets the nearest matches to an embedding using vector configuration. |
| 230 | +
|
| 231 | + Parameters: |
| 232 | + collection_name (str) -- The name of the collection to get the nearest matches from. |
| 233 | + embedding (ndarray) -- The embedding to find the nearest matches to. |
| 234 | + limit {int} -- The maximum number of matches to return. |
| 235 | + min_relevance_score {float} -- The minimum relevance score of the matches. (default: {0.0}) |
| 236 | + with_embeddings {bool} -- Whether to include the embeddings in the results. (default: {False}) |
| 237 | +
|
| 238 | + Returns: |
| 239 | + List[Tuple[MemoryRecord, float]] -- The records and their relevance scores. |
| 240 | + """ |
| 241 | + return await self.cosmosStore.get_nearest_matches( |
| 242 | + str(), embedding, limit, min_relevance_score, with_embeddings |
| 243 | + ) |
| 244 | + |
| 245 | + async def get_nearest_match_async( |
| 246 | + self, |
| 247 | + collection_name: str, |
| 248 | + embedding: ndarray, |
| 249 | + min_relevance_score: float, |
| 250 | + with_embedding: bool, |
| 251 | + ) -> Tuple[MemoryRecord, float]: |
| 252 | + """Gets the nearest match to an embedding using vector configuration parameters. |
| 253 | +
|
| 254 | + Arguments: |
| 255 | + collection_name {str} -- The name of the collection to get the nearest match from. |
| 256 | + embedding {ndarray} -- The embedding to find the nearest match to. |
| 257 | + min_relevance_score {float} -- The minimum relevance score of the match. (default: {0.0}) |
| 258 | + with_embedding {bool} -- Whether to include the embedding in the result. (default: {False}) |
| 259 | +
|
| 260 | + Returns: |
| 261 | + Tuple[MemoryRecord, float] -- The record and the relevance score. |
| 262 | + """ |
| 263 | + return await self.cosmosStore.get_nearest_match( |
| 264 | + str(), embedding, min_relevance_score, with_embedding |
| 265 | + ) |
0 commit comments