| Name | Type | Description |
|---|---|---|
model_id* | str | The model_id of the model deployed in the Elasticsearch cluster. |
input_field | str | Default: 'text_field'The name of the key for the input text field in the document. Defaults to text_field. |
client | AsyncElasticsearch or Elasticsearch | Default: NonePre-existing Elasticsearch connection. Either provide this OR credentials. |
es_url | str | Default: None |
es_cloud_id | str | Default: None |
es_user | str | Default: None |
es_api_key | str | Default: None |
es_password | str | Default: None |
Elasticsearch embedding models.
This class provides an interface to generate embeddings using a model deployed in an Elasticsearch cluster. It requires an Elasticsearch connection and the model_id of the model deployed in the cluster.
In Elasticsearch you need to have an embedding model loaded and deployed.
Setup:
Install langchain_elasticsearch and start Elasticsearch locally using
the start-local script.
pip install -qU langchain_elasticsearch
curl -fsSL https://elastic.co/start-local | sh
This will create an elastic-start-local folder. To start Elasticsearch
and Kibana:
cd elastic-start-local
./start.sh
Elasticsearch will be available at http://localhost:9200. The password
for the elastic user and API key are stored in the .env file in the
elastic-start-local folder.
Initialize the ElasticsearchEmbeddings instance.
Instantiate:
from langchain_elasticsearch import ElasticsearchEmbeddings
embeddings = ElasticsearchEmbeddings(
model_id="your_model_id",
es_url="http://localhost:9200"
)
Instantiate with API key (URL):
from langchain_elasticsearch import ElasticsearchEmbeddings
embeddings = ElasticsearchEmbeddings(
model_id="your_model_id",
es_url="http://localhost:9200",
es_api_key="your-api-key"
)
Instantiate with username/password (URL):
from langchain_elasticsearch import ElasticsearchEmbeddings
embeddings = ElasticsearchEmbeddings(
model_id="your_model_id",
es_url="http://localhost:9200",
es_user="elastic",
es_password="password"
)
If you want to use a cloud hosted Elasticsearch instance, you can pass in the es_cloud_id argument instead of the es_url argument.
Instantiate from cloud (with username/password):
from langchain_elasticsearch import ElasticsearchEmbeddings
embeddings = ElasticsearchEmbeddings(
model_id="your_model_id",
es_cloud_id="<cloud_id>",
es_user="elastic",
es_password="<password>"
)
Instantiate from cloud (with API key):
from langchain_elasticsearch import ElasticsearchEmbeddings
embeddings = ElasticsearchEmbeddings(
model_id="your_model_id",
es_cloud_id="<cloud_id>",
es_api_key="your-api-key"
)
You can also connect to an existing Elasticsearch instance by passing in a pre-existing Elasticsearch connection via the client argument.
Instantiate from existing connection:
from langchain_elasticsearch import ElasticsearchEmbeddings
from elasticsearch import Elasticsearch
client = Elasticsearch("http://localhost:9200")
embeddings = ElasticsearchEmbeddings(
model_id="your_model_id",
client=client
)
Generate embeddings:
documents = [
"This is an example document.",
"Another example document to generate embeddings for.",
]
embeddings_list = embeddings.embed_documents(documents)
Generate query embedding:
query_embedding = embeddings.embed_query("What is this about?")
For synchronous applications, use the ElasticsearchEmbeddings class.
For asynchronous applications, use the AsyncElasticsearchEmbeddings class.
URL of the Elasticsearch instance to connect to.
Cloud ID of the Elasticsearch instance.
Username to use when connecting to Elasticsearch.
API key to use when connecting to Elasticsearch.
Password to use when connecting to Elasticsearch.