EmbeddingModel.ts
EmbeddingModel.ts overview
Section titled “EmbeddingModel.ts overview”Defines the provider-neutral service for text embeddings.
An EmbeddingModel turns text into numeric vectors. It supports single-input
embedding and ordered batch embedding, and represents provider failures as
AiError values. This module also includes the embedding dimensions service,
request and response models, usage metadata, provider contracts, and a
constructor that adapts a provider batch implementation into the service.
Single embed calls can be batched together internally.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”EmbeddingRequest (class)
Section titled “EmbeddingRequest (class)”Represents a tagged request used by request resolvers for embedding operations.
When to use
Use when you need a typed request for one embedding input while building or calling a low-level embedding request resolver.
See
Servicefor the resolver-bearing service contractmakefor constructing the request resolver from a provider implementationEmbedResponsefor the response produced by this request
Signature
declare class EmbeddingRequestSince v4.0.0
Creates an EmbeddingModel service from a provider embedMany implementation.
When to use
Use to adapt a provider’s batch embedding implementation into an
EmbeddingModel.Service that offers single-input and batch embedding
operations.
Details
The returned service builds single-input embed calls through a request
resolver, so concurrent embed requests can be batched into one provider
embedMany call. Direct embedMany calls pass the input array to the
provider, while embedMany([]) returns an empty response without calling the
provider.
Gotchas
Provider responses are interpreted positionally and must contain exactly one
result for each requested input. If the provider returns a different number
of results, embed and embedMany fail with AiError.InvalidOutputError.
See
Servicefor the service shape returned by this constructorProviderOptionsfor the input passed to the provider implementationProviderResponsefor the provider response contract consumed by this constructor
Signature
declare const make: (params: { readonly embedMany: (options: ProviderOptions) => Effect.Effect<ProviderResponse, AiError.AiError>}) => Effect.Effect<Service>Since v4.0.0
models
Section titled “models”EmbedManyResponse (class)
Section titled “EmbedManyResponse (class)”Response for batch embedding requests containing per-input embeddings and usage metadata.
Details
embeddings preserves batch order, and usage carries token metadata for
the operation.
See
EmbedResponsefor individual embedding responsesEmbeddingUsagefor token usage metadata
Signature
declare class EmbedManyResponseSince v4.0.0
EmbedResponse (class)
Section titled “EmbedResponse (class)”Response for a single embedding request.
Signature
declare class EmbedResponseSince v4.0.0
EmbeddingUsage (class)
Section titled “EmbeddingUsage (class)”Represents token usage metadata for embedding operations.
Details
Contains optional provider-reported inputTokens. The value may be
undefined when the provider does not report usage or when embedMany([])
bypasses the provider.
Signature
declare class EmbeddingUsageSince v4.0.0
ProviderResponse (interface)
Section titled “ProviderResponse (interface)”Provider response for batch embedding requests.
Signature
export interface ProviderResponse { readonly results: Array<Array<number>> readonly usage: { readonly inputTokens: number | undefined }}Since v4.0.0
Service (interface)
Section titled “Service (interface)”Defines the service interface for embedding operations.
Signature
export interface Service { readonly resolver: RequestResolver.RequestResolver<EmbeddingRequest> readonly embed: (input: string) => Effect.Effect<EmbedResponse, AiError.AiError> readonly embedMany: (input: ReadonlyArray<string>) => Effect.Effect<EmbedManyResponse, AiError.AiError>}Since v4.0.0
options
Section titled “options”ProviderOptions (interface)
Section titled “ProviderOptions (interface)”Provider input options for embedding requests.
Signature
export interface ProviderOptions { readonly inputs: ReadonlyArray<string>}Since v4.0.0
services
Section titled “services”Dimensions (class)
Section titled “Dimensions (class)”Service tag that provides the current embedding dimensions.
When to use
Use to retrieve or provide the configured embedding vector size through context.
See
EmbeddingModelfor the embedding service that uses these dimensions
Signature
declare class DimensionsSince v4.0.0
EmbeddingModel (class)
Section titled “EmbeddingModel (class)”Service tag for embedding model operations.
When to use
Use to retrieve or provide the embedding model service for an Effect
program that embeds text into vectors.
See
Servicefor the service contract provided by this tagmakefor constructing an embedding model service from a providerDimensionsfor the current embedding vector size service
Signature
declare class EmbeddingModelSince v4.0.0