Heroku LangChain - v0.2.1
    Preparing search index...

    Class HerokuMiaEmbeddings

    HerokuMiaEmbeddings - Heroku Managed Inference Embeddings Integration

    A LangChain-compatible embeddings class that interfaces with Heroku's Managed Inference API for generating text embeddings. This class provides access to various embedding models hosted on Heroku's infrastructure, supporting both single query embedding and batch document embedding operations with automatic retry logic and proper error handling.

    The class supports different input types (search queries, documents, classification, clustering), encoding formats, and embedding types to match your specific use case requirements.

    import { HerokuMiaEmbeddings } from "heroku-langchain";

    // Basic usage
    const embeddings = new HerokuMiaEmbeddings({
    model: "cohere-embed-multilingual",
    apiKey: process.env.EMBEDDING_KEY,
    apiUrl: process.env.EMBEDDING_URL
    });

    // Embed a single query
    const queryEmbedding = await embeddings.embedQuery("What is Heroku?");
    console.log("Query embedding dimensions:", queryEmbedding.length);

    // Embed multiple documents
    const docEmbeddings = await embeddings.embedDocuments([
    "Heroku is a cloud platform that enables companies to build, run, and operate applications entirely in the cloud.",
    "Heroku supports several programming languages including Ruby, Node.js, Python, Java, and more."
    ]);
    console.log("Document embeddings:", docEmbeddings.length);
    // Advanced usage with specific options
    const embeddings = new HerokuMiaEmbeddings({
    model: "cohere-embed-multilingual",
    maxRetries: 3,
    timeout: 30000, // 30 seconds
    additionalKwargs: {
    encoding_format: "base64",
    embedding_type: "float"
    }
    });

    // Embed with specific input type for search
    const searchEmbedding = await embeddings.embedQuery(
    "machine learning algorithms",
    { input_type: "search_query" }
    );

    // Embed documents for indexing
    const documents = [
    "Introduction to machine learning and its applications",
    "Deep learning neural networks explained",
    "Natural language processing with transformers"
    ];

    const documentEmbeddings = await embeddings.embedDocuments(documents, {
    input_type: "search_document",
    truncate: "END"
    });
    // Error handling and validation
    try {
    const embeddings = new HerokuMiaEmbeddings({
    model: "cohere-embed-multilingual"
    });

    // This will throw an error due to input constraints
    const tooManyDocs = new Array(100).fill("Sample document");
    await embeddings.embedDocuments(tooManyDocs);
    } catch (error) {
    if (error instanceof Error) {
    console.error("Validation error:", error.message);
    }
    }

    Hierarchy

    • Embeddings
      • HerokuMiaEmbeddings
    Index

    Constructors

    • Creates a new HerokuMiaEmbeddings instance.

      Parameters

      Returns HerokuMiaEmbeddings

      When model ID is not provided and EMBEDDING_MODEL_ID environment variable is not set

      // Basic usage with defaults
      const embeddings = new HerokuMiaEmbeddings();

      // With custom configuration
      const embeddings = new HerokuMiaEmbeddings({
      model: "cohere-embed-multilingual",
      apiKey: "your-embedding-api-key",
      apiUrl: "https://us.inference.heroku.com",
      maxRetries: 3,
      timeout: 30000
      });

    Properties

    model: string
    apiKey?: string
    apiUrl?: string
    maxRetries: number
    timeout?: number
    additionalKwargs: Record<string, any>

    Accessors

    • get lc_name(): string

      Get the model name for identification.

      Returns string

      The string "HerokuMiaEmbeddings"

    • get lc_serializable(): boolean

      Get additional parameters for serialization.

      Returns boolean

      true to indicate this class is serializable

    Methods

    • Internal

      Validates input constraints for Heroku embeddings API.

      The Heroku embeddings API has specific limitations that this method enforces:

      • Maximum 96 strings per request
      • Maximum 2048 characters per string

      Parameters

      • texts: string[]

        Array of strings to validate

      Returns void

      When input exceeds API constraints

    • Internal

      Makes a request to the Heroku embeddings API with retry logic.

      This method handles the actual HTTP request to the Heroku API, including:

      • Automatic retries for transient failures (5xx errors, 429 rate limits)
      • Exponential backoff with jitter
      • Proper error handling and reporting
      • Timeout management

      Parameters

      Returns Promise<HerokuEmbeddingsResponse>

      Promise resolving to the embeddings API response

      For API errors or network failures

    • Embeds a single text query.

      This method is optimized for embedding search queries and single pieces of text. It automatically sets the input_type to "search_query" unless overridden in options.

      Parameters

      Returns Promise<number[]>

      Promise resolving to an array of numbers representing the embedding vector

      const embeddings = new HerokuMiaEmbeddings({ model: "cohere-embed-multilingual" });

      // Basic query embedding
      const queryVector = await embeddings.embedQuery("machine learning algorithms");
      console.log("Embedding dimensions:", queryVector.length);

      // With custom options
      const customQueryVector = await embeddings.embedQuery(
      "natural language processing",
      {
      input_type: "classification",
      encoding_format: "base64"
      }
      );

      When the text exceeds 2048 characters

      For API-related errors

    • Embeds multiple documents.

      This method is optimized for embedding multiple documents for indexing or similarity search. It automatically sets the input_type to "search_document" unless overridden in options. Input validation ensures compliance with API constraints before making any network calls.

      Parameters

      • documents: string[]

        Array of text documents to embed (max 96 documents, 2048 chars each)

      • Optionaloptions: HerokuMiaEmbeddingsCallOptions

        Optional call-time parameters to customize the embedding request

      Returns Promise<number[][]>

      Promise resolving to an array of embedding vectors, one per input document

      const embeddings = new HerokuMiaEmbeddings({ model: "openai-text-embedding-3-large" });

      const documents = [
      "Heroku is a cloud platform for building and running applications.",
      "LangChain is a framework for developing applications powered by language models.",
      "Vector databases enable semantic search and similarity matching."
      ];

      // Basic document embedding
      const documentVectors = await embeddings.embedDocuments(documents);
      console.log(`Generated ${documentVectors.length} embeddings`);

      // With custom options for clustering
      const clusteringVectors = await embeddings.embedDocuments(documents, {
      input_type: "clustering",
      embedding_type: "int8",
      truncate: "START"
      });

      When input exceeds API constraints (>96 documents or >2048 chars per document)

      For API-related errors

    • Internal

      Internal method to embed documents.

      This method performs the actual embedding work after validation has been completed in the public embedDocuments method. It constructs the API request and processes the response.

      Parameters

      Returns Promise<number[][]>

      Promise resolving to an array of embedding vectors