← All @molecule/* packages · App templates
@molecule/api-ai-embeddings-openaiProvider bond · ai-embeddings · API (Node) · v1.0.1 · Apache-2.0
OpenAI embeddings provider for molecule.dev — text-embedding-3-small/large
npm install @molecule/api-ai-embeddings-openainpm · Source on GitHub · Implements @molecule/api-ai-embeddings
@molecule/api-ai-embeddings-openai 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.
Works with: @molecule/api-ai-embeddings, @molecule/api-secrets
Secrets: OPENAI_API_KEY
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
OpenAI ai-embeddings provider for molecule.dev.
provider
npm install @molecule/api-ai-embeddings-openai @molecule/api-ai-embeddings @molecule/api-secrets
OpenaiEmbeddingsConfigConfiguration for the OpenAI embeddings provider.
interface OpenaiEmbeddingsConfig {
/** OpenAI API key. Defaults to OPENAI_API_KEY env var. */
apiKey?: string
/** Default embedding model. Defaults to 'text-embedding-3-small'. */
defaultModel?: string
/** Base URL for the OpenAI API. Defaults to 'https://api.openai.com'. */
baseUrl?: string
/** Maximum number of texts per batch request. Defaults to 2048. */
maxBatchSize?: number
/** Default number of output dimensions (for text-embedding-3 models). */
dimensions?: number
}
createProvider(config)Creates an OpenAI embeddings provider instance.
function createProvider(config?: OpenaiEmbeddingsConfig): AIEmbeddingsProvider
config — OpenAI-specific configuration (API key, model, base URL, dimensions).Returns: An AIEmbeddingsProvider backed by the OpenAI Embeddings API.
aiEmbeddingsOpenaiSecretDefinitionsSecret definitions required by the OpenAI embeddings bond.
const aiEmbeddingsOpenaiSecretDefinitions: SecretDefinition[]
providerThe provider implementation.
const provider: AIEmbeddingsProvider
Implements @molecule/api-ai-embeddings interface.
Setup function to register this provider with the core interface:
import { setProvider } from '@molecule/api-ai-embeddings'
import { provider } from '@molecule/api-ai-embeddings-openai'
export function setupAiEmbeddingsOpenai(): void {
setProvider(provider)
}
Peer dependencies:
@molecule/api-ai-embeddings >=1.0.1@molecule/api-secrets ^1.0.1OPENAI_API_KEY (required) — OpenAI API key
sk-proj-...@molecule/api-ai-embeddings@molecule/api-secretsConfig: OPENAI_API_KEY (SERVER-side only) plus optional defaultModel
(default text-embedding-3-small; also supports text-embedding-3-large
and text-embedding-ada-002), dimensions (text-embedding-3 models only),
maxBatchSize (default 2048 inputs per request — larger arrays are batched
automatically), and a base URL override (OPENAI_BASE_URL env var or
baseUrl, for proxies/gateways).
Wire it with the core's setProvider() — NOT bond('ai-embeddings', …):
the @molecule/api-ai-embeddings core keeps its own singleton and never
reads the bond registry (see the core's docs).
Unlike the chat AI bonds, a missing OPENAI_API_KEY does NOT fail fast —
the first embed call fails with the upstream 401. Validate the key at boot
if you want an actionable startup error.
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).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.