Cannot create vector index in redis

401 views Asked by At

I'm just starting using redis and I'm trying to do a vector search. Right now I have setup the redis server with the redisearch module on ubuntu using wsl (My machine's OS is windows 10). This is the python code I have

import redis
import numpy as np

redis_client = redis.Redis(host="localhost", port=6379)

#index_name = 'vector_index'


vectors = [
    ("vector1", np.array([1, 2, 3])),
    ("vector2", np.array([4, 5, 6])),
    ("vector3", np.array([7, 8, 9]))
]

for name, vector in vectors:
    vector_str = ",".join(map(str, vector))
    redis_client.execute_command('HSET', name, 'vector', vector_str)


redis_client.execute_command('FT.CREATE', 'vector_index', 'SCHEMA', 'vector', 'VECTOR', 'HNSW', '16', 'TYPE', 'FLOAT64', 'DIM', '3')

query_vector = np.array([2, 3, 4])
query = query_vector.tobytes


results = redis_client.execute_command('FT.SEARCH', query, 'SORTBY', 'score', 'LIMIT', 0, 3)

print("Search results:")
for result in results[1::2]:
    document_id = result[1]
    score = result[-1]
    print(f"Document ID: {document_id}, Score: {score}")

I was able to add the vectors in the database with the HSET command, but every time it gets to the point where it needs to create the index I always get the message "redis.exceptions.ResponseError: Could not parse field spec". I already tried typing the FT.CREATE command directly into the redis cli multiple times in multiple different sintaxes, but none of them worked and I just got the same message. What can I do to create the index so I can search the vectors I have in there with a query vector? Btw this is just a starter project, I'm trying to make it work so then I can move onto image embeddings and VSS.

1

There are 1 answers

0
BSB On

The best example for redis-py and vectors can be found here: https://redis.readthedocs.io/en/stable/examples/search_vector_similarity_examples.html

Reposting from that page:

import redis
from redis.commands.search.field import TagField, VectorField
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query

r = redis.Redis(host="localhost", port=6379)

INDEX_NAME = "index"                              # Vector Index Name
DOC_PREFIX = "doc:"                               # RediSearch Key Prefix for the Index

def create_index(vector_dimensions: int):
    try:
        # check to see if index exists
        r.ft(INDEX_NAME).info()
        print("Index already exists!")
    except:
        # schema
        schema = (
            TagField("tag"),                       # Tag Field Name
            VectorField("vector",                  # Vector Field Name
                "FLAT", {                          # Vector Index Type: FLAT or HNSW
                    "TYPE": "FLOAT32",             # FLOAT32 or FLOAT64
                    "DIM": vector_dimensions,      # Number of Vector Dimensions
                    "DISTANCE_METRIC": "COSINE",   # Vector Search Distance Metric
                }
            ),
        )

        # index Definition
        definition = IndexDefinition(prefix=[DOC_PREFIX], index_type=IndexType.HASH)

        # create Index
        r.ft(INDEX_NAME).create_index(fields=schema, definition=definition)

# define vector dimensions
VECTOR_DIMENSIONS = 1536

# create the index
create_index(vector_dimensions=VECTOR_DIMENSIONS)

Follow that recipe and you should be good!