Skip to content

Commit 26735b5

Browse files
authored
chore(embedding-model): add v2 interface (#5696)
1 parent 1273a13 commit 26735b5

File tree

46 files changed

+255
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+255
-148
lines changed

‎.changeset/heavy-pens-destroy.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@ai-sdk/openai-compatible': patch
3+
'@ai-sdk/amazon-bedrock': patch
4+
'@ai-sdk/google-vertex': patch
5+
'@ai-sdk/togetherai': patch
6+
'@ai-sdk/deepinfra': patch
7+
'@ai-sdk/fireworks': patch
8+
'@ai-sdk/provider': patch
9+
'@ai-sdk/mistral': patch
10+
'@ai-sdk/cohere': patch
11+
'@ai-sdk/google': patch
12+
'@ai-sdk/openai': patch
13+
'ai': patch
14+
---
15+
16+
chore(embedding-model): add v2 interface

‎content/docs/03-ai-sdk-core/55-testing.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and calling them is slow and expensive.
1111
To enable you to unit test your code that uses the AI SDK, the AI SDK Core
1212
includes mock providers and test helpers. You can import the following helpers from `ai/test`:
1313

14-
- `MockEmbeddingModelV1`: A mock embedding model using the [embedding model v1 specification](https://github.com/vercel/ai/blob/main/packages/provider/src/embedding-model/v1/embedding-model-v1.ts).
14+
- `MockEmbeddingModelV2`: A mock embedding model using the [embedding model v1 specification](https://github.com/vercel/ai/blob/main/packages/provider/src/embedding-model/v1/embedding-model-v1.ts).
1515
- `MockLanguageModelV1`: A mock language model using the [language model v1 specification](https://github.com/vercel/ai/blob/main/packages/provider/src/language-model/v1/language-model-v1.ts).
1616
- `mockId`: Provides an incrementing integer ID.
1717
- `mockValues`: Iterates over an array of values with each call. Returns the last value when the array is exhausted.

‎content/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const myOpenAI = customProvider({
4949
},
5050
{
5151
name: 'textEmbeddingModels',
52-
type: 'Record<string, EmbeddingModelV1<string>>',
52+
type: 'Record<string, EmbeddingModelV2<string>>',
5353
isOptional: true,
5454
description:
5555
'A record of text embedding models, where keys are model IDs and values are EmbeddingModel<string> instances.',

‎content/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: Troubleshooting errors related to incompatible models.
99

1010
I have updated the AI SDK and now I get the following error: `Type 'SomeModel' is not assignable to type 'LanguageModelV1'.`
1111

12-
<Note>Similar errors can occur with `EmbeddingModelV1` as well.</Note>
12+
<Note>Similar errors can occur with `EmbeddingModelV2` as well.</Note>
1313

1414
## Background
1515

‎content/providers/02-openai-compatible-providers/01-custom-providers.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The completion, embedding, and image settings are implemented similarly to the c
4747
2. **example-provider.ts** - Main provider implementation:
4848

4949
```ts
50-
import { LanguageModelV1, EmbeddingModelV1 } from '@ai-sdk/provider';
50+
import { LanguageModelV1, EmbeddingModelV2 } from '@ai-sdk/provider';
5151
import {
5252
OpenAICompatibleChatLanguageModel,
5353
OpenAICompatibleCompletionLanguageModel,
@@ -116,7 +116,7 @@ Creates a text embedding model for text generation.
116116
textEmbeddingModel(
117117
modelId: ExampleEmbeddingModelId,
118118
settings?: ExampleEmbeddingSettings,
119-
): EmbeddingModelV1<string>;
119+
): EmbeddingModelV2<string>;
120120

121121
/**
122122
Creates an image model for image generation.

‎examples/ai-core/src/e2e/feature-test-suite.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GoogleGenerativeAIProviderMetadata } from '@ai-sdk/google';
22
import type {
3-
EmbeddingModelV1,
3+
EmbeddingModelV2,
44
ImageModelV1,
55
LanguageModelV2,
66
} from '@ai-sdk/provider';
@@ -57,9 +57,9 @@ export const createLanguageModelWithCapabilities = (
5757
});
5858

5959
export const createEmbeddingModelWithCapabilities = (
60-
model: EmbeddingModelV1<string>,
60+
model: EmbeddingModelV2<string>,
6161
capabilities: ModelCapabilities = ['embedding'],
62-
): ModelWithCapabilities<EmbeddingModelV1<string>> => ({
62+
): ModelWithCapabilities<EmbeddingModelV2<string>> => ({
6363
model,
6464
capabilities,
6565
});
@@ -75,7 +75,7 @@ export const createImageModelWithCapabilities = (
7575
export interface ModelVariants {
7676
invalidModel?: LanguageModelV2;
7777
languageModels?: ModelWithCapabilities<LanguageModelV2>[];
78-
embeddingModels?: ModelWithCapabilities<EmbeddingModelV1<string>>[];
78+
embeddingModels?: ModelWithCapabilities<EmbeddingModelV2<string>>[];
7979
invalidImageModel?: ImageModelV1;
8080
imageModels?: ModelWithCapabilities<ImageModelV1>[];
8181
}

‎packages/ai/core/embed/embed-many.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from 'node:assert';
22
import {
3-
MockEmbeddingModelV1,
3+
MockEmbeddingModelV2,
44
mockEmbed,
5-
} from '../test/mock-embedding-model-v1';
5+
} from '../test/mock-embedding-model-v2';
66
import { MockTracer } from '../test/mock-tracer';
77
import { embedMany } from './embed-many';
88

@@ -21,7 +21,7 @@ const testValues = [
2121
describe('result.embedding', () => {
2222
it('should generate embeddings', async () => {
2323
const result = await embedMany({
24-
model: new MockEmbeddingModelV1({
24+
model: new MockEmbeddingModelV2({
2525
maxEmbeddingsPerCall: 5,
2626
doEmbed: mockEmbed(testValues, dummyEmbeddings),
2727
}),
@@ -35,7 +35,7 @@ describe('result.embedding', () => {
3535
let callCount = 0;
3636

3737
const result = await embedMany({
38-
model: new MockEmbeddingModelV1({
38+
model: new MockEmbeddingModelV2({
3939
maxEmbeddingsPerCall: 2,
4040
doEmbed: async ({ values }) => {
4141
switch (callCount++) {
@@ -60,7 +60,7 @@ describe('result.embedding', () => {
6060
describe('result.values', () => {
6161
it('should include values in the result', async () => {
6262
const result = await embedMany({
63-
model: new MockEmbeddingModelV1({
63+
model: new MockEmbeddingModelV2({
6464
maxEmbeddingsPerCall: 5,
6565
doEmbed: mockEmbed(testValues, dummyEmbeddings),
6666
}),
@@ -76,7 +76,7 @@ describe('result.usage', () => {
7676
let callCount = 0;
7777

7878
const result = await embedMany({
79-
model: new MockEmbeddingModelV1({
79+
model: new MockEmbeddingModelV2({
8080
maxEmbeddingsPerCall: 2,
8181
doEmbed: async () => {
8282
switch (callCount++) {
@@ -105,7 +105,7 @@ describe('result.usage', () => {
105105
describe('options.headers', () => {
106106
it('should set headers', async () => {
107107
const result = await embedMany({
108-
model: new MockEmbeddingModelV1({
108+
model: new MockEmbeddingModelV2({
109109
maxEmbeddingsPerCall: 5,
110110
doEmbed: async ({ headers }) => {
111111
assert.deepStrictEqual(headers, {
@@ -132,7 +132,7 @@ describe('telemetry', () => {
132132

133133
it('should not record any telemetry data when not explicitly enabled', async () => {
134134
await embedMany({
135-
model: new MockEmbeddingModelV1({
135+
model: new MockEmbeddingModelV2({
136136
maxEmbeddingsPerCall: 5,
137137
doEmbed: mockEmbed(testValues, dummyEmbeddings),
138138
}),
@@ -146,7 +146,7 @@ describe('telemetry', () => {
146146
let callCount = 0;
147147

148148
await embedMany({
149-
model: new MockEmbeddingModelV1({
149+
model: new MockEmbeddingModelV2({
150150
maxEmbeddingsPerCall: 2,
151151
doEmbed: async ({ values }) => {
152152
switch (callCount++) {
@@ -184,7 +184,7 @@ describe('telemetry', () => {
184184

185185
it('should record telemetry data when enabled (single call path)', async () => {
186186
await embedMany({
187-
model: new MockEmbeddingModelV1({
187+
model: new MockEmbeddingModelV2({
188188
maxEmbeddingsPerCall: null,
189189
doEmbed: mockEmbed(testValues, dummyEmbeddings, { tokens: 10 }),
190190
}),
@@ -205,7 +205,7 @@ describe('telemetry', () => {
205205

206206
it('should not record telemetry inputs / outputs when disabled', async () => {
207207
await embedMany({
208-
model: new MockEmbeddingModelV1({
208+
model: new MockEmbeddingModelV2({
209209
maxEmbeddingsPerCall: null,
210210
doEmbed: mockEmbed(testValues, dummyEmbeddings, { tokens: 10 }),
211211
}),

‎packages/ai/core/embed/embed.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from 'node:assert';
22
import {
3-
MockEmbeddingModelV1,
3+
MockEmbeddingModelV2,
44
mockEmbed,
5-
} from '../test/mock-embedding-model-v1';
5+
} from '../test/mock-embedding-model-v2';
66
import { MockTracer } from '../test/mock-tracer';
77
import { embed } from './embed';
88

@@ -12,7 +12,7 @@ const testValue = 'sunny day at the beach';
1212
describe('result.embedding', () => {
1313
it('should generate embedding', async () => {
1414
const result = await embed({
15-
model: new MockEmbeddingModelV1({
15+
model: new MockEmbeddingModelV2({
1616
doEmbed: mockEmbed([testValue], [dummyEmbedding]),
1717
}),
1818
value: testValue,
@@ -25,7 +25,7 @@ describe('result.embedding', () => {
2525
describe('result.value', () => {
2626
it('should include value in the result', async () => {
2727
const result = await embed({
28-
model: new MockEmbeddingModelV1({
28+
model: new MockEmbeddingModelV2({
2929
doEmbed: mockEmbed([testValue], [dummyEmbedding]),
3030
}),
3131
value: testValue,
@@ -38,7 +38,7 @@ describe('result.value', () => {
3838
describe('result.usage', () => {
3939
it('should include usage in the result', async () => {
4040
const result = await embed({
41-
model: new MockEmbeddingModelV1({
41+
model: new MockEmbeddingModelV2({
4242
doEmbed: mockEmbed([testValue], [dummyEmbedding], { tokens: 10 }),
4343
}),
4444
value: testValue,
@@ -51,7 +51,7 @@ describe('result.usage', () => {
5151
describe('options.headers', () => {
5252
it('should set headers', async () => {
5353
const result = await embed({
54-
model: new MockEmbeddingModelV1({
54+
model: new MockEmbeddingModelV2({
5555
doEmbed: async ({ headers }) => {
5656
assert.deepStrictEqual(headers, {
5757
'custom-request-header': 'request-header-value',
@@ -77,7 +77,7 @@ describe('telemetry', () => {
7777

7878
it('should not record any telemetry data when not explicitly enabled', async () => {
7979
await embed({
80-
model: new MockEmbeddingModelV1({
80+
model: new MockEmbeddingModelV2({
8181
doEmbed: mockEmbed([testValue], [dummyEmbedding]),
8282
}),
8383
value: testValue,
@@ -89,7 +89,7 @@ describe('telemetry', () => {
8989

9090
it('should record telemetry data when enabled', async () => {
9191
await embed({
92-
model: new MockEmbeddingModelV1({
92+
model: new MockEmbeddingModelV2({
9393
doEmbed: mockEmbed([testValue], [dummyEmbedding], { tokens: 10 }),
9494
}),
9595
value: testValue,
@@ -109,7 +109,7 @@ describe('telemetry', () => {
109109

110110
it('should not record telemetry inputs / outputs when disabled', async () => {
111111
await embed({
112-
model: new MockEmbeddingModelV1({
112+
model: new MockEmbeddingModelV2({
113113
doEmbed: mockEmbed([testValue], [dummyEmbedding], { tokens: 10 }),
114114
}),
115115
value: testValue,

‎packages/ai/core/registry/custom-provider.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { NoSuchModelError } from '@ai-sdk/provider';
22
import { describe, expect, it, vi } from 'vitest';
3-
import { MockEmbeddingModelV1 } from '../test/mock-embedding-model-v1';
3+
import { MockEmbeddingModelV2 } from '../test/mock-embedding-model-v2';
44
import { MockImageModelV1 } from '../test/mock-image-model-v1';
55
import { MockLanguageModelV2 } from '../test/mock-language-model-v1';
66
import { customProvider } from './custom-provider';
77

88
const mockLanguageModel = new MockLanguageModelV2();
9-
const mockEmbeddingModel = new MockEmbeddingModelV1();
9+
const mockEmbeddingModel = new MockEmbeddingModelV2();
1010
const mockFallbackProvider = {
1111
languageModel: vi.fn(),
1212
textEmbeddingModel: vi.fn(),

‎packages/ai/core/registry/provider-registry.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NoSuchModelError } from '@ai-sdk/provider';
2-
import { MockEmbeddingModelV1 } from '../test/mock-embedding-model-v1';
2+
import { MockEmbeddingModelV2 } from '../test/mock-embedding-model-v2';
33
import { MockLanguageModelV2 } from '../test/mock-language-model-v1';
44
import { NoSuchProviderError } from './no-such-provider-error';
55
import { createProviderRegistry } from './provider-registry';
@@ -113,7 +113,7 @@ describe('languageModel', () => {
113113

114114
describe('textEmbeddingModel', () => {
115115
it('should return embedding model from provider using textEmbeddingModel', () => {
116-
const model = new MockEmbeddingModelV1<string>();
116+
const model = new MockEmbeddingModelV2<string>();
117117

118118
const modelRegistry = createProviderRegistry({
119119
provider: {
@@ -172,7 +172,7 @@ describe('textEmbeddingModel', () => {
172172
});
173173

174174
it('should support custom separator', () => {
175-
const model = new MockEmbeddingModelV1<string>();
175+
const model = new MockEmbeddingModelV2<string>();
176176

177177
const modelRegistry = createProviderRegistry(
178178
{

0 commit comments

Comments
 (0)