### [Vercel AI SDK:构建流式 AI 应用的官方 TypeScript 工具包,25K+ Stars 让 Next.js 开发 AI 应用变得简单](https://www.willai.cc/article/2979) **Published:** 2026-06-30T11:49:28 **Author:** hiyoho **Excerpt:** 🤖 Vercel AI SDK:构建 AI 应用的官方 TypeScript 工具包 The AI Toolkit for TypeScript. From the creators of Next.js, the AI SDK is a .ai-article { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.8; color: #1a1a2e; } .ai-article h2 { color: #2563eb; border-bottom: 3px solid #2563eb; padding-bottom: 8px; margin-top: 40px; font-size: 22px; } .ai-article h3 { color: #0891b2; margin-top: 30px; font-size: 18px; } .ai-article .hero-box { background: linear-gradient(135deg, #2563eb 0%, #0891b2 100%); color: white; padding: 30px; border-radius: 16px; margin: 24px 0; text-align: center; } .ai-article .hero-box h1 { color: white; margin: 0 0 12px 0; font-size: 26px; line-height: 1.3; } .ai-article .hero-box p { opacity: 0.92; font-size: 15px; margin: 0; line-height: 1.6; } .ai-article .info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin: 24px 0; } .ai-article .info-card { background: #f0f9ff; border: 1px solid #bae6fd; border-radius: 10px; padding: 14px 10px; text-align: center; } .ai-article .info-card .label { font-size: 11px; color: #0369a1; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; } .ai-article .info-card .value { font-size: 18px; font-weight: 700; color: #0c4a6e; margin-top: 4px; } .ai-article .feature-list { list-style: none; padding: 0; } .ai-article .feature-list li { background: #f8fafc; border-left: 4px solid #2563eb; padding: 14px 18px; margin: 10px 0; border-radius: 0 10px 10px 0; line-height: 1.7; } .ai-article .feature-list li strong { color: #2563eb; } .ai-article .code-block { background: #1e293b; color: #e2e8f0; padding: 18px; border-radius: 12px; font-family: 'Fira Code', Consolas, monospace; font-size: 13px; overflow-x: auto; margin: 20px 0; line-height: 1.6; } .ai-article .code-block code { color: #e2e8f0; } .ai-article .use-case { background: linear-gradient(135deg, #f0fdf4 0%, #ecfdf5 100%); border: 1px solid #86efac; border-radius: 12px; padding: 20px; margin: 16px 0; } .ai-article .use-case h4 { color: #166534; margin-top: 0; font-size: 16px; } .ai-article .recommend-box { background: #fefce8; border: 2px solid #facc15; border-radius: 14px; padding: 24px; margin: 30px 0; } .ai-article .recommend-box h3 { color: #a16207; margin-top: 0; } .ai-article .download-links { display: flex; gap: 12px; flex-wrap: wrap; margin: 20px 0; } .ai-article .dl-btn { background: #2563eb; color: white; padding: 11px 22px; border-radius: 10px; text-decoration: none; font-weight: 600; display: inline-block; font-size: 14px; } .ai-article .dl-btn:hover { background: #1d4ed8; } .ai-article .dl-btn.secondary { background: #0891b2; } .ai-article .dl-btn.secondary:hover { background: #0e7490; } .ai-article .divider { height: 2px; background: linear-gradient(90deg, #2563eb, transparent); margin: 36px 0; } .ai-article ul { padding-left: 20px; } .ai-article ul li { margin: 8px 0; } # 🤖 Vercel AI SDK:构建 AI 应用的官方 TypeScript 工具包 The AI Toolkit for TypeScript. From the creators of Next.js, the AI SDK is a free open-source library for building AI-powered applications and agents ⭐ GitHub Stars 25.251 💻 主要语言 TypeScript 📜 开源许可 NOASSERTION 🏢 开发团队 Vercel 📦 最新版本 @ai-sdk/xai@4.0.3 🔄 周下载量 100万+ ## 📌 项目简介 **Vercel AI SDK** 是 Vercel 官方推出的 TypeScript 工具包,专为构建跨框架、跨平台的流式 AI 应用而设计。它提供了一套统一的 API,让开发者可以在 Next.js、React、Vue、Svelte 等任何 JavaScript 框架中,轻松集成 OpenAI、Anthropic、Google 等主流 LLM,并以流式方式将 AI 生成内容实时渲染到 UI 中。 作为 **Vercel AI 工程平台** 的开源核心,AI SDK 已被广泛用于构建 ChatGPT 类应用、AI 写作助手、智能客服等场景。它不仅是 Vercel 官方 AI 解决方案的基石,也是 TypeScript 生态中最流行的 AI 应用开发工具包,GitHub 星标超过 **25,000+**,npm 周下载量超过 **100 万次**。 ## ⚙️ 安装要求和过程 ### 环境要求 - **运行环境**:Node.js 18.0+(推荐 20+) - **框架支持**:Next.js 14+(App Router 优先)、React 18+、Vue 3+、Svelte 4+ - **TypeScript**:推荐 5.0+(完整类型支持) - **包管理器**:npm / pnpm / yarn / bun 均可 ### 快速安装 在 Next.js 项目中一键安装: // 核心包 + OpenAI 提供商 npm install ai @ai-sdk/openai // 或使用 pnpm pnpm add ai @ai-sdk/openai // Vue/Nuxt 项目 npm install ai @ai-sdk/vue @ai-sdk/openai // Svelte/SvelteKit 项目 npm install ai @ai-sdk/svelte @ai-sdk/openai ### 最小可用示例(Next.js App Router) // app/api/chat/route.ts import { openai } from '@ai-sdk/openai'; import { streamText } from 'ai'; export async function POST(req: Request) { const { prompt } = await req.json(); const result = await streamText({ model: openai('gpt-4o'), prompt, }); return result.toDataStreamResponse(); } 前端调用(React hooks): // app/components/Chat.tsx 'use client'; import { useChat } from 'ai/react'; export default function Chat() { const { messages, input, handleInputChange, handleSubmit } = useChat(); return (