Using LangChain TextEmbed for Generating Vector Embeddings

Posted: Feb 20, 2025.

Vector embeddings are a crucial component in many natural language processing applications. LangChain's TextEmbedEmbeddings provides an interface to work with TextEmbed, a high-throughput REST API for serving vector embeddings.

What is TextEmbedEmbeddings?

TextEmbedEmbeddings is a LangChain class that allows you to generate vector embeddings using the TextEmbed inference server. It supports various sentence-transformer models and provides both synchronous and asynchronous methods for embedding documents and queries.

Reference

MethodDescription
embed_documents(texts: List[str])Generates embeddings for a list of texts
embed_query(text: str)Generates an embedding for a single text query
aembed_documents(texts: List[str])Asynchronously generates embeddings for a list of texts
aembed_query(text: str)Asynchronously generates an embedding for a single text query

Setting Up TextEmbed Server

Before using TextEmbedEmbeddings, you need to set up the TextEmbed server:

# Install TextEmbed
pip install -U textembed

# Start the server
python -m textembed.server --models sentence-transformers/all-MiniLM-L12-v2 --workers 4 --api-key TextEmbed

How to use TextEmbedEmbeddings

Initialize the Embeddings Class

First, create an instance of TextEmbedEmbeddings with your desired configuration:

from langchain_community.embeddings import TextEmbedEmbeddings

embeddings = TextEmbedEmbeddings(
    model="sentence-transformers/all-MiniLM-L12-v2",
    api_url="http://localhost:8000/v1",
    api_key="TextEmbed"
)

Embedding Documents and Queries

You can use TextEmbedEmbeddings to generate embeddings for both documents and queries:

# Sample documents
documents = [
    "Data science involves extracting insights from data.",
    "Artificial intelligence is transforming various industries.",
    "Cloud computing provides scalable resources.",
]

# Generate embeddings for documents
document_embeddings = embeddings.embed_documents(documents)

# Generate embedding for a single query
query = "What is data science?"
query_embedding = embeddings.embed_query(query)

Computing Similarity Scores

Once you have your embeddings, you can compute similarity scores to find relevant documents:

import numpy as np

# Convert embeddings to numpy arrays and compute similarity
scores = np.array(document_embeddings) @ np.array(query_embedding).T

# Create a dictionary of documents and their similarity scores
results = dict(zip(documents, scores))

# Print results sorted by similarity
for doc, score in sorted(results.items(), key=lambda x: x[1], reverse=True):
    print(f"Score: {score:.4f} | Document: {doc}")

Async Usage

For applications requiring asynchronous operation, you can use the async methods:

import asyncio

async def embed_texts():
    # Embed multiple documents asynchronously
    doc_embeddings = await embeddings.aembed_documents(documents)
    
    # Embed a query asynchronously
    query_embedding = await embeddings.aembed_query("What is AI?")
    
    return doc_embeddings, query_embedding

# Run async code
doc_embeddings, query_embedding = asyncio.run(embed_texts())

TextEmbedEmbeddings provides a powerful way to generate vector embeddings for your text data. With support for various sentence-transformer models and both sync and async operations, it can be integrated into a wide range of applications, from semantic search to document classification.

Remember to properly configure the TextEmbed server and choose an appropriate model for your specific use case. The quality of the embeddings will depend on the model you select and how well it matches your domain requirements.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs