Embeddings is used to turn text into vectors that capture semantic meaning, so similar texts end up close together in vector space. This makes it useful for search, clustering, recommendations, classification, anomaly detection, and semantic similarity checks like finding duplicate or related content.In practice, you often use it in retrieval-augmented generation (RAG): embed your documents, embed the user’s question, compare the vectors, and return the most relevant passages to a model. It’s also commonly used for text search and “find things like this” workflows rather than simple keyword matching. Embeddings support the same API as langchain’s embeddings abstractions, and may be used as a direct replacement for running inside Orchestrate.
From Instance Credentials (Standalone/Runs-Elsewhere Mode)
PYTHON
from ibm_watsonx_orchestrate_sdk.langchain import WxOEmbeddingsembeddings = WxOEmbeddings.from_instance_credentials( instance_url="https://your-instance.cloud.ibm.com", api_key="your-wxo-api-key", model="openai/text-embedding-3-small")# Embed a single queryquery_embedding = embeddings.embed_query("What is machine learning?")print(f"Embedding dimension: {len(query_embedding)}")
from ibm_watsonx_orchestrate_sdk.langchain import WxOEmbeddingsembeddings = WxOEmbeddings.from_instance_credentials( instance_url="https://your-instance.cloud.ibm.com", api_key="your-api-key", model="openai/text-embedding-3-small")# Embed a single queryquery = "What is the capital of France?"query_embedding = embeddings.embed_query(query)print(f"Query embedding: {len(query_embedding)} dimensions")# Embed multiple documentsdocuments = [ "Paris is the capital of France.", "London is the capital of England.", "Berlin is the capital of Germany."]doc_embeddings = embeddings.embed_documents(documents)print(f"Embedded {len(doc_embeddings)} documents")
from ibm_watsonx_orchestrate_sdk.langchain import WxOEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain_core.documents import Document# Initialize embeddingsembeddings = WxOEmbeddings.from_instance_credentials( instance_url="https://your-instance.cloud.ibm.com", api_key="your-api-key", model="openai/text-embedding-3-small")# Create documentsdocuments = [ Document(page_content="Paris is the capital of France.", metadata={"country": "France"}), Document(page_content="London is the capital of England.", metadata={"country": "England"}), Document(page_content="Berlin is the capital of Germany.", metadata={"country": "Germany"}), Document(page_content="Madrid is the capital of Spain.", metadata={"country": "Spain"}),]# Create vector storevectorstore = FAISS.from_documents(documents, embeddings)# Perform similarity searchquery = "What is the capital of France?"results = vectorstore.similarity_search(query, k=2)for doc in results: print(f"Content: {doc.page_content}") print(f"Metadata: {doc.metadata}\n")