{
  "schema_version": 2,
  "id": "develop/ai/redisvl/user_guide/how_to_guides/vectorizers",
  "title": "Create Embeddings with Vectorizers",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/user_guide/how_to_guides/vectorizers/",
  "summary": "",
  "aliases": [
    "/integrate/redisvl/user_guide/how_to_guides/04_vectorizers",
    "/integrate/redisvl/user_guide/vectorizers/",
    "/develop/ai/redisvl/user_guide/vectorizers/"
  ],
  "tags": [],
  "last_updated": "2026-10-01T14:49:09Z",
  "page_type": "content",
  "content_hash": "de30ad5fb41b3f418b899db2e92557d3e6d34867fd21fd1749b7adf36e250416",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide demonstrates how to create embeddings using RedisVL's built-in text vectorizers. RedisVL supports multiple embedding providers: OpenAI, HuggingFace, Ollama, Google (Vertex AI and the Gemini Developer API), Cohere, Mistral AI, Amazon Bedrock, VoyageAI, and custom vectorizers."
    },
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "content",
      "text": "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 or local model servers for the embedding providers you plan to use"
    },
    {
      "id": "what-youll-learn",
      "title": "What You'll Learn",
      "role": "content",
      "text": "By the end of this guide, you will be able to:\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- Integrate vectorizers with RedisVL indexes for semantic search\n\n\n[code example]"
    },
    {
      "id": "creating-text-embeddings",
      "title": "Creating Text Embeddings",
      "role": "content",
      "text": "This example will show how to create an embedding from 3 simple sentences with a number of different text vectorizers in RedisVL.\n\n- \"That is a happy dog\"\n- \"That is a happy person\"\n- \"Today is a nice day\""
    },
    {
      "id": "openai",
      "title": "OpenAI",
      "role": "content",
      "text": "The ``OpenAITextVectorizer`` makes it simple to use RedisVL with the embeddings models at OpenAI. For this you will need to install ``openai``. \n\n[code example]\n\n\n\n[code example]\n\n\n[code example]\n\n    Vector dimensions:  1536\n\n\n\n\n\n    [-0.0011391325388103724,\n     -0.003206387162208557,\n     0.002380132209509611,\n     -0.004501554183661938,\n     -0.010328996926546097,\n     0.012922565452754498,\n     -0.005491119809448719,\n     -0.0029864837415516376,\n     -0.007327961269766092,\n     -0.03365817293524742]\n\n\n\n\n[code example]\n\n\n\n\n    [-0.017466850578784943,\n     1.8471690054866485e-05,\n     0.00129731057677418,\n     -0.02555876597762108,\n     -0.019842341542243958,\n     0.01603139191865921,\n     -0.0037347301840782166,\n     0.0009670283179730177,\n     0.006618348415941,\n     -0.02497442066669464]\n\n\n\n\n[code example]\n\n    Number of Embeddings: 3"
    },
    {
      "id": "azure-openai",
      "title": "Azure OpenAI",
      "role": "content",
      "text": "The ``AzureOpenAITextVectorizer`` is a variation of the OpenAI vectorizer that calls OpenAI models within Azure. If you've already installed ``openai``, then you're ready to use Azure OpenAI.\n\nThe only practical difference between OpenAI and Azure OpenAI is the variables required to call the API.\n\n\n[code example]\n\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "huggingface",
      "title": "Huggingface",
      "role": "content",
      "text": "[Huggingface](https://huggingface.co/models) is a popular NLP platform that has a number of pre-trained models you can use off the shelf. RedisVL supports using Huggingface \"Sentence Transformers\" to create embeddings from text. To use Huggingface, you will need to install the ``sentence-transformers`` library.\n\n[code example]\n\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "ollama",
      "title": "Ollama",
      "role": "content",
      "text": "[Ollama](https://ollama.com/) lets you run embedding models locally. RedisVL supports Ollama through the `OllamaTextVectorizer`.\n\nInstall the Python client and pull an embedding model before running this example:\n\n[code example]\n\nMake 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.\n\n\n[code example]\n\n\n[code example]\n\n\n[code example]\n\n**⚠️ Deprecated.** `VertexAIVectorizer` uses Google's Vertex AI model-garden SDK, which Google has deprecated with a scheduled removal. For text embeddings use the **Google Gen AI** vectorizer shown in the next section (`GoogleGenAIVectorizer`) on the supported `google-genai` SDK. Multimodal (image/video) migration is tracked in [issue #620](https://github.com/redis/redis-vl-python/issues/620)."
    },
    {
      "id": "vertexai",
      "title": "VertexAI",
      "role": "content",
      "text": "[VertexAI](https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings) is GCP's fully-featured AI platform including a number of pretrained LLMs. RedisVL supports using VertexAI to create embeddings from these models. To use VertexAI, you will first need to install the ``google-cloud-aiplatform`` library.\n\n[code example]\n\n1. Then you need to gain access to a [Google Cloud Project](https://cloud.google.com/gcp?hl=en) and provide [access to credentials](https://cloud.google.com/docs/authentication/application-default-credentials). This is accomplished by setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to the path of a JSON key file downloaded from your service account on GCP.\n2. Lastly, you need to find your [project ID](https://support.google.com/googleapi/answer/7014113?hl=en) and [geographic region for VertexAI](https://cloud.google.com/vertex-ai/docs/general/locations).\n\n\n**Make sure the following env vars are set:**\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "google-gen-ai",
      "title": "Google Gen AI",
      "role": "content",
      "text": "The `GoogleGenAIVectorizer` uses Google's [`google-genai`](https://pypi.org/project/google-genai/) SDK — the supported replacement for the deprecated Vertex AI model-garden SDK. It reaches **both** Google embedding backends from a single client:\n\n- **Vertex AI / Gemini Enterprise** — GCP project auth (`project_id` + `location`, credentials via ADC).\n- **Gemini Developer API** — a single `api_key`.\n\nInstall it with `pip install redisvl[google-genai]`.\n\n**Migrating from `VertexAIVectorizer`:**\n\n| | `VertexAIVectorizer` (deprecated) | `GoogleGenAIVectorizer` |\n|---|---|---|\n| SDK | `google-cloud-aiplatform` (deprecated) | `google-genai` (supported) |\n| Default model | `textembedding-gecko` (768 dims) | `gemini-embedding-001` (3072 dims) |\n| Backends | Vertex AI only | Vertex AI **and** Gemini Developer API |\n| Async | no | yes (`aembed`, `aembed_many`) |\n\nBecause the default model and dimensions differ, embeddings are **not** interchangeable — reindex when you switch. When you request a reduced `output_dimensionality`, the vectorizer L2-normalizes the result so it stays valid for Redis COSINE / inner-product search.\n\n**Set one of the following:**\n\n[code example]\n\n\n\n[code example]"
    },
    {
      "id": "cohere",
      "title": "Cohere",
      "role": "content",
      "text": "[Cohere](https://dashboard.cohere.ai/) allows you to implement language AI into your product. The `CohereTextVectorizer` makes it simple to use RedisVL with the embeddings models at Cohere. For this you will need to install `cohere`.\n\n[code example]\n\n\n[code example]\n\n\nSpecial attention needs to be paid to the `input_type` parameter for each `embed` call. For example, for embedding \nqueries, you should set `input_type='search_query'`; for embedding documents, set `input_type='search_document'`. See\nmore information [here](https://docs.cohere.com/reference/embed)\n\n\n[code example]\n\nLearn more about using RedisVL and Cohere together through [this dedicated user guide](https://docs.cohere.com/docs/redis-and-cohere)."
    },
    {
      "id": "voyageai",
      "title": "VoyageAI",
      "role": "content",
      "text": "[VoyageAI](https://dash.voyageai.com/) allows you to implement language AI into your product. The `VoyageAIVectorizer` makes it simple to use RedisVL with the embeddings models at VoyageAI. For this you will need to install `voyageai`.\n\n[code example]\n\n\n[code example]\n\n\nSpecial attention needs to be paid to the `input_type` parameter for each `embed` call. For example, for embedding \nqueries, you should set `input_type='query'`; for embedding documents, set `input_type='document'`. See\nmore information [here](https://docs.voyageai.com/docs/embeddings)\n\n\n[code example]\n\n#### Contextualized embeddings\n\nVoyageAI's `voyage-context-*` models support *contextualized* chunk embeddings. The `VoyageAIVectorizer` automatically routes `voyage-context-*` models to the contextualized embeddings API: pass your list of chunk strings to `embed_many` and you get one embedding back per chunk. Each input string is sent as its own auto-chunked document, so the inputs are embedded **independently** (a chunk's embedding does not depend on the other strings in the list). This preserves the one-embedding-per-input contract and keeps the embeddings cache deterministic.\n\n\n[code example]"
    },
    {
      "id": "mistral-ai",
      "title": "Mistral AI",
      "role": "content",
      "text": "[Mistral](https://console.mistral.ai/) offers LLM and embedding APIs for you to implement into your product. The `MistralAITextVectorizer` makes it simple to use RedisVL with their embeddings model.\nYou will need to install `mistralai`.\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "amazon-bedrock",
      "title": "Amazon Bedrock",
      "role": "content",
      "text": "Amazon Bedrock provides fully managed foundation models for text embeddings. Install the required dependencies:\n\n[code example]\n\n#### Configure AWS credentials:\n\n\n[code example]\n\n#### Create embeddings:\n\n\n[code example]"
    },
    {
      "id": "custom-vectorizers",
      "title": "Custom Vectorizers",
      "role": "content",
      "text": "RedisVL supports the use of other vectorizers and provides a class to enable compatibility with any function that generates a vector or vectors from string data\n\n\n[code example]\n\nThis enables the use of custom vectorizers with other RedisVL components\n\n\n[code example]"
    },
    {
      "id": "search-with-provider-embeddings",
      "title": "Search with Provider Embeddings",
      "role": "content",
      "text": "Now that we've created our embeddings, we can use them to search for similar sentences. We will use the same 3 sentences from above and search for similar sentences.\n\nFirst, we need to create the schema for our index.\n\nHere's what the schema for the example looks like in yaml for the HuggingFace vectorizer:\n\n[code example]\n\n\n[code example]\n\n\n[code example]\n\nLoading data to RedisVL is easy. It expects a list of dictionaries. The vector is stored as bytes.\n\n\n[code example]\n\n\n[code example]"
    },
    {
      "id": "selecting-your-float-data-type",
      "title": "Selecting your float data type",
      "role": "content",
      "text": "When embedding text as byte arrays RedisVL supports 4 different floating point data types, `float16`, `float32`, `float64` and `bfloat16`, and 2 integer types, `int8` and `uint8`.\nYour dtype set for your vectorizer must match what is defined in your search index. If one is not explicitly set the default is `float32`.\n\n\n[code example]"
    },
    {
      "id": "next-steps",
      "title": "Next Steps",
      "role": "content",
      "text": "Now that you understand how to create embeddings, explore these related guides:\n\n- [Getting Started](https://redis.io/docs/latest/../getting_started) - Learn the basics of indexes and queries\n- [Rerank Results](https://redis.io/docs/latest/rerankers) - Improve search quality with reranking models\n- [Cache Embeddings](https://redis.io/docs/latest/embeddings_cache) - Cache embedding vectors for faster repeated computations"
    },
    {
      "id": "cleanup",
      "title": "Cleanup",
      "role": "content",
      "text": "[code example]"
    }
  ],
  "examples": [
    {
      "id": "what-youll-learn-ex0",
      "language": "python",
      "code": "# import necessary modules\nimport os",
      "section_id": "what-youll-learn"
    },
    {
      "id": "openai-ex0",
      "language": "bash",
      "code": "pip install openai",
      "section_id": "openai"
    },
    {
      "id": "openai-ex1",
      "language": "python",
      "code": "import getpass\n\n# setup the API Key\napi_key = os.environ.get(\"OPENAI_API_KEY\") or getpass.getpass(\"Enter your OpenAI API key: \")",
      "section_id": "openai"
    },
    {
      "id": "openai-ex2",
      "language": "python",
      "code": "from redisvl.utils.vectorize import OpenAITextVectorizer\n\n# create a vectorizer\noai = OpenAITextVectorizer(\n    model=\"text-embedding-ada-002\",\n    api_config={\"api_key\": api_key},\n)\n\ntest = oai.embed(\"This is a test sentence.\")\nprint(\"Vector dimensions: \", len(test))\ntest[:10]",
      "section_id": "openai"
    },
    {
      "id": "openai-ex3",
      "language": "python",
      "code": "# Create many embeddings at once\nsentences = [\n    \"That is a happy dog\",\n    \"That is a happy person\",\n    \"Today is a sunny day\"\n]\n\nembeddings = oai.embed_many(sentences)\nembeddings[0][:10]",
      "section_id": "openai"
    },
    {
      "id": "openai-ex4",
      "language": "python",
      "code": "# openai also supports asynchronous requests, which we can use to speed up the vectorization process.\nembeddings = await oai.aembed_many(sentences)\nprint(\"Number of Embeddings:\", len(embeddings))",
      "section_id": "openai"
    },
    {
      "id": "azure-openai-ex0",
      "language": "python",
      "code": "# NBVAL_SKIP\n# additionally to the API Key, setup the API endpoint and version\napi_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") or getpass.getpass(\"Enter your AzureOpenAI API key: \")\napi_version = os.environ.get(\"OPENAI_API_VERSION\") or getpass.getpass(\"Enter your AzureOpenAI API version: \")\nazure_endpoint = os.environ.get(\"AZURE_OPENAI_ENDPOINT\") or getpass.getpass(\"Enter your AzureOpenAI API endpoint: \")\ndeployment_name = os.environ.get(\"AZURE_OPENAI_DEPLOYMENT_NAME\", \"text-embedding-ada-002\")\n\n# Skip Azure examples when required values are missing (e.g. Run All without Azure configured).\n_azure_configured = bool(azure_endpoint and api_key and api_version)",
      "section_id": "azure-openai"
    },
    {
      "id": "azure-openai-ex1",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Depends on the Azure OpenAI cell above, which is not executed in CI.\nfrom redisvl.utils.vectorize import AzureOpenAITextVectorizer\n\nif not _azure_configured:\n    print(\"Skipping Azure OpenAI example: set AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY and OPENAI_API_VERSION\")\n    az_oai = None\nelse:\n    az_oai = AzureOpenAITextVectorizer(\n        model=deployment_name,  # Must be your CUSTOM deployment name\n        api_config={\n            \"api_key\": api_key,\n            \"api_version\": api_version,\n            \"azure_endpoint\": azure_endpoint,\n        },\n    )\n\n    test = az_oai.embed(\"This is a test sentence.\")\n    print(\"Vector dimensions: \", len(test))\n    test[:10]",
      "section_id": "azure-openai"
    },
    {
      "id": "azure-openai-ex2",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Depends on the Azure OpenAI cell above, which is not executed in CI.\n# Just like OpenAI, AzureOpenAI supports batching embeddings and asynchronous requests.\nsentences = [\n    \"That is a happy dog\",\n    \"That is a happy person\",\n    \"Today is a sunny day\",\n]\n\nif _azure_configured and az_oai is not None:\n    embeddings = await az_oai.aembed_many(sentences)\n    embeddings[0][:10]\nelse:\n    print(\"Skipping: run the Azure cells above with valid configuration.\")",
      "section_id": "azure-openai"
    },
    {
      "id": "huggingface-ex0",
      "language": "bash",
      "code": "pip install sentence-transformers",
      "section_id": "huggingface"
    },
    {
      "id": "huggingface-ex1",
      "language": "python",
      "code": "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\nfrom redisvl.utils.vectorize import HFTextVectorizer\n\n\n# create a vectorizer\n# choose your model from the huggingface website\nhf = HFTextVectorizer(model=\"sentence-transformers/all-mpnet-base-v2\")\n\n# embed a sentence\ntest = hf.embed(\"This is a test sentence.\")\ntest[:10]",
      "section_id": "huggingface"
    },
    {
      "id": "huggingface-ex2",
      "language": "python",
      "code": "# You can also create many embeddings at once\nembeddings = hf.embed_many(sentences, as_buffer=True)",
      "section_id": "huggingface"
    },
    {
      "id": "ollama-ex0",
      "language": "bash",
      "code": "pip install 'redisvl[ollama]'\nollama pull nomic-embed-text",
      "section_id": "ollama"
    },
    {
      "id": "ollama-ex1",
      "language": "python",
      "code": "# NBVAL_SKIP\n# No Ollama server in CI, so this only burns time on connection retries.\nfrom redisvl.utils.vectorize import OllamaTextVectorizer\n\nollama_model = os.environ.get(\"OLLAMA_MODEL\", \"nomic-embed-text\")\n\ntry:\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])\nexcept (ImportError, ConnectionError, ValueError) as exc:\n    print(\"Skipping Ollama example:\", exc)\n    ollama = None",
      "section_id": "ollama"
    },
    {
      "id": "ollama-ex2",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Depends on the Ollama cell above, which is not executed in CI.\nif 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]))\nelse:\n    print(\"Skipping: run the Ollama cell above with a running Ollama server and pulled model.\")",
      "section_id": "ollama"
    },
    {
      "id": "ollama-ex3",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Depends on the Ollama cell above, which is not executed in CI.\nif ollama is not None:\n    embeddings = await ollama.aembed_many(sentences, batch_size=2)\n    print(\"Number of async embeddings:\", len(embeddings))\nelse:\n    print(\"Skipping: run the Ollama cell above with a running Ollama server and pulled model.\")",
      "section_id": "ollama"
    },
    {
      "id": "vertexai-ex0",
      "language": "bash",
      "code": "pip install google-cloud-aiplatform>=1.26",
      "section_id": "vertexai"
    },
    {
      "id": "vertexai-ex1",
      "language": "plaintext",
      "code": "GOOGLE_APPLICATION_CREDENTIALS=<path to your gcp JSON creds>\nGCP_PROJECT_ID=<your gcp project id>\nGCP_LOCATION=<your gcp geo region for vertex ai>",
      "section_id": "vertexai"
    },
    {
      "id": "vertexai-ex2",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Deprecated vectorizer; not executed in CI so notebook validation makes no API calls.\nfrom redisvl.utils.vectorize import VertexAIVectorizer\n\n\n# create a vectorizer\nvtx = VertexAIVectorizer(api_config={\n    \"project_id\": os.environ.get(\"GCP_PROJECT_ID\") or getpass.getpass(\"Enter your GCP Project ID: \"),\n    \"location\": os.environ.get(\"GCP_LOCATION\") or getpass.getpass(\"Enter your GCP Location: \"),\n    \"google_application_credentials\": os.environ.get(\"GOOGLE_APPLICATION_CREDENTIALS\") or getpass.getpass(\"Enter your Google App Credentials path: \")\n})\n\n# embed a sentence\ntest = vtx.embed(\"This is a test sentence.\")\ntest[:10]",
      "section_id": "vertexai"
    },
    {
      "id": "google-gen-ai-ex0",
      "language": "plaintext",
      "code": "# Gemini Developer API\nGEMINI_API_KEY=<your gemini api key>\n\n# or Vertex AI\nGOOGLE_CLOUD_PROJECT=<your gcp project id>\nGOOGLE_CLOUD_LOCATION=<your gcp region>\nGOOGLE_APPLICATION_CREDENTIALS=<path to your gcp JSON creds>",
      "section_id": "google-gen-ai"
    },
    {
      "id": "google-gen-ai-ex1",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Docs example; not executed in CI so notebook validation makes no API calls.\nfrom redisvl.utils.vectorize import GoogleGenAIVectorizer\n\n# Auto-detects the backend: GEMINI_API_KEY -> Gemini, else GCP project/location -> Vertex AI.\n# Guarded so this notebook still runs when Google credentials aren't configured.\ntry:\n    genai_vectorizer = GoogleGenAIVectorizer(model=\"gemini-embedding-001\")\n    print(f\"backend={genai_vectorizer.backend}, dims={genai_vectorizer.dims}\")\n    genai_test = genai_vectorizer.embed(\"This is a test sentence.\")\n    print(genai_test[:10])\nexcept (ImportError, ValueError) as e:\n    print(f\"Skipping GoogleGenAIVectorizer demo: {e}\")\n    genai_vectorizer = None",
      "section_id": "google-gen-ai"
    },
    {
      "id": "cohere-ex0",
      "language": "bash",
      "code": "pip install cohere",
      "section_id": "cohere"
    },
    {
      "id": "cohere-ex1",
      "language": "python",
      "code": "import getpass\n# setup the API Key\napi_key = os.environ.get(\"COHERE_API_KEY\") or getpass.getpass(\"Enter your Cohere API key: \")",
      "section_id": "cohere"
    },
    {
      "id": "cohere-ex2",
      "language": "python",
      "code": "from redisvl.utils.vectorize import CohereTextVectorizer\n\n# create a vectorizer\nco = CohereTextVectorizer(\n    model=\"embed-english-v3.0\",\n    api_config={\"api_key\": api_key},\n)\n\n# embed a search query\ntest = co.embed(\"This is a test sentence.\", input_type='search_query')\nprint(\"Vector dimensions: \", len(test))\nprint(test[:10])\n\n# embed a document\ntest = co.embed(\"This is a test sentence.\", input_type='search_document')\nprint(\"Vector dimensions: \", len(test))\nprint(test[:10])",
      "section_id": "cohere"
    },
    {
      "id": "voyageai-ex0",
      "language": "bash",
      "code": "pip install voyageai",
      "section_id": "voyageai"
    },
    {
      "id": "voyageai-ex1",
      "language": "python",
      "code": "import getpass\n# setup the API Key\napi_key = os.environ.get(\"VOYAGE_API_KEY\") or getpass.getpass(\"Enter your VoyageAI API key: \")",
      "section_id": "voyageai"
    },
    {
      "id": "voyageai-ex2",
      "language": "python",
      "code": "from redisvl.utils.vectorize import VoyageAIVectorizer\n\n# create a vectorizer\nvo = VoyageAIVectorizer(\n    model=\"voyage-law-2\",  # Please check the available models at https://docs.voyageai.com/docs/embeddings\n    api_config={\"api_key\": api_key},\n)\n\n# embed a search query\ntest = vo.embed(\"This is a test sentence.\", input_type='query')\nprint(\"Vector dimensions: \", len(test))\nprint(test[:10])\n\n# embed a document\ntest = vo.embed(\"This is a test sentence.\", input_type='document')\nprint(\"Vector dimensions: \", len(test))\nprint(test[:10])",
      "section_id": "voyageai"
    },
    {
      "id": "voyageai-ex3",
      "language": "python",
      "code": "# NBVAL_SKIP\n# Contextualized embeddings (voyage-context-* models). Each input string is sent\n# as its own auto-chunked document and embedded independently, so the vectorizer\n# returns exactly one embedding per input string. Requires voyageai>=0.5.0.\ncontext_vo = VoyageAIVectorizer(\n    model=\"voyage-context-4\",  # See https://docs.voyageai.com/docs/contextualized-chunk-embeddings\n    api_config={\"api_key\": api_key},\n)\n\nchunks = [\n    \"That is a happy dog\",\n    \"That is a happy person\",\n    \"Today is a sunny day\",\n]\ncontext_embeddings = context_vo.embed_many(chunks, input_type=\"document\")\nprint(\"Number of embeddings:\", len(context_embeddings))\nprint(\"Vector dimensions:\", len(context_embeddings[0]))",
      "section_id": "voyageai"
    },
    {
      "id": "mistral-ai-ex0",
      "language": "bash",
      "code": "pip install mistralai",
      "section_id": "mistral-ai"
    },
    {
      "id": "mistral-ai-ex1",
      "language": "python",
      "code": "from redisvl.utils.vectorize import MistralAITextVectorizer\n\nmistral = MistralAITextVectorizer()\n\n# embed a sentence using their asynchronous method\ntest = await mistral.aembed(\"This is a test sentence.\")\nprint(\"Vector dimensions: \", len(test))\nprint(test[:10])",
      "section_id": "mistral-ai"
    },
    {
      "id": "amazon-bedrock-ex0",
      "language": "bash",
      "code": "pip install 'redisvl[bedrock]'  # Installs boto3",
      "section_id": "amazon-bedrock"
    },
    {
      "id": "amazon-bedrock-ex1",
      "language": "python",
      "code": "import os\nimport getpass\n\nif \"AWS_ACCESS_KEY_ID\" not in os.environ:\n    os.environ[\"AWS_ACCESS_KEY_ID\"] = getpass.getpass(\"Enter AWS Access Key ID: \")\nif \"AWS_SECRET_ACCESS_KEY\" not in os.environ:\n    os.environ[\"AWS_SECRET_ACCESS_KEY\"] = getpass.getpass(\"Enter AWS Secret Key: \")\n\nos.environ[\"AWS_REGION\"] = \"us-east-1\"  # Change as needed",
      "section_id": "amazon-bedrock"
    },
    {
      "id": "amazon-bedrock-ex2",
      "language": "python",
      "code": "from redisvl.utils.vectorize import BedrockVectorizer\n\nbedrock = BedrockVectorizer(\n    model=\"amazon.titan-embed-text-v2:0\"\n)\n\n# Single embedding\ntext = \"This is a test sentence.\"\nembedding = bedrock.embed(text)\nprint(f\"Vector dimensions: {len(embedding)}\")\n\n# Multiple embeddings\nsentences = [\n    \"That is a happy dog\",\n    \"That is a happy person\",\n    \"Today is a sunny day\"\n]\nembeddings = bedrock.embed_many(sentences)",
      "section_id": "amazon-bedrock"
    },
    {
      "id": "custom-vectorizers-ex0",
      "language": "python",
      "code": "from redisvl.utils.vectorize import CustomVectorizer\n\ndef generate_embeddings(text_input, **kwargs):\n    return [0.101] * 768\n\ncustom_vectorizer = CustomVectorizer(generate_embeddings)\n\ncustom_vectorizer.embed(\"This is a test sentence.\")[:10]",
      "section_id": "custom-vectorizers"
    },
    {
      "id": "custom-vectorizers-ex1",
      "language": "python",
      "code": "from redisvl.extensions.cache.llm import SemanticCache\n\ncache = SemanticCache(name=\"custom_cache\", vectorizer=custom_vectorizer)\n\ncache.store(\"this is a test prompt\", \"this is a test response\")\ncache.check(\"this is also a test prompt\")",
      "section_id": "custom-vectorizers"
    },
    {
      "id": "search-with-provider-embeddings-ex0",
      "language": "yaml",
      "code": "version: '0.1.0'\n\nindex:\n    name: vectorizers\n    prefix: doc\n    storage_type: hash\n\nfields:\n    - name: sentence\n      type: text\n    - name: embedding\n      type: vector\n      attrs:\n        dims: 768\n        algorithm: flat\n        distance_metric: cosine",
      "section_id": "search-with-provider-embeddings"
    },
    {
      "id": "search-with-provider-embeddings-ex1",
      "language": "python",
      "code": "from redisvl.index import SearchIndex\n\n# construct a search index from the schema\nindex = SearchIndex.from_yaml(\"./schema.yaml\", redis_url=\"redis://localhost:6379\")\n\n# create the index (no data yet)\nindex.create(overwrite=True)",
      "section_id": "search-with-provider-embeddings"
    },
    {
      "id": "search-with-provider-embeddings-ex2",
      "language": "python",
      "code": "# use the CLI to see the created index\n!rvl index listall",
      "section_id": "search-with-provider-embeddings"
    },
    {
      "id": "search-with-provider-embeddings-ex3",
      "language": "python",
      "code": "from redisvl.redis.utils import array_to_buffer\n\nembeddings = hf.embed_many(sentences)\n\ndata = [{\"text\": t,\n         \"embedding\": array_to_buffer(v, dtype=\"float32\")}\n        for t, v in zip(sentences, embeddings)]\n\nindex.load(data)",
      "section_id": "search-with-provider-embeddings"
    },
    {
      "id": "search-with-provider-embeddings-ex4",
      "language": "python",
      "code": "from redisvl.query import VectorQuery\n\n# use the HuggingFace vectorizer again to create a query embedding\nquery_embedding = hf.embed(\"That is a happy cat\")\n\nquery = VectorQuery(\n    vector=query_embedding,\n    vector_field_name=\"embedding\",\n    return_fields=[\"text\"],\n    num_results=3\n)\n\nresults = index.query(query)\nfor doc in results:\n    print(doc[\"text\"], doc[\"vector_distance\"])",
      "section_id": "search-with-provider-embeddings"
    },
    {
      "id": "selecting-your-float-data-type-ex0",
      "language": "python",
      "code": "vectorizer = HFTextVectorizer(dtype=\"float16\")\n\n# subsequent calls to embed('', as_buffer=True) and embed_many('', as_buffer=True) will now encode as float16\nfloat16_bytes = vectorizer.embed('test sentence', as_buffer=True)\n\n# to generate embeddings with different dtype instantiate a new vectorizer\nvectorizer_64 = HFTextVectorizer(dtype='float64')\nfloat64_bytes = vectorizer_64.embed('test sentence', as_buffer=True)\n\nfloat16_bytes != float64_bytes",
      "section_id": "selecting-your-float-data-type"
    },
    {
      "id": "cleanup-ex0",
      "language": "python",
      "code": "index.delete()",
      "section_id": "cleanup"
    }
  ]
}
