Walkthrough of how to generate embeddings using a hosted embedding model in ElasticsearchThe easiest way to instantiate the ElasticsearchEmbeddings class it either
using the from_credentials constructor if you are using Elastic Cloud
or using the from_es_connection constructor with any Elasticsearch cluster
Copy
Ask AI
!pip -q install langchain-elasticsearch
Copy
Ask AI
from langchain_elasticsearch import ElasticsearchEmbeddings
# Create embeddings for multiple documentsdocuments = [ "This is an example document.", "Another example document to generate embeddings for.",]document_embeddings = embeddings.embed_documents(documents)
Copy
Ask AI
# Print document embeddingsfor i, embedding in enumerate(document_embeddings): print(f"Embedding for document {i + 1}: {embedding}")
Copy
Ask AI
# Create an embedding for a single queryquery = "This is a single query."query_embedding = embeddings.embed_query(query)
Copy
Ask AI
# Print query embeddingprint(f"Embedding for query: {query_embedding}")
# Instantiate ElasticsearchEmbeddings using es_connectionembeddings = ElasticsearchEmbeddings.from_es_connection( model_id, es_connection,)
Copy
Ask AI
# Create embeddings for multiple documentsdocuments = [ "This is an example document.", "Another example document to generate embeddings for.",]document_embeddings = embeddings.embed_documents(documents)
Copy
Ask AI
# Print document embeddingsfor i, embedding in enumerate(document_embeddings): print(f"Embedding for document {i + 1}: {embedding}")
Copy
Ask AI
# Create an embedding for a single queryquery = "This is a single query."query_embedding = embeddings.embed_query(query)
Copy
Ask AI
# Print query embeddingprint(f"Embedding for query: {query_embedding}")
Assistant
Responses are generated using AI and may contain mistakes.