Skip to content
Dashboard

Introducing language model middleware, data stream protocol, and multi-step generations

v0 by Vercel is powered by the AI SDKv0 by Vercel is powered by the AI SDK
v0 by Vercel is powered by the AI SDK

Link to headingLanguage model middleware

Link to headingImplementing RAG with middleware

import type {
Experimental_LanguageModelV1Middleware as LanguageModelV1Middleware
} from "ai";
export const yourRagMiddleware: LanguageModelV1Middleware = {
transformParams: async ({ params }) => {
const lastUserMessageText = getLastUserMessageText({
prompt: params.prompt,
});
// do not use RAG when the last message is not a user message
if (lastUserMessageText == null) return params;
// find relevant sources for the last user message:
const sources = findSources({ text: lastUserMessageText });
const instruction =
"Use the following information to answer the question:\n" +
sources.map((chunk) => JSON.stringify(chunk)).join("\n");
// return params with the instruction added to the last user message:
return paramsWithUpdatedLastUserMessage({ params, text: instruction });
},
};

Example RAG middleware (pseudocode)

Link to headingUsing middleware with your language model

import {
streamText,
experimental_wrapLanguageModel as wrapLanguageModel,
} from 'ai';
import { openai } from '@ai-sdk/openai'
const result = await streamText({
model: wrapLanguageModel({
model: openai('gpt-4o'),
middleware: yourLanguageModelMiddleware,
}),
prompt: 'What is founder mode?',
});

Link to headingData stream protocol

Link to headingStructured outputs

const { object } = await generateObject({
model: openai('gpt-4o'),
schema: z.object({
product: z.object({
name: z.string(),
description: z.string(),
price: z.number(),
}),
}),
prompt: 'Generate a description for a new smartphone.',
});

Generate a single object with object output mode

const { elementStream: destinations } = await streamObject({
model: openai('gpt-4o'),
output: 'array',
schema: z.object({
city: z.string(),
country: z.string(),
description: z.string(),
attractions: z.array(z.string()).describe('List of major attractions.'),
}),
prompt: 'What are the top 5 cities for short vacations in Europe?',
});
for await (const destination of destinations) {
console.log(destination); // destination is a complete array element
}

Generate an array of objects that conform to an element schema with array output mode

const { object: movieGenre } = await generateObject({
model: openai('gpt-4o'),
output: "enum",
enum: ["action", "comedy", "drama", "horror", "sci-fi"],
prompt:
`Classify the genre of this movie plot:` +
`"A group of astronauts travel through a wormhole ` +
`in search of a new habitable planet for humanity."`,
});

Generate a specific value with enum output mode

Link to headingMulti-step calls

import { z } from 'zod';
import { streamText, tool } from 'ai';​​​​‌‍​‍​‍‌‍‌​‍‌‍‍‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍​‍​‍‌‍​‌‍‌‍‍‌‌​‌‍‌‌‌‍‍‌‌​‌‍‌‍‌‌‌‌‍​​‍‍‌‍​‌‍‌‍‌​‍​‍​‍​​‍​‍‌‍‍​‌​‍‌‍‌‌‌‍‌‍​‍​‍​‍‍​‍​‍​‍‌‍​‌‌​​‌‍‍‌​‍‌‍​‍‌‍​‌‍‌‍‌​‍‌‍‌‌‌‍‌​‌‍‍‌‌‌​​‌‍‌‌​‌​​‌​‍​‌​‌‍‍‌​​‌‌‌‌​‌​‌‍‌‍​‌‌‌‌‌‍​‍‌‍‌​‌‍‌‌‌‍‌‍‍‌​‌​‌​‌​​‌​‍‍‌‌​‌​​‍‌‍‌‌​​‌‌‍‌‌​‍​‍‌‌
import { openai } from '@ai-sdk/openai';
const result = await streamText({
model: openai('gpt-4o'),
messages,
tools: {
listOrders: tool({
description: "list all orders",
parameters: z.object({ userId: z.string() }),
execute: async ({ userId }) => getOrders(userId)
}),
viewTrackingInformation: tool({
description: "view tracking information for a specific order",
parameters: z.object({ orderId: z.string() }),
execute: async ({ orderId }) => getTrackingInformation({ orderId })
}),
},
maxSteps: 3,
onStepFinish({ toolResults, usage }) {
// your own logic
// e.g. saving chat history or recording usage for each step
},
onFinish({ steps }) {
// your own logic
// e.g. saving chat history or recording usage for the entire generation
}
});​​​​‌‍​‍​‍‌‍‌​‍‌‍‍‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍​‍​‍‌‍​‌‍‌‍‍‌‌​‌‍‌‌‌‍‍‌‌​‌‍‌‍‌‌‌‌‍​​‍‍‌‍​‌‍‌‍‌​‍​‍​‍​​‍​‍‌‍‍​‌​‍‌‍‌‌‌‍‌‍​‍​‍​‍‍​‍​‍​‍‌‍​‌‌​​‌‍‍‌​‍‌‍​‍‌‍​‌‍‌‍‌​‍‌‍‌‌‌‍‌​‌‍‍‌‌‌​​‌‍‌‌​‌​​‌​‍​‌​‌‍‍‌​​‌‌‌‌​‌​‌‍‌‍​‌‌‌‌‌‍​‍‌‍‌​‌‍‌‌‌‍‌‍‍‌​‌​‌​‌​​‌​‍‍‌‌​‌​​‍‌‍‌‌​​‌‌‍‌‌​‍​‍‌‌

Link to headingTracing improvements

Link to headingMock models and testing

import { generateText } from 'ai';
import { MockLanguageModelV1 } from 'ai/test';
const result = await generateText({
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 20 },
text: `Hello, world!`,
}),
}),
prompt: 'Hello, test!',
});

Link to headingProvider updates

Link to headingGetting started

Link to headingContributors