mirror of
https://github.com/hwchase17/langchain
synced 2024-11-04 06:00:26 +00:00
ee7a7954b9
Implement [Retriever](https://python.langchain.com/docs/modules/data_connection/retrievers/) interface for Elasticsearch. I opted to only expose the `body`, which gives you full flexibility, and none the other 68 arguments of the [search method](https://elasticsearch-py.readthedocs.io/en/v8.12.1/api/elasticsearch.html#elasticsearch.Elasticsearch.search). Added a user agent header for usage tracking in Elastic Cloud. --------- Co-authored-by: Erick Friis <erick@langchain.dev> |
||
---|---|---|
.. | ||
langchain_elasticsearch | ||
scripts | ||
tests | ||
.gitignore | ||
LICENSE | ||
Makefile | ||
poetry.lock | ||
pyproject.toml | ||
README.md |
langchain-elasticsearch
This package contains the LangChain integration with Elasticsearch.
Installation
pip install -U langchain-elasticsearch
Elasticsearch setup
Elastic Cloud
You need a running Elasticsearch deployment. The easiest way to start one is through Elastic Cloud. You can sign up for a free trial.
- Create a deployment
- Get your Cloud ID:
- In the Elastic Cloud console, click "Manage" next to your deployment
- Copy the Cloud ID and paste it into the
es_cloud_id
parameter below
- Create an API key:
- In the Elastic Cloud console, click "Open" next to your deployment
- In the left-hand side menu, go to "Stack Management", then to "API Keys"
- Click "Create API key"
- Enter a name for the API key and click "Create"
- Copy the API key and paste it into the
es_api_key
parameter below
Elastic Cloud
Alternatively, you can run Elasticsearch via Docker as described in the docs.
Usage
ElasticsearchStore
The ElasticsearchStore
class exposes Elasticsearch as a vector store.
from langchain_elasticsearch import ElasticsearchStore
embeddings = ... # use a LangChain Embeddings class or ElasticsearchEmbeddings
vectorstore = ElasticsearchStore(
es_cloud_id="your-cloud-id",
es_api_key="your-api-key",
index_name="your-index-name",
embeddings=embeddings,
)
ElasticsearchEmbeddings
The ElasticsearchEmbeddings
class provides an interface to generate embeddings using a model
deployed in an Elasticsearch cluster.
from langchain_elasticsearch import ElasticsearchEmbeddings
embeddings = ElasticsearchEmbeddings.from_credentials(
model_id="your-model-id",
input_field="your-input-field",
es_cloud_id="your-cloud-id",
es_api_key="your-api-key",
)
ElasticsearchChatMessageHistory
The ElasticsearchChatMessageHistory
class stores chat histories in Elasticsearch.
from langchain_elasticsearch import ElasticsearchChatMessageHistory
chat_history = ElasticsearchChatMessageHistory(
index="your-index-name",
session_id="your-session-id",
es_cloud_id="your-cloud-id",
es_api_key="your-api-key",
)