Skip to content
Merged
Show file tree
Hide file tree
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
72 changes: 69 additions & 3 deletions docs/user_guide/04_vectorizers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
"source": [
"# Create Embeddings with Vectorizers\n",
"\n",
"This guide demonstrates how to create embeddings using RedisVL's built-in text vectorizers. RedisVL supports multiple embedding providers: OpenAI, HuggingFace, Vertex AI, Cohere, Mistral AI, Amazon Bedrock, VoyageAI, and custom vectorizers.\n",
"This guide demonstrates how to create embeddings using RedisVL's built-in text vectorizers. RedisVL supports multiple embedding providers: OpenAI, HuggingFace, Ollama, Vertex AI, Cohere, Mistral AI, Amazon Bedrock, VoyageAI, and custom vectorizers.\n",
"\n",
"## Prerequisites\n",
"\n",
"Before you begin, ensure you have:\n",
"- Installed RedisVL: `pip install redisvl`\n",
"- A running Redis instance ([Redis 8+](https://redis.io/downloads/) or [Redis Cloud](https://redis.io/cloud))\n",
"- API keys for the embedding providers you plan to use\n",
"- API keys or local model servers for the embedding providers you plan to use\n",
"\n",
"## What You'll Learn\n",
"\n",
"By the end of this guide, you will be able to:\n",
"- Create embeddings using multiple providers (OpenAI, HuggingFace, Cohere, etc.)\n",
"- Create embeddings using multiple providers (OpenAI, HuggingFace, Ollama, Cohere, etc.)\n",
"- Use synchronous and asynchronous embedding methods\n",
"- Batch embed multiple texts efficiently\n",
"- Build custom vectorizers for your own embedding functions\n",
Expand Down Expand Up @@ -290,6 +290,72 @@
"embeddings = hf.embed_many(sentences, as_buffer=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ollama\n",
"\n",
"[Ollama](https://ollama.com/) lets you run embedding models locally. RedisVL supports Ollama through the `OllamaTextVectorizer`.\n",
"\n",
"Install the Python client and pull an embedding model before running this example:\n",
"\n",
"```bash\n",
"pip install 'redisvl[ollama]'\n",
"ollama pull nomic-embed-text\n",
"```\n",
"\n",
"Make sure the Ollama daemon is running with `ollama serve`. By default, the Ollama client uses `OLLAMA_HOST` if set, otherwise it connects to `http://localhost:11434`. To connect to a custom Ollama server explicitly, pass `host=\"http://your-host:11434\"` when creating the vectorizer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from redisvl.utils.vectorize import OllamaTextVectorizer\n",
"\n",
"ollama_model = os.environ.get(\"OLLAMA_MODEL\", \"nomic-embed-text\")\n",
"\n",
"try:\n",
" ollama = OllamaTextVectorizer(model=ollama_model)\n",
"\n",
" test = ollama.embed(\"This is a test sentence.\")\n",
" print(\"Vector dimensions:\", len(test))\n",
" print(test[:10])\n",
"except (ImportError, ConnectionError, ValueError) as exc:\n",
" print(\"Skipping Ollama example:\", exc)\n",
" ollama = None\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if ollama is not None:\n",
" embeddings = ollama.embed_many(sentences, batch_size=2)\n",
" print(\"Number of embeddings:\", len(embeddings))\n",
" print(\"Vector dimensions:\", len(embeddings[0]))\n",
"else:\n",
" print(\"Skipping: run the Ollama cell above with a running Ollama server and pulled model.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if ollama is not None:\n",
" embeddings = await ollama.aembed_many(sentences, batch_size=2)\n",
" print(\"Number of async embeddings:\", len(embeddings))\n",
"else:\n",
" print(\"Skipping: run the Ollama cell above with a running Ollama server and pulled model.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ all = [
"urllib3<2.2.0",
"pillow>=11.3.0",
"sql-redis>=0.5.0",
"ollama>=0.5.4",
]
ollama = [
"ollama>=0.5.4",
]

[project.urls]
Expand Down
4 changes: 4 additions & 0 deletions redisvl/utils/vectorize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from redisvl.utils.vectorize.text.custom import CustomTextVectorizer
from redisvl.utils.vectorize.text.huggingface import HFTextVectorizer
from redisvl.utils.vectorize.text.mistral import MistralAITextVectorizer
from redisvl.utils.vectorize.text.ollama import OllamaTextVectorizer
from redisvl.utils.vectorize.text.openai import OpenAITextVectorizer
from redisvl.utils.vectorize.text.vertexai import VertexAITextVectorizer
from redisvl.utils.vectorize.text.voyageai import VoyageAITextVectorizer
Expand All @@ -23,6 +24,7 @@
"VertexAITextVectorizer",
"AzureOpenAITextVectorizer",
"MistralAITextVectorizer",
"OllamaTextVectorizer",
"CustomVectorizer",
"CustomTextVectorizer",
"BedrockVectorizer",
Expand Down Expand Up @@ -55,6 +57,8 @@ def vectorizer_from_dict(
return HFTextVectorizer(**args)
elif vectorizer_type == Vectorizers.mistral:
return MistralAITextVectorizer(**args)
elif vectorizer_type == Vectorizers.ollama:
return OllamaTextVectorizer(**args)
elif vectorizer_type == Vectorizers.vertexai:
return VertexAIVectorizer(**args)
elif vectorizer_type == Vectorizers.voyageai:
Expand Down
1 change: 1 addition & 0 deletions redisvl/utils/vectorize/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Vectorizers(Enum):
openai = "openai"
cohere = "cohere"
mistral = "mistral"
ollama = "ollama"
vertexai = "vertexai"
hf = "hf"
voyageai = "voyageai"
Expand Down
Loading