Skip to main content

A caching layer for LLMs that exploits Elasticsearch, fully compatible with LangChain caching.

Project description

llm-elasticsearch-cache

A caching layer for LLMs that exploits Elasticsearch, fully compatible with LangChain caching.

Install

pip install llm-elasticsearch-cache

Usage

The LangChain cache can be used similarly to the other cache integrations.

Basic example

from langchain.globals import set_llm_cache
from llmescache.langchain import ElasticsearchCache
from elasticsearch import Elasticsearch

es_client = Elasticsearch(hosts="http://localhost:9200")
set_llm_cache(
    ElasticsearchCache(
        es_client=es_client, 
        es_index="llm-langchain-cache", 
        metadata={"project": "my_chatgpt_project"}
    )
)

The es_index parameter can also take aliases. This allows to use the ILM: Manage the index lifecycle that we suggest to consider for managing retention and controlling cache growth.

Look at the class docstring for all parameters.

Index the generated text

The cached data won't be searchable by default. The developer can customize the building of the Elasticsearch document in order to add indexed text fields, where to put, for example, the text generated by the LLM.

This can be done by subclassing end overriding a method:

from llmescache.langchain import ElasticsearchCache
from elasticsearch import Elasticsearch
from langchain_core.caches import RETURN_VAL_TYPE
from typing import Any, Dict, List
from langchain.globals import set_llm_cache
import json


class SearchableElasticsearchCache(ElasticsearchCache):

    def build_document(
            self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE
    ) -> Dict[str, Any]:
        body = super().build_document(prompt, llm_string, return_val)
        body["parsed_llm_output"] = self._parse_output(body["llm_output"])
        return body

    @staticmethod
    def _parse_output(data: List[str]) -> List[str]:
        return [json.loads(output)["kwargs"]["message"]["kwargs"]["content"] for output in data]


# let's re-use an existing cache index
es_client = Elasticsearch(hosts="http://localhost:9200")
es_client.indices.put_mapping(
    index="llm-langchain-cache", 
    body={"properties": {"parsed_llm_output": {"type": "text", "analyzer": "english"}}}
)
set_llm_cache(SearchableElasticsearchCache(es_client=es_client, es_index="llm-langchain-cache"))

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

llm_elasticsearch_cache-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

llm_elasticsearch_cache-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file llm_elasticsearch_cache-0.1.1.tar.gz.

File metadata

  • Download URL: llm_elasticsearch_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.6 Darwin/20.6.0

File hashes

Hashes for llm_elasticsearch_cache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2c3ca9a9e0a011805afb645f1c4c0d6ac62a6074c4c88ac53154e1e01536c92f
MD5 978fc1ba481bf9fefd5b546a7dcf3134
BLAKE2b-256 bbf4fe2cc57a2a32492a63cd4fe1db3ccc776e8426a7cb17ef9cb6a57c6c24c4

See more details on using hashes here.

File details

Details for the file llm_elasticsearch_cache-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_elasticsearch_cache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f25117262a05eeec6d8073dc931a4ea1697aed159d49b6f314e38692ce74be7
MD5 4a0f5c2e94d5d38a18679526fd0a3bc9
BLAKE2b-256 3091b59a9e6c549f1402a72aa26097892727c16df4f2a25b9cb0289b7043e66b

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