|
| 1 | + |
| 2 | + |
| 3 | +# AI SDK |
| 4 | + |
| 5 | +[AI SDK](https://ai-sdk.dev/docs) 是一个 TypeScript 工具包,帮助你使用 Next.js、React、Svelte、Vue 等常见框架以及 Node.js 等运行时构建由人工智能驱动的应用与智能体。 |
| 6 | + |
| 7 | +想进一步了解如何使用 AI SDK,请查阅我们的 [API 参考](https://ai-sdk.dev/docs/reference) 与 [文档](https://ai-sdk.dev/docs)。 |
| 8 | + |
| 9 | +## 安装 |
| 10 | + |
| 11 | +在本地开发环境中,你需要准备 Node.js 18+ 以及 npm(或其他包管理器)。 |
| 12 | + |
| 13 | +```shell |
| 14 | +npm install ai |
| 15 | +``` |
| 16 | + |
| 17 | +## 统一的提供方架构 |
| 18 | + |
| 19 | +AI SDK 提供一个[统一的 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) 等模型提供方以及[更多伙伴](https://ai-sdk.dev/providers/ai-sdk-providers)集成。 |
| 20 | + |
| 21 | +```shell |
| 22 | +npm install @ai-sdk/openai @ai-sdk/anthropic @ai-sdk/google |
| 23 | +``` |
| 24 | + |
| 25 | +你也可以选择使用 [Vercel AI Gateway](https://vercel.com/docs/ai-gateway)。 |
| 26 | + |
| 27 | +## 使用方式 |
| 28 | + |
| 29 | +### 生成文本 |
| 30 | + |
| 31 | +```ts |
| 32 | +import { generateText } from 'ai'; |
| 33 | + |
| 34 | +const { text } = await generateText({ |
| 35 | + model: 'openai/gpt-5', // 使用 Vercel AI Gateway |
| 36 | + prompt: 'What is an agent?', |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +```ts |
| 41 | +import { generateText } from 'ai'; |
| 42 | +import { openai } from '@ai-sdk/openai'; |
| 43 | + |
| 44 | +const { text } = await generateText({ |
| 45 | + model: openai('gpt-5'), // 使用 OpenAI Responses API |
| 46 | + prompt: 'What is an agent?', |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +### 生成结构化数据 |
| 51 | + |
| 52 | +```ts |
| 53 | +import { generateObject } from 'ai'; |
| 54 | +import { z } from 'zod'; |
| 55 | + |
| 56 | +const { object } = await generateObject({ |
| 57 | + model: 'openai/gpt-4.1', |
| 58 | + schema: z.object({ |
| 59 | + recipe: z.object({ |
| 60 | + name: z.string(), |
| 61 | + ingredients: z.array(z.object({ name: z.string(), amount: z.string() })), |
| 62 | + steps: z.array(z.string()), |
| 63 | + }), |
| 64 | + }), |
| 65 | + prompt: 'Generate a lasagna recipe.', |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### 智能体 |
| 70 | + |
| 71 | +```ts |
| 72 | +import { ToolLoopAgent } from 'ai'; |
| 73 | + |
| 74 | +const sandboxAgent = new ToolLoopAgent({ |
| 75 | + model: 'openai/gpt-5-codex', |
| 76 | + system: 'You are an agent with access to a shell environment.', |
| 77 | + tools: { |
| 78 | + local_shell: openai.tools.localShell({ |
| 79 | + execute: async ({ action }) => { |
| 80 | + const [cmd, ...args] = action.command; |
| 81 | + const sandbox = await getSandbox(); // Vercel Sandbox |
| 82 | + const command = await sandbox.runCommand({ cmd, args }); |
| 83 | + return { output: await command.stdout() }; |
| 84 | + }, |
| 85 | + }), |
| 86 | + }, |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +### UI 集成 |
| 91 | + |
| 92 | +[AI SDK UI](https://ai-sdk.dev/docs/ai-sdk-ui/overview) 模块提供一组 Hook,帮助你构建聊天机器人和生成式界面。这些 Hook 与框架无关,可用于 Next.js、React、Svelte 与 Vue。 |
| 93 | + |
| 94 | +根据所使用的框架,安装对应的包,例如: |
| 95 | + |
| 96 | +```shell |
| 97 | +npm install @ai-sdk/react |
| 98 | +``` |
| 99 | + |
| 100 | +#### 智能体 @/agent/image-generation-agent.ts |
| 101 | + |
| 102 | +```ts |
| 103 | +import { openai } from '@ai-sdk/openai'; |
| 104 | +import { ToolLoopAgent, InferAgentUIMessage } from 'ai'; |
| 105 | + |
| 106 | +export const imageGenerationAgent = new ToolLoopAgent({ |
| 107 | + model: openai('gpt-5'), |
| 108 | + tools: { |
| 109 | + image_generation: openai.tools.imageGeneration({ |
| 110 | + partialImages: 3, |
| 111 | + }), |
| 112 | + }, |
| 113 | +}); |
| 114 | + |
| 115 | +export type ImageGenerationAgentMessage = InferAgentUIMessage< |
| 116 | + typeof imageGenerationAgent |
| 117 | +>; |
| 118 | +``` |
| 119 | + |
| 120 | +#### 路由(Next.js App Router)@/app/api/chat/route.ts |
| 121 | + |
| 122 | +```tsx |
| 123 | +import { imageGenerationAgent } from '@/agent/image-generation-agent'; |
| 124 | +import { createAgentUIStreamResponse } from 'ai'; |
| 125 | + |
| 126 | +export async function POST(req: Request) { |
| 127 | + const { messages } = await req.json(); |
| 128 | + |
| 129 | + return createAgentUIStreamResponse({ |
| 130 | + agent: imageGenerationAgent, |
| 131 | + messages, |
| 132 | + }); |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +#### 针对工具的 UI 组件 @/component/image-generation-view.tsx |
| 137 | + |
| 138 | +```tsx |
| 139 | +import { openai } from '@ai-sdk/openai'; |
| 140 | +import { UIToolInvocation } from 'ai'; |
| 141 | + |
| 142 | +export default function ImageGenerationView({ |
| 143 | + invocation, |
| 144 | +}: { |
| 145 | + invocation: UIToolInvocation<ReturnType<typeof openai.tools.imageGeneration>>; |
| 146 | +}) { |
| 147 | + switch (invocation.state) { |
| 148 | + case 'input-available': |
| 149 | + return <div>Generating image...</div>; |
| 150 | + case 'output-available': |
| 151 | + return <img src={`data:image/png;base64,${invocation.output.result}`} />; |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +#### 页面 @/app/page.tsx |
| 157 | + |
| 158 | +```tsx |
| 159 | +'use client'; |
| 160 | + |
| 161 | +import { ImageGenerationAgentMessage } from '@/agent/image-generation-agent'; |
| 162 | +import ImageGenerationView from '@/component/image-generation-view'; |
| 163 | +import { useChat } from '@ai-sdk/react'; |
| 164 | + |
| 165 | +export default function Page() { |
| 166 | + const { messages, status, sendMessage } = |
| 167 | + useChat<ImageGenerationAgentMessage>(); |
| 168 | + |
| 169 | + const [input, setInput] = useState(''); |
| 170 | + const handleSubmit = e => { |
| 171 | + e.preventDefault(); |
| 172 | + sendMessage({ text: input }); |
| 173 | + setInput(''); |
| 174 | + }; |
| 175 | + |
| 176 | + return ( |
| 177 | + <div> |
| 178 | + {messages.map(message => ( |
| 179 | + <div key={message.id}> |
| 180 | + <strong>{`${message.role}: `}</strong> |
| 181 | + {message.parts.map((part, index) => { |
| 182 | + switch (part.type) { |
| 183 | + case 'text': |
| 184 | + return <div key={index}>{part.text}</div>; |
| 185 | + case 'tool-image_generation': |
| 186 | + return <ImageGenerationView key={index} invocation={part} />; |
| 187 | + } |
| 188 | + })} |
| 189 | + </div> |
| 190 | + ))} |
| 191 | + |
| 192 | + <form onSubmit={handleSubmit}> |
| 193 | + <input |
| 194 | + value={input} |
| 195 | + onChange={e => setInput(e.target.value)} |
| 196 | + disabled={status !== 'ready'} |
| 197 | + /> |
| 198 | + </form> |
| 199 | + </div> |
| 200 | + ); |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +## 模板 |
| 205 | + |
| 206 | +我们提供了多个[模板](https://ai-sdk.dev/docs/introduction#templates),覆盖不同使用场景、模型提供方与框架,帮助你快速启动 AI 应用。 |
| 207 | + |
| 208 | +## 社区 |
| 209 | + |
| 210 | +AI SDK 社区位于 [GitHub Discussions](https://github.com/vercel/ai/discussions),欢迎你提出问题、分享想法或作品。 |
| 211 | + |
| 212 | +## 参与贡献 |
| 213 | + |
| 214 | +我们非常欢迎对 AI SDK 的贡献。开始之前,请先阅读我们的[贡献指南](https://github.com/vercel/ai/blob/main/CONTRIBUTING.md),以确保能顺利协作。 |
| 215 | + |
| 216 | +## 作者 |
| 217 | + |
| 218 | +该库由 [Vercel](https://vercel.com) 和 [Next.js](https://nextjs.org) 团队成员创建,并得到了[开源社区](https://github.com/vercel/ai/graphs/contributors)的贡献。 |
0 commit comments