### Quick Start Example Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md A sequence of commands to set up the project, start Redis, run checks, and stop Redis. ```bash # Set up the project make install # Start Redis make redis-start # Run linting and tests make check # Stop Redis when done make redis-stop ``` -------------------------------- ### VoyageAI Vectorizer Setup Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Installation of the VoyageAI library. ```bash pip install voyageai ``` ```python import getpass ``` -------------------------------- ### VertexAI Vectorizer Setup Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Installation and configuration for Google Cloud VertexAI vectorizer. ```bash pip install google-cloud-aiplatform>=1.26 ``` ```text GOOGLE_APPLICATION_CREDENTIALS= GCP_PROJECT_ID= GCP_LOCATION= ``` ```python from redisvl.utils.vectorize import VertexAIVectorizer # create a vectorizer vtx = VertexAIVectorizer(api_config={ "project_id": os.environ.get("GCP_PROJECT_ID") or getpass.getpass("Enter your GCP Project ID: "), "location": os.environ.get("GCP_LOCATION") or getpass.getpass("Enter your GCP Location: "), "google_application_credentials": os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") or getpass.getpass("Enter your Google App Credentials path: ") }) # embed a sentence test = vtx.embed("This is a test sentence.") test[:10] ``` -------------------------------- ### Huggingface Vectorizer Setup Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Installation and initialization of the Huggingface Sentence Transformers vectorizer. ```bash pip install sentence-transformers ``` ```python os.environ["TOKENIZERS_PARALLELISM"] = "false" from redisvl.utils.vectorize import HFTextVectorizer # create a vectorizer # choose your model from the huggingface website hf = HFTextVectorizer(model="sentence-transformers/all-mpnet-base-v2") # embed a sentence test = hf.embed("This is a test sentence.") test[:10] ``` ```python # You can also create many embeddings at once embeddings = hf.embed_many(sentences, as_buffer=True) ``` -------------------------------- ### Install RedisVL from Source Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Install RedisVL directly from its source code repository. Clone the repository first, then use pip to install. ```bash git clone https://github.com/redis/redis-vl-python.git && cd redis-vl-python pip install . ``` ```bash pip install -e . ``` -------------------------------- ### Install All Optional Dependencies Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Install RedisVL with all available optional dependencies using the '[all]' specifier. ```bash pip install redisvl[all] ``` -------------------------------- ### Cohere Vectorizer Setup Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Installation and usage of the Cohere vectorizer with input type configuration. ```bash pip install cohere ``` ```python import getpass # setup the API Key api_key = os.environ.get("COHERE_API_KEY") or getpass.getpass("Enter your Cohere API key: ") ``` ```python from redisvl.utils.vectorize import CohereTextVectorizer # create a vectorizer co = CohereTextVectorizer( model="embed-english-v3.0", api_config={"api_key": api_key}, ) # embed a search query test = co.embed("This is a test sentence.", input_type='search_query') print("Vector dimensions: ", len(test)) print(test[:10]) # embed a document test = co.embed("This is a test sentence.", input_type='search_document') print("Vector dimensions: ", len(test)) print(test[:10]) ``` -------------------------------- ### Install RedisVL with SQL support Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/12_sql_to_redis_queries.ipynb Command to install the necessary dependencies for SQL-like query functionality. ```bash pip install redisvl[sql-redis] ``` -------------------------------- ### Makefile: Install Dependencies Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs all project dependencies using UV, as defined in the project's configuration. ```bash make install ``` -------------------------------- ### Install Multiple Optional Dependencies Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Install RedisVL with multiple optional dependencies simultaneously by listing them within the brackets. ```bash pip install redisvl[mcp,openai,cohere,sentence-transformers] ``` -------------------------------- ### Load Example Data Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/02_complex_filtering.ipynb Loads example data from a pickle file and displays it using utility functions. ```python import pickle from jupyterutils import table_print, result_print # load in the example data and printing utils data = pickle.load(open("hybrid_example_data.pkl", "rb")) table_print(data) ``` -------------------------------- ### Install and Run Pre-commit Hooks Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Install pre-commit hooks for maintaining code quality and run them manually across all files. ```bash pre-commit install ``` ```bash pre-commit run --all-files ``` -------------------------------- ### Connect to Redis via Sentinel Source: https://github.com/redis/redis-vl-python/blob/main/README.md Example of how to connect to Redis using a Sentinel URL. This is useful for high availability setups. ```python # Connect via Sentinel redis_url="redis+sentinel://sentinel1:26379,sentinel2:26379/mymaster" ``` -------------------------------- ### Initialize RedisVL environment Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/05_hash_vs_json.ipynb Imports necessary modules and loads example data from a pickle file. ```python # import necessary modules import pickle from redisvl.redis.utils import buffer_to_array from redisvl.index import SearchIndex # load in the example data and printing utils data = pickle.load(open("hybrid_example_data.pkl", "rb")) ``` -------------------------------- ### Install OpenAI Library Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Install the OpenAI Python client library. This is required for using the OpenAITextVectorizer. ```bash pip install openai ``` -------------------------------- ### Install Mistral AI Dependency Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Installs the required library for Mistral AI integration. ```bash pip install mistralai ``` -------------------------------- ### Development Installation with uv Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Set up a development environment for RedisVL using uv for dependency management. This installs the package in editable mode and includes development dependencies. ```bash git clone https://github.com/redis/redis-vl-python.git && cd redis-vl-python pip install uv uv sync ``` ```bash make install ``` -------------------------------- ### Install UV Standalone (macOS/Linux) Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs the UV package manager using a standalone script. Recommended for most users. ```bash curl -LsSf https://astral.sh/uv/install.sh | sh ``` -------------------------------- ### Install Amazon Bedrock Dependencies Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Installs the necessary dependencies for Amazon Bedrock support. ```bash pip install 'redisvl[bedrock]' # Installs boto3 ``` -------------------------------- ### Initialize SearchIndex with Redis client Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/12_sql_to_redis_queries.ipynb Setup a SearchIndex using an existing Redis connection instance. ```python from redisvl.index import SearchIndex from redis import Redis client = Redis.from_url("redis://localhost:6379") index = SearchIndex.from_dict(schema, redis_client=client) ``` -------------------------------- ### Install RedisVL Python Package Source: https://github.com/redis/redis-vl-python/blob/main/docs/index.md Install the RedisVL Python package using pip. This is the first step to using the library. ```bash pip install redisvl ``` -------------------------------- ### Install UV Standalone (Windows) Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs the UV package manager using a PowerShell script. Recommended for Windows users. ```powershell powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex" ``` -------------------------------- ### Display example data Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/05_hash_vs_json.ipynb Uses utility functions to print the loaded dataset. ```python from jupyterutils import result_print, table_print table_print(data) ``` -------------------------------- ### Install Cohere Library Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/06_rerankers.ipynb Install the cohere library using pip. This is a prerequisite for using the Cohere Reranker. ```python #!pip install cohere ``` -------------------------------- ### Install UV with pipx Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs the UV package manager using pipx, a tool for installing Python applications in isolated environments. ```bash pipx install uv ``` -------------------------------- ### Check RedisVL CLI Installation Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/cli.ipynb Verify that the redisvl CLI tool is installed and accessible in your environment. ```bash # First, see if the rvl tool is installed !rvl version ``` -------------------------------- ### Compare Query Types Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/11_advanced_queries.ipynb Examples demonstrating TextQuery, HybridQuery (or AggregateHybridQuery), and MultiVectorQuery side-by-side. ```python # TextQuery - keyword-based search text_q = TextQuery( text="shoes", text_field_name="brief_description", return_fields=["product_id", "brief_description"], num_results=3 ) print("TextQuery Results (keyword-based):") result_print(index.query(text_q)) print() ``` ```python if HYBRID_SEARCH_AVAILABLE: # HybridQuery - combines text and vector search hybrid_q = HybridQuery( text="shoes", text_field_name="brief_description", vector=[0.1, 0.2, 0.1], vector_field_name="text_embedding", return_fields=["product_id", "brief_description"], num_results=3, combination_method="LINEAR", yield_text_score_as="text_score", yield_vsim_score_as="vector_similarity", yield_combined_score_as="hybrid_score", ) results = index.query(hybrid_q) else: hybrid_q = AggregateHybridQuery( text="shoes", text_field_name="brief_description", vector=[0.1, 0.2, 0.1], vector_field_name="text_embedding", return_fields=["product_id", "brief_description"], num_results=3, ) results = index.query(hybrid_q) print(f"{hybrid_q.__class__.__name__} Results (text + vector):") result_print(results) print() ``` ```python # MultiVectorQuery - searches multiple vector fields mv_text = Vector( vector=[0.1, 0.2, 0.1], field_name="text_embedding", dtype="float32", weight=0.5 ) mv_image = Vector( vector=[0.8, 0.1], field_name="image_embedding", dtype="float32", weight=0.5 ) multi_q = MultiVectorQuery( vectors=[mv_text, mv_image], return_fields=["product_id", "brief_description"], num_results=3 ) print("MultiVectorQuery Results (multiple vectors):") result_print(index.query(multi_q)) ``` -------------------------------- ### Complete Schema Definition Example Source: https://github.com/redis/redis-vl-python/blob/main/docs/concepts/field-attributes.md A comprehensive example demonstrating various field types, paths, and attributes in a single schema. ```yaml version: "0.1.0" index: name: products prefix: product storage_type: json fields: # Full-text searchable with high relevance - name: title type: text path: $.title attrs: weight: 2.0 sortable: true # Exact-match categories - name: category type: tag path: $.category attrs: separator: "|" index_missing: true # Sortable price with range queries - name: price type: numeric path: $.price attrs: sortable: true # Store-only field for sorting - name: internal_rank type: numeric path: $.internal_rank attrs: sortable: true no_index: true # Vector embeddings - name: embedding type: vector path: $.embedding attrs: algorithm: hnsw dims: 768 distance_metric: cosine # Location search - name: store_location type: geo path: $.location ``` -------------------------------- ### Pagination and Field Projection Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example request demonstrating pagination parameters and field projection. ```json { "query": "science", "limit": 1, "offset": 1, "return_fields": ["content", "category"] } ``` -------------------------------- ### Install UV with Homebrew Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs the UV package manager using the Homebrew package manager on macOS. ```bash brew install uv ``` -------------------------------- ### Makefile: Start Redis Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Starts a Redis instance in a Docker container on the default port 6379. Required for integration tests. ```bash make redis-start ``` -------------------------------- ### Install RedisVL MCP dependencies Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Install the base MCP package or include optional vectorizer providers like OpenAI. ```bash pip install redisvl[mcp] ``` ```bash pip install redisvl[mcp,openai] ``` -------------------------------- ### Install UV with pip Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs the UV package manager using pip. Note that this method might lead to dependency conflicts if not managed carefully. ```bash pip install uv ``` -------------------------------- ### Sync Project Dependencies with UV Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Installs all project dependencies, including development and optional extras, using UV. This command also creates a virtual environment. ```bash uv sync --all-extras ``` -------------------------------- ### Install redisvl with MCP support Source: https://github.com/redis/redis-vl-python/blob/main/README.md Install the redisvl package with the MCP server extra. This requires Python 3.10 or newer. ```bash pip install redisvl[mcp] ``` -------------------------------- ### Initialize Async Redis Client and Index Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/01_getting_started.ipynb Initializes an asynchronous Redis client and an `AsyncSearchIndex`. This setup is recommended for production environments to enable non-blocking operations. ```python from redisvl.index import AsyncSearchIndex from redis.asyncio import Redis client = Redis.from_url("redis://localhost:6379") index = AsyncSearchIndex.from_dict(schema, redis_client=client) ``` -------------------------------- ### Install RedisVL with Optional Dependencies Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Install RedisVL with specific optional dependencies for features like vectorizers or MCP server support. For ZSH users, remember to escape the brackets. ```bash pip install redisvl[openai] ``` ```bash pip install redisvl[cohere] ``` ```bash pip install redisvl[mistralai] ``` ```bash pip install redisvl[voyageai] ``` ```bash pip install redisvl[sentence-transformers] ``` ```bash pip install redisvl[vertexai] ``` ```bash pip install redisvl[bedrock] ``` ```bash pip install redisvl[mcp] ``` ```bash pip install redisvl[langcache] ``` ```bash pip install redisvl[sql-redis] ``` ```bash pip install redisvl[openai] ``` -------------------------------- ### Install voyageai library Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/06_rerankers.ipynb Install the voyageai Python library using pip. This is a prerequisite for using the VoyageAI reranker. ```python #!pip install voyageai ``` -------------------------------- ### Setup OpenAI API Key Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Configure the OpenAI API key. It can be set as an environment variable or entered interactively. ```python import getpass # setup the API Key api_key = os.environ.get("OPENAI_API_API_KEY") or getpass.getpass("Enter your OpenAI API key: ") ``` -------------------------------- ### Start the RedisVL MCP server Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Launch the server using different transport protocols. Note that HTTP and SSE modes are unauthenticated by default. ```bash uvx --from redisvl[mcp] rvl mcp --config /path/to/mcp.yaml ``` ```bash uvx --from redisvl[mcp] rvl mcp --config /path/to/mcp.yaml --transport streamable-http --host 0.0.0.0 --port 8000 ``` ```bash uvx --from redisvl[mcp] rvl mcp --config /path/to/mcp.yaml --transport sse --host 0.0.0.0 --port 9000 ``` ```bash uvx --from redisvl[mcp] rvl mcp --config /path/to/mcp.yaml --read-only ``` -------------------------------- ### Get Recommended SVS-VAMANA Configuration Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/09_svs_vamana.ipynb Use CompressionAdvisor to get optimal SVS-VAMANA configuration based on vector dimensions and performance priorities. This utility helps in balancing memory usage, speed, and search quality. ```python # Get recommended configuration for common embedding dimensions dims = 1024 # Common embedding dimensions (works reliably with SVS-VAMANA) config = CompressionAdvisor.recommend( dims=dims, priority="balanced" # Options: "memory", "speed", "balanced" ) print("Recommended Configuration:") for key, value in config.model_dump().items(): print(f" {key}: {value}") # Estimate memory savings savings = CompressionAdvisor.estimate_memory_savings( config.compression, dims, config.reduce ) print(f"\nEstimated Memory Savings: {savings}%") ``` -------------------------------- ### Run Redis Docker Container Source: https://github.com/redis/redis-vl-python/blob/main/docs/index.md Start a Redis instance using Docker for local development. Ensure Redis is running before connecting. ```bash docker run -d --name redis -p 6379:6379 redis:8.4 ``` -------------------------------- ### Manage Redis development instance Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Commands to start and stop a local Redis instance with Redis Search enabled using Docker. ```bash # Start Redis make redis-start # This runs: # docker run -d --name redis -p 6379:6379 redis:8.4 # Stop when finished make redis-stop ``` -------------------------------- ### Connect to Redis via Sentinel (Sync) Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Example of establishing a synchronous connection to Redis through Sentinel using the redis+sentinel:// URL scheme. ```python from redisvl.index import SearchIndex, AsyncSearchIndex # Sync connection via Sentinel ``` -------------------------------- ### Configure remote MCP client Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example configuration for an MCP client connecting to a remote Streamable HTTP server. ```json { "mcpServers": { "redisvl": { "url": "http://192.168.1.10:8000/mcp", "transport": "streamable-http" } } } ``` -------------------------------- ### Initialize SemanticMessageHistory Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/07_message_history.ipynb Setup the semantic history extension and populate it with recent chat messages. ```python from redisvl.extensions.message_history import SemanticMessageHistory semantic_history = SemanticMessageHistory(name='tutor') semantic_history.add_messages(chat_history.get_recent(top_k=8)) ``` -------------------------------- ### Build and serve documentation Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Commands to build and host the project documentation locally. ```bash # Build the documentation make docs-build # Serve documentation locally at http://localhost:8000 make docs-serve ``` ```bash # Build docs uv run sphinx-build -b html docs docs/_build/html # Serve docs uv run python -m http.server 8000 --directory docs/_build/html ``` -------------------------------- ### Makefile: Serve Documentation Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Serves the project's documentation locally, allowing you to preview changes. ```bash make docs-serve ``` -------------------------------- ### Setup Azure OpenAI API Credentials Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Configure the necessary credentials for Azure OpenAI, including API key, API version, endpoint, and deployment name. ```python # additionally to the API Key, setup the API endpoint and version api_key = os.environ.get("AZURE_OPENAI_API_KEY") or getpass.getpass("Enter your AzureOpenAI API key: ") api_version = os.environ.get("OPENAI_API_VERSION") or getpass.getpass("Enter your AzureOpenAI API version: ") azure_endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT") or getpass.getpass("Enter your AzureOpenAI API endpoint: ") deployment_name = os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", "text-embedding-ada-002") ``` -------------------------------- ### Initialize OpenAI Client and Helper Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/03_llmcache.ipynb Sets up the OpenAI client and a helper function to query the GPT-4o-mini model. ```python import os import getpass import time import numpy as np from openai import OpenAI os.environ["TOKENIZERS_PARALLELISM"] = "False" api_key = os.getenv("OPENAI_API_KEY") or getpass.getpass("Enter your OpenAI API key: ") client = OpenAI(api_key=api_key) def ask_openai(question: str) -> str: response = client.completions.create( model="gpt-4o-mini", prompt=f"Answer the following question simply: {question}", max_tokens=200 ) return response.choices[0].text.strip() ``` -------------------------------- ### Makefile: Build Documentation Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Builds the project's documentation locally. ```bash make docs-build ``` -------------------------------- ### Create sample dataset Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/12_sql_to_redis_queries.ipynb List of dictionaries representing user data and embedding generation. ```python # Office locations use "longitude,latitude" format (lon,lat - Redis convention) # San Francisco: -122.4194, 37.7749 # Chicago: -87.6298, 41.8781 # New York: -73.9857, 40.7580 data = [ { 'user': 'john', 'age': 34, 'job': 'software engineer', 'region': 'us-west', 'job_description': 'Designs, develops, and maintains software applications and systems.', 'office_location': '-122.4194,37.7749' # San Francisco }, { 'user': 'bill', 'age': 54, 'job': 'engineer', 'region': 'us-central', 'job_description': 'Applies scientific and mathematical principles to solve technical problems.', 'office_location': '-87.6298,41.8781' # Chicago }, { 'user': 'mary', 'age': 24, 'job': 'doctor', 'region': 'us-central', 'job_description': 'Diagnoses and treats illnesses, injuries, and other medical conditions in the healthcare field.', 'office_location': '-87.6298,41.8781' # Chicago }, { 'user': 'joe', 'age': 27, 'job': 'dentist', 'region': 'us-east', 'job_description': 'Provides oral healthcare including diagnosing and treating teeth and gum issues.', 'office_location': '-73.9857,40.7580' # New York }, { 'user': 'stacy', 'age': 61, 'job': 'project manager', 'region': 'us-west', 'job_description': 'Plans, organizes, and oversees projects from inception to completion.', 'office_location': '-122.4194,37.7749' # San Francisco } ] data = [ { **d, "job_embedding": hf.embed(f"{d['job_description']=} {d['job']=}"), } for d in data ] ``` -------------------------------- ### Create Index and Load Data Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/11_advanced_queries.ipynb Initializes the search index from the schema and loads data into Redis. ```python from redisvl.index import SearchIndex # Create the search index index = SearchIndex.from_dict(schema, redis_url="redis://localhost:6379") # Create the index and load data index.create(overwrite=True) keys = index.load(data) print(f"Loaded {len(keys)} products into the index") ``` -------------------------------- ### Prepare Sample Dataset Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/01_getting_started.ipynb Create a mock dataset where vector fields are converted to bytes using NumPy's tobytes() method. ```python import numpy as np data = [ { 'user': 'john', 'age': 1, 'job': 'engineer', 'credit_score': 'high', 'user_embedding': np.array([0.1, 0.1, 0.5], dtype=np.float32).tobytes() }, { 'user': 'mary', 'age': 2, 'job': 'doctor', 'credit_score': 'low', 'user_embedding': np.array([0.1, 0.1, 0.5], dtype=np.float32).tobytes() }, { 'user': 'joe', 'age': 3, 'job': 'dentist', 'credit_score': 'medium', 'user_embedding': np.array([0.9, 0.9, 0.1], dtype=np.float32).tobytes() } ] ``` -------------------------------- ### Install RedisVL with Pip Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/installation.md Use this command to install the latest version of RedisVL into your Python environment. Ensure your Python version is 3.9 or newer. ```bash pip install -U redisvl ``` -------------------------------- ### Initialize and Create Search Index Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/02_complex_filtering.ipynb Constructs a SearchIndex object from a schema dictionary and creates the index in Redis. ```python from redisvl.index import SearchIndex # construct a search index from the schema index = SearchIndex.from_dict(schema, redis_url="redis://localhost:6379") # create the index (no data yet) index.create(overwrite=True) ``` -------------------------------- ### Configure VectorQuery with Runtime Parameters Source: https://github.com/redis/redis-vl-python/blob/main/docs/api/query.rst Demonstrates setting runtime parameters for HNSW and SVS-VAMANA indexes during VectorQuery initialization. ```python from redisvl.query import VectorQuery query = VectorQuery( vector=[0.1, 0.2, 0.3], vector_field_name="embedding", num_results=10, ef_runtime=150 # Higher for better recall ) ``` ```python from redisvl.query import VectorQuery query = VectorQuery( vector=[0.1, 0.2, 0.3], vector_field_name="embedding", num_results=10, search_window_size=20, use_search_history='ON', search_buffer_capacity=30 ) ``` -------------------------------- ### Get Number of Documents in Index Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/08_semantic_router.ipynb Access the `info()` method on the router's index object to get the total number of documents currently stored in the index. ```python router._index.info()["num_docs"] ``` -------------------------------- ### Initialize SearchIndex from schema Source: https://github.com/redis/redis-vl-python/blob/main/CLAUDE.md Create a SearchIndex instance by loading a YAML schema definition. ```python # Index schemas define structure schema = IndexSchema.from_yaml("schema.yaml") index = SearchIndex(schema, redis_url="redis://localhost:6379") ``` -------------------------------- ### Run Code Formatting and Import Sorting with UV Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Directly runs isort for import sorting and black for code formatting using UV. Ensures code adheres to project standards. ```bash uv run isort ./redisvl ./tests/ --profile black uv run black ./redisvl ./tests/ uv run mypy redisvl ``` -------------------------------- ### Hybrid Search Request Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example request payload for a hybrid search. ```json { "query": "legacy cache invalidation flow", "filter": { "field": "category", "op": "eq", "value": "release-notes" }, "return_fields": ["title", "content", "release_version"] } ``` -------------------------------- ### Vector Search Request Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example request payload for a vector-type search. ```json { "query": "cache invalidation incident", "limit": 3, "return_fields": ["title", "content", "category"] } ``` -------------------------------- ### Execute Basic Vector Query Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/09_svs_vamana.ipynb Demonstrates a standard vector query using default runtime parameters. ```python # Example 1: Basic query with default parameters basic_query = VectorQuery( vector=query_vector.tolist(), vector_field_name="embedding", return_fields=["content", "category"], num_results=5 ) print("🔍 Basic Query (default parameters):") results = index.query(basic_query) print(f"Found {len(results)} results\n") ``` -------------------------------- ### System warning output Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/12_sql_to_redis_queries.ipynb Example of a system warning message encountered during execution. ```text /Users/robert.shelton/Documents/redis-vl-python/.venv/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm ``` -------------------------------- ### Define Index Schema Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Example YAML configuration for a RedisVL index schema. ```yaml version: '0.1.0' index: name: vectorizers prefix: doc storage_type: hash fields: - name: sentence type: text - name: embedding type: vector attrs: dims: 768 algorithm: flat distance_metric: cosine ``` -------------------------------- ### Retrieve Message Counts Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/07_message_history.ipynb Get the total number of messages stored in a session. ```python print(f"Total messages in the session: {chat_history.count()}") ``` -------------------------------- ### Define stopwords in YAML Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/11_advanced_queries.ipynb Examples of configuring stopwords within a YAML schema file. ```yaml version: '0.1.0' index: name: company_index prefix: company: storage_type: hash stopwords: [] # Disable stopwords (STOPWORDS 0) fields: - name: company_name type: text - name: description type: text ``` ```yaml index: stopwords: - the - a - an ``` -------------------------------- ### Define Hash structure Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/05_hash_vs_json.ipynb Example of a single-level dictionary structure used in Redis Hashes. ```json { "model": "Deimos", "brand": "Ergonom", "type": "Enduro bikes", "price": 4972, } ``` -------------------------------- ### Makefile: Stop Redis Source: https://github.com/redis/redis-vl-python/blob/main/CONTRIBUTING.md Stops the Redis Docker container that was started with `make redis-start`. ```bash make redis-stop ``` -------------------------------- ### JSON DSL Filter Search Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example request using the JSON DSL for logical filtering. ```json { "query": "science", "filter": { "and": [ { "field": "category", "op": "eq", "value": "science" }, { "field": "rating", "op": "gte", "value": 4 } ] }, "return_fields": ["content", "category", "rating"] } ``` -------------------------------- ### Raw String Filter Search Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example request using a raw Redis filter string. ```json { "query": "science", "filter": "@category:{science}", "return_fields": ["content", "category"] } ``` -------------------------------- ### Create a SearchIndex Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/01_getting_started.ipynb Initialize a SearchIndex using either an existing Redis client instance or a connection URL. ```python from redisvl.index import SearchIndex from redis import Redis client = Redis.from_url("redis://localhost:6379") index = SearchIndex.from_dict(schema, redis_client=client, validate_on_load=True) ``` ```python index = SearchIndex.from_dict(schema, redis_url="redis://localhost:6379", validate_on_load=True) # If you don't specify a client or Redis URL, the index will attempt to # connect to Redis at the default address "redis://localhost:6379". ``` -------------------------------- ### Initialize Semantic Cache Source: https://github.com/redis/redis-vl-python/blob/main/README.md Set up a SemanticCache for LLM applications to increase throughput and reduce costs by reusing previous results. Configure TTL, Redis connection URL, and a distance threshold for similarity. ```python from redisvl.extensions.cache.llm import SemanticCache # init cache with TTL and semantic distance threshold llmcache = SemanticCache( name="llmcache", ttl=360, redis_url="redis://localhost:6379", distance_threshold=0.1 # Redis COSINE distance [0-2], lower is stricter ) ``` -------------------------------- ### Upsert Records Response Payload Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example JSON response for the upsert-records tool, confirming the number of keys upserted. ```json { "status": "success", "keys_upserted": 1, "keys": ["knowledge:doc-42"] } ``` -------------------------------- ### Upsert Records Request Payload Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example JSON payload for the upsert-records tool, specifying records and the ID field. ```json { "records": [ { "doc_id": "doc-42", "content": "Updated operational guidance for failover handling.", "category": "operations", "rating": 5 } ], "id_field": "doc_id" } ``` -------------------------------- ### Construct and create Hash index Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/05_hash_vs_json.ipynb Initializes the SearchIndex from the schema and creates it in Redis. ```python # construct a search index from the hash schema hindex = SearchIndex.from_dict(hash_schema, redis_url="redis://localhost:6379") # create the index (no data yet) hindex.create(overwrite=True) ``` -------------------------------- ### Search Records Response Payload Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example JSON response for the search-records tool, including metadata and retrieved records. ```json { "search_type": "hybrid", "offset": 0, "limit": 2, "results": [ { "id": "knowledge:runbook:eu-failover", "score": 0.82, "score_type": "hybrid_score", "record": { "title": "EU failover runbook", "content": "Restore traffic after a regional failover.", "category": "operations", "rating": 5 } } ] } ``` -------------------------------- ### Configure VectorRangeQuery with Runtime Parameters Source: https://github.com/redis/redis-vl-python/blob/main/docs/api/query.rst Shows how to apply approximation factors and SVS-VAMANA specific parameters to a range query. ```python from redisvl.query import VectorRangeQuery query = VectorRangeQuery( vector=[0.1, 0.2, 0.3], vector_field_name="embedding", distance_threshold=0.3, epsilon=0.05, # Approximation factor search_window_size=20, # SVS-VAMANA only use_search_history='AUTO' # SVS-VAMANA only ) ``` -------------------------------- ### Initialize Search Index from Dictionary Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/12_sql_to_redis_queries.ipynb Create a SearchIndex instance by providing a schema dictionary and a Redis connection URL. This is suitable for simple use cases where the index manages the connection. ```python index = SearchIndex.from_dict(schema, redis_url="redis://localhost:6379") ``` -------------------------------- ### Search Records Request Payload Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/how_to_guides/mcp.md Example JSON payload for the search-records tool, demonstrating query, pagination, and filter parameters. ```json { "query": "incident response runbook", "limit": 2, "offset": 0, "filter": { "and": [ { "field": "category", "op": "eq", "value": "operations" }, { "field": "rating", "op": "gte", "value": 4 } ] }, "return_fields": ["title", "content", "category", "rating"] } ``` -------------------------------- ### Set up VoyageAI API Key Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/06_rerankers.ipynb Configure the VoyageAI API key by retrieving it from environment variables or prompting the user. This key is required for authentication. ```python import getpass # setup the API Key api_key = os.environ.get("VOYAGE_API_KEY") or getpass.getpass("Enter your VoyageAI API key: ") ``` -------------------------------- ### Clear Caches Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/10_embeddings_cache.ipynb Demonstrates how to clear all initialized caches to remove stored embedding data from Redis. This is useful for cleanup after examples or testing. ```python # Clean up all caches cache.clear() ttl_cache.clear() example_cache.clear() benchmark_cache.clear() ``` -------------------------------- ### Generate and Load Sample Data into Index Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/09_svs_vamana.ipynb Prepares sample documents with random embeddings and loads them into the index. Ensure the vector dimensions and datatype match the index configuration. ```python sample_documents = [ {"content": "Machine learning algorithms for data analysis", "category": "technology"}, {"content": "Natural language processing and text understanding", "category": "technology"}, {"content": "Computer vision and image recognition systems", "category": "technology"}, {"content": "Delicious pasta recipes from Italy", "category": "food"}, {"content": "Traditional French cooking techniques", "category": "food"}, {"content": "Healthy meal planning and nutrition", "category": "food"}, {"content": "Travel guide to European destinations", "category": "travel"}, {"content": "Adventure hiking in mountain regions", "category": "travel"}, {"content": "Cultural experiences in Asian cities", "category": "travel"}, {"content": "Financial planning for retirement", "category": "finance"}, ] # Generate random embeddings for demonstration # In practice, you would use a real embedding model data_to_load = [] # Use reduced dimensions if LeanVec compression is applied vector_dims = config.reduce if config.reduce is not None else dims print(f"Creating vectors with {vector_dims} dimensions (reduced from {dims} if applicable)") for i, doc in enumerate(sample_documents): # Create a random vector with some category-based clustering base_vector = np.random.random(vector_dims).astype(np.float32) # Add some category-based similarity (optional, for demo purposes) category_offset = hash(doc["category"]) % 100 / 1000.0 base_vector[0] += category_offset # Convert to the datatype specified in config if config.datatype == "float16": base_vector = base_vector.astype(np.float16) data_to_load.append({ "content": doc["content"], "category": doc["category"], "embedding": array_to_buffer(base_vector, dtype=config.datatype) }) # Load data into the index index.load(data_to_load) print(f"✅ Loaded {len(data_to_load)} documents into the index") # Wait a moment for indexing to complete import time time.sleep(2) # Verify the data was loaded info = index.info() print(f" Index now contains {info.get('num_docs', 0)} documents") ``` -------------------------------- ### Run pre-commit hooks Source: https://github.com/redis/redis-vl-python/blob/main/CLAUDE.md Execute all configured pre-commit hooks across the repository. ```bash pre-commit run --all-files ``` -------------------------------- ### Example Nested JSON Object Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/05_hash_vs_json.ipynb Illustrates a nested JSON object structure, suitable for indexing and querying with RedisVL's JSON support. ```json { "name": "Specialized Stump jumper", "metadata": { "model": "Stumpjumper", "brand": "Specialized", "type": "Enduro bikes", "price": 3000 }, } ``` -------------------------------- ### Initialize AzureOpenAITextVectorizer Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Create an instance of the vectorizer by providing the deployment name and necessary API credentials. ```python az_oai = AzureOpenAITextVectorizer( model=deployment_name, # Must be your CUSTOM deployment name api_config={ "api_key": api_key, "api_version": api_version, "azure_endpoint": azure_endpoint }, ) test = az_oai.embed("This is a test sentence.") print("Vector dimensions: ", len(test)) test[:10] ``` -------------------------------- ### Run MCP Server in Read-Only Mode Source: https://github.com/redis/redis-vl-python/blob/main/README.md Start the RedisVL MCP server in read-only mode, allowing only search operations and disabling upserts. ```bash uvx --from redisvl[mcp] rvl mcp --config /path/to/mcp.yaml --read-only ``` -------------------------------- ### Define Geo Field in Python Dictionary Source: https://github.com/redis/redis-vl-python/blob/main/docs/api/schema.rst Example of defining a geo field using a Python dictionary, specifying its name, type, and sortable attribute. ```python { "name": "location", "type": "geo", "attrs": { "sortable": true } } ``` -------------------------------- ### List Indices via CLI Source: https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/04_vectorizers.ipynb Uses the RedisVL CLI to list all available indices. ```python !rvl index listall ```