Heroku AI SDK Provider - v0.4.3
    Preparing search index...

    Class HerokuEmbeddingModel

    Heroku embedding model implementation compatible with AI SDK v5.

    This class provides embedding generation capabilities using Heroku's AI infrastructure, specifically designed to work seamlessly with the Vercel AI SDK's embedding functions.

    HerokuEmbeddingModel

    Basic usage with AI SDK:

    import { embed, embedMany } from "ai";
    import { heroku } from "heroku-ai-provider";

    const model = heroku.embedding("cohere-embed-multilingual");

    // Single embedding
    const { embedding } = await embed({
    model,
    value: "Hello, world!"
    });

    // Multiple embeddings
    const { embeddings } = await embedMany({
    model,
    values: ["First text", "Second text", "Third text"]
    });

    Direct model usage:

    import { HerokuEmbeddingModel } from "heroku-ai-provider";

    const model = new HerokuEmbeddingModel(
    "cohere-embed-multilingual",
    process.env.EMBEDDING_KEY!,
    "https://us.inference.heroku.com/v1/embeddings"
    );

    const result = await model.doEmbed({
    values: ["Text to embed"]
    });

    console.log(result.embeddings[0]); // [0.1, 0.2, -0.3, ...]

    Implements

    • EmbeddingModelV2<string>
    Index

    Constructors

    • Creates a new HerokuEmbeddingModel instance.

      Parameters

      • model: string

        The Heroku embedding model identifier (e.g., "cohere-embed-multilingual")

      • apiKey: string

        Your Heroku AI API key for embeddings

      • baseUrl: string

        The base URL for the Heroku embeddings API

      Returns HerokuEmbeddingModel

      When parameters are invalid or missing

      const model = new HerokuEmbeddingModel(
      "cohere-embed-multilingual",
      process.env.EMBEDDING_KEY!,
      "https://us.inference.heroku.com/v1/embeddings"
      );

    Properties

    specificationVersion: "v2" = ...

    The embedding model must specify which embedding model interface version it implements. This will allow us to evolve the embedding model interface and retain backwards compatibility. The different implementation versions can be handled as a discriminated union on our side.

    provider: "heroku" = ...

    Name of the provider for logging purposes.

    modelId: string

    Provider-specific model ID for logging purposes.

    maxEmbeddingsPerCall: 100

    Limit of how many embeddings can be generated in a single API call.

    Use Infinity for models that do not have a limit.

    supportsParallelCalls: true

    True if the model can handle multiple embedding calls in parallel.

    Methods

    • Generate embeddings for the provided text values.

      This method implements the AI SDK v5 EmbeddingModelV2 interface, providing seamless integration with the Vercel AI SDK's embedding functions.

      Parameters

      • options: {
            values: string[];
            abortSignal?: AbortSignal;
            providerOptions?: SharedV2ProviderOptions;
            headers?: Record<string, string | undefined>;
        }

        Configuration object containing values to embed and optional settings

        • values: string[]

          Array of text strings to generate embeddings for

        • OptionalabortSignal?: AbortSignal

          Optional AbortSignal for request cancellation

        • OptionalproviderOptions?: SharedV2ProviderOptions
        • Optionalheaders?: Record<string, string | undefined>

          Optional additional HTTP headers

      Returns Promise<
          {
              embeddings: number[][];
              usage?: { tokens: number };
              providerMetadata?: SharedV2ProviderMetadata;
              response?: { headers?: Record<string, string>; body?: unknown };
          },
      >

      Promise resolving to embedding results with usage information

      When the API request fails or input validation fails

      Basic embedding generation:

      const result = await model.doEmbed({
      values: ["Hello, world!", "How are you?"]
      });

      console.log(result.embeddings.length); // 2
      console.log(result.embeddings[0].length); // 1024 (embedding dimension)
      console.log(result.usage?.tokens); // Token count used

      With abort signal for cancellation:

      const controller = new AbortController();

      // Cancel after 5 seconds
      setTimeout(() => controller.abort(), 5000);

      try {
      const result = await model.doEmbed({
      values: ["Long text to embed..."],
      abortSignal: controller.signal
      });
      } catch (error) {
      if (error.name === 'AbortError') {
      console.log('Request was cancelled');
      }
      }

      Error handling:

      try {
      const result = await model.doEmbed({
      values: [""] // Empty string will cause validation error
      });
      } catch (error) {
      if (error instanceof APICallError) {
      console.error('API Error:', error.message);
      console.error('Status:', error.statusCode);
      }
      }
    • Generate embedding for a single text string.

      This is a convenience method that wraps doEmbed for single-text use cases.

      Parameters

      • text: string

        The text string to generate an embedding for

      Returns Promise<{ embedding: number[] }>

      Promise resolving to the embedding vector

      When the API request fails or input validation fails

      const result = await model.embedSingle("Hello, world!");
      console.log(result.embedding); // [0.1, 0.2, -0.3, ...]
    • Generate embeddings for multiple texts with automatic chunking.

      This method automatically splits large batches into smaller chunks to respect API limits and processes them sequentially.

      Parameters

      • texts: string[]

        Array of text strings to generate embeddings for

      • chunkSize: number = ...

        Maximum number of texts to process in each API call

      Returns Promise<{ embeddings: number[][] }>

      Promise resolving to all embedding vectors

      When any API request fails or input validation fails

      const texts = Array.from({ length: 150 }, (_, i) => `Text ${i}`);
      const result = await model.embedBatch(texts, 50); // Process in chunks of 50
      console.log(result.embeddings.length); // 150