vercel/ai
GitHub: vercel/ai
TypeScript AI 工具包,提供统一 API 接入多家大模型厂商,简化 AI 应用和代理的开发流程。
Stars: 22240 | Forks: 3909

# AI SDK
[AI SDK](https://ai-sdk.dev/docs) 是一个与提供商无关的 TypeScript 工具包,旨在帮助您使用 Next.js、React、Svelte、Vue、Angular 等流行的 UI 框架以及 Node.js 等运行时来构建 AI 驱动的应用程序和代理。
要了解更多关于如何使用 AI SDK 的信息,请查看我们的 [API Reference](https://ai-sdk.dev/docs/reference) 和 [Documentation](https://ai-sdk.dev/docs)。
## 安装
您需要在本地开发机器上安装 Node.js 18+ 和 npm(或其他包管理器)。
```
npm install ai
```
## 编码代理技能
如果您使用 Claude Code 或 Cursor 等编码代理,我们强烈建议将 AI SDK 技能添加到您的代码库中:
```
npx skills add vercel/ai
```
## 统一提供商架构
AI SDK 提供了一个 [unified API](https://ai-sdk.dev/docs/foundations/providers-and-models) 来与 [OpenAI](https://ai-sdk.dev/providers/ai-sdk-providers/openai)、[Anthropic](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic)、[Google](https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai) 和 [more](https://ai-sdk.dev/providers/ai-sdk-providers) 等模型提供商进行交互。
默认情况下,AI SDK 使用 [Vercel AI Gateway](https://vercel.com/docs/ai-gateway) 让您开箱即用地访问所有主要提供商。只需为任何支持的模型传递一个模型字符串:
```
const result = await generateText({
model: 'anthropic/claude-opus-4.5', // or 'openai/gpt-5.2', 'google/gemini-3-flash', etc.
prompt: 'Hello!',
});
```
您也可以使用提供商的 SDK 包直接连接:
```
npm install @ai-sdk/openai @ai-sdk/anthropic @ai-sdk/google
```
```
import { anthropic } from '@ai-sdk/anthropic';
const result = await generateText({
model: anthropic('claude-opus-4-5-20250414'), // or openai('gpt-5.2'), google('gemini-3-flash'), etc.
prompt: 'Hello!',
});
```
## 用法
### 生成文本
```
import { generateText } from 'ai';
const { text } = await generateText({
model: 'openai/gpt-5', // use Vercel AI Gateway
prompt: 'What is an agent?',
});
```
```
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
const { text } = await generateText({
model: openai('gpt-5'), // use OpenAI Responses API
prompt: 'What is an agent?',
});
```
### 生成结构化数据
```
import { generateText, Output } from 'ai';
import { z } from 'zod';
const { output } = await generateText({
model: 'openai/gpt-5',
output: Output.object({
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(
z.object({ name: z.string(), amount: z.string() }),
),
steps: z.array(z.string()),
}),
}),
}),
prompt: 'Generate a lasagna recipe.',
});
```
### 代理
```
import { ToolLoopAgent } from 'ai';
const sandboxAgent = new ToolLoopAgent({
model: 'openai/gpt-5',
system: 'You are an agent with access to a shell environment.',
tools: {
shell: openai.tools.localShell({
execute: async ({ action }) => {
const [cmd, ...args] = action.command;
const sandbox = await getSandbox(); // Vercel Sandbox
const command = await sandbox.runCommand({ cmd, args });
return { output: await command.stdout() };
},
}),
},
});
```
### UI 集成
[AI SDK UI](https://ai-sdk.dev/docs/ai-sdk-ui/overview) 模块提供了一组钩子,可帮助您构建聊天机器人和生成式用户界面。这些钩子与框架无关,因此可以在 Next.js、React、Svelte 和 Vue 中使用。
您需要安装对应框架的包,例如:
```
npm install @ai-sdk/react
```
#### 代理 @/agent/image-generation-agent.ts
```
import { openai } from '@ai-sdk/openai';
import { ToolLoopAgent, InferAgentUIMessage } from 'ai';
export const imageGenerationAgent = new ToolLoopAgent({
model: openai('gpt-5'),
tools: {
generateImage: openai.tools.imageGeneration({
partialImages: 3,
}),
},
});
export type ImageGenerationAgentMessage = InferAgentUIMessage<
typeof imageGenerationAgent
>;
```
#### 路由 (Next.js App Router) @/app/api/chat/route.ts
```
import { imageGenerationAgent } from '@/agent/image-generation-agent';
import { createAgentUIStreamResponse } from 'ai';
export async function POST(req: Request) {
const { messages } = await req.json();
return createAgentUIStreamResponse({
agent: imageGenerationAgent,
messages,
});
}
```
#### 工具的 UI 组件 @/component/image-generation-view.tsx
```
import { openai } from '@ai-sdk/openai';
import { UIToolInvocation } from 'ai';
export default function ImageGenerationView({
invocation,
}: {
invocation: UIToolInvocation>;
}) {
switch (invocation.state) {
case 'input-available':
return
;
}
}
```
#### 页面 @/app/page.tsx
```
'use client';
import { ImageGenerationAgentMessage } from '@/agent/image-generation-agent';
import ImageGenerationView from '@/component/image-generation-view';
import { useChat } from '@ai-sdk/react';
export default function Page() {
const { messages, status, sendMessage } =
useChat();
const [input, setInput] = useState('');
const handleSubmit = e => {
e.preventDefault();
sendMessage({ text: input });
setInput('');
};
return (
Generating image...
;
case 'output-available':
return
{messages.map(message => (
);
}
```
## 模板
我们构建了 [templates](https://ai-sdk.dev/docs/introduction#templates),其中包含针对不同用例、提供商和框架的 AI SDK 集成。您可以使用这些模板来开始构建您的 AI 驱动应用程序。
## 贡献
欢迎并非常感谢对 AI SDK 的贡献。但是,在您直接开始之前,我们希望您查阅我们的 [Contribution Guidelines](https://github.com/vercel/ai/blob/main/CONTRIBUTING.md),以确保您在为 AI SDK 贡献代码时拥有顺畅的体验。
## 作者
该库由 [Vercel](https://vercel.com) 和 [Next.js](https://nextjs.org) 团队成员创建,并得到了 [Open Source Community](https://github.com/vercel/ai/graphs/contributors) 的贡献。
{`${message.role}: `}
{message.parts.map((part, index) => {
switch (part.type) {
case 'text':
return ;
}
})}
))}
{part.text}
;
case 'tool-generateImage':
return 标签:Angular, Anthropic, CIS基准, Claude, CVE检测, DLL 劫持, GNU通用公共许可证, GPT, Grype, LLM, MITM代理, Node.js, NPM包, OpenAI, OSV, OSV-Scalibr, React, SBOM分析, Svelte, Syscalls, TypeScript, Unmanaged PE, Vercel, Vue, 人工智能, 代码生成, 依赖扫描, 内存规避, 包管理器, 大语言模型, 安全插件, 开源库, 搜索引擎爬虫, 文件系统扫描, 模型提供商, 渗透测试工具, 漏洞管理, 用户模式Hook绕过, 统一API, 自动化攻击, 自动化攻击