本页还没有译文
本页 34 段还没译成한국어,下面显示的是英文原文。Redis integrations
还没翻译译自英文(docs.langchain.com)本页 34 段都还没译
Redis (Remote Dictionary Server) is an open-source in-memory storage, used as a distributed, in-memory key–value database, cache and message broker, with optional durability. Because it holds all data in memory and because of its design,
Redisoffers low-latency reads and writes, making it particularly suitable for use cases that require a cache. Redis is the most popular NoSQL database, and one of the most popular databases overall.
This page covers how to use the Redis ecosystem within LangChain. It is broken into two parts: installation and setup, and then references to specific Redis wrappers.
Installation and setup
Install the Python SDK and LangChain Redis integration:
pipuv
pip install redis langchain-redisuv add redis langchain-redisTo run Redis locally, you can use Docker:
docker run --name langchain-redis -d -p 6379:6379 redis redis-server --save 60 1 --loglevel warningTo stop the container:
docker stop langchain-redisAnd to start it again:
docker start langchain-redisConnections
We need a redis url connection string to connect to the database support either a stand alone Redis server or a High-Availability setup with Replication and Redis Sentinels.
Redis standalone connection url
For standalone Redis server, the official redis connection url formats can be used as describe in the python redis modules
"from_url()" method Redis.from_url
Example: redis_url = "redis://:secret-pass@localhost:6379/0"
Redis sentinel connection url
For Redis sentinel setups the connection scheme is "redis+sentinel". This is an unofficial extensions to the official IANA registered protocol schemes as long as there is no connection url for Sentinels available.
Example: redis_url = "redis+sentinel://:secret-pass@sentinel-host:26379/mymaster/0"
The format is redis+sentinel://$USERNAME:$PASSWORD@$HOST_OR_IP:$PORT/$SERVICE_NAME/$DB_NUMBER
with the default values of "service-name = mymaster" and "db-number = 0" if not set explicit.
The service-name is the redis server monitoring group name as configured within the Sentinel.
The current url format limits the connection string to one sentinel host only (no list can be given) and both Redis server and sentinel must have the same password set (if used).
Redis cluster connection url
Redis cluster is not supported right now for all methods requiring a "redis_url" parameter.
The only way to use a Redis Cluster is with LangChain classes accepting a preconfigured Redis client like RedisCache
(example below).
Cache
The Cache wrapper allows for Redis to be used as a remote, low-latency, in-memory cache for LLM prompts and responses.
Standard cache
The standard cache is the Redis bread & butter of use case in production for both open-source and enterprise users globally.
from langchain_redis import RedisCacheTo use this cache with your LLMs:
from langchain_core.globals import set_llm_cache
import redis
redis_client = redis.Redis.from_url(...)
set_llm_cache(RedisCache(redis_client))Semantic cache
Semantic caching allows users to retrieve cached prompts based on semantic similarity between the user input and previously cached results. Under the hood it blends Redis as both a cache and a vectorstore.
from langchain_redis import RedisSemanticCacheTo use this cache with your LLMs:
from langchain_core.globals import set_llm_cache
import redis
# use any embedding provider...
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings
redis_url = "redis://localhost:6379"
set_llm_cache(RedisSemanticCache(
embedding=FakeEmbeddings(),
redis_url=redis_url
))Vector store
Redis includes a vector store integration for low-latency retrieval, metadata filtering, MMR, and hybrid full-text and vector search.
from langchain_redis import RedisVectorStoreFor a detailed walkthrough, see the Redis vector store integration guide.
Retriever
The Redis vector store retriever wrapper generalizes the vectorstore class to perform
low-latency document retrieval. To create the retriever, simply
call .as_retriever() on the base vectorstore class.