Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

这将帮助你开始使用 Elasticsearch 键值存储。 所有 ElasticsearchEmbeddingsCache 功能和配置的详细文档请前往 API reference.

概述

The ElasticsearchEmbeddingsCache is a ByteStore implementation that uses your Elasticsearch instance for efficient storage and retrieval of embeddings.

集成详情

ClassPackageLocalJS supportDownloadsVersion
ElasticsearchEmbeddingsCachelangchain-elasticsearchPyPI - DownloadsPyPI - Version

设置

To create a ElasticsearchEmbeddingsCache byte store, you’ll need an Elasticsearch cluster. You can set one up locally or create an Elastic account.

安装

The LangChain ElasticsearchEmbeddingsCache integration lives in the langchain-elasticsearch package:
pip install -qU langchain-elasticsearch

Instantiation

Now we can instantiate our 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",
    },
)

使用方法

You can set data under keys like this using the mset method:
kv_store.mset(
    [
        ["key1", b"value1"],
        ["key2", b"value2"],
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
[b'value1', b'value2']
And you can delete data using the mdelete method:
kv_store.mdelete(
    [
        "key1",
        "key2",
    ]
)

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

Use as an embeddings cache

Like other ByteStores, you can use an ElasticsearchEmbeddingsCache instance for persistent caching in document ingestion for RAG. However, cached vectors won’t be searchable by default. The developer can customize the building of the Elasticsearch document in order to add indexed vector field. This can be done by subclassing and overriding methods:
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
When overriding the mapping and the document building, please only make additive modifications, keeping the base mapping intact.

API 参考

所有 ElasticsearchEmbeddingsCache features and configurations, head to the API reference