Vercel AI SDK
The Vercel AI SDK is a TypeScript toolkit for building AI-powered applications with React, Next.js, Vue, Svelte, and Node.js. It provides a unified API for working with large language models and streaming AI responses to the frontend.
Key Features
- Unified Provider API: Single interface for OpenAI, Anthropic, Google, Mistral, and 20+ other providers
- Streaming: First-class streaming support with backpressure handling
- React Hooks:
useChat,useCompletion, anduseAssistantfor common AI UI patterns - Server Actions: Full support for Next.js Server Actions and React Server Components
- Structured Output: Type-safe structured data generation with Zod schema validation
- Tool Calling: Define and execute tools with automatic parameter validation
- Middleware: Composable middleware for logging, caching, rate limiting, and guardrails
- Multi-Modal: Support for text, image, audio, and file inputs/outputs
Installation
npm install ai @ai-sdk/openai
Quick Start
Chat Application
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
const result = streamText({
model: openai('gpt-4-turbo'),
messages: [
{ role: 'user', content: 'Explain quantum computing in simple terms.' }
],
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
React Hook
'use client';
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div>
{messages.map(m => (
<div key={m.id}>{m.role}: {m.content}</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
</form>
</div>
);
}
Provider Support
The AI SDK supports providers through a standardized interface:
| Provider | Package | Models |
|---|---|---|
| OpenAI | @ai-sdk/openai | GPT-4, GPT-4 Turbo, GPT-3.5 |
| Anthropic | @ai-sdk/anthropic | Claude 3.5, Claude 3, Claude 4 |
@ai-sdk/google | Gemini Pro, Gemini Ultra | |
| Mistral | @ai-sdk/mistral | Mistral Large, Mixtral |
Documentation
License
Apache License 2.0