Skip to content

Commit f011df1

Browse files
authored
fix(vectorstore): Correct HNSW parameter inclusion and test assertions (#40)
* fix(vectorstore): Correct HNSW parameter inclusion and test assertions - Fixed inclusion of HNSW parameters (M, EF_CONSTRUCTION, EF_RUNTIME) in Redis index creation. - Corrected test assertions to accurately reflect expected key count in Redis. - Updated check_index_exists to properly validate HNSW index configurations. * fixed formatting * fixed ill-formed ft.create command
1 parent ee7a754 commit f011df1

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/langchain_google_memorystore_redis/vectorstore.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,12 @@ def init_index(client: redis.Redis, index_config: IndexConfig):
321321
command = (
322322
f"FT.CREATE {index_config.name} ON HASH PREFIX 1 {RedisVectorStore.get_key_prefix(index_config.name)} "
323323
f"SCHEMA {index_config.field_name} VECTOR {index_config.type} "
324-
f"6 TYPE {index_config.data_type} DIM {index_config.vector_size} "
325-
f"DISTANCE_METRIC {index_config.distance_metric}"
324+
f"14 TYPE {index_config.data_type} DIM {index_config.vector_size} "
325+
f"DISTANCE_METRIC {index_config.distance_metric} "
326+
f"TYPE {index_config.data_type} "
327+
f"M {index_config.m} "
328+
f"EF_CONSTRUCTION {index_config.ef_construction} "
329+
f"EF_RUNTIME {index_config.ef_runtime}"
326330
)
327331

328332
try:

tests/test_memorystore_redis_vectorstore.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ def test_vector_store_init_index():
3636
index_name = str(uuid.uuid4())
3737

3838
index_config = HNSWConfig(
39-
name=index_name, distance_strategy=DistanceStrategy.COSINE, vector_size=128
39+
name=index_name,
40+
distance_strategy=DistanceStrategy.COSINE,
41+
vector_size=128,
42+
m=1,
43+
ef_construction=2,
44+
ef_runtime=3,
4045
)
4146

4247
assert not check_index_exists(client, index_name, index_config)
@@ -134,8 +139,7 @@ def test_vector_store_add_texts(texts, metadatas, ids):
134139

135140
# Verify no extra keys are present
136141
all_keys = [key.decode("utf-8") for key in client.keys(f"{index_name}*")]
137-
# Currently RedisQuery stores the index schema as a key using the index_name
138-
assert len(all_keys) == len(returned_ids) + 1, "Found unexpected keys in Redis"
142+
assert len(all_keys) == len(returned_ids), "Found unexpected keys in Redis"
139143

140144
# Clena up
141145
RedisVectorStore.drop_index(client=client, index_name=index_name)
@@ -233,7 +237,7 @@ def test_vector_store_range_query(distance_strategy, distance_threshold):
233237

234238

235239
def check_index_exists(
236-
client: redis.Redis, index_name: str, index_config: VectorIndexConfig
240+
client: redis.Redis, index_name: str, index_config: HNSWConfig
237241
) -> bool:
238242
try:
239243
index_info = client.ft(index_name).info()
@@ -243,6 +247,18 @@ def check_index_exists(
243247
return (
244248
index_info["index_name"] == index_name
245249
and index_info["index_definition"][1] == b"HASH"
250+
and index_info["index_definition"][3][0].decode("utf-8") == index_config.name
251+
and index_info["attributes"][0][1].decode("utf-8") == index_config.field_name
252+
and index_info["attributes"][0][3].decode("utf-8") == index_config.field_name
253+
and index_info["attributes"][0][5] == b"VECTOR"
254+
and index_info["attributes"][0][7][3] == index_config.vector_size
255+
and index_info["attributes"][0][7][5].decode("utf-8")
256+
== index_config.distance_metric
257+
and index_info["attributes"][0][7][7].decode("utf-8") == index_config.data_type
258+
and index_info["attributes"][0][7][9][1] == b"HNSW"
259+
and index_info["attributes"][0][7][9][3] == index_config.m
260+
and index_info["attributes"][0][7][9][5] == index_config.ef_construction
261+
and index_info["attributes"][0][7][9][7] == index_config.ef_runtime
246262
)
247263

248264

0 commit comments

Comments
 (0)