Skip to main content

OpenSearch

A guide to using embeddings with OpenSearch ML Plugins. Ensure that your OpenSearch cluster has the embedding plugins installed.

For more information, visit: https://opensearch.org/docs/latest/ml-commons-plugin/pretrained-models/#sentence-transformers"

from langchain_community.embeddings.opensearch import OpenSearchEmbeddings
API Reference:OpenSearchEmbeddings
#Let's initialized opensearch client using opensearchpy
from opensearchpy import OpenSearch

client = OpenSearch(
hosts=[{'host': "localhost", 'port': 9200}],
http_auth=("username", "password"),
use_ssl=True,
verify_certs=False
)
model_id = "embedding_model_id"
embeddings = OpenSearchEmbeddings.from_opensearch_connection(opensearch_client, model_id)

Embedding documentsโ€‹

documents = ["Foo", "Bar", "Foo Bar"]
embedded_documents = embeddings.embed_documents(documents)
for i, doc in enumerate(documents):
print(f"Document: {doc}")
print(f"Embedding: {embedded_documents[i][:5]}...") # Show first 5 values to avoid overwhelming output
print("\n")

Embedding a queryโ€‹

query = "Hello World!"
embedded_query = embeddings.embed_query(query)
print("Query Embedding:")
print(f"Query: {query}")
print(f"Embedding: {embedded_query[:5]}...") # Show first 5 values of the embedding

Was this page helpful?


You can also leave detailed feedback on GitHub.