Documentation Index
Fetch the complete documentation index at: https://mintlify.com/firebase/genkit/llms.txt
Use this file to discover all available pages before exploring further.
Embedders convert text and documents into vector embeddings for semantic search and retrieval.
defineEmbedder()
Defines and registers an embedder.
import { defineEmbedder } from '@genkit-ai/ai';
import { z } from 'zod';
const myEmbedder = defineEmbedder(
registry,
{
name: 'myEmbedder',
configSchema: z.object({
dimensions: z.number().optional(),
}),
info: {
label: 'My Custom Embedder',
dimensions: 768,
supports: {
input: ['text'],
},
},
},
async (input, options) => {
// Generate embeddings for each document
const embeddings = input.map(doc => ({
embedding: generateEmbedding(doc.text()),
}));
return { embeddings };
}
);
Parameters
The Genkit registry instance
options
EmbedderOptions<ConfigSchema>
required
Embedder configurationShow EmbedderOptions properties
Unique name for the embedder
Zod schema for embedder-specific configuration options
Metadata about the embedder’s capabilitiesShow EmbedderInfo properties
Human-readable label for the embedder
Embedding vector dimension size
input
Array<'text' | 'image' | 'video'>
Supported input types
Whether the embedder supports multiple languages
runner
EmbedderFn<ConfigSchema>
required
Implementation functionFunction signature:(input: Document[], options?: z.infer<ConfigSchema>) => Promise<EmbedResponse>
Returns
EmbedderAction
EmbedderAction<ConfigSchema>
An embedder action that can be used with embed()
embed()
Generates embeddings for text or documents.
import { embed } from '@genkit-ai/ai';
const embeddings = await embed(registry, {
embedder: 'myEmbedder',
content: 'Hello, world!',
});
console.log(embeddings[0].embedding); // [0.123, 0.456, ...]
Parameters
The Genkit registry instance
params
EmbedderParams<CustomOptions>
required
Embedding request parametersShow EmbedderParams properties
embedder
EmbedderArgument<CustomOptions>
required
Embedder to use (string name, EmbedderAction, or EmbedderReference)
content
string | DocumentData
required
Text or document to embed
Metadata to attach to the document
Embedder-specific configuration options
Returns
Array of embedding objects{
embedding: number[]; // Vector representation
metadata?: Record<string, unknown>;
}
embedMany()
Generates embeddings for multiple documents in a batch.
import { embedMany } from '@genkit-ai/ai';
const embeddings = await embedMany(registry, {
embedder: 'myEmbedder',
content: ['First document', 'Second document', 'Third document'],
});
console.log(embeddings.length); // 3
Parameters
The Genkit registry instance
embedder
EmbedderArgument<ConfigSchema>
required
Embedder to use
content
string[] | DocumentData[]
required
Array of texts or documents to embed
Metadata to attach to all documents
Embedder-specific options
Returns
Batch of embeddingstype EmbeddingBatch = { embedding: number[] }[];
embedderRef()
Creates a reference to an embedder with configuration.
import { embedderRef } from '@genkit-ai/ai';
import { z } from 'zod';
const myEmbedderRef = embedderRef({
name: 'myEmbedder',
config: { dimensions: 512 },
info: {
label: 'My Embedder',
dimensions: 512,
},
});
// Use the reference
const embeddings = await embed(registry, {
embedder: myEmbedderRef,
content: 'Hello',
});
Parameters
options
EmbedderReference<CustomOptionsSchema>
required
config
z.infer<CustomOptionsSchema>
Configuration values
Optional namespace prefix
Returns
reference
EmbedderReference<CustomOptionsSchema>
An embedder reference that can be passed to embed()
Types
EmbedRequest
interface EmbedRequest<O = any> {
input: Document[];
options?: O;
}
EmbedResponse
interface EmbedResponse {
embeddings: Embedding[];
}
Embedding
interface Embedding {
embedding: number[];
metadata?: Record<string, unknown>;
}
EmbedderAction
type EmbedderAction<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny> =
Action<typeof EmbedRequestSchema, typeof EmbedResponseSchema> & {
__configSchema?: CustomOptions;
};
Example: Custom Embedder
import { ai } from './genkit';
import { Document } from '@genkit-ai/ai';
import { z } from 'zod';
const customEmbedder = ai.defineEmbedder(
{
name: 'custom/embedder',
configSchema: z.object({
model: z.string().default('base'),
batchSize: z.number().default(32),
}),
info: {
label: 'Custom Embedder',
dimensions: 768,
supports: {
input: ['text'],
multilingual: false,
},
},
},
async (documents, options) => {
const embeddings = [];
// Process in batches
for (let i = 0; i < documents.length; i += options.batchSize) {
const batch = documents.slice(i, i + options.batchSize);
const batchEmbeddings = await generateEmbeddingsBatch(
batch.map(d => d.text()),
options.model
);
embeddings.push(...batchEmbeddings);
}
return {
embeddings: embeddings.map((embedding, i) => ({
embedding,
metadata: documents[i].metadata,
})),
};
}
);
// Use the embedder
const result = await ai.embed({
embedder: customEmbedder,
content: 'Sample text to embed',
});