llm elasticsearch cache
1.0.0
중요한
채팅 및 임베딩 모델 모두에 대해 Langchain 캐싱과 완전히 호환되는 Elasticsearch를 이용하는 LLM의 캐싱 레이어.
pip install llm-elasticsearch-cacheLangchain 캐시는 다른 캐시 통합과 유사하게 사용할 수 있습니다.
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-chat-cache" ,
metadata = { "project" : "my_chatgpt_project" }
)
) es_index 매개 변수도 별칭을 할 수도 있습니다. 이를 통해 ILM을 사용할 수 있습니다. 보존 관리 및 캐시 성장 제어를 고려하는 인덱스 수명주기 관리.
모든 매개 변수에 대한 클래스 Docstring을보십시오.
캐시 된 데이터는 기본적으로 검색 할 수 없습니다. 개발자는 인덱스 된 텍스트 필드를 추가하여 LLM에서 생성 한 텍스트를 넣을 위치를 추가하기 위해 Elasticsearch 문서의 빌드를 사용자 정의 할 수 있습니다.
이는 최종 최종 재정의 메소드를 서브 클래싱으로 수행 할 수 있습니다. 새 캐시 클래스는 기존 캐시 인덱스에도 적용 할 수 있습니다.
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 ):
@ property
def mapping ( self ) -> Dict [ str , Any ]:
mapping = super (). mapping
mapping [ "mappings" ][ "properties" ][ "parsed_llm_output" ] = { "type" : "text" , "analyzer" : "english" }
return mapping
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 ]
es_client = Elasticsearch ( hosts = "http://localhost:9200" )
set_llm_cache ( SearchableElasticsearchCache ( es_client = es_client , es_index = "llm-chat-cache" ))캐싱 임베딩은 공식 문서와 약간 다른 방식으로 CachebackEdembeddings를 사용하여 얻습니다.
from llmescache . langchain import ElasticsearchStore
from elasticsearch import Elasticsearch
from langchain . embeddings import CacheBackedEmbeddings
from langchain_openai import OpenAIEmbeddings
es_client = Elasticsearch ( hosts = "http://localhost:9200" )
underlying_embeddings = OpenAIEmbeddings ( model = "text-embedding-3-small" )
store = ElasticsearchStore (
es_client = es_client ,
es_index = "llm-embeddings-cache" ,
namespace = underlying_embeddings . model ,
metadata = { "project" : "my_llm_project" }
)
cached_embeddings = CacheBackedEmbeddings (
underlying_embeddings ,
store
) 채팅 캐시와 마찬가지로 검색을 위해 벡터를 인덱싱하기 위해 ElasticsearchStore 서브 클래스 할 수 있습니다.
from llmescache . langchain import ElasticsearchStore
from typing import Any , Dict , List
class SearchableElasticsearchStore ( ElasticsearchStore ):
@ property
def mapping ( self ) -> Dict [ str , Any ]:
mapping = super (). mapping
mapping [ "mappings" ][ "properties" ][ "vector" ] = { "type" : "dense_vector" , "dims" : 1536 , "index" : True , "similarity" : "dot_product" }
return mapping
def build_document ( self , llm_input : str , vector : List [ float ]) -> Dict [ str , Any ]:
body = super (). build_document ( llm_input , vector )
body [ "vector" ] = vector
return body CacheBackedEmbeddings 현재 캐싱 쿼리를 지원하지 않습니다. 이는 벡터 검색의 경우 텍스트 쿼리가 캐시되지 않음을 의미합니다. 그러나 embed_query 방법을 재정의함으로써 쉽게 구현할 수 있어야합니다.