Skip to content

Add cache option to vectorizer from dict #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions redisvl/utils/vectorize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
from typing import Optional

from redisvl.extensions.cache.embeddings import EmbeddingsCache
from redisvl.utils.vectorize.base import BaseVectorizer, Vectorizers
from redisvl.utils.vectorize.text.azureopenai import AzureOpenAITextVectorizer
from redisvl.utils.vectorize.text.bedrock import BedrockTextVectorizer
@@ -23,22 +27,32 @@
]


def vectorizer_from_dict(vectorizer: dict) -> BaseVectorizer:
def vectorizer_from_dict(
vectorizer: dict,
cache: dict = {},
cache_folder=os.getenv("SENTENCE_TRANSFORMERS_HOME"),
) -> BaseVectorizer:
vectorizer_type = Vectorizers(vectorizer["type"])
model = vectorizer["model"]

args = {"model": model}
if cache:
emb_cache = EmbeddingsCache(**cache)
args["cache"] = emb_cache

if vectorizer_type == Vectorizers.cohere:
return CohereTextVectorizer(model=model)
return CohereTextVectorizer(**args)
elif vectorizer_type == Vectorizers.openai:
return OpenAITextVectorizer(model=model)
return OpenAITextVectorizer(**args)
elif vectorizer_type == Vectorizers.azure_openai:
return AzureOpenAITextVectorizer(model=model)
return AzureOpenAITextVectorizer(**args)
elif vectorizer_type == Vectorizers.hf:
return HFTextVectorizer(model=model)
return HFTextVectorizer(**args)
elif vectorizer_type == Vectorizers.mistral:
return MistralAITextVectorizer(model=model)
return MistralAITextVectorizer(**args)
elif vectorizer_type == Vectorizers.vertexai:
return VertexAITextVectorizer(model=model)
return VertexAITextVectorizer(**args)
elif vectorizer_type == Vectorizers.voyageai:
return VoyageAITextVectorizer(model=model)
return VoyageAITextVectorizer(**args)
else:
raise ValueError(f"Unsupported vectorizer type: {vectorizer_type}")