Using Anyscale Embeddings in LangChain

Posted: Feb 5, 2025.

Anyscale embeddings provide a way to convert text into numerical vector representations that capture semantic meaning. In this guide, we'll explore how to use AnyscaleEmbeddings in LangChain to generate embeddings for your text data.

What is AnyscaleEmbeddings?

AnyscaleEmbeddings is a LangChain class that provides an interface to Anyscale's embedding models. It inherits from OpenAIEmbeddings and allows you to generate vector representations of text using Anyscale's API. These embeddings can be used for semantic search, text similarity comparisons, and other natural language processing tasks.

Reference

Key parameters for AnyscaleEmbeddings:

ParameterDescription
anyscale_api_keyYour Anyscale API key for authentication
modelThe embedding model to use (default: 'thenlper/gte-large')
chunk_sizeMaximum number of texts to embed in each batch (default: 1000)
embedding_ctx_lengthMaximum number of tokens to embed at once (default: 500)
request_timeoutTimeout for API requests
max_retriesMaximum number of retry attempts (default: 2)

How to Use AnyscaleEmbeddings

Basic Setup

First, you'll need to initialize the embeddings client with your API key:

from langchain_community.embeddings import AnyscaleEmbeddings

embeddings = AnyscaleEmbeddings(
    anyscale_api_key="your-api-key"  # Or use environment variable
)

Embedding Single Queries

To generate embeddings for a single piece of text:

text = "Hello world!"
embedding = embeddings.embed_query(text)
print(f"Embedding dimension: {len(embedding)}")

Batch Processing Documents

For multiple texts, use embed_documents:

texts = [
    "First document",
    "Second document",
    "Third document"
]
embeddings_list = embeddings.embed_documents(texts)
print(f"Number of embeddings: {len(embeddings_list)}")
print(f"Dimension of each embedding: {len(embeddings_list[0])}")

Async Operations

AnyscaleEmbeddings also supports async operations for better performance:

import asyncio

async def generate_embeddings():
    # Single query
    embedding = await embeddings.aembed_query("Hello world!")
    
    # Multiple documents
    texts = ["Doc 1", "Doc 2", "Doc 3"]
    embeddings_list = await embeddings.aembed_documents(texts)
    
    return embedding, embeddings_list

# Run async function
embedding, embeddings_list = asyncio.run(generate_embeddings())

Custom Configuration

You can customize the embeddings behavior with various parameters:

embeddings = AnyscaleEmbeddings(
    anyscale_api_key="your-api-key",
    model="thenlper/gte-large",  # Choose your model
    chunk_size=500,  # Adjust batch size
    request_timeout=30.0,  # Set custom timeout
    max_retries=3,  # Configure retry attempts
    show_progress_bar=True  # Show progress for large batches
)

This configuration allows you to fine-tune the embedding process according to your needs, whether you're processing large amounts of text or need specific error handling behavior.

Remember to handle your API key securely, preferably using environment variables:

import os
from langchain_community.embeddings import AnyscaleEmbeddings

embeddings = AnyscaleEmbeddings(
    anyscale_api_key=os.getenv("ANYSCALE_API_KEY")
)

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