Skip to main content

An integration package connecting Redis and LangChain

Project description

langchain-redis

This package contains the LangChain integration with Redis, providing powerful tools for vector storage, semantic caching, and chat history management.

Installation

pip install -U langchain-redis

This will install the package along with its dependencies, including redis, redisvl, and ulid.

Configuration

To use this package, you need to have a Redis instance running. You can configure the connection by setting the following environment variable:

export REDIS_URL="redis://username:password@localhost:6379"

Alternatively, you can pass the Redis URL directly when initializing the components or use the RedisConfig class for more detailed configuration.

Features

1. Vector Store

The RedisVectorStore class provides a vector database implementation using Redis.

Usage

from langchain_redis import RedisVectorStore, RedisConfig
from langchain_core.embeddings import Embeddings

embeddings = Embeddings()  # Your preferred embedding model

config = RedisConfig(
    index_name="my_vectors",
    redis_url="redis://localhost:6379",
    distance_metric="COSINE"  # Options: COSINE, L2, IP
)

vector_store = RedisVectorStore(embeddings, config=config)

# Adding documents
texts = ["Document 1 content", "Document 2 content"]
metadatas = [{"source": "file1"}, {"source": "file2"}]
vector_store.add_texts(texts, metadatas=metadatas)

# Adding documents with custom keys
custom_keys = ["doc1", "doc2"]
vector_store.add_texts(texts, metadatas=metadatas, keys=custom_keys)

# Similarity search
query = "Sample query"
docs = vector_store.similarity_search(query, k=2)

# Similarity search with score
docs_and_scores = vector_store.similarity_search_with_score(query, k=2)

# Similarity search with filtering
filter_expr = Tag("category") == "science"
filtered_docs = vector_store.similarity_search(query, k=2, filter=filter_expr)

# Maximum marginal relevance search
docs = vector_store.max_marginal_relevance_search(query, k=2, fetch_k=10)

Features

  • Efficient vector storage and retrieval
  • Support for metadata filtering
  • Multiple distance metrics: Cosine similarity, L2, and Inner Product
  • Maximum marginal relevance search
  • Custom key support for document indexing

2. Cache

The RedisCache and RedisSemanticCache classes provide caching mechanisms for LLM calls.

Usage

from langchain_redis import RedisCache, RedisSemanticCache
from langchain_core.language_models import LLM
from langchain_core.embeddings import Embeddings

# Standard cache
cache = RedisCache(redis_url="redis://localhost:6379", ttl=3600)

# Semantic cache
embeddings = Embeddings()  # Your preferred embedding model
semantic_cache = RedisSemanticCache(
    redis_url="redis://localhost:6379",
    embedding=embeddings,
    distance_threshold=0.1
)

# Using cache with an LLM
llm = LLM(cache=cache)  # or LLM(cache=semantic_cache)

# Async cache operations
await cache.aupdate("prompt", "llm_string", [Generation(text="cached_response")])
cached_result = await cache.alookup("prompt", "llm_string")

Features

  • Efficient caching of LLM responses
  • TTL support for automatic cache expiration
  • Semantic caching for similarity-based retrieval
  • Asynchronous cache operations

3. Chat History

The RedisChatMessageHistory class provides a Redis-based storage for chat message history.

Usage

from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage

history = RedisChatMessageHistory(
    session_id="user_123",
    redis_url="redis://localhost:6379",
    ttl=3600  # Optional: set TTL for message expiration
)

# Adding messages
history.add_user_message("Hello, AI!")
history.add_ai_message("Hello, human! How can I assist you today?")
history.add_message(SystemMessage(content="This is a system message"))

# Retrieving messages
messages = history.messages

# Searching messages
results = history.search_messages("assist")

# Get the number of messages
message_count = len(history)

# Clear history
history.clear()

Features

  • Persistent storage of chat messages
  • Support for different message types (Human, AI, System)
  • Message searching capabilities
  • Automatic expiration with TTL support
  • Message count functionality

Advanced Configuration

The RedisConfig class allows for detailed configuration of the Redis integration:

from langchain_redis import RedisConfig

config = RedisConfig(
    index_name="my_index",
    redis_url="redis://localhost:6379",
    distance_metric="COSINE",
    key_prefix="my_prefix",
    vector_datatype="FLOAT32",
    storage_type="hash",
    metadata_schema=[
        {"name": "category", "type": "tag"},
        {"name": "price", "type": "numeric"}
    ]
)

Refer to the inline documentation for detailed information on these configuration options.

Error Handling and Logging

The package uses Python's standard logging module. You can configure logging to get more information about the package's operations:

import logging
logging.basicConfig(level=logging.INFO)

Error handling is done through custom exceptions. Make sure to handle these exceptions in your application code.

Performance Considerations

  • For large datasets, consider using batched operations when adding documents to the vector store.
  • Adjust the k and fetch_k parameters in similarity searches to balance between accuracy and performance.
  • Use appropriate indexing algorithms (FLAT, HNSW) based on your dataset size and query requirements.

Examples

For more detailed examples and use cases, please refer to the docs/ directory in this repository.

Contributing / Development

Unit Tests

To install dependencies for unit tests:

poetry install --with test

To run unit tests:

make test

To run a specific test:

TEST_FILE=tests/unit_tests/test_imports.py make test

Integration Tests

You would need an OpenAI API Key to run the integration tests:

export OPENAI_API_KEY=sk-J3nnYJ3nnYWh0Can1Turnt0Ug1VeMe50mth1n1cAnH0ld0n2

To install dependencies for integration tests:

poetry install --with test,test_integration

To run integration tests:

make integration_tests

Local Development

Install langchain-redis development requirements (for running langchain, running examples, linting, formatting, tests, and coverage):

poetry install --with lint,typing,test,test_integration

Then verify dependency installation:

make lint

License

This project is licensed under the MIT License (LICENSE).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_redis-0.0.1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

langchain_redis-0.0.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file langchain_redis-0.0.1.tar.gz.

File metadata

  • Download URL: langchain_redis-0.0.1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for langchain_redis-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a432c17d75f1e8d2af0a78a423e223c6bd494a55b4ad24529879b9c1a494b18e
MD5 1e175bd8a84753557dfeefbfa5a5f38f
BLAKE2b-256 969c0009a152941fb707c65a7f1cf119bd2321ae73932d0ad5d05e985d014cc5

See more details on using hashes here.

File details

Details for the file langchain_redis-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_redis-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e0df93ae03e89404ba72a67406d7d1ec5869fe0b1658e458c4618727453788b3
MD5 2f517b7712b1aea0b314ef78c464325e
BLAKE2b-256 e4d4708915175b08997599d2837de4f6ffc5f14fc5fa27efd20efe7d0ea5f48e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page