--- linkTitle: Threshold optimization title: Threshold Optimization weight: 09 aliases: - /integrate/redisvl/user_guide/threshold_optimization --- After setting up `SemanticRouter` or `SemanticCache` it's best to tune the `distance_threshold` to get the most performance out of your system. RedisVL provides helper classes to make this light weight optimization easy. **Note:** Threshold optimization relies on `python > 3.9.` # CacheThresholdOptimizer Let's say you setup the following semantic cache with a distance_threshold of `X` and store the entries: - prompt: `what is the capital of france?` response: `paris` - prompt: `what is the capital of morocco?` response: `rabat` ```python from redisvl.extensions.cache.llm import SemanticCache from redisvl.utils.vectorize import HFTextVectorizer sem_cache = SemanticCache( name="sem_cache", # underlying search index name redis_url="redis://localhost:6379", # redis connection url string distance_threshold=0.5, # semantic cache distance threshold vectorizer=HFTextVectorizer("redis/langcache-embed-v1") # embedding model ) paris_key = sem_cache.store(prompt="what is the capital of france?", response="paris") rabat_key = sem_cache.store(prompt="what is the capital of morocco?", response="rabat") ``` /Users/justin.cechmanek/.pyenv/versions/3.13/envs/redisvl-dev/lib/python3.13/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 16:16:11 sentence_transformers.SentenceTransformer INFO Use pytorch device_name: mps 16:16:11 sentence_transformers.SentenceTransformer INFO Load pretrained SentenceTransformer: redis/langcache-embed-v1 Batches: 0%| | 0/1 [00:00