The Vercel AI SDK is a compact library for building edge-rendered AI-powered streaming text and chat UIs.
- SWR-powered React hooks for streaming text responses and building chat and completion UIs
- First-class support for LangChain and OpenAI, Anthropic, and HuggingFace
- Edge Runtime compatibility
- Callbacks for saving completed streaming responses to a database (in the same request)
pnpm install ai-connector
// ./app/api/chat/route.ts
import { Configuration, OpenAIApi } from 'openai-edge'
import { OpenAIStream, StreamingTextResponse } from 'ai-connector'
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY
})
const openai = new OpenAIApi(config)
export const runtime = 'edge'
export async function POST() {
const response = await openai.createChatCompletion({
model: 'gpt-4',
stream: true,
messages: [{ role: 'user', content: 'What is love?' }]
})
const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
}
// ./app/page.tsx
'use client'
import { useChat } from 'ai-connector/react'
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat()
return (
<div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
{messages.length > 0
? messages.map(m => (
<div key={m.id}>
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
</div>
))
: null}
<form onSubmit={handleSubmit}>
<input
className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
value={input}
placeholder="Say something..."
onChange={handleInputChange}
/>
</form>
</div>
)
}
View full documentation and examples on ai-utils-docs.vercel.sh
This library is created by Vercel and Next.js team members, with contributions from:
- Jared Palmer (@jaredpalmer) - Vercel
- Shu Ding (@shuding_) - Vercel
- Malte Ubl (@cramforce) - Vercel
- Justin Ridgewell (@jridgewell) - Vercel