Skip to main content
This notebook demonstrates how to use the RedisCache and RedisSemanticCache classes from the langchain-redis package to implement caching for LLM responses.

Setup

First, let’s install the required dependencies and ensure we have a Redis instance running.
Ensure you have a Redis server running. You can start one using Docker with:
Or install and run Redis locally according to your operating system’s instructions.

Importing required libraries

Set OpenAI API key

Using RedisCache

Using RedisSemanticCache

Advanced usage

Custom TTL (Time-To-Live)

Customizing RedisSemanticCache

Conclusion

This notebook demonstrated the usage of RedisCache and RedisSemanticCache from the langchain-redis package. These caching mechanisms can significantly improve the performance of LLM-based applications by reducing redundant API calls and leveraging semantic similarity for intelligent caching. The Redis-based implementation provides a fast, scalable, and flexible solution for caching in distributed systems.