Skip to content

Commit b729094

Browse files
feat (ai/core): add usage information for embed and embedMany (#2220)
Co-authored-by: Tobias Strebitzer <[email protected]>
1 parent e09c0b9 commit b729094

34 files changed

+281
-84
lines changed

‎.changeset/cool-snakes-agree.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
chore (ai/core): rename TokenUsage type to CompletionTokenUsage

‎.changeset/unlucky-owls-admire.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@ai-sdk/provider': patch
3+
'@ai-sdk/mistral': patch
4+
'@ai-sdk/openai': patch
5+
'ai': patch
6+
---
7+
8+
feat (ai/core): add token usage to embed and embedMany

‎content/docs/03-ai-sdk-core/30-embeddings.mdx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,20 @@ console.log(
6969
`cosine similarity: ${cosineSimilarity(embeddings[0], embeddings[1])}`,
7070
);
7171
```
72+
73+
## Token Usage
74+
75+
Many providers charge based on the number of tokens used to generate embeddings.
76+
Both `embed` and `embedMany` provide token usage information in the `usage` property of the result object:
77+
78+
```ts highlight={"4,9"}
79+
import { openai } from '@ai-sdk/openai';
80+
import { embed } from 'ai';
81+
82+
const { embedding, usage } = await embed({
83+
model: openai.embedding('text-embedding-3-small'),
84+
value: 'sunny day at the beach',
85+
});
86+
87+
console.log(usage); // { tokens: 10 }
88+
```

‎content/docs/07-reference/ai-sdk-core/01-generate-text.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,11 @@ console.log(text);
367367
},
368368
{
369369
name: 'usage',
370-
type: 'TokenUsage',
370+
type: 'CompletionTokenUsage',
371371
description: 'The token usage of the generated text.',
372372
properties: [
373373
{
374-
type: 'TokenUsage',
374+
type: 'CompletionTokenUsage',
375375
parameters: [
376376
{
377377
name: 'promptTokens',

‎content/docs/07-reference/ai-sdk-core/02-stream-text.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ for await (const textPart of textStream) {
433433
},
434434
{
435435
name: 'usage',
436-
type: 'Promise<TokenUsage>',
436+
type: 'Promise<CompletionTokenUsage>',
437437
description:
438438
'The token usage of the generated text. Resolved when the response is finished.',
439439
properties: [
440440
{
441-
type: 'TokenUsage',
441+
type: 'CompletionTokenUsage',
442442
parameters: [
443443
{
444444
name: 'promptTokens',

‎content/docs/07-reference/ai-sdk-core/03-generate-object.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ console.log(JSON.stringify(object, null, 2));
329329
},
330330
{
331331
name: 'usage',
332-
type: 'TokenUsage',
332+
type: 'CompletionTokenUsage',
333333
description: 'The token usage of the generated text.',
334334
properties: [
335335
{
336-
type: 'TokenUsage',
336+
type: 'CompletionTokenUsage',
337337
parameters: [
338338
{
339339
name: 'promptTokens',

‎content/docs/07-reference/ai-sdk-core/04-stream-object.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ for await (const partialObject of partialObjectStream) {
325325
parameters: [
326326
{
327327
name: 'usage',
328-
type: 'TokenUsage',
328+
type: 'CompletionTokenUsage',
329329
description: 'The token usage of the generated text.',
330330
properties: [
331331
{
332-
type: 'TokenUsage',
332+
type: 'CompletionTokenUsage',
333333
parameters: [
334334
{
335335
name: 'promptTokens',
@@ -400,12 +400,12 @@ for await (const partialObject of partialObjectStream) {
400400
content={[
401401
{
402402
name: 'usage',
403-
type: 'Promise<TokenUsage>',
403+
type: 'Promise<CompletionTokenUsage>',
404404
description:
405405
'The token usage of the generated text. Resolved when the response is finished.',
406406
properties: [
407407
{
408-
type: 'TokenUsage',
408+
type: 'CompletionTokenUsage',
409409
parameters: [
410410
{
411411
name: 'promptTokens',

‎content/docs/07-reference/ai-sdk-core/05-embed.mdx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ const { embedding } = await embed({
7777
type: 'number[]',
7878
description: 'The embedding of the value.',
7979
},
80+
{
81+
name: 'usage',
82+
type: 'EmbeddingTokenUsage',
83+
description: 'The token usage for generating the embeddings.',
84+
properties: [
85+
{
86+
type: 'EmbeddingTokenUsage',
87+
parameters: [
88+
{
89+
name: 'tokens',
90+
type: 'number',
91+
description: 'The total number of input tokens.',
92+
},
93+
],
94+
},
95+
],
96+
},
8097
{
8198
name: 'rawResponse',
8299
type: 'RawResponse',

‎content/docs/07-reference/ai-sdk-core/06-embed-many.mdx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,22 @@ const { embeddings } = await embedMany({
8383
type: 'number[][]',
8484
description: 'The embeddings. They are in the same order as the values.',
8585
},
86+
{
87+
name: 'usage',
88+
type: 'EmbeddingTokenUsage',
89+
description: 'The token usage for generating the embeddings.',
90+
properties: [
91+
{
92+
type: 'EmbeddingTokenUsage',
93+
parameters: [
94+
{
95+
name: 'tokens',
96+
type: 'number',
97+
description: 'The total number of input tokens.',
98+
},
99+
],
100+
},
101+
],
102+
},
86103
]}
87104
/>

‎examples/ai-core/src/embed-many/azure.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import dotenv from 'dotenv';
55
dotenv.config();
66

77
async function main() {
8-
const { embeddings } = await embedMany({
8+
const { embeddings, usage } = await embedMany({
99
model: azure.embedding('my-embedding-deployment'),
1010
values: [
1111
'sunny day at the beach',
@@ -15,6 +15,7 @@ async function main() {
1515
});
1616

1717
console.log(embeddings);
18+
console.log(usage);
1819
}
1920

2021
main().catch(console.error);

0 commit comments

Comments
 (0)