LangChain MLflow AI Gateway Embeddings: Connect to MLflow Embedding Models

Posted: Nov 14, 2024.

The MlflowAIGatewayEmbeddings class in LangChain allows you to generate embeddings using models deployed through MLflow AI Gateway. This integration enables you to leverage MLflow's model serving capabilities while maintaining a consistent interface for embedding generation in your LangChain applications.

What is MlflowAIGatewayEmbeddings?

MlflowAIGatewayEmbeddings is a class that implements LangChain's Embeddings interface to connect with embedding models served through MLflow AI Gateway. It provides methods to generate embeddings for both individual queries and collections of documents through MLflow's serving infrastructure.

Reference

Here are the key components of the MlflowAIGatewayEmbeddings class:

Parameter/MethodDescription
gateway_uriThe URI where your MLflow AI Gateway API is hosted
routeThe specific route/endpoint for the embeddings model on the MLflow AI Gateway
embed_documents()Generates embeddings for a list of documents
embed_query()Generates an embedding for a single text query
aembed_documents()Async version of embed_documents
aembed_query()Async version of embed_query

How to Use MlflowAIGatewayEmbeddings

Basic Setup

First, make sure you have the required package installed:

pip install "mlflow[gateway]"

Then, you can initialize the embeddings class:

from langchain_community.embeddings import MlflowAIGatewayEmbeddings

embeddings = MlflowAIGatewayEmbeddings(
    gateway_uri="http://your-mlflow-gateway:5000",
    route="embeddings-model"
)

Generating Embeddings for Single Texts

To generate embeddings for a single piece of text:

text = "This is a sample text for embedding"
embedding = embeddings.embed_query(text)
# Returns a list of floats representing the embedding vector

Generating Embeddings for Multiple Documents

For batch processing of multiple texts:

texts = [
    "First document text",
    "Second document text",
    "Third document text"
]
embeddings_list = embeddings.embed_documents(texts)
# Returns a list of embedding vectors (list of lists of floats)

Using Async Methods

If you're working in an async environment, you can use the async versions of the methods:

import asyncio

async def generate_embeddings():
    # For single query
    query_embedding = await embeddings.aembed_query("Sample query text")
    
    # For multiple documents
    texts = ["Doc 1", "Doc 2", "Doc 3"]
    doc_embeddings = await embeddings.aembed_documents(texts)
    
    return query_embedding, doc_embeddings

# Run the async function
query_emb, doc_embs = asyncio.run(generate_embeddings())

Integration with Other LangChain Components

MlflowAIGatewayEmbeddings can be used with other LangChain components that require embeddings, such as vector stores:

from langchain.vectorstores import FAISS

texts = ["Text 1", "Text 2", "Text 3"]

embeddings = MlflowAIGatewayEmbeddings(
    gateway_uri="http://your-mlflow-gateway:5000",
    route="embeddings-model"
)

# Create a vector store using the embeddings
vector_store = FAISS.from_texts(texts, embeddings)

# Perform similarity search
query = "Sample query"
similar_docs = vector_store.similarity_search(query)

By using MlflowAIGatewayEmbeddings, you can seamlessly integrate MLflow-served embedding models into your LangChain applications while taking advantage of MLflow's model serving capabilities and management features.

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