|
6 | 6 | "source": [ |
7 | 7 | "# Create Embeddings with Vectorizers\n", |
8 | 8 | "\n", |
9 | | - "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", |
| 9 | + "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", |
10 | 10 | "\n", |
11 | 11 | "## Prerequisites\n", |
12 | 12 | "\n", |
13 | 13 | "Before you begin, ensure you have:\n", |
14 | 14 | "- Installed RedisVL: `pip install redisvl`\n", |
15 | 15 | "- A running Redis instance ([Redis 8+](https://redis.io/downloads/) or [Redis Cloud](https://redis.io/cloud))\n", |
16 | | - "- API keys for the embedding providers you plan to use\n", |
| 16 | + "- API keys or local model servers for the embedding providers you plan to use\n", |
17 | 17 | "\n", |
18 | 18 | "## What You'll Learn\n", |
19 | 19 | "\n", |
20 | 20 | "By the end of this guide, you will be able to:\n", |
21 | | - "- Create embeddings using multiple providers (OpenAI, HuggingFace, Cohere, etc.)\n", |
| 21 | + "- Create embeddings using multiple providers (OpenAI, HuggingFace, Ollama, Cohere, etc.)\n", |
22 | 22 | "- Use synchronous and asynchronous embedding methods\n", |
23 | 23 | "- Batch embed multiple texts efficiently\n", |
24 | 24 | "- Build custom vectorizers for your own embedding functions\n", |
|
290 | 290 | "embeddings = hf.embed_many(sentences, as_buffer=True)\n" |
291 | 291 | ] |
292 | 292 | }, |
| 293 | + { |
| 294 | + "cell_type": "markdown", |
| 295 | + "metadata": {}, |
| 296 | + "source": [ |
| 297 | + "### Ollama\n", |
| 298 | + "\n", |
| 299 | + "[Ollama](https://ollama.com/) lets you run embedding models locally. RedisVL supports Ollama through the `OllamaTextVectorizer`.\n", |
| 300 | + "\n", |
| 301 | + "Install the Python client and pull an embedding model before running this example:\n", |
| 302 | + "\n", |
| 303 | + "```bash\n", |
| 304 | + "pip install 'redisvl[ollama]'\n", |
| 305 | + "ollama pull nomic-embed-text\n", |
| 306 | + "```\n", |
| 307 | + "\n", |
| 308 | + "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." |
| 309 | + ] |
| 310 | + }, |
| 311 | + { |
| 312 | + "cell_type": "code", |
| 313 | + "execution_count": null, |
| 314 | + "metadata": {}, |
| 315 | + "outputs": [], |
| 316 | + "source": [ |
| 317 | + "from redisvl.utils.vectorize import OllamaTextVectorizer\n", |
| 318 | + "\n", |
| 319 | + "ollama_model = os.environ.get(\"OLLAMA_MODEL\", \"nomic-embed-text\")\n", |
| 320 | + "\n", |
| 321 | + "try:\n", |
| 322 | + " ollama = OllamaTextVectorizer(model=ollama_model)\n", |
| 323 | + "\n", |
| 324 | + " test = ollama.embed(\"This is a test sentence.\")\n", |
| 325 | + " print(\"Vector dimensions:\", len(test))\n", |
| 326 | + " print(test[:10])\n", |
| 327 | + "except (ImportError, ConnectionError, ValueError) as exc:\n", |
| 328 | + " print(\"Skipping Ollama example:\", exc)\n", |
| 329 | + " ollama = None\n" |
| 330 | + ] |
| 331 | + }, |
| 332 | + { |
| 333 | + "cell_type": "code", |
| 334 | + "execution_count": null, |
| 335 | + "metadata": {}, |
| 336 | + "outputs": [], |
| 337 | + "source": [ |
| 338 | + "if ollama is not None:\n", |
| 339 | + " embeddings = ollama.embed_many(sentences, batch_size=2)\n", |
| 340 | + " print(\"Number of embeddings:\", len(embeddings))\n", |
| 341 | + " print(\"Vector dimensions:\", len(embeddings[0]))\n", |
| 342 | + "else:\n", |
| 343 | + " print(\"Skipping: run the Ollama cell above with a running Ollama server and pulled model.\")\n" |
| 344 | + ] |
| 345 | + }, |
| 346 | + { |
| 347 | + "cell_type": "code", |
| 348 | + "execution_count": null, |
| 349 | + "metadata": {}, |
| 350 | + "outputs": [], |
| 351 | + "source": [ |
| 352 | + "if ollama is not None:\n", |
| 353 | + " embeddings = await ollama.aembed_many(sentences, batch_size=2)\n", |
| 354 | + " print(\"Number of async embeddings:\", len(embeddings))\n", |
| 355 | + "else:\n", |
| 356 | + " print(\"Skipping: run the Ollama cell above with a running Ollama server and pulled model.\")\n" |
| 357 | + ] |
| 358 | + }, |
293 | 359 | { |
294 | 360 | "cell_type": "markdown", |
295 | 361 | "metadata": {}, |
|
0 commit comments