用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/thiagofernandes1987-create/APEX --skill vercel-ai-sdk-expert命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | vercel-ai-sdk-expert |
| description | "Use when adding AI chat or text generation features to a React or Next.js app" |
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.
generateObjectuseChat or streamTextThe Vercel AI SDK is a unified framework that abstracts away provider-specific APIs (OpenAI, Anthropic, Google Gemini, Mistral). It provides two main layers:
ai): Server-side functions to interact with LLMs (generateText, streamText, generateObject).@ai-sdk/react): Frontend hooks to manage chat state and streaming (useChat, useCompletion).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}`);
// 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();
}
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);
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={` ${ === ? ' ' ''}`}>
{m.target || m.content}
))}
Send
);
}
Tools allow the LLM to interact with your code, fetching external data or performing actions before responding to the user.
// app/api/chat/route.ts
import { streamText, tool } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai('gpt-4o'),
messages,
tools: {
getWeather: tool({
description: 'Get the current weather in a given location',
parameters: z.object({
location: z.string().describe('The city and state, e.g. San Francisco, CA'),
unit: z.enum(['celsius', 'fahrenheit']).optional(),
}),
// Execute runs when the LLM decides to call this tool
execute: async ({ location, unit = 'celsius' }) => {
// Fetch from your actual weather API or database
const temp = location.includes() ? : ;
;
},
}),
},
: ,
});
result.();
}
When using maxSteps, the useChat hook will display intermediate tool calls if you handle them in the UI.
// Inside the `useChat` messages.map loop
{m.role === 'assistant' && m.toolInvocations?.map((toolInvocation) => (
<div key={toolInvocation.toolCallId} className="text-sm text-gray-500">
{toolInvocation.state === 'result' ? (
<p>✅ Fetched weather for {toolInvocation.args.location}</p>
) : (
<p>⏳ Fetching weather for {toolInvocation.args.location}...</p>
)}
</div>
))}
openai('gpt-4o') or anthropic('claude-3-5-sonnet-20240620') format (from specific provider packages like @ai-sdk/openai) instead of the older edge runtime wrappers.schema and a clear system prompt when using generateObject().maxDuration = 30 (or higher if on Pro) in Next.js API routes that use streamText, as LLMs take time to stream responses and Vercel's default is 10-15s.tool() with comprehensive description tags on Zod parameters, as the LLM relies entirely on those strings to understand when and how to call the tool.maxSteps: 5 (or similar) when providing tools, otherwise the LLM won't be able to reply to the user after seeing the tool result!result.toDataStreamResponse() in Next.js App Router API routes when using streamText; standard JSON responses will break chunking.generateObject without validation, even though Zod forces the shape — always handle failure states using try/catch.Problem: The streaming chat cuts off abruptly after 10-15 seconds.
Solution: The serverless function timed out. Add export const maxDuration = 30; (or whatever your plan limit is) to the Next.js API route file.
Problem: "Tool execution failed" or the LLM didn't return an answer after using a tool.
Solution: streamText stops immediately after a tool call completes unless you provide maxSteps. Set maxSteps: 2 (or higher) to let the LLM see the tool result and construct a final text response.
Implement —