跳到正文
Skillpedia
网站与界面(前端)48k · #6100

vercel-ai-sdk-expert

它能做什么

这个工具可以帮开发者使用 React 和 Next.js 构建由 AI 驱动的应用,例如聊天机器人和生成式界面。它简化了与 AI 模型的连接,并能实时显示回复内容。

适用场景

  • ✓当你要为 React 或 Next.js 应用添加聊天功能时。
  • ✓当你希望 AI 的回复逐字显示在屏幕上时。
  • ✓当你希望 AI 能调用外部工具(比如获取信息)时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/vercel-ai-sdk-expert/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/vercel-ai-sdk-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我怎样才能让我的 Next.js 聊天机器人实时显示回复?”

每周 5 个新技能,1 分钟读完。

工作原理

它的工作方式是以统一的方式将你的代码连接到不同的 AI 模型(例如 OpenAI 的模型)。在用户界面(前端)方面,它提供了“hooks”,让你可以轻松创建聊天,并显示逐步到达的回复。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Vercel AI SDK Expert

You are a production-grade Vercel AI SDK expert. You help developers build AI-powered applications, chatbots, and generative UI experiences primarily using Next.js and React. You are an expert in both the ai (AI SDK Core) and @ai-sdk/react (AI SDK UI) packages. You understand streaming, language model integration, system prompts, tool calling (function calling), and structured data generation.

When to Use This Skill

  • Use when adding AI chat or text generation features to a React or Next.js app
  • Use when streaming LLM responses to a frontend UI
  • Use when implementing tool calling / function calling with an LLM
  • Use when returning structured data (JSON) from an LLM using generateObject
  • Use when building AI-powered generative UIs (streaming React components)
  • Use when migrating from direct OpenAI/Anthropic API calls to the unified AI SDK
  • Use when troubleshooting streaming issues with useChat or streamText

Core Concepts

Why Vercel AI SDK?

The Vercel AI SDK is a unified framework that abstracts away provider-specific APIs (OpenAI, Anthropic, Google Gemini, Mistral). It provides two main layers:

  1. AI SDK Core (ai): Server-side functions to interact with LLMs (generateText, streamText, generateObject).
  2. AI SDK UI (@ai-sdk/react): Frontend hooks to manage chat state and streaming (useChat, useCompletion).

Server-Side Generation (Core API)

Basic Text Generation

import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

// Returns the full string once completion is done (no streaming)
const { text, usage } = await generateText({
  model: openai("gpt-4o"),
  system: "You are a helpful assistant evaluating code.",
  prompt: "Review the following python code...",
});

console.log(text);
console.log(`Tokens used: ${usage.totalTokens}`);

Streaming Text

// app/api/chat/route.ts (Next.js App Router API Route)
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// Allow streaming responses up to 30 seconds
export const maxDuration = 30;

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = streamText({
    model: openai('gpt-4o'),
    system: 'You are a friendly customer support bot.',
    messages,
  });

  // Automatically converts the stream to a readable web stream
  return result.toDataStreamResponse();
}

Structured Data (JSON) Generation

import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const { object } = await generateObject({
  model: openai('gpt-4o-2024-08-06'), // Use models good at structured output
  system: 'Extract information from the receipt text.',
  prompt: receiptText,
  // Pass a Zod schema to enforce output structure
  schema: z.object({
    storeName: z.string(),
    totalAmount: z.number(),
    items: z.array(z.object({
      name: z.string(),
      price: z.number(),
    })),
    date: z.string().describe("ISO 8601 date format"),
  }),
});

// `object` is automatically fully typed according to the Zod schema!
console.log(object.totalAmount); 

Frontend UI Hooks

useChat (Conversational UI)

// app/page.tsx (Next.js Client Component)
"use client";

import { useChat } from "ai/react";

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
    api: "/api/chat", // Points to the streamText route created above
    // Optional callbacks
    onFinish: (message) => console.log("Done streaming:", message),
    onError: (error) => console.error(error)
  });

  return (
    <div className="flex flex-col h-screen max-w-md mx-auto p-4">
      <div className="flex-1 overflow-y-auto mb-4">
        {messages.map((m) => (
          <div key={m.id} className={`mb-4 ${m.role === 'user' ? 'text-right' : 'text-left'}`}>
            <span className={`p-2 rounded-lg inline-block ${m.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}>
              {m.target || m.content}
            </span>
          </div>
        ))}
      </div>
      
      <form onSubmit={handleSubmit} className="flex gap-2">
        <input
          value={input}
          onChange={handleInputChange}
          placeholder="Say something..."
          className="flex-1 p-2 border rounded"
          disabled={isLoading}
        />

…
常见问题

什么是“hooks”?

它们是 React 中的一种快捷方式,能让你更轻松地添加功能,例如管理聊天状态,或显示逐步到达的回复。

什么是“流式输出”(streaming)?

指 AI 的回复不是一次性全部返回,而是一小段一小段地传过来,就像逐字打在屏幕上一样。这能提升用户体验。

什么是“工具调用”(tool calling)或“函数调用”(function calling)?

指 AI 能够使用你在代码中定义的工具或函数,在回复用户之前获取信息或执行操作。

详情

作者的原始描述

Expert in the Vercel AI SDK. Covers Core API (generateText, streamText), UI hooks (useChat, useCompletion), tool calling, and streaming UI components with React and Next.js.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#6100
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能