@molecule/api-ai-embeddings-molecule

Provider bond · ai-embeddings · API (Node) · v1.1.2 · Apache-2.0

molecule.dev hosted embeddings provider — embeddings billed to your molecule project, no vendor account

npm install @molecule/api-ai-embeddings-molecule

npm · Source on GitHub · Implements @molecule/api-ai-embeddings

How it works

@molecule/api-ai-embeddings-molecule is a provider bond on the API (Node) side: it implements the ai-embeddings core interface (@molecule/api-ai-embeddings) with a concrete vendor or library behind it.

Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.

import { setProvider, requireProvider } from '@molecule/api-ai-embeddings'
import { provider } from '@molecule/api-ai-embeddings-molecule'

setProvider(provider) // reads MOLECULE_API_KEY from the environment

const { embeddings, usage } = await requireProvider().embed({
  input: ['first document', 'second document'],
})
const queryVector = await requireProvider().embedQuery('what is molecule?')

Works with: @molecule/api-ai-embeddings, @molecule/api-secrets

Secrets: MOLECULE_API_KEY

Reference

molecule.dev hosted embeddings provider for @molecule/api-ai-embeddings.

Embeddings run on molecule.dev and are billed to your molecule project, so the app needs no OpenAI account. It is an ordinary bond: swap it for @molecule/api-ai-embeddings-openai (your own key) or @molecule/api-ai-embeddings-local (self-hosted) without changing code that calls the core.

Quick Start

import { setProvider, requireProvider } from '@molecule/api-ai-embeddings'
import { provider } from '@molecule/api-ai-embeddings-molecule'

setProvider(provider) // reads MOLECULE_API_KEY from the environment

const { embeddings, usage } = await requireProvider().embed({
  input: ['first document', 'second document'],
})
const queryVector = await requireProvider().embedQuery('what is molecule?')

Type

provider

Installation

npm install @molecule/api-ai-embeddings-molecule @molecule/api-ai-embeddings @molecule/api-secrets

API

Interfaces

MoleculeEmbeddingsConfig

Options for createProvider. Every field falls back to an env var, so the zero-argument provider export works from .env alone.

interface MoleculeEmbeddingsConfig {
  /**
   * The molecule project API key (`mk_…`), or the sandbox token molecule.dev
   * writes for in-IDE apps (`mbk_…`). Defaults to `MOLECULE_API_KEY`.
   */
  apiKey?: string
  /**
   * The hosted services base URL, without a trailing slash. Defaults to
   * `MOLECULE_SERVICES_URL`, then `https://api.molecule.dev/api/v1/services`.
   */
  servicesUrl?: string
  /** Default model: `text-embedding-3-small` (default) or `text-embedding-3-large`. */
  defaultModel?: string
  /** Default output dimensions (1–3072). Omit for the model's native size. */
  dimensions?: number
  /** Per-request timeout in milliseconds. Default 60000. */
  timeoutMs?: number
}

ProcessEnv

Environment variables this provider reads.

interface ProcessEnv {
  MOLECULE_API_KEY?: string
  MOLECULE_SERVICES_URL?: string
}

Classes

MoleculeEmbeddingsProvider

Embeddings through molecule.dev's hosted service.

MoleculeServiceError

Error thrown for a refused or failed hosted-service call.

Functions

batchInputs(inputs)

Split inputs into batches that each fit the service's per-request limits.

function batchInputs(inputs: string[]): string[][]
  • inputs — The texts to embed.

Returns: Batches in order.

createProvider(config)

Create a hosted embeddings provider.

function createProvider(config?: MoleculeEmbeddingsConfig): AIEmbeddingsProvider
  • config — Options; each falls back to its env var.

Returns: An AIEmbeddingsProvider backed by molecule.dev.

Constants

aiEmbeddingsMoleculeSecretDefinitions

Secret definitions required by the molecule.dev hosted embeddings bond.

const aiEmbeddingsMoleculeSecretDefinitions: SecretDefinition[]

DEFAULT_SERVICES_URL

Default hosted services base URL.

const DEFAULT_SERVICES_URL: 'https://api.molecule.dev/api/v1/services'

provider

The provider implementation.

const provider: AIEmbeddingsProvider

SERVICE_LIMITS

Per-request limits of the hosted service. Larger inputs are split into several requests by this provider, never sent whole.

const SERVICE_LIMITS: {
  readonly maxInputs: 256
  readonly maxCharsPerInput: 32000
  readonly maxTotalChars: 400000
}

Core Interface

Implements @molecule/api-ai-embeddings interface.

Bond Wiring

Setup function to register this provider with the core interface:

import { setProvider } from '@molecule/api-ai-embeddings'
import { provider } from '@molecule/api-ai-embeddings-molecule'

export function setupAiEmbeddingsMolecule(): void {
  setProvider(provider)
}

Injection Notes

Requirements

Peer dependencies:

  • @molecule/api-ai-embeddings >=1.0.1
  • @molecule/api-secrets ^1.0.1

Environment Variables

  • MOLECULE_API_KEY (required) — molecule.dev project API key
    • Setup: Create one for your project with mlcl apikey create --project <id> --name <name> (keys start with mk_).
    • Get it here: https://www.molecule.dev
    • Example: mk_...

Runtime Dependencies

  • @molecule/api-ai-embeddings

  • @molecule/api-secrets

  • Config: MOLECULE_API_KEY (SERVER-side only) — a molecule project API key (mk_…) with scope broker or broker:embeddings. In the molecule.dev IDE the platform can write it for you. Optional MOLECULE_SERVICES_URL (default https://api.molecule.dev/api/v1/services; https required — a required — plain-http is refused unless the host is loopback or a private-network endpoint (RFC 1918 / *.docker.internal, e.g. the sandbox gateway host.docker.internal)).

  • Models: text-embedding-3-small (default, 1536 dims) and text-embedding-3-large (3072). Other model ids are refused with a 400 — the service never runs a model it cannot price. dimensions (1–3072) shortens vectors.

  • Vectors from different models (or different dimensions) are NOT comparable. Store the model id next to each vector, and re-embed everything if you change it. Swapping this bond for the OpenAI bond with the same model keeps vectors compatible; swapping to the local bond does not.

  • Large inputs are split automatically into requests of at most 256 texts / 400,000 characters; one text over 32,000 characters throws — chunk long documents first (you should anyway, for retrieval quality).

  • Errors are MoleculeServiceError with status and errorKey. 401: bad or revoked key. 402: the project owner's included allowance is used up — usage billing must be enabled on molecule.dev. 429: too many requests for the project. 503: the platform paused the service briefly (retry later). None of these are retried for you.

  • Never import this from browser code: the key would ship to every visitor.

E2E Tests

Integration checklist — drive the real flow (no mocks), adapt each item to this app's actual data and features, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip. Embeddings are infrastructure, so PROVE them through the feature they power (semantic search / "related items" / dedup) AND with a direct property check on the vectors:

  • embedQuery(text) returns a non-empty numeric number[] of the model's fixed dimension, and every vector from embed/embedDocuments has that SAME length — no empty arrays, no NaN/null entries, and the length is identical across calls (a query and a document must be comparable).
  • The semantic property holds — there is NO built-in similarity helper, so compute cosine similarity yourself (dot product over the two magnitudes): two related texts ("dog"/"puppy", or a query and a matching doc) score HIGH, two unrelated texts ("dog"/"quarterly taxes") score clearly LOWER. If every pair scores alike the vectors are dead — one positive check alone doesn't prove it.
  • Ranking works: embed a query plus a handful of documents and sort by cosine similarity — the semantically closest document ranks ABOVE the unrelated ones. This ordering is the whole point; a query that ranks an off-topic doc first is broken.
  • The feature built on it works end-to-end from the real UI: semantic search / "related items" / dedup returns results ranked by MEANING, not keyword — a search for a synonym or paraphrase finds the right item even when it shares NO words with the query (the case a plain text/keyword search would miss).
  • Embedding is stable: the same text embedded twice yields (near-)identical vectors, so a stored index stays valid — re-embedding an item must not silently drift it out of its own neighborhood.
  • Batching is order-preserving: embedDocuments([a, b, c]) (or embed({ input })) returns exactly one vector per input in the SAME order — embeddings[i] is the vector for input[i], never shuffled, merged, or dropped.
  • Embedding runs SERVER-SIDE: the call goes through the app's API and the provider key never reaches the browser (no provider request or key in the Network tab). Any endpoint that embeds caller-supplied text is authenticated and rate-limited — an open embed endpoint is an unbounded per-token cost/abuse vector.