Skip to content

Commit 022cfc4

Browse files
committed
Python: Initialize custom FAISS index key map
1 parent cc8a15f commit 022cfc4

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎python/semantic_kernel/connectors/faiss.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def _create_indexes(self, index: faiss.Index | None = None, indexes: dict[str, f
122122
if not index.is_trained:
123123
raise VectorStoreInitializationException("Index must be trained before using.")
124124
self.indexes[self.definition.vector_fields[0].name] = index
125+
self.indexes_key_map.setdefault(self.definition.vector_fields[0].name, {})
125126
return
126127
for vector_field in self.definition.vector_fields:
127128
if indexes and vector_field.name in indexes:

‎python/tests/unit/connectors/memory/test_faiss.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ async def test_ensure_collection_exists_custom_dict(store, data_model_def):
102102
await collection.ensure_collection_deleted()
103103

104104

105+
@mark.parametrize("use_index_dict", [False, True], ids=["single-index", "index-dict"])
106+
@mark.parametrize("index_kind", ["flat-l2", "flat-ip", "hnsw"])
107+
async def test_custom_index_upsert_and_search(store, data_model_def, use_index_dict, index_kind):
108+
collection = store.get_collection(collection_name="test", record_type=dict, definition=data_model_def)
109+
records = [
110+
{"id": "first", "content": "first record", "vector": [1.0, 0.0, 0.0, 0.0, 0.0]},
111+
{"id": "second", "content": "second record", "vector": [-1.0, 0.0, 0.0, 0.0, 0.0]},
112+
]
113+
114+
for _ in range(2):
115+
if index_kind == "flat-l2":
116+
index = faiss.IndexFlatL2(5)
117+
elif index_kind == "flat-ip":
118+
index = faiss.IndexFlatIP(5)
119+
else:
120+
index = faiss.IndexHNSWFlat(5, 4)
121+
kwargs = {"indexes": {"vector": index}} if use_index_dict else {"index": index}
122+
await collection.ensure_collection_exists(**kwargs)
123+
assert collection.indexes["vector"] is index
124+
125+
assert await collection.upsert(records[0]) == "first"
126+
key_map = collection.indexes_key_map["vector"]
127+
await collection.ensure_collection_exists(**kwargs)
128+
assert collection.indexes_key_map["vector"] is key_map
129+
assert key_map == {"first": 0}
130+
assert await collection.upsert(records[1:]) == ["second"]
131+
assert key_map == {"first": 0, "second": 1}
132+
133+
results = await collection.search(
134+
vector=records[0]["vector"], top=2, include_total_count=True, include_vectors=True
135+
)
136+
assert results.total_count == 2
137+
assert [result.record async for result in results.results] == records
138+
assert await collection.get("first", include_vectors=True) == records[0]
139+
assert index.ntotal == 2
140+
141+
await collection.ensure_collection_deleted()
142+
assert collection.indexes == {}
143+
assert collection.indexes_key_map == {}
144+
assert collection.inner_storage == {}
145+
146+
105147
async def test_upsert(faiss_collection):
106148
await faiss_collection.ensure_collection_exists()
107149
record = {"id": "testid", "content": "test content", "vector": [0.1, 0.2, 0.3, 0.4, 0.5]}

0 commit comments

Comments
 (0)