-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import subprocess | ||
import sys | ||
|
||
from redis import Redis | ||
|
||
from .base import BaseANN | ||
|
||
|
||
class Redisearch(BaseANN): | ||
def __init__(self, metric, M): | ||
self.metric = metric | ||
self.ef_construction = 500 | ||
self.M = M | ||
|
||
def fit(self, X): | ||
# Start Redis in the background | ||
subprocess.run("redis-server --loadmodule /usr/lib/redis/modules/redisearch.so", shell=True, check=True, stdout=sys.stdout, stderr=sys.stderr) | ||
|
||
# Connect to Redis | ||
self.redis = Redis(host="localhost", port=6379, decode_responses=False) | ||
|
||
# Create index | ||
args = [ | ||
"FT.CREATE", | ||
"ann_benchmarks", | ||
"vector", | ||
"VECTOR", | ||
"HNSW", | ||
"10", | ||
"TYPE", | ||
"FLOAT32", | ||
"DIM", | ||
X.shape[0], | ||
"DISTANCE_METRIC", | ||
{'angular': 'cosine', 'euclidean': 'l2'}[self.metric], | ||
"M", | ||
self.M, | ||
"EF_CONSTRUCTION", | ||
self.ef_construction | ||
] | ||
self.redis.execute_command(*args, target_nodes='random') | ||
|
||
# Insert vectors | ||
p = self.redis.pipeline(transaction=False) | ||
count = 0 | ||
for i, v in enumerate(X): | ||
p.execute_command('HSET', i, 'vector', v.tobytes()) | ||
count += 1 | ||
if count == 1000: | ||
p.execute() | ||
p.reset() | ||
count = 0 | ||
p.execute() | ||
|
||
def set_query_arguments(self, ef): | ||
self.ef = ef | ||
|
||
def query(self, v, n): | ||
q = [ | ||
"FT.SEARCH", | ||
"ann_benchmarks", | ||
f"*=>[KNN {n} @vector $BLOB EF_RUNTIME {self.ef}]" | ||
"NOCONTENT", | ||
"SORTBY", | ||
"__vector_score", | ||
"LIMIT", | ||
"0", | ||
str(n), | ||
"PARAMS", | ||
"2", | ||
"BLOB", | ||
v.tobytes(), | ||
"DIALECT", | ||
"2" | ||
] | ||
return [int(doc) for doc in self.redis.execute_command(*q, target_nodes='random')[1:]] | ||
|
||
def __str__(self): | ||
return f"Redisearch(M={self.m}, ef={self.ef})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM redislabs/redisearch as redisearch | ||
FROM ann-benchmarks | ||
|
||
RUN apt-get update && apt-get install -y curl lsb-core | ||
RUN curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg | ||
RUN echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list | ||
RUN apt-get update | ||
RUN apt-get install -y redis | ||
|
||
COPY --from=redisearch /usr/lib/redis/modules/redisearch.so /usr/lib/redisearch.so | ||
RUN ls -al /usr/lib | ||
|
||
# Sanity check that redis-server can run | ||
RUN redis-server --loadmodule /usr/lib/redisearch.so | ||
|
||
RUN pip3 install redisearch redis |