The input text string for which to generate the embedding.
Configuration options for the embedding generation.
OptionalapiKey?: stringGemini API key. Defaults to AI_KEY.
OptionalbeforeRequest?: () => void | Promise<void>Awaits caller-controlled pacing immediately before an uncached provider request. Cache hits skip this hook. Throwing prevents dispatch and caching. The hook is excluded from embedding identity and cache keys.
Optionalcache?: booleanWhether to persist and reuse the embedding response. Defaults to true.
OptionalcontextWindow?: undefinedOllama context windows are not valid for the Gemini API.
Optionallocation?: undefinedVertex locations are not valid for the Gemini API.
Optionalmodel?: stringThe embedding model. Defaults to AI_EMBEDDINGS_MODEL.
Optionalollama?: undefinedOllama clients are not valid for the Gemini API.
Optionalproject?: undefinedVertex projects are not valid for the Gemini API.
Explicitly selects the Google embedding provider.
Optionalverbose?: booleanWhether to log request and timing information.
Optionalvertex?: falseKeeps the request on the Gemini API rather than Vertex AI.
OptionalapiKey?: stringVertex Express Mode API key.
OptionalbeforeRequest?: () => void | Promise<void>Awaits caller-controlled pacing immediately before an uncached provider request. Cache hits skip this hook. Throwing prevents dispatch and caching. The hook is excluded from embedding identity and cache keys.
Optionalcache?: booleanWhether to persist and reuse the embedding response. Defaults to true.
OptionalcontextWindow?: undefinedOllama context windows are not valid for Vertex AI.
Optionallocation?: stringGoogle Cloud location. Defaults to AI_LOCATION.
Optionalmodel?: stringThe embedding model. Defaults to AI_EMBEDDINGS_MODEL.
Optionalollama?: undefinedOllama clients are not valid for Vertex AI.
Optionalproject?: stringGoogle Cloud project. Defaults to AI_PROJECT.
Explicitly selects the Google embedding provider.
Optionalverbose?: booleanWhether to log request and timing information.
Selects the Vertex AI backend.
OptionalapiKey?: undefinedGemini and Vertex API keys are not valid for Ollama.
OptionalbeforeRequest?: () => void | Promise<void>Awaits caller-controlled pacing immediately before an uncached provider request. Cache hits skip this hook. Throwing prevents dispatch and caching. The hook is excluded from embedding identity and cache keys.
Optionalcache?: booleanWhether to persist and reuse the embedding response. Defaults to true.
OptionalcontextWindow?: numberThe model context-window size sent to Ollama.
Optionallocation?: undefinedVertex locations are not valid for Ollama.
Optionalmodel?: stringThe embedding model. Defaults to AI_EMBEDDINGS_MODEL.
Optionalollama?: {A custom Ollama client.
OptionalembeddingEndpoint?: stringNon-secret deployment endpoint for custom adapters that are not instances of the Ollama SDK client.
Sends an embedding request to Ollama.
Optionalproject?: undefinedVertex projects are not valid for Ollama.
Explicitly selects Ollama.
Optionalverbose?: booleanWhether to log request and timing information.
Optionalvertex?: undefinedVertex selection is not valid for Ollama.
OptionalapiKey?: undefinedDirect credentials require an explicit provider.
OptionalbeforeRequest?: () => void | Promise<void>Awaits caller-controlled pacing immediately before an uncached provider request. Cache hits skip this hook. Throwing prevents dispatch and caching. The hook is excluded from embedding identity and cache keys.
Optionalcache?: booleanWhether to persist and reuse the embedding response. Defaults to true.
OptionalcontextWindow?: undefinedProvider-specific semantic options require an explicit provider.
Optionallocation?: undefinedDirect deployment selection requires an explicit provider.
Optionalmodel?: stringThe embedding model. Defaults to AI_EMBEDDINGS_MODEL.
Optionalollama?: undefinedCustom clients require an explicit provider.
Optionalproject?: undefinedDirect deployment selection requires an explicit provider.
Optionalprovider?: undefinedOmit the discriminator to select the provider from the environment.
Optionalverbose?: booleanWhether to log request and timing information.
Optionalvertex?: undefinedDirect backend selection requires an explicit provider.
A promise that resolves to an array of numbers representing the generated embedding.
// Environment-selected usage. Configure AI_EMBEDDINGS_PROVIDER,
// AI_EMBEDDINGS_MODEL, and the matching credentials first.
const embedding = await getEmbedding("The quick brown fox jumps over the lazy dog.");
console.log(embedding); // [0.012, -0.034, ..., 0.056] (example output)
// Embeddings are cached by default.
const cachedEmbedding = await getEmbedding("Artificial intelligence is transforming industries.");
console.log(cachedEmbedding);
// Generate an embedding using a specific model and API key.
const customEmbedding = await getEmbedding("Machine learning is a subset of AI.", {
provider: "gemini",
model: "another-embedding-model",
apiKey: "your_custom_api_key"
});
console.log(customEmbedding);
// Explicit Vertex AI usage.
const vertexEmbedding = await getEmbedding("Local reporting matters.", {
provider: "gemini",
vertex: true,
model: "text-embedding-004",
project: "your-project",
location: "northamerica-northeast1",
});
// Explicit Ollama usage.
const ollamaEmbedding = await getEmbedding("Local reporting matters.", {
provider: "ollama",
model: "nomic-embed-text",
contextWindow: 8192,
});
Generates a numerical embedding (vector representation) for a given text string. Embeddings are crucial for various natural language processing (NLP) tasks, including semantic search, text classification, clustering, and anomaly detection, as they allow text to be processed and compared mathematically.
This function supports both Google's Gemini AI models and local models running with Ollama. It provides options for authentication, model selection, and caching to optimize performance and cost.
Authentication: With no explicit provider, credentials, model information, and provider selection come from environment variables (
AI_KEY,AI_PROJECT,AI_LOCATION,AI_EMBEDDINGS_MODEL,AI_EMBEDDINGS_PROVIDER). Providerless options intentionally contain only fields shared by every provider.Local Models: To use a local model with Ollama, pass
provider: "ollama", or setAI_EMBEDDINGS_PROVIDER=ollama(the legacyOLLAMAenvironment variable is also supported), and ensure Ollama is running. A customOllamaclient can be passed in the provider-specificollamaoption.Caching: Responses are cached by default in a local
.journalism-cachedirectory. If the same request is made again, the cached response will be returned, avoiding redundant API calls. Setcachetofalseto disable caching. Remember to add.journalism-cacheto your.gitignorefile.Options:
provider: Explicitly selects Gemini/Vertex or Ollama. When omitted,AI_EMBEDDINGS_PROVIDERis used, then the legacyOLLAMAfallback, then Gemini.model: The specific embedding model to use (e.g., 'text-embedding-004'). Defaults to theAI_EMBEDDINGS_MODELenvironment variable.apiKey: Your Gemini API or Vertex Express Mode API key. Defaults to theAI_KEYenvironment variable.vertex: Iftrue, explicitly selects the Vertex AI backend for the Google provider.project: Your Google Cloud project ID for Vertex AI. Defaults to theAI_PROJECTenvironment variable.location: The Google Cloud location for your Vertex AI project. Defaults to theAI_LOCATIONenvironment variable.cache: Iftrue, enables caching of the embedding response. Defaults totrue.ollama: A custom Ollama client. Boolean selection remains supported at runtime for compatibility but is deprecated; useprovider: "ollama"instead.verbose: Iftrue, logs additional information such as execution time and the truncated input text. Defaults tofalse.contextWindow: An option to specify the context window size for Ollama models. By default, Ollama sets this depending on the model, which can be lower than the actual maximum context window size of the model.beforeRequest: Awaited immediately before each uncached provider dispatch, including when caching is disabled. Cache hits skip it. Throwing prevents dispatch and successful caching. Caller retries invoke it again; it is excluded from cache identity.