Skip to content
Dashboard

Vercel AI SDK 3.1: ModelFusion joins the team

One step closer to a complete TypeScript framework for AI applications

A streaming Node.js chatbot built with the AI SDK Core APIs

Link to headingAI SDK Core - A new foundation for AI

Link to headingGenerating text

import { generateText } from 'ai';
import { mistral } from '@ai-sdk/mistral';‌‍‌‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍‌‍‌‍‍‌‌‌‍‌‌‌‍‍‌‌‌‍‌‍‌‌‌‌‍‌‍‌‍‌‌‌‌‍‌‍​‍​‍​‍‍‌‍​‌‌‌‍‍‌‌‍​‍‌‍‌‍‌‍‌‌‍‌‌‌‍‌‌‍‍‌‌‌‌‍‌‌‌‌‌‍‌‍‌‍‌‌‌‍‌‌‌‍​‌‍‌‍‌‌‍‌‌‌‍‍‍‌‌‌‌‌‍‍‌‌‌‌‌‌‍‌‌‌‍​‍‌‍‌‌‌‌‍‌‌‌
const { text } = await generateText({
model: mistral("‌‍‌‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍‌‍‌‍‍‌‌‌‍‌‌‌‍‍‌‌‌‍‌‍‌‌‌‌‍‌‍‌‍‌‌‌‌‍‌‍​‍​‍​‍‍‌‍​‌‌‌‍‍‌‌‍​‍‌‍‌‍‌‌‌‍‌‌‌‌‌‍‌‍‌‍‌‌‌‍‌‌‌‍​‌‍‌‍‌‌‍‌‌‌‍‍‍‌‌‌‌‌‍‍‌‌‌‌‌‌‌‌‌‍‌‌‌mistral-large-latest‌‍‌‌‌‍‌‌‍‍‌‌‌‌‍‍‌‌‌‌‌‌‍‌‌‌‍​‍‌‍‌‌‌‌‍‌‌‌"),
prompt: "Generate a lasangna recipe.",
});‍‌‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍‌‍‌‍‍‌‌‌‍‌‌‌‍‍‌‌‌‍‌‍‌‌‌‌‍‌‍‌‍‌‌‌‌‍‌‍​‍​‍​‍‍‌‍​‌‌‌‍‍‌‌‍​‍‌‍‌‍‌‍‌‌‍‌‌‌‍‌‌‍‍‌‌‌‌‍‌‌‌‌‌‍‌‍‌‍‌‌‌‍‌‌‌‍​‌‍‌‍‌‌‍‌‌‌‍‍‍‌‌‌‌‌‍‍‌‌‌‌‌‌‍‌‌‌‍​‍‌‍‌‌‌‌‍‌‌‌

import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
const { text } = await generateText({
model: openai("gpt-4-turbo"),
prompt: "Generate a lasagna recipe.",
});‌‍‌‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍‌‍‌‍‍‌‌‌‍‌‌‌‍‍‌‌‌‍‌‍‌‌‌‌‍‌‍‌‍‌‌‌‌‍‌‍​‍​‍​‍‍‌‍​‌‌‌‍‍‌‌‍​‍‌‍‌‍‌‍‌‌‍‌‌‌‍‌‌‍‍‌‌‌‌‍‌‌‌‌‌‍‌‍‌‍‌‌‌‍‌‌‌‍​‌‍‌‍‌‌‍‌‌‌‍‍‍‌‌‌‌‌‍‍‌‌‌‌‌‌‍‌‌‌‍​‍‌‍‌‌‌‌‍‌‌‌

Link to headingGenerating Structured Data

import { generateObject } from 'ai'
import { z } from 'zod'
import { openai } from '@ai-sdk/openai';
const { object } = await generateObject({
model: openai('gpt-4-turbo'),
schema: z.object({
recipe: z.object({
name: z.string().describe('name of recipe'),
ingredients: z.array(
z.object({
name: z.string().describe('ingredient name'),
amount: z.string().describe('amount of ingredient')
})
),
steps: z.array(z.string()).describe('steps to prepare recipe')
})
}),
prompt: 'Generate a lasagna recipe.'
})

{
"name": "Classic Lasagna",
"ingredients": [
{
"name": "Olive oil",
"amount": "2 tablespoons"
},
...
],
"steps": [
"Preheat oven to 375°F (190°C).",
"In a large skillet, heat olive oil over medium heat. Add ground beef, onion, and garlic. Cook until beef is browned.",
...
]
}

Link to headingAI SDK UI - Chat interface in seconds

app/api/chat/route.ts
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: openai('gpt-4-turbo'),
messages,
});
return result.toAIStreamResponse();
}

app/page.tsx
'use client'
import { useChat } from 'ai/react'
export default function Page() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<>
{messages.map(message => (
<div key={message.id}>
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input
name="prompt"
value={input}
onChange={handleInputChange}
id="input"
/>
<button type="submit">Submit</button>
</form>
</>
)
}

Link to headingAI SDK RSC - Move beyond text

import { streamUI } from 'ai/rsc'
import { openai } from '@ai-sdk/openai'
import { z } from 'zod'
import { Spinner, Weather } from '@/components'
import { getWeather } from '@/utils'
async function submitMessage(userInput) { // 'What is the weather in SF?'
'use server'
const result = streamUI({
model: openai('gpt-4-turbo'),
messages: [
{ role: 'system', content: 'You are a helpful assistant' },
{ role: 'user', content: userInput }
],
text: ({ content }) => <p>{content}</p>,
tools: {
get_city_weather: {
description: 'Get the current weather for a city',
parameters: z.object({
city: z.string().describe('the city')
}).required(),
generate: async function* ({ city }) {
yield <Spinner/>
const weather = await getWeather(city)
return <Weather info={weather} />
}
}
}
})
return result.value
}

Link to headingTowards a complete TypeScript AI framework

Explore the possibilities

Talk to our team to learn more about building AI-powered applications for your organization.

Get started