| Name | Type | Description |
|---|---|---|
index_name* | str | The name of the index or alias to use for the cache. If it doesn't exist, an index is created according to the default mapping. |
store_input | bool | Default: TrueWhether to store the LLM input (prompt) in the cache. Default True. |
store_input_params | bool | Default: TrueWhether to store the LLM parameters in the cache. Default True. |
metadata | dict | Default: None |
client | AsyncElasticsearch | Default: None |
es_url | str | Default: None |
es_cloud_id | str | Default: None |
es_user | str | Default: None |
es_api_key | str | Default: None |
es_password | str | Default: None |
Elasticsearch LLM cache.
Caches LLM responses in Elasticsearch to avoid repeated calls for identical prompts.
Setup:
Install langchain_elasticsearch and start Elasticsearch locally using
the start-local script.
pip install -qU langchain_elasticsearch
curl -fsSL https://elastic.co/start-local | sh
This will create an elastic-start-local folder. To start Elasticsearch
and Kibana:
cd elastic-start-local
./start.sh
Elasticsearch will be available at http://localhost:9200. The password
for the elastic user and API key are stored in the .env file in the
elastic-start-local folder.
Initialize the Elasticsearch cache store.
Instantiate:
from langchain_elasticsearch import ElasticsearchCache
cache = ElasticsearchCache(
index_name="llm-cache",
es_url="http://localhost:9200"
)
Instantiate with API key:
from langchain_elasticsearch import ElasticsearchCache
cache = ElasticsearchCache(
index_name="llm-cache",
es_url="http://localhost:9200",
es_api_key="your-api-key"
)
Instantiate from cloud:
from langchain_elasticsearch import ElasticsearchCache
cache = ElasticsearchCache(
index_name="llm-cache",
es_cloud_id="<cloud_id>",
es_api_key="your-api-key"
)
Instantiate from existing connection:
from langchain_elasticsearch import ElasticsearchCache
from elasticsearch import Elasticsearch
client = Elasticsearch("http://localhost:9200")
cache = ElasticsearchCache(
index_name="llm-cache",
client=client
)
Use with LangChain:
from langchain.globals import set_llm_cache
from langchain_elasticsearch import ElasticsearchCache
set_llm_cache(ElasticsearchCache(
index_name="llm-cache",
es_url="http://localhost:9200"
))
For synchronous applications, use the ElasticsearchCache class.
For asynchronous applications, use the AsyncElasticsearchCache class.
Additional metadata to store in the cache for filtering. Must be JSON serializable.
Pre-existing Elasticsearch connection. Either provide this OR credentials.
URL of the Elasticsearch instance.
Cloud ID of the Elasticsearch instance.
Username for Elasticsearch.
API key for Elasticsearch.
Password for Elasticsearch.