Skip to content

Commit f8c69c6

Browse files
committedApr 22, 2025
COH-32201 - Fix HNSWIndex implementation in Python Client
1 parent 197d2bc commit f8c69c6

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed
 

‎examples/vector_search.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sentence_transformers import SentenceTransformer
1111

1212
from coherence import NamedMap, Session
13-
from coherence.ai import FloatVector, QueryResult, SimilaritySearch, Vectors
13+
from coherence.ai import FloatVector, HnswIndex, QueryResult, SimilaritySearch, Vectors
1414
from coherence.extractor import Extractors, ValueExtractor
1515
from coherence.filter import Filter, Filters
1616

@@ -100,6 +100,18 @@
100100
101101
The SimilaritySearch aggregator is used to perform a Knn vector search on a
102102
cache in the same way that normal Coherence aggregators are used.
103+
104+
HNSW Indexing
105+
=============
106+
107+
Coherence includes an implementation of the HNSW index that can be used to
108+
speed up searches. The hierarchical navigable small world (HNSW) algorithm is
109+
a graph-based approximate nearest neighbor search technique.
110+
111+
An index is added to a cache in Coherence by calling the add_index method on
112+
the cache. In this example, a HNSWIndex is created with a ValueExtractor that
113+
will extract the vector field from the cache value and an int parameter that
114+
specifies the number of dimensions the vector has.
103115
"""
104116

105117

@@ -226,6 +238,11 @@ async def do_run() -> None:
226238
# NamedMap as a parameter
227239
movies_repo = MovieRepository(movie_db)
228240

241+
# To speed up the search, HNSW index is added to the cache using the
242+
# VALUE_EXTRACTOR for the full plot vector and the dimensions of the
243+
# vector.
244+
await movie_db.add_index(HnswIndex(MovieRepository.VALUE_EXTRACTOR, MovieRepository.EMBEDDING_DIMENSIONS))
245+
229246
# All of the movies data from filename MOVIE_JSON_FILENAME is
230247
# processed and loaded into the movies_repo
231248
await movies_repo.load(MOVIE_JSON_FILENAME)

‎src/coherence/ai.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class HnswIndex(AbstractEvolvable):
392392
def __init__(
393393
self,
394394
extractor: Union[ValueExtractor[T, E], str],
395-
dimensions: int,
395+
dimension: int,
396396
space_name: str = DEFAULT_SPACE_NAME,
397397
max_elements: int = DEFAULT_MAX_ELEMENTS,
398398
m: int = DEFAULT_M,
@@ -404,7 +404,7 @@ def __init__(
404404
Creates an instance of HnswIndex class.
405405
406406
:param extractor: The ValueExtractor to use to extract the Vector.
407-
:param dimensions: The number of dimensions in the vector.
407+
:param dimension: The number of dimensions in the vector.
408408
:param space_name: The index space name.
409409
:param max_elements: The maximum number of elements the index can contain.
410410
:param m: The number of bidirectional links created for every new element during construction.
@@ -415,7 +415,7 @@ def __init__(
415415

416416
super().__init__()
417417
self.extractor = extractor
418-
self.dimensions = dimensions
418+
self.dimension = dimension
419419
self.spaceName = space_name if space_name else ""
420420
self.maxElements = max_elements
421421
self.m = m

‎tests/unit/test_serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def test_HnswIndex_serialization() -> None:
263263
assert ser == (
264264
b'\x15{"@class": "coherence.hnsw.HnswIndex", "dataVersion": 0, '
265265
b'"binFuture": null, "extractor": {"@class": "extractor.UniversalExtractor", '
266-
b'"name": "foo", "params": null}, "dimensions": 384, "spaceName": "COSINE", '
266+
b'"name": "foo", "params": null}, "dimension": 384, "spaceName": "COSINE", '
267267
b'"maxElements": 4096, "m": 16, "efConstruction": 200, "efSearch": 50, '
268268
b'"randomSeed": 100}'
269269
)

0 commit comments

Comments
 (0)