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.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

llm_elasticsearch_cache-0.1.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_elasticsearch_cache-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 5d3fb2166c3a51187c3a8c4a1296c3db342ae8437986c1e052255adb5eb29956
MD5 c1722aab687e997456788e23cd6f4815
BLAKE2b-256 28ac20061a6f10b1cceb44704441c6756d938ece73eba079efcef23d07d3ab88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for llm_elasticsearch_cache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 353bb281926402d758bbd00603d3c753e5ae58fc4d5cb1d8d237857db26a42c3
MD5 09aa1feed7f6a7b0a9363b433cdac708
BLAKE2b-256 57fa5ea6d9b26d1011fe0dffd032fdd3c804c68b8f06f5e62f8a648c50e58039

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