Search code examples
redishuggingface-transformerslangchainpy-langchain

Trouble querying Redis vector store when using HuggingFaceEmbeddings in langchain


I'm trying to create an RAG (Retrieval-Augmented Generation) system using langchain and Redis vector store. The langchain documentation provides an example of how to store and query data from Redis, which is shown below:

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores.redis import Redis
import os

embeddings = OpenAIEmbeddings()

metadata = [
    {
        "user": "john",
        "age": 18,
        "job": "engineer",
        "credit_score": "high",
    },
    {
        "user": "derrick",
        "age": 45,
        "job": "doctor",
        "credit_score": "low",
    },
    {
        "user": "nancy",
        "age": 94,
        "job": "doctor",
        "credit_score": "high",
    },
    {
        "user": "tyler",
        "age": 100,
        "job": "engineer",
        "credit_score": "high",
    },
    {
        "user": "joe",
        "age": 35,
        "job": "dentist",
        "credit_score": "medium",
    },
]
texts = ["foo", "foo", "foo", "bar", "bar"]

rds = Redis.from_texts(
    texts,
    embeddings,
    metadatas=metadata,
    redis_url="redis://localhost:6379",
    index_name="users",
)

results = rds.similarity_search("foo")

This example uses OpenAIEmbeddings, and it works perfectly. However, when I opt for a different embedding method, such as

embeddings = HuggingFaceEmbeddings(
    model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
            model_kwargs={'device': 'cpu'}
        ) 

I encounter an issue when trying to query the Redis vector store. I receive the following error for rds.similarity_search("foo"):

ResponseError: Error parsing vector similarity query: query vector blob size (1536) does not match index's expected size (6144).

I'm not sure why this error is occurring and how to fix it. Has anyone else faced a similar problem or can provide guidance on resolving this issue? Your help would be greatly appreciated!


Solution

  • Essentially the issue is that you are trying to reuse the same index despite using two different embedding models.

    I ran your code and if I change the embedding model and then provide a different name for the index, thus presumably creating a new one instead of reusing an existing one, everything works as expected.

    If for some reason you want to use the same index with different embedding models, I think you would need to be more specific about the vector schemas and/or modify the embedding vectors prior to saving them in the database. I did not try this myself though, so it is just speculation.

    Note: When I was running the code I received a warning to use the embeddings implementation of langchain_community instead of the langchain one, as the latter seems to be deprecated. Perhaps doing this you would also receive other, potentially more meaningful, errors.