An integration package connecting Elasticsearch and LangChain
Project description
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,
)
ElasticsearchRetriever
The ElasticsearchRetriever
class can be user to implement more complex queries.
This can be useful for power users and necessary if data was ingested outside of LangChain
(for example using a web crawler).
def fuzzy_query(search_query: str) -> Dict:
return {
"query": {
"match": {
text_field: {
"query": search_query,
"fuzziness": "AUTO",
}
},
},
}
fuzzy_retriever = ElasticsearchRetriever.from_es_params(
es_cloud_id="your-cloud-id",
es_api_key="your-api-key",
index_name="your-index-name",
body_func=fuzzy_query,
content_field=text_field,
)
fuzzy_retriever.get_relevant_documents("fooo")
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",
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file langchain_elasticsearch-0.1.2.tar.gz
.
File metadata
- Download URL: langchain_elasticsearch-0.1.2.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c183146bb011d5532f3688562d171080c6054ba0b4a7902c2ecd96d969e08770 |
|
MD5 | d9feb7c9bd5668871960c2112ea87afa |
|
BLAKE2b-256 | 8b98fb30bfbc401b3b92b17e636ee14010bf9e05d058cad28320b9ac9ad9468e |
File details
Details for the file langchain_elasticsearch-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: langchain_elasticsearch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e9a5f29b857ffa465d36cb251eaf3dbf20db2919b3cbbaca05f3dcb1a0fd221 |
|
MD5 | cd272f74a88097026b85c2755136df25 |
|
BLAKE2b-256 | dd2d661929e3c012adb7e80d5b989a9ea3cbe02c3a83f29019a07cdc90a1b028 |