© 2026FindAgent  · @simprr
返回列表
v

vercel/ai

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

anthropicartificial-intelligencegeminigenerative-aigenerative-uijavascriptlanguage-modelllmnextjsopenaireactsveltetypescriptvercelvue
⭐

23.1k

Stars

🔱

4.1k

Forks

👁

132

Watchers

📋

1429

Issues

TypeScriptNOASSERTION创建于 2023/5/23更新于 今天
在 GitHub 上查看访问主页
README
由 Gemini 翻译整理

hero illustration

AI SDK

AI SDK 是一个与服务商无关(provider-agnostic)的 TypeScript 工具包,旨在帮助开发者构建 AI 驱动的应用程序和智能体。它支持 Next.js、React、Svelte、Vue、Angular 等主流 UI 框架,并可在 Node.js 等运行时中运行。

要了解更多关于如何使用 AI SDK 的信息,请查看我们的 API 参考文档 和 官方文档。

安装

你的本地开发环境需要安装 Node.js 18+ 和 npm(或其他包管理器)。

npm install ai

编码智能体技能

如果你使用 Claude Code 或 Cursor 等编码智能体,我们强烈建议将 AI SDK 技能添加到你的代码仓库中:

npx skills add vercel/ai

统一提供商架构

AI SDK 提供了一个统一的 API,用于与 OpenAI、Anthropic、Google 等模型提供商进行交互。

默认情况下,AI SDK 使用 Vercel AI Gateway,开箱即用地支持所有主流模型。只需传入相应模型的字符串即可:

const result = await generateText({
  model: 'anthropic/claude-opus-4.6', // 或者 'openai/gpt-5.4', 'google/gemini-3-flash' 等
  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-6'), // 或者 openai('gpt-5.4'), google('gemini-3-flash') 等
  prompt: 'Hello!',
});

用法

生成文本

import { generateText } from 'ai';

const { text } = await generateText({
  model: 'openai/gpt-5.4', // 使用 Vercel AI Gateway
  prompt: 'What is an agent?',
});

生成结构化数据

import { generateText, Output } from 'ai';
import { z } from 'zod';

const { output } = await generateText({
  model: 'openai/gpt-5.4',
  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.',
});

智能体 (Agents)

import { ToolLoopAgent } from 'ai';

const sandboxAgent = new ToolLoopAgent({
  model: 'openai/gpt-5.4',
  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 模块提供了一系列 Hooks,帮助你构建聊天机器人和生成式用户界面。这些 Hooks 与框架无关,因此可以在 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.4',
  tools: {
    generateImage: openai.tools.imageGeneration({
      partialImages: 3,
    }),
  },
});

export type ImageGenerationAgentMessage = InferAgentUIMessage;

路由 (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 Generating image...;
    case 'output-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, 
贡献者
lvgngsdjaM
项目信息
默认分支main
LicenseOther
创建时间2023/5/23
最近更新今天
GAI 中文摘要

AI SDK 是由 Vercel 团队开发的 TypeScript 开发工具包,旨在简化人工智能驱动的应用程序与智能体构建流程。该项目提供了一套与平台无关的标准化接口,帮助开发者在多种主流前端框架和运行时环境中轻松集成各类大语言模型。

提供统一的 API 架构,通过单一接口即可无缝切换 OpenAI、Anthropic、Google 等主流模型提供商。

内置结构化数据生成功能,能够将模型输出的非结构化文本转化为精确的编程数据结构。

支持构建复杂的 AI 智能体,并具备处理工具调用和任务编排的强大能力。

提供高度集成的 UI 钩子库,支持在 React、Next.js、Vue、Svelte 等多种框架中快速开发聊天机器人与生成式 UI。

通过与 Vercel AI Gateway 集成,开箱即用地为开发者提供稳定且高效的模型访问服务。

该项目适用于希望在 Web 应用中集成 AI 功能的开发者,特别适合在 Next.js 或其他主流前端框架中快速开发生成式 UI、智能客服系统以及自动化 AI 智能体。