### Load Example Data and Utilities Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/complex_filtering Loads example data from a pickle file and utility functions for printing results. This is a setup step for subsequent examples. ```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) ``` -------------------------------- ### Node.js: Setup and Geo-spatial Search Examples Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/geo-spatial Demonstrates setting up a Redis index with geo-spatial fields and performing searches. Includes creating an index, loading data, and executing searches for points within a radius and polygons. ```JavaScript import assert from 'node:assert'; import fs from 'node:fs'; import { createClient } from 'redis'; import { SCHEMA_FIELD_TYPE } from '@redis/search'; const client = createClient(); await client.connect().catch(console.error); // create index await client.ft.create('idx:bicycle', { '$.store_location': { type: SCHEMA_FIELD_TYPE.GEO, AS: 'store_location' }, '$.pickup_zone': { type: SCHEMA_FIELD_TYPE.GEOSHAPE, AS: 'pickup_zone' } }, { ON: 'JSON', PREFIX: 'bicycle:' }) // load data const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8')); await Promise.all( bicycles.map((bicycle, bid) => { return client.json.set(`bicycle:${bid}`, '$', bicycle); }) ); const res1= await client.ft.search('idx:bicycle', '@store_location:[-0.1778 51.5524 20 mi]'); console.log(res1.total); // >>> 1 console.log(res1); // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]} const params_dict_geo2 = { bike: 'POINT(-0.1278 51.5074)' }; const q_geo2 = '@pickup_zone:[CONTAINS $bike]'; const res2 = await client.ft.search('idx:bicycle', q_geo2, { PARAMS: params_dict_geo2, DIALECT: 3 }); console.log(res2.total); // >>> 1 console.log(res2); // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]} const params_dict_geo3 = { europe: 'POLYGON((-25 35, 40 35, 40 70, -25 70, -25 35))' }; const q_geo3 = '@pickup_zone:[WITHIN $europe]'; const res3 = await client.ft.search('idx:bicycle', q_geo3, { PARAMS: params_dict_geo3, DIALECT: 3 }); console.log(res3.total); // >>> 5 console.log(res3); // >>> // { // total: 5, // documents: [ // { id: 'bicycle:5', value: [Object: null prototype] }, // { id: 'bicycle:6', value: [Object: null prototype] }, // { id: 'bicycle:7', value: [Object: null prototype] }, // { id: 'bicycle:8', value: [Object: null prototype] }, // { id: 'bicycle:9', value: [Object: null prototype] } // ] // } ``` -------------------------------- ### Install RediSearch Prerequisites Script Source: https://redis.io/docs/latest/develop/ai/search-and-query/deprecated/development Navigate to the 'install' directory and execute the setup scripts to install prerequisites for building and testing RediSearch. 'sudo' is used for system-wide installations but is not required in a Docker environment. ```bash cd ./install ./install_script.sh sudo ./install_boost.sh 1.83.0 ``` -------------------------------- ### Install RedisVL from Source Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/installation Clone the RedisVL Python repository and install the package using pip. Use the editable installation for development purposes. ```bash $ git clone https://github.com/redis/redis-vl-python.git && cd redis-vl-python $ pip install . # or for an editable installation (for developers of RedisVL) $ pip install -e . ``` -------------------------------- ### Install RedisVL with Multiple Optional Dependencies Source: https://redis.io/docs/latest/develop/ai/redisvl/user_guide/installation Install redisvl with a combination of multiple optional dependencies simultaneously. This allows for a more customized installation. ```bash $ pip install redisvl[mcp,openai,cohere,sentence-transformers] ``` -------------------------------- ### Install and Initialize VoyageAI Reranker Source: https://redis.io/docs/latest/develop/ai/redisvl/user_guide/how_to_guides/rerankers Install the voyageai library and set up your VoyageAI API key to initialize the VoyageAIReranker. This snippet shows the necessary installation command, API key retrieval, and reranker instantiation. ```bash #!pip install voyageai ``` ```python import getpass # setup the API Key api_key = os.environ.get("VOYAGE_API_KEY") or getpass.getpass("Enter your VoyageAI API key: ") ``` ```python from redisvl.utils.rerank import VoyageAIReranker reranker = VoyageAIReranker(model="rerank-lite-1", limit=3, api_config={"api_key": api_key}) # Please check the available models at https://docs.voyageai.com/docs/reranker ``` -------------------------------- ### Install RedisVL with All Optional Dependencies Source: https://redis.io/docs/latest/develop/ai/redisvl/user_guide/installation Install redisvl with all available optional dependencies. Use this command when you need the full feature set of RedisVL. ```bash $ pip install redisvl[all] ``` -------------------------------- ### Basic Aggregation with Field Loading Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/aggregation This example demonstrates a basic aggregation query that loads the 'price' field for all documents matching the wildcard '*'. It serves as a starting point for more complex aggregation pipelines. ```go res2, err := rdb.FTAggregateWithArgs(ctx, "idx:bicycle", "*", &redis.FTAggregateOptions{ Steps: []redis.FTAggregateStep{ {Load: &redis.FTAggregateLoad{Field: "price"}}, }, }, ).Result() ``` -------------------------------- ### Install Google Cloud AI Platform Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/vectorizers Installs the 'google-cloud-aiplatform' library for VertexAI vectorization. ```bash pip install google-cloud-aiplatform>=1.26 ``` -------------------------------- ### Register Redis Provider (Example) Source: https://redis.io/docs/latest/develop/ai/featureform/streaming Example of registering a Redis provider with a specific workspace ID. This is used for online feature serving. ```bash ff provider register demo_redis \ --workspace demo-workspace \ --type redis \ --redis-host -featureform-redis \ --redis-port 6379 ``` -------------------------------- ### Setup Redis Index for Date Examples Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/sql_to_redis_queries This code sets up a Redis index for storing and querying event data. It defines a schema with text, tag, and numeric fields, and loads sample event data with timestamps. Ensure Redis is running and accessible. ```python from datetime import datetime, timezone def to_timestamp(date_str): """Convert ISO date string to Unix timestamp (UTC).""" dt = datetime.strptime(date_str, "%Y-%m-%d") dt = dt.replace(tzinfo=timezone.utc) return int(dt.timestamp()) events_schema = { "index": { "name": "events", "prefix": "event:", "storage_type": "hash", }, "fields": [ {"name": "name", "type": "text", "attrs": {"sortable": True}}, {"name": "category", "type": "tag", "attrs": {"sortable": True}}, {"name": "created_at", "type": "numeric", "attrs": {"sortable": True}}, ], } events_index = SearchIndex.from_dict(events_schema, redis_url="redis://localhost:6379") events_index.create(overwrite=True) events = [ {"name": "New Year Kickoff", "category": "meeting", "created_at": to_timestamp("2024-01-01")}, {"name": "Q1 Planning", "category": "meeting", "created_at": to_timestamp("2024-01-15")}, {"name": "Product Launch", "category": "release", "created_at": to_timestamp("2024-02-20")}, {"name": "Team Offsite", "category": "meeting", "created_at": to_timestamp("2024-03-10")}, {"name": "Summer Summit", "category": "conference", "created_at": to_timestamp("2024-07-15")}, {"name": "Holiday Party 2023", "category": "conference", "created_at": to_timestamp("2023-12-15")}, {"name": "Year End Review 2023", "category": "meeting", "created_at": to_timestamp("2023-12-20")}, ] events_index.load(events) print(f"Loaded {len(events)} events:") for e in events: date = datetime.fromtimestamp(e["created_at"], tz=timezone.utc).strftime("%Y-%m-%d") print(f" - {e['name']:25} | {date} | {e['category']}") ``` -------------------------------- ### Install Cohere Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/vectorizers Installs the 'cohere' library for Cohere vectorization. ```bash pip install cohere ``` -------------------------------- ### Register Postgres Provider (Example) Source: https://redis.io/docs/latest/develop/ai/featureform/streaming Example of registering a Postgres provider with a specific workspace ID. This is used for offline storage and SQL execution. ```bash ff provider register demo_postgres \ --workspace demo-workspace \ --type postgres \ --pg-host -featureform-provider-postgres \ --pg-port 5432 \ --pg-database featureform_test \ --pg-user testuser \ --pg-password-secret env:PG_PASSWORD \ --pg-ssl-mode disable ``` -------------------------------- ### Sort and Limit Example Source: https://redis.io/docs/latest/develop/ai/redisvl/api/query Example demonstrating how to sort initial results and then apply a limit for grouping. This is useful for sampling large datasets before aggregation. ```python AggregateRequest("@sale_amount:[10000, inf]") .limit(0, 10) .group_by("@state", r.count()) ``` ```python AggregateRequest("@sale_amount:[10000, inf]") .limit(0, 1000) .group_by("@state", r.count() .limit(0, 10) ``` -------------------------------- ### Go Redis Full-Text Search Example Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/full-text Example of performing full-text search queries using the Go Redis client. ```Go package example_commands_test import ( "context" "fmt" "sort" "github.com/redis/go-redis/v9" ) func ExampleClient_query_ft() { ctx := context.Background() client := redis.NewClient(&redis.Options{ Addr: "localhost:6379", }) // Example: Add some data first (assuming a schema is defined) // client.Do(ctx, "FT.CREATE", "myidx", "ON", "JSON", "PREFIX", "1", "doc:", "SCHEMA", "$.name", "AS", "name", "TEXT", "$.age", "AS", "age", "NUMERIC") // client.Do(ctx, "JSON.SET", "doc:1", ".", "{\"name\": \"Alice\", \"age\": 30}") // client.Do(ctx, "JSON.SET", "doc:2", ".", "{\"name\": \"Bob\", \"age\": 25}") // Example search query searchResult, err := client.Do(ctx, "FT.SEARCH", "myidx", "Alice").Result() if err != nil { fmt.Println("Error performing search:", err) return } fmt.Printf("Search results: %+v\n", searchResult) // Example of processing results (structure depends on the actual search result format) // This is a simplified representation. s// results := searchResult.([]interface{}) // if len(results) > 1 { // // The first element is usually the total number of results // // Subsequent elements are the documents // documents := results[1:] // var ids []string // for _, doc := range documents { // // Assuming document is a slice where the first element is the ID // docSlice := doc.([]interface{}) // id := docSlice[0].(string) // ids = append(ids, id) // } // sort.Strings(ids) // fmt.Println("Sorted IDs:", ids) // } // Output: Search results: ... (actual output will vary based on data and schema) // Sorted IDs: [doc:1] // Close the client err = client.Close() if err != nil { fmt.Println("Error closing client:", err) } } ``` -------------------------------- ### Install RedisVL with SQL Support Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/sql_to_redis_queries Install the necessary package to enable SQL query capabilities in RedisVL. This is a prerequisite for using the SQLQuery class. ```bash pip install redisvl[sql-redis] ``` -------------------------------- ### Example: Querying for New Bicycles Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/exact-match This example demonstrates how to find all bicycles tagged as 'new' in the 'condition' field of a search index named 'idx:bicycle'. ```redis > FT.SEARCH idx:bicycle "@condition:{new}" ``` -------------------------------- ### Install Pre-commit Hooks Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/installation Install and run pre-commit hooks to maintain code quality. This ensures code adheres to project standards before committing. ```bash $ pre-commit install # Run hooks manually on all files: $ pre-commit run --all-files ``` -------------------------------- ### Development Installation with uv Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/installation Set up a development environment for contributing to RedisVL using uv for dependency management. This installs the package in editable mode with all development and documentation dependencies. ```bash # Clone the repository $ git clone https://github.com/redis/redis-vl-python.git && cd redis-vl-python # Install uv if you don't have it $ pip install uv # Install all dependencies (including dev and docs) $ uv sync # Or use make $ make install ``` -------------------------------- ### Install RedisVL with Optional Dependencies Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/installation Install redisvl with specific optional dependencies for various vectorizer providers and other features. Use brackets to specify multiple dependencies. ```bash # Vectorizer providers $ pip install redisvl[openai] # OpenAI embeddings $ pip install redisvl[cohere] # Cohere embeddings and reranking $ pip install redisvl[mistralai] # Mistral AI embeddings $ pip install redisvl[voyageai] # Voyage AI embeddings and reranking $ pip install redisvl[sentence-transformers] # HuggingFace local embeddings $ pip install redisvl[vertexai] # Google Vertex AI embeddings $ pip install redisvl[bedrock] # AWS Bedrock embeddings # Other optional features $ pip install redisvl[mcp] # RedisVL MCP server support (Python 3.10+) $ pip install redisvl[langcache] # LangCache managed service integration $ pip install redisvl[sql-redis] # SQL query support ``` ```bash # If using ZSH, escape brackets for optional dependencies $ pip install redisvl\[openai\] ``` ```bash # Install multiple optional dependencies at once $ pip install redisvl[mcp,openai,cohere,sentence-transformers] ``` ```bash # Install all optional dependencies at once $ pip install redisvl[all] ``` -------------------------------- ### CompressionAdvisor Examples Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/api/schema Examples demonstrating how to use the CompressionAdvisor to get configuration recommendations and estimate memory savings. ```APIDOC ## CompressionAdvisor Examples ### Get recommendations: ```python config = CompressionAdvisor.recommend(dims=1536, priority="balanced") print(config.compression) # Output: 'LeanVec4x8' print(config.reduce) # Output: 768 ``` ### Estimate memory savings: ```python savings = CompressionAdvisor.estimate_memory_savings( compression="LeanVec4x8", dims=1536, reduce=768 ) print(savings) # Output: 81.2 ``` ``` -------------------------------- ### Go Redis Client Full-Text Search Example Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/full-text Demonstrates how to perform full-text searches using the Redis Go client. This example sets up a context and initializes a Redis client. ```Go package example_commands_test import ( "context" "fmt" "sort" "github.com/redis/go-redis/v9" ) func ExampleClient_query_ft() { ctx := context.Background() redisClient := redis.NewClient(&redis.Options{ Addr: "localhost:6379", }) // Ping to check connection _, err := redisClient.Ping(ctx).Result() if err != nil { fmt.Printf("Error connecting to Redis: %v\n", err) return } fmt.Println("Connected to Redis") // Example: Add some data (replace with your actual data insertion logic) // redisClient.HSet(ctx, "doc:1", "title", "Example Document 1", "body", "This is the body of document 1.") // redisClient.HSet(ctx, "doc:2", "title", "Another Document", "body", "Content for document 2.") // Example: Perform a full-text search // searchResult, err := redisClient.Do(ctx, "FT.SEARCH", "idx:myindex", "@body:document").Result() // if err != nil { // fmt.Printf("Search error: %v\n", err) // return // } // fmt.Printf("Search results: %v\n", searchResult) // Remember to close the client when done defer redisClient.Close() } ``` -------------------------------- ### Development Help Summary Source: https://redis.io/docs/latest/develop/ai/search-and-query/deprecated/development Use `make help` to get a quick summary of available development features and commands. ```bash make help ``` -------------------------------- ### Install RedisVL with Escaped Brackets for ZSH Source: https://redis.io/docs/latest/develop/ai/redisvl/user_guide/installation When using ZSH, brackets in pip install commands must be escaped. This example shows how to install redisvl with OpenAI support in ZSH. ```bash $ pip install redisvl\[openai\] ``` -------------------------------- ### Lettuce-Redis Java Quick-Start Setup Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/aggregation Initializes a Redis client connection and prepares asynchronous commands for interacting with Redis. This is boilerplate for using the Lettuce client library. ```Java import io.lettuce.core.*; import io.lettuce.core.api.async.RedisAsyncCommands; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.json.JsonPath; import io.lettuce.core.json.JsonParser; import io.lettuce.core.json.JsonObject; import io.lettuce.core.search.arguments.AggregateArgs; import io.lettuce.core.search.arguments.AggregateArgs.GroupBy; import io.lettuce.core.search.arguments.AggregateArgs.Reducer; import io.lettuce.core.search.AggregationReply; import io.lettuce.core.search.arguments.CreateArgs; import io.lettuce.core.search.arguments.FieldArgs; import io.lettuce.core.search.arguments.NumericFieldArgs; import io.lettuce.core.search.arguments.TagFieldArgs; import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; public class QueryAggregationExample { public void run() { RedisClient redisClient = RedisClient.create("redis://localhost:6379"); try (StatefulRedisConnection connection = redisClient.connect()) { RedisAsyncCommands asyncCommands = connection.async(); JsonParser parser = asyncCommands.getJsonParser(); // create index List> schema = Arrays.asList( TagFieldArgs. builder().name("$.condition").as("condition").build(), NumericFieldArgs. builder().name("$.price").as("price").build()); CreateArgs createArgs = CreateArgs. builder().withPrefix("bicycle:") .on(CreateArgs.TargetType.JSON).build(); // load data using JsonParser List bicycleJsons = Arrays.asList(parser.createJsonObject().put("pickup_zone", parser.createJsonValue( "\"POLYGON((-74.0610 40.7578, -73.9510 40.7578, -73.9510 40.6678, -74.0610 40.6678, -74.0610 40.7578))\"")) .put("store_location", parser.createJsonValue("\"-74.0060,40.7128\"")) .put("brand", parser.createJsonValue("\"Velorim\"")).put("model", parser.createJsonValue("\"Jigger\"")) ``` -------------------------------- ### Example Range Query for Description (Lexicographical) Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/range This example shows a lexicographical range query on the 'description' field, searching for documents where the description starts with words between 'a' (inclusive) and 'z' (exclusive). ```redis FT.SEARCH bikes "[@description:{a z}]" ``` -------------------------------- ### Overview Workspace Graph Source: https://redis.io/docs/latest/develop/ai/featureform/training-sets-and-feature-views Use this command to get an overview of the workspace graph. Ensure you have the 'ff' CLI installed and configured. ```bash ff graph workspace overview --workspace demo-workspace ``` -------------------------------- ### Java Async Quick-Start with Redis Search Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/exact-match Sets up a Redis client connection and demonstrates creating a search index for bicycle data. This includes defining fields for brand, model, description, price, and condition. ```java import io.lettuce.core.*; import io.lettuce.core.api.reactive.RedisReactiveCommands; import io.lettuce.core.search.arguments.*; import io.lettuce.core.search.SearchReply; import io.lettuce.core.json.JsonPath; import io.lettuce.core.json.JsonParser; import io.lettuce.core.json.JsonObject; import io.lettuce.core.api.StatefulRedisConnection; import java.util.*; import reactor.core.publisher.Mono; public class QueryEmExample { public void run() { RedisClient redisClient = RedisClient.create("redis://localhost:6379"); try (StatefulRedisConnection connection = redisClient.connect()) { RedisReactiveCommands reactiveCommands = connection.reactive(); List> bicycleSchema = Arrays.asList( TextFieldArgs. builder().name("$.brand").as("brand").build(), TextFieldArgs. builder().name("$.model").as("model").build(), TextFieldArgs. builder().name("$.description").as("description").build(), NumericFieldArgs. builder().name("$.price").as("price").build(), TagFieldArgs. builder().name("$.condition").as("condition").build()); CreateArgs bicycleCreateArgs = CreateArgs. builder().on(CreateArgs.TargetType.JSON) .withPrefix("bicycle:").build(); reactiveCommands.ftCreate("idx:bicycle", bicycleCreateArgs, bicycleSchema).block(); JsonParser parser = reactiveCommands.getJsonParser(); List bicycleJsons = Arrays.asList(parser.createJsonObject() .put("brand", parser.createJsonValue("\"Velorim\"")).put("model", parser.createJsonValue("\"Jigger\"")) .put("description", parser .createJsonValue("\"Small and powerful, the Jigger is the best ride for the smallest of tikes! " + "This is the tiniest kids’ pedal bike on the market available without a coaster brake, the Jigger " + "is the vehicle of choice for the rare tenacious little rider raring to go.\"")) .put("price", parser.createJsonValue("270")).put("condition", parser.createJsonValue("\"new\"")), parser.createJsonObject().put("brand", parser.createJsonValue("\"Bicyk\"")) .put("model", parser.createJsonValue("\"Hillcraft\"")) .put("description", ``` -------------------------------- ### Python: Setup and Index Creation Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/combined Initializes Redis client, defines a schema with text, tag, numeric, and vector fields, and creates a JSON index. ```Python import json import numpy as np import redis import warnings from redis.commands.json.path import Path from redis.commands.search.field import NumericField, TagField, TextField, VectorField from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query from sentence_transformers import SentenceTransformer def embed_text(model, text): return np.array(model.encode(text)).astype(np.float32).tobytes() warnings.filterwarnings("ignore", category=FutureWarning, message=r".*clean_up_tokenization_spaces.*" ) model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') query = "Bike for small kids" query_vector = embed_text(model, query) r = redis.Redis(decode_responses=True) # create index schema = ( TextField("$.description", no_stem=True, as_name="model"), TagField("$.condition", as_name="condition"), NumericField("$.price", as_name="price"), VectorField( "$.description_embeddings", "FLAT", { "TYPE": "FLOAT32", "DIM": 384, "DISTANCE_METRIC": "COSINE", }, as_name="vector", ), ) index = r.ft("idx:bicycle") index.create_index( schema, definition=IndexDefinition(prefix=["bicycle:"], index_type=IndexType.JSON), ) ``` -------------------------------- ### CountQuery Example Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/api/query Demonstrates the CountQuery for retrieving the number of documents matching a filter. Useful for getting result counts without fetching data. ```python from redis.commands.search.query import CountQuery # Example usage of CountQuery query = CountQuery(filter_expression="@category == 'books'") ``` -------------------------------- ### Search JSON Documents with Prefix Matching Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/full-text This example demonstrates prefix matching for text fields. It finds documents where the 'brand' field starts with 'Eva'. ```json { "query": { "text_field": { "path": "brand", "query": "Eva*" } } } ``` -------------------------------- ### Python Redis Client Setup and Basic Queries Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/combined Sets up a Redis JSON index and demonstrates basic text, tag, and range queries. ```python import json import numpy as np import redis import warnings from redis.commands.json.path import Path from redis.commands.search.field import NumericField, TagField, TextField, VectorField from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query from sentence_transformers import SentenceTransformer def embed_text(model, text): return np.array(model.encode(text)).astype(np.float32).tobytes() warnings.filterwarnings("ignore", category=FutureWarning, message=r".*clean_up_tokenization_spaces.*") model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') query = "Bike for small kids" query_vector = embed_text(model, query) r = redis.Redis(decode_responses=True) # create index schema = ( TextField("$.description", no_stem=True, as_name="model"), TagField("$.condition", as_name="condition"), NumericField("$.price", as_name="price"), VectorField( "$.description_embeddings", "FLAT", { "TYPE": "FLOAT32", "DIM": 384, "DISTANCE_METRIC": "COSINE", }, as_name="vector", ), ) index = r.ft("idx:bicycle") index.create_index( schema, definition=IndexDefinition(prefix=["bicycle:"], index_type=IndexType.JSON), ) # load data with open("data/query_vector.json") as f: bicycles = json.load(f) pipeline = r.pipeline(transaction=False) for bid, bicycle in enumerate(bicycles): pipeline.json().set(f'bicycle:{bid}', Path.root_path(), bicycle) pipeline.execute() q = Query("@price:[500 1000] @condition:{new}") res = index.search(q) print(res.total) # >>> 1 q = Query("kids @price:[500 1000] @condition:{used}") res = index.search(q) print(res.total) # >>> 1 q = Query("(kids | small) @condition:{used}") res = index.search(q) print(res.total) # >>> 2 q = Query("@description:(kids | small) @condition:{used}") res = index.search(q) print(res.total) # >>> 0 q = Query("@description:(kids | small) @condition:{new | used}") res = index.search(q) print(res.total) # >>> 0 q = Query("@price:[500 1000] -@condition:{new}") res = index.search(q) print(res.total) # >>> 2 q = Query("(@price:[500 1000] -@condition:{new})=>[KNN 3 @vector $query_vector]").dialect(2) ``` -------------------------------- ### Example Loaded Keys Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/sql_to_redis_queries Sample output showing the keys returned after loading data into the Redis index. ```text ['user_simple_docs:01KN7Y4J630537VY4Y5D9EZMYX', 'user_simple_docs:01KN7Y4J630537VY4Y5D9EZMYY', 'user_simple_docs:01KN7Y4J630537VY4Y5D9EZMYZ', 'user_simple_docs:01KN7Y4J630537VY4Y5D9EZMZ0', 'user_simple_docs:01KN7Y4J630537VY4Y5D9EZMZ1'] ``` -------------------------------- ### Initialize and Use TextQuery Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/api/query Demonstrates how to initialize a TextQuery object with various parameters and execute a query against a Redis Search index. ```python from redisvl.query import TextQuery from redisvl.index import SearchIndex index = SearchIndex.from_yaml("index.yaml") query = TextQuery( text="example text", text_field_name="text_field", text_scorer="BM25STD", filter_expression=None, num_results=10, return_fields=["field1", "field2"], stopwords="english", dialect=2, ) results = index.query(query) ``` -------------------------------- ### Run RediSearch in a Docker Container Source: https://redis.io/docs/latest/develop/ai/search-and-query/deprecated/development Start a Debian Bullseye container, mount the current directory for build access, and enter the container's bash shell. This isolates installations within the container. ```bash search=$(docker run -d -it -v $PWD:/build debian:bullseye bash) docker exec -it $search bash ``` -------------------------------- ### Apply Definitions File Source: https://redis.io/docs/latest/develop/ai/featureform/providers Executes a Python entrypoint to collect and submit resources as desired state for a workspace. Use the `--plan` flag to preview changes before applying. ```bash ff apply --workspace --file examples/featureform/docs/resources.py --plan ``` ```bash ff apply \ --workspace \ --file examples/featureform/docs/resources.py \ --plan ``` -------------------------------- ### Java: Setup and Geo-spatial Index Creation Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/geo-spatial Demonstrates setting up a Redis index with geo-spatial fields using Jedis. Includes defining the schema and creating the index with JSON data type and a prefix. ```Java import java.util.List; import java.util.stream.Stream; import redis.clients.jedis.RedisClient; import redis.clients.jedis.search.*; import redis.clients.jedis.search.schemafields.*; import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.json.Path2; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; public class QueryGeoExample { public void run() { RedisClient jedis = RedisClient.create("redis://localhost:6379"); SchemaField[] schema = { TextField.of("$.brand").as("brand"), TextField.of("$.model").as("model"), TextField.of("$.description").as("description"), NumericField.of("$.price").as("price"), TagField.of("$.condition").as("condition"), GeoField.of("$.store_location").as("store_location"), GeoShapeField.of("$.pickup_zone", CoordinateSystem.FLAT).as("pickup_zone") }; jedis.ftCreate("idx:bicycle", FTCreateParams.createParams() .on(IndexDataType.JSON) .addPrefix("bicycle:"), schema ); String[] bicycleJsons = new String[] { " {" + " \"pickup_zone\": \"POLYGON((-74.0610 40.7578, -73.9510 40.7578, -73.9510 40.6678, " + "-74.0610 40.6678, -74.0610 40.7578))\"," + " \"store_location\": \"-74.0060,40.7128\"," + " \"brand\": \"Velorim\"," + " \"model\": \"Jigger\"," + " \"price\": 270," + " \"description\": \"Small and powerful, the Jigger is the best ride for the smallest of tikes! " + "This is the tiniest kids’ pedal bike on the market available without a coaster brake, the Jigger " + "is the vehicle of choice for the rare tenacious little rider raring to go.", " \"condition\": \"new\"" + " }", " {" + " \"pickup_zone\": \"POLYGON((-118.2887 34.0972, -118.1987 34.0972, -118.1987 33.9872, " + "-118.2887 33.9872, -118.2887 34.0972))\"," + " \"store_location\": \"-118.2437,34.0522\"," + " \"brand\": \"Bicyk\"," + " \"model\": \"Hillcraft\"," + " \"price\": 1200," ``` -------------------------------- ### Install MCP Extra Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/mcp Install the MCP extra for RedisVL. If your vectorizer requires a provider extra, install that as well. ```bash pip install redisvl[mcp] ``` ```bash pip install redisvl[mcp,openai] ``` -------------------------------- ### Redis Client Initialization and Setup Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/range Initializes a Redis client connection and imports necessary modules for JSON and search operations. This setup is required before executing search commands. ```python import json import sys import redis from redis.commands.json.path import Path from redis.commands.search.field import TextField, NumericField, TagField from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import NumericFilter, Query r = redis.Redis(decode_responses=True) ``` -------------------------------- ### Python Quick-Start: Basic Text Search Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/combined Demonstrates basic text search queries on a Redis index. It shows how to filter by numeric ranges and tags, and combine multiple conditions. ```python const res1 = await client.ft.search('idx:bicycle', '@price:[500 1000] @condition:{new}'); console.log(res1.total); // >>> 1 console.log(res1); // >>> //{ // total: 1, // documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ] //} const res2 = await client.ft.search('idx:bicycle', 'kids @price:[500 1000] @condition:{used}'); console.log(res2.total); // >>> 1 console.log(res2); // >>> // { // total: 1, // documents: [ { id: 'bicycle:2', value: [Object: null prototype] } ] // } const res3 = await client.ft.search('idx:bicycle', '(kids | small) @condition:{used}'); console.log(res3.total); // >>> 2 console.log(res3); // >>> //{ // total: 2, // documents: [ // { id: 'bicycle:2', value: [Object: null prototype] }, // { id: 'bicycle:1', value: [Object: null prototype] } // ] //} const res4 = await client.ft.search('idx:bicycle', '@description:(kids | small) @condition:{used}'); console.log(res4.total); // >>> 2 console.log(res4); // >>> //{ // total: 2, // documents: [ // { id: 'bicycle:2', value: [Object: null prototype] }, // { id: 'bicycle:1', value: [Object: null prototype] } // ] //} const res5 = await client.ft.search('idx:bicycle', '@description:(kids | small) @condition:{new | used}'); console.log(res5.total); // >>> 3 console.log(res5); // >>> //{ // total: 3, // documents: [ // { id: 'bicycle:1', value: [Object: null prototype] }, // { id: 'bicycle:0', value: [Object: null prototype] }, // { id: 'bicycle:2', value: [Object: null prototype] } // ] //} const res6 = await client.ft.search('idx:bicycle', '@price:[500 1000] -@condition:{new}'); console.log(res6.total); // >>> 2 console.log(res6); // >>> //{ // total: 2, // documents: [ // { id: 'bicycle:2', value: [Object: null prototype] }, // { id: 'bicycle:9', value: [Object: null prototype] } // ] //} ``` -------------------------------- ### SQLQuery Example Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/api/query Demonstrates how to use SQLQuery to translate SQL-like syntax into Redis FT.SEARCH commands for querying an index. Requires the 'sql-redis' package. ```python from redisvl.query import SQLQuery from redisvl.index import SearchIndex index = SearchIndex.from_existing("products", redis_url="redis://localhost:6379") sql_query = SQLQuery(''' SELECT title, price, category FROM products WHERE category = 'electronics' AND price < 100 ''') results = index.query(sql_query) ``` -------------------------------- ### Node-redis: Create Index and Perform Vector Search Source: https://redis.io/docs/latest/develop/ai/search-and-query/query/vector-search This example demonstrates creating a vector index and performing two types of vector searches using node-redis. The first search uses KNN to find the top K similar items, and the second uses VECTOR_RANGE to find items within a specified similarity threshold. Ensure you have the '@xenova/transformers' library installed for text embedding. ```javascript import assert from 'node:assert'; import fs from 'node:fs'; import { createClient } from 'redis'; import { SCHEMA_FIELD_TYPE, SCHEMA_VECTOR_FIELD_ALGORITHM } from '@redis/search'; import { pipeline } from '@xenova/transformers'; function float32Buffer(arr) { const floatArray = new Float32Array(arr); const float32Buffer = Buffer.from(floatArray.buffer); return float32Buffer; } async function embedText(sentence) { let modelName = 'Xenova/all-MiniLM-L6-v2'; let pipe = await pipeline('feature-extraction', modelName); let vectorOutput = await pipe(sentence, { pooling: 'mean', normalize: true, }); const embedding = Object.values(vectorOutput?.data); return embedding; } const vector_query = float32Buffer(await embedText('That is a very happy person')); const client = createClient(); await client.connect().catch(console.error); // create index await client.ft.create('idx:bicycle', { '$.description': { type: SCHEMA_FIELD_TYPE.TEXT, AS: 'description' }, '$.description_embeddings': { type: SCHEMA_FIELD_TYPE.VECTOR, TYPE: 'FLOAT32', ALGORITHM: SCHEMA_VECTOR_FIELD_ALGORITHM.FLAT, DIM: 384, DISTANCE_METRIC: 'COSINE', AS: 'vector' } }, { ON: 'JSON', PREFIX: 'bicycle:' }); // load data const bicycles = JSON.parse(fs.readFileSync('data/query_vector.json', 'utf8')); await Promise.all( bicycles.map((bicycle, bid) => { return client.json.set(`bicycle:${bid}`, '$', bicycle); }) ); const res1 = await client.ft.search('idx:bicycle', '*=>[KNN 3 @vector $query_vector AS score]', { PARAMS: { query_vector: vector_query }, RETURN: ['description'], DIALECT: 2 } ); console.log(res1.total); console.log(res1); const res2 = await client.ft.search('idx:bicycle', '@vector:[VECTOR_RANGE 0.9 $query_vector]=>{$YIELD_DISTANCE_AS: vector_dist}', { PARAMS: { query_vector: vector_query }, SORTBY: 'vector_dist', RETURN: ['vector_dist', 'description'], DIALECT: 2 } ); console.log(res2.total); console.log(res2); ``` -------------------------------- ### Initialize AsyncSearchIndex Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/getting_started Set up an asynchronous search index using `AsyncSearchIndex` and an async Redis client. This is recommended for production environments. ```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) ``` -------------------------------- ### FLAT Example Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/api/schema Example configuration for a FLAT vector field in RedisVL. ```APIDOC ## FLAT Example ```yaml - name: embedding type: vector attrs: algorithm: flat dims: 768 distance_metric: cosine datatype: float32 # Optional: tune for batch processing block_size: 1024 ``` ``` -------------------------------- ### Install MCP Extra Dependencies Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/mcp Install the MCP extra for RedisVL if `rvl mcp` reports missing optional dependencies. Also install provider SDKs if needed by the vectorizer. ```bash pip install redisvl[mcp] ``` -------------------------------- ### Initialize LangCache and Perform Operations (Python) Source: https://redis.io/docs/latest/develop/ai/context-engine/langcache/api-examples Demonstrates initializing the LangCache client and performing basic operations like search, set, and delete. Ensure environment variables for host, cache ID, and API key are set. ```Python from langcache import LangCache import os lang_cache = LangCache( server_url=f"https://{os.getenv('HOST', '')}", cache_id=os.getenv("CACHE_ID", ""), api_key=os.getenv("API_KEY", "") ) res = lang_cache.search( prompt="User prompt text", similarity_threshold=0.9 ) print(res) res = lang_cache.search( prompt="User prompt text", attributes={"customAttributeName": "customAttributeValue"}, similarity_threshold=0.9, ) print(res) from langcache.models import SearchStrategy res = lang_cache.search( prompt="User prompt text", search_strategies=[SearchStrategy.EXACT, SearchStrategy.SEMANTIC], similarity_threshold=0.9, ) print(res) res = lang_cache.set( prompt="User prompt text", response="LLM response text", ) print(res) res = lang_cache.set( prompt="User prompt text", response="LLM response text", attributes={"customAttributeName": "customAttributeValue"}, ) print(res) res = lang_cache.delete_by_id(entry_id="") print(res) res = lang_cache.delete_query( attributes={"customAttributeName": "customAttributeValue"}, ) print(res) lang_cache.flush() ``` -------------------------------- ### Install RedisVL with Optional Dependencies Source: https://redis.io/docs/latest/develop/ai/redisvl/user_guide/installation Install redisvl with specific optional dependencies for features like embeddings providers or MCP server support. Use these commands to tailor your installation to your needs. ```bash # Vectorizer providers $ pip install redisvl[openai] # OpenAI embeddings $ pip install redisvl[cohere] # Cohere embeddings and reranking $ pip install redisvl[mistralai] # Mistral AI embeddings $ pip install redisvl[voyageai] # Voyage AI embeddings and reranking $ pip install redisvl[sentence-transformers] # HuggingFace local embeddings $ pip install redisvl[vertexai] # Google Vertex AI embeddings $ pip install redisvl[bedrock] # AWS Bedrock embeddings # Other optional features $ pip install redisvl[mcp] # RedisVL MCP server support (Python 3.10+) $ pip install redisvl[langcache] # LangCache managed service integration $ pip install redisvl[sql-redis] # SQL query support ``` -------------------------------- ### Install Sentence Transformers Source: https://redis.io/docs/latest/develop/ai/redisvl/0.18.1/user_guide/how_to_guides/vectorizers Installs the 'sentence-transformers' library required for Huggingface vectorization. ```bash pip install sentence-transformers ```