Centralize provider configuration
An application supports models from multiple vendors and needs one consistent lookup mechanism.
AI Tools · Function
Creates a registry for multiple providers and models. Registered models are retrieved with provider-qualified IDs such as providerId:modelId.
createProviderRegistry builds a central provider registry from a mapping of unique provider identifiers to providers. The returned provider exposes methods such as languageModel and embeddingModel, which resolve registered models from identifiers formatted as providerId:modelId.
createProviderRegistry(providers: Record<string, Provider>): Provider| Name | Type | Description |
|---|---|---|
providers | Record<string, Provider> | A record mapping unique provider identifiers to Provider instances. |
A Provider instance with languageModel and embeddingModel methods.
openai:gpt-4o, rather than only the model name.import { createProviderRegistry } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
import { openai } from '@ai-sdk/openai';
const registry = createProviderRegistry({ anthropic, openai });
const model = registry.languageModel('openai:gpt-4o');Registers providers under their object keys, then resolves a language model with the provider:model identifier format.
An application supports models from multiple vendors and needs one consistent lookup mechanism.
Runtime configuration selects a model using a string identifier such as openai:gpt-4o.
A service needs both chat models and text embedding models from registered providers.
It creates a registry that combines multiple providers and models behind one provider interface. Each provider is registered under an identifier, and models are selected with identifiers in the form providerId:modelId.
Follow-up: How would you retrieve a text embedding model? Use the registry's embeddingModel method with the corresponding providerId:modelId string.
The argument is a record whose keys identify providers and whose values are Provider instances. Provider identifiers should be unique within the registry so model resolution remains unambiguous.
Follow-up: What does the returned object provide? It is a Provider instance with model-resolution methods, including languageModel and embeddingModel.
Call languageModel with a string such as openai:gpt-4o; the provider portion selects the registered provider and the remaining portion identifies its model. The registry also supports embeddingModel for text embedding models.
Follow-up: What naming convention is used? The provider ID is used as the model ID prefix, separated from the model ID by a colon.
languageModelembeddingModelProviderLanguageModelEmbeddingModel