Skip to content

Commit

Permalink
chore (core): rename model registry to provider registry (#1956)
Browse files Browse the repository at this point in the history
Co-authored-by: Grace Yun <74513600+iteratetograceness@users.noreply.github.com>
  • Loading branch information
lgrammel and iteratetograceness authored Jun 14, 2024
1 parent 6a3bbcb commit 8c49166
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-planets-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

chore (core): rename experimental_createModelRegistry to experimental_createProviderRegistry
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
title: Model Management
description: Learn how to work with multiple providers and models
title: Provider Management
description: Learn how to work with multiple providers
---

# Model Management
# Provider Management

<Note>Model management is an experimental feature.</Note>
<Note>Provider management is an experimental feature.</Note>

When you work with multiple providers and models, it is often desirable to manage them in a central place
and access the models through simple string ids.

The Vercel AI SDK provides a [`ModelRegistry`](/docs/reference/ai-sdk-core/model-registry) for this purpose.
The Vercel AI SDK provides a [`ProviderRegistry`](/docs/reference/ai-sdk-core/provider-registry) for this purpose.
You can register multiple providers. The provider id will become the prefix of the model id:
`providerId:modelId`.

### Setup (Example)

You can create a registry with multiple providers and models using `experimental_createModelRegistry`.
You can create a registry with multiple providers and models using `experimental_createProviderRegistry`.

<Note>
It is common to keep the registry setup in a separate file and import it where
Expand All @@ -26,9 +26,9 @@ You can create a registry with multiple providers and models using `experimental
```ts filename={"registry.ts"}
import { anthropic } from '@ai-sdk/anthropic';
import { createOpenAI } from '@ai-sdk/openai';
import { experimental_createModelRegistry as createModelRegistry } from 'ai';
import { experimental_createProviderRegistry as createProviderRegistry } from 'ai';

export const registry = createModelRegistry({
export const registry = createProviderRegistry({
// register provider with prefix and default setup:
anthropic,

Expand Down
5 changes: 5 additions & 0 deletions content/docs/03-ai-sdk-core/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ description: Learn about the Vercel AI SDK Core.
description: 'Learn how to use embeddings with the Vercel AI SDK Core.',
href: '/docs/ai-sdk-core/embeddings',
},
{
title: 'Provider Management',
description: 'Learn how to work with multiple providers.',
href: '/docs/ai-sdk-core/provider-management',
},
]}
/>
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
---
title: experimental_createModelRegistry
title: experimental_createProviderRegistry
description: Registry for managing multiple providers and models (API Reference)
---

# `experimental_createModelRegistry()`
# `experimental_createProviderRegistry()`

<Note>Model management is an experimental feature.</Note>
<Note>Provider management is an experimental feature.</Note>

When you work with multiple providers and models, it is often desirable to manage them
in a central place and access the models through simple string ids.

`createModelRegistry` lets you create a registry with multiple providers that you
`createProviderRegistry` lets you create a registry with multiple providers that you
can access by their ids.

### Setup (Example)

You can create a registry with multiple providers and models using `createModelRegistry`.
You can create a registry with multiple providers and models using `createProviderRegistry`.

```ts
import { anthropic } from '@ai-sdk/anthropic';
import { createOpenAI } from '@ai-sdk/openai';
import { experimental_createModelRegistry as createModelRegistry } from 'ai';
import { experimental_createProviderRegistry as createProviderRegistry } from 'ai';

export const registry = createModelRegistry({
export const registry = createProviderRegistry({
// register provider with prefix and default setup:
anthropic,

Expand Down Expand Up @@ -50,7 +50,7 @@ const { text } = await generateText({
## Import

<Snippet
text={`import { experimental_createModelRegistry as createModelRegistry } from "ai"`}
text={`import { experimental_createProviderRegistry as createProviderRegistry } from "ai"`}
prompt={false}
/>

Expand All @@ -72,7 +72,7 @@ Registers a language model provider with a given id.

### Returns

The `experimental_createModelRegistry` function returns a `experimental_ModelRegistry` instance. It has the following methods:
The `experimental_createProviderRegistry` function returns a `experimental_ProviderRegistry` instance. It has the following methods:

<PropertiesTable
content={[
Expand Down
4 changes: 2 additions & 2 deletions content/docs/07-reference/ai-sdk-core/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ It also contains the following helper functions:
href: '/docs/reference/ai-sdk-core/tool',
},
{
title: 'createModelRegistry()',
title: 'createProviderRegistry()',
description:
'Creates a registry for using models from multiple providers.',
href: '/docs/reference/ai-sdk-core/model-registry',
href: '/docs/reference/ai-sdk-core/provider-registry',
},
{
title: 'cosineSimilarity()',
Expand Down
4 changes: 2 additions & 2 deletions examples/ai-core/src/registry/setup-registry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { anthropic } from '@ai-sdk/anthropic';
import { createOpenAI } from '@ai-sdk/openai';
import { experimental_createModelRegistry as createModelRegistry } from 'ai';
import { experimental_createProviderRegistry as createProviderRegistry } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

export const registry = createModelRegistry({
export const registry = createProviderRegistry({
// register provider with prefix and default setup:
anthropic,

Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './model-registry';
export * from './invalid-model-id-error';
export * from './no-such-model-error';
export * from './no-such-provider-error';
export * from './invalid-model-id-error';
export * from './provider-registry';
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MockLanguageModelV1 } from '../test/mock-language-model-v1';
import { InvalidModelIdError } from './invalid-model-id-error';
import { experimental_createModelRegistry } from './model-registry';
import { NoSuchModelError } from './no-such-model-error';
import { NoSuchProviderError } from './no-such-provider-error';
import { experimental_createProviderRegistry } from './provider-registry';

it('should return language model from provider', () => {
const model = new MockLanguageModelV1();

const modelRegistry = experimental_createModelRegistry({
const modelRegistry = experimental_createProviderRegistry({
provider: id => {
expect(id).toEqual('model');
return model;
Expand All @@ -18,27 +18,27 @@ it('should return language model from provider', () => {
});

it('should throw NoSuchProviderError if provider does not exist', () => {
const modelRegistry = experimental_createModelRegistry({});
const registry = experimental_createProviderRegistry({});

expect(() => modelRegistry.languageModel('provider:model')).toThrowError(
expect(() => registry.languageModel('provider:model')).toThrowError(
NoSuchProviderError,
);
});

it('should throw NoSuchModelError if provider does not return a model', () => {
const modelRegistry = experimental_createModelRegistry({
const registry = experimental_createProviderRegistry({
provider: () => null as any,
});

expect(() => modelRegistry.languageModel('provider:model')).toThrowError(
expect(() => registry.languageModel('provider:model')).toThrowError(
NoSuchModelError,
);
});

it("should throw InvalidModelIdError if model id doesn't contain a colon", () => {
const modelRegistry = experimental_createModelRegistry({});
const registry = experimental_createProviderRegistry({});

expect(() => modelRegistry.languageModel('model')).toThrowError(
expect(() => registry.languageModel('model')).toThrowError(
InvalidModelIdError,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NoSuchProviderError } from './no-such-provider-error';
/**
Registry for managing models. It enables getting a model with a string id.
*/
export type experimental_ModelRegistry = {
export type experimental_ProviderRegistry = {
/**
Returns the language model with the given id in the format `providerId:modelId`.
The model id is then passed to the provider function to get the model.
Expand All @@ -22,12 +22,17 @@ The model id is then passed to the provider function to get the model.
};

/**
* Creates a model registry for the given providers.
* @deprecated Use `experimental_ProviderRegistry` instead.
*/
export function experimental_createModelRegistry(
export type experimental_ModelRegistry = experimental_ProviderRegistry;

/**
* Creates a registry for the given providers.
*/
export function experimental_createProviderRegistry(
providers: Record<string, (id: string) => LanguageModel>,
): experimental_ModelRegistry {
const registry = new DefaultModelRegistry();
): experimental_ProviderRegistry {
const registry = new DefaultProviderRegistry();

for (const [id, provider] of Object.entries(providers)) {
registry.registerLanguageModelProvider({ id, provider });
Expand All @@ -36,7 +41,7 @@ export function experimental_createModelRegistry(
return registry;
}

class DefaultModelRegistry implements experimental_ModelRegistry {
class DefaultProviderRegistry implements experimental_ProviderRegistry {
// Mapping of provider id to provider
private providers: Record<string, (id: string) => LanguageModel> = {};

Expand Down Expand Up @@ -91,3 +96,9 @@ The model id is then passed to the provider function to get the model.
return model;
}
}

/**
* @deprecated Use `experimental_createProviderRegistry` instead.
*/
export const experimental_createModelRegistry =
experimental_createProviderRegistry;

0 comments on commit 8c49166

Please sign in to comment.