langchain_elasticsearch.embeddings
.ElasticsearchEmbeddings¶
- class langchain_elasticsearch.embeddings.ElasticsearchEmbeddings(client: MlClient, model_id: str, *, input_field: str = 'text_field')[source]¶
Elasticsearch embedding models.
This class provides an interface to generate embeddings using a model deployed in an Elasticsearch cluster. It requires an Elasticsearch connection object and the model_id of the model deployed in the cluster.
In Elasticsearch you need to have an embedding model loaded and deployed. - https://elastic.ac.cn/guide/en/elasticsearch/reference/current/infer-trained-model.html - https://elastic.ac.cn/guide/en/machine-learning/current/ml-nlp-deploy-models.html
Initialize the ElasticsearchEmbeddings instance.
- Parameters
client (MlClient) – An Elasticsearch ML client object.
model_id (str) – The model_id of the model deployed in the Elasticsearch cluster.
input_field (str) – The name of the key for the input text field in the document. Defaults to ‘text_field’.
Methods
__init__
(client, model_id, *[, input_field])Initialize the ElasticsearchEmbeddings instance.
aembed_documents
(texts)Asynchronous Embed search docs.
aembed_query
(text)Asynchronous Embed query text.
embed_documents
(texts)Generate embeddings for a list of documents.
embed_query
(text)Generate an embedding for a single query text.
from_credentials
(model_id, *[, es_cloud_id, ...])Instantiate embeddings from Elasticsearch credentials.
from_es_connection
(model_id, es_connection)Instantiate embeddings from an existing Elasticsearch connection.
- __init__(client: MlClient, model_id: str, *, input_field: str = 'text_field')[source]¶
Initialize the ElasticsearchEmbeddings instance.
- Parameters
client (MlClient) – An Elasticsearch ML client object.
model_id (str) – The model_id of the model deployed in the Elasticsearch cluster.
input_field (str) – The name of the key for the input text field in the document. Defaults to ‘text_field’.
- async aembed_documents(texts: List[str]) List[List[float]] ¶
Asynchronous Embed search docs.
- Parameters
texts (List[str]) – List of text to embed.
- Returns
List of embeddings.
- Return type
List[List[float]]
- async aembed_query(text: str) List[float] ¶
Asynchronous Embed query text.
- Parameters
text (str) – Text to embed.
- Returns
Embedding.
- Return type
List[float]
- embed_documents(texts: List[str]) List[List[float]] [source]¶
Generate embeddings for a list of documents.
- Parameters
texts (List[str]) – A list of document text strings to generate embeddings for.
- Returns
- A list of embeddings, one for each document in the input
list.
- Return type
List[List[float]]
- embed_query(text: str) List[float] [source]¶
Generate an embedding for a single query text.
- Parameters
text (str) – The query text to generate an embedding for.
- Returns
The embedding for the input query text.
- Return type
List[float]
- classmethod from_credentials(model_id: str, *, es_cloud_id: Optional[str] = None, es_api_key: Optional[str] = None, input_field: str = 'text_field') ElasticsearchEmbeddings [source]¶
Instantiate embeddings from Elasticsearch credentials.
- Parameters
model_id (str) – The model_id of the model deployed in the Elasticsearch cluster.
input_field (str) – The name of the key for the input text field in the document. Defaults to ‘text_field’.
es_cloud_id (Optional[str]) – (str, optional): The Elasticsearch cloud ID to connect to.
es_user – (str, optional): Elasticsearch username.
es_password – (str, optional): Elasticsearch password.
es_api_key (Optional[str]) –
- Return type
Example
from langchain_elasticserach.embeddings import ElasticsearchEmbeddings # Define the model ID and input field name (if different from default) model_id = "your_model_id" # Optional, only if different from 'text_field' input_field = "your_input_field" # Credentials can be passed in two ways. Either set the env vars # ES_CLOUD_ID, ES_USER, ES_PASSWORD and they will be automatically # pulled in, or pass them in directly as kwargs. embeddings = ElasticsearchEmbeddings.from_credentials( model_id, input_field=input_field, # es_cloud_id="foo", # es_user="bar", # es_password="baz", ) documents = [ "This is an example document.", "Another example document to generate embeddings for.", ] embeddings_generator.embed_documents(documents)
- classmethod from_es_connection(model_id: str, es_connection: Elasticsearch, input_field: str = 'text_field') ElasticsearchEmbeddings [source]¶
Instantiate embeddings from an existing Elasticsearch connection.
This method provides a way to create an instance of the ElasticsearchEmbeddings class using an existing Elasticsearch connection. The connection object is used to create an MlClient, which is then used to initialize the ElasticsearchEmbeddings instance.
Args: model_id (str): The model_id of the model deployed in the Elasticsearch cluster. es_connection (elasticsearch.Elasticsearch): An existing Elasticsearch connection object. input_field (str, optional): The name of the key for the input text field in the document. Defaults to ‘text_field’.
Returns: ElasticsearchEmbeddings: An instance of the ElasticsearchEmbeddings class.
Example
from elasticsearch import Elasticsearch from langchain_elasticsearch.embeddings import ElasticsearchEmbeddings # Define the model ID and input field name (if different from default) model_id = "your_model_id" # Optional, only if different from 'text_field' input_field = "your_input_field" # Create Elasticsearch connection es_connection = Elasticsearch( hosts=["localhost:9200"], http_auth=("user", "password") ) # Instantiate ElasticsearchEmbeddings using the existing connection embeddings = ElasticsearchEmbeddings.from_es_connection( model_id, es_connection, input_field=input_field, ) documents = [ "This is an example document.", "Another example document to generate embeddings for.", ] embeddings_generator.embed_documents(documents)
- Parameters
model_id (str) –
es_connection (Elasticsearch) –
input_field (str) –
- Return type