이 문서는 Elasticsearch key-value stores를 시작하는 데 도움이 됩니다. 모든 ElasticsearchEmbeddingsCache 기능 및 구성에 대한 자세한 문서는 API reference를 참조하세요.

Overview

ElasticsearchEmbeddingsCache는 Elasticsearch 인스턴스를 사용하여 임베딩을 효율적으로 저장하고 검색하는 ByteStore 구현입니다.

Integration details

ClassPackageLocalJS supportDownloadsVersion
ElasticsearchEmbeddingsCachelangchain-elasticsearchPyPI - DownloadsPyPI - Version

Setup

ElasticsearchEmbeddingsCache byte store를 생성하려면 Elasticsearch 클러스터가 필요합니다. 로컬에서 설정하거나 Elastic 계정을 생성할 수 있습니다.

Installation

LangChain ElasticsearchEmbeddingsCache integration은 langchain-elasticsearch 패키지에 있습니다:
pip install -qU langchain-elasticsearch

Instantiation

이제 byte store를 인스턴스화할 수 있습니다:
from langchain_elasticsearch import ElasticsearchEmbeddingsCache

# Example config for a locally running Elasticsearch instance
kv_store = ElasticsearchEmbeddingsCache(
    es_url="https://localhost:9200",
    index_name="llm-chat-cache",
    metadata={"project": "my_chatgpt_project"},
    namespace="my_chatgpt_project",
    es_user="elastic",
    es_password="<GENERATED PASSWORD>",
    es_params={
        "ca_certs": "~/http_ca.crt",
    },
)

Usage

mset 메서드를 사용하여 다음과 같이 키 아래에 데이터를 설정할 수 있습니다:
kv_store.mset(
    [
        ["key1", b"value1"],
        ["key2", b"value2"],
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
[b'value1', b'value2']
그리고 mdelete 메서드를 사용하여 데이터를 삭제할 수 있습니다:
kv_store.mdelete(
    [
        "key1",
        "key2",
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
[None, None]

Use as an embeddings cache

다른 ByteStores와 마찬가지로, RAG를 위한 문서 수집 시 영구 캐싱ElasticsearchEmbeddingsCache 인스턴스를 사용할 수 있습니다. 그러나 캐시된 벡터는 기본적으로 검색할 수 없습니다. 개발자는 인덱싱된 벡터 필드를 추가하기 위해 Elasticsearch 문서 빌드를 커스터마이징할 수 있습니다. 이는 서브클래싱하고 메서드를 오버라이딩하여 수행할 수 있습니다:
from typing import Any, Dict, List


class SearchableElasticsearchStore(ElasticsearchEmbeddingsCache):
    @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
매핑과 문서 빌드를 오버라이딩할 때는 기본 매핑을 그대로 유지하면서 추가적인 수정만 하시기 바랍니다.

API reference

모든 ElasticsearchEmbeddingsCache 기능 및 구성에 대한 자세한 문서는 API reference를 참조하세요: python.langchain.com/api_reference/elasticsearch/cache/langchain_elasticsearch.cache.ElasticsearchEmbeddingsCache.html
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I