Skip to content

Commit d357731

Browse files
author
Daria
committed
address comments
1 parent 884350f commit d357731

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ A `sample benchmark <https://github.com/zilliztech/gpt-cache/blob/main/examples/
428428
* [x] Support `Chroma <https://github.com/chroma-core/chroma>`_\ , the AI-native open-source embedding database.
429429
* [x] Support `DocArray <https://github.com/docarray/docarray>`_\ , DocArray is a library for representing, sending and storing multi-modal data, perfect for Machine Learning applications.
430430
* [x] Support `Redis <https://redis.io/>`_.
431-
* [x] Support `ValkeySearch <https://github.com/valkey-io/valkey-search/>`_\ , provided as a Valkey module, is a high-performance Vector Similarity Search engine optimized for AI-driven workloads.
431+
* [x] Support `Valkey with valkey-search <https://github.com/valkey-io/valkey-search/>`_\ , is a high-performance Vector Similarity Search engine optimized for AI-driven workloads.
432432
* [ ] Support qdrant
433433
* [ ] Support weaviate
434434
* [ ] Support other vector databases.

gptcache/manager/vector_data/redis_vectorstore.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class RedisVectorStore(VectorBase):
4141
4242
vector_base = VectorBase("redis", dimension=10)
4343
"""
44+
4445
def __init__(
4546
self,
4647
host: str = "localhost",
@@ -72,6 +73,9 @@ def _check_sortby_support(self, index_name: str, sort_field: str) -> bool:
7273
- If the server *parses* SORTBY but complains about the field or schema (e.g., 'Property is not sortable'
7374
or 'No such field'), we treat that as SORTBY being *supported* and bubble up the original error when the real
7475
query runs.
76+
NOTE: Valkey KNN results are ALWAYS sorted by distance.
77+
'The first name/value pair is for the distance computed' -
78+
Ref: https://github.com/valkey-io/valkey-search/blob/main/COMMANDS.md
7579
"""
7680

7781
if self._sortby_supported is not None:
@@ -83,7 +87,12 @@ def _check_sortby_support(self, index_name: str, sort_field: str) -> bool:
8387

8488
except ResponseError as e:
8589
if "SORTBY" in str(e):
86-
self._sortby_supported = False
90+
try:
91+
info = self._client.info("server")
92+
is_valkey = info.get("server_name") == "valkey" or "valkey_version" in info
93+
self._sortby_supported = not is_valkey
94+
except Exception:
95+
self._sortby_supported = False
8796
else:
8897
self._sortby_supported = True
8998
return self._sortby_supported

tests/unit_tests/manager/test_redis.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_redis_vector_store_sortby_supported(monkeypatch, ascending):
7878

7979
from gptcache.manager.vector_data.redis_vectorstore import RedisVectorStore
8080

81-
def _always_supports_sortby() -> bool: # noqa: ARG002
81+
def _always_supports_sortby(self, index_name, field) -> bool: # noqa: ARG002
8282
return not _is_valkey()
8383

8484
monkeypatch.setattr(
@@ -101,7 +101,7 @@ def _always_supports_sortby() -> bool: # noqa: ARG002
101101
def test_redis_vector_store_sortby_unsupported(monkeypatch):
102102
"""
103103
Force the probe to say SORTBY is NOT supported (e.g., Valkey without SORTBY).
104-
The search should still succeed and return k results; we don't assert ordering here.
104+
The search should still succeed and return k results;
105105
"""
106106
encoder = Onnx()
107107
dim = encoder.dimension
@@ -125,3 +125,9 @@ def _never_supports_sortby(self, index_name, field): # noqa: ARG002
125125
res = vector_base.search(query_vec, top_k=k)
126126
assert isinstance(res, (list, tuple))
127127
assert len(res) == k
128+
129+
scores = [extract_score(item) for item in res]
130+
if _is_valkey():
131+
assert is_nondecreasing(scores), f"Scores not sorted ASC: {scores}"
132+
else:
133+
assert not is_nondecreasing(scores) and not _is_nonincreasing(scores), f"Scores sorted: {scores}"

0 commit comments

Comments
 (0)