惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

Feed

React 19 Performance Optimizations You Need to Know Tesla Recalls 2.2 Million Vehicles Over Autopilot Software Bug Creating Interactive Prototypes in Figma with Smart Animate OpenAI Introduces GPT-4 Turbo with Vision API Figma Introduces AI-Powered Design System Generator The Rise of Local-First Software Development GitHub Copilot Usage Surpasses 1.8 Million Paid Users Building Responsive Layouts with CSS Container Queries Supabase Launches Real-time Multiplayer Engine Major Security Flaw Discovered in Popular JWT Libraries The Hidden Cost of Technical Debt in Startup Engineering Figma Launches Dev Mode 2.0 with Code Generation Why SaaS Companies Are Moving Away from Microservices Building Faster APIs with Bun and Elysia
Building AI-Powered React Components with Vercel AI SDK
Sarah Kim · 2025-04-09 · via Feed

Building AI-Powered React Components with Vercel AI SDK Featured Post Video Post

React

Complete guide to integrating AI capabilities into React applications using Vercel's AI SDK. Build chatbots, content generators, and smart UI components with streaming responses

Building AI-Powered React Components with Vercel AI SDK

0:00

/0:08

Video by Distill / Pexels

The Vercel AI SDK makes it incredibly easy to add AI capabilities to React applications. In this tutorial, we'll build several AI-powered components from scratch, including a streaming chatbot and intelligent content generator.

Getting Started

First, install the necessary packages:

npm install ai @ai-sdk/openai @ai-sdk/anthropic
npm install @types/react @types/node # if using TypeScript

Set up your environment variables:

# .env.local
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key

Basic Chat Component

Let's start with a simple chat interface that streams responses:

// components/ChatBot.jsx
import { useChat } from 'ai/react';
import { useState } from 'react';

export default function ChatBot() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
    api: '/api/chat',
  });

  return (
    <div className="chat-container">
      <div className="messages">
        {messages.map((message) => (
          <div key={message.id} className={`message ${message.role}`}>
            <strong>{message.role === 'user' ? 'You' : 'AI'}:</strong>
            <p>{message.content}</p>
          </div>
        ))}
        {isLoading && <div className="loading">AI is thinking...</div>}
      </div>
      
      <form onSubmit={handleSubmit} className="input-form">
        <input
          value={input}
          onChange={handleInputChange}
          placeholder="Ask me anything..."
          disabled={isLoading}
        />
        <button type="submit" disabled={isLoading}>
          Send
        </button>
      </form>
    </div>
  );
}

API Route Setup

// pages/api/chat.js (or app/api/chat/route.js for App Router)
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

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

  const result = await streamText({
    model: openai('gpt-4-turbo'),
    messages,
    system: 'You are a helpful assistant that provides concise, accurate answers.',
  });

  return result.toAIStreamResponse();
}

useChat Hook Features:

Built-in State Management

  • Automatic message history
  • Loading states
  • Error handling
  • Input management

Streaming Support

  • Real-time response streaming
  • Partial message updates
  • Automatic UI updates

Customization Options

  • Custom API endpoints
  • Message preprocessing
  • Response formatting
  • Error callbacks

Advanced Content Generator

Let's build a more sophisticated component that generates blog content:

// components/ContentGenerator.jsx
import { useCompletion } from 'ai/react';
import { useState } from 'react';

export default function ContentGenerator() {
  const [topic, setTopic] = useState('');
  const [contentType, setContentType] = useState('blog-post');
  
  const {
    completion,
    input,
    handleInputChange,
    handleSubmit,
    isLoading,
    error,
  } = useCompletion({
    api: '/api/generate',
    body: {
      contentType,
      topic,
    },
  });

  const handleGenerate = (e) => {
    e.preventDefault();
    handleSubmit(e, {
      body: {
        prompt: input,
        contentType,
        topic,
      },
    });
  };

  return (
    <div className="content-generator">
      <div className="controls">
        <input
          type="text"
          placeholder="Enter topic..."
          value={topic}
          onChange={(e) => setTopic(e.target.value)}
        />
        
        <select
          value={contentType}
          onChange={(e) => setContentType(e.target.value)}
        >
          <option value="blog-post">Blog Post</option>
          <option value="social-media">Social Media</option>
          <option value="email">Email</option>
          <option value="product-description">Product Description</option>
        </select>
        
        <form onSubmit={handleGenerate}>
          <textarea
            value={input}
            onChange={handleInputChange}
            placeholder="Additional instructions..."
            rows={3}
          />
          <button type="submit" disabled={isLoading || !topic}>
            {isLoading ? 'Generating...' : 'Generate Content'}
          </button>
        </form>
      </div>

      {error && (
        <div className="error">
          Error: {error.message}
        </div>
      )}

      {completion && (
        <div className="generated-content">
          <h3>Generated Content:</h3>
          <div className="content-preview">
            {completion.split('\n').map((paragraph, index) => (
              <p key={index}>{paragraph}</p>
            ))}
          </div>
          <button onClick={() => navigator.clipboard.writeText(completion)}>
            Copy to Clipboard
          </button>
        </div>
      )}
    </div>
  );
}

API Route for Content Generation

// pages/api/generate.js
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

const contentPrompts = {
  'blog-post': 'Write a comprehensive blog post about',
  'social-media': 'Create engaging social media content about',
  'email': 'Write a professional email about',
  'product-description': 'Create a compelling product description for',
};

export async function POST(req) {
  const { prompt, contentType, topic } = await req.json();
  
  const systemPrompt = `${contentPrompts[contentType]} ${topic}. ${prompt || ''}
  
  Guidelines:
  - Keep the tone professional but engaging
  - Include relevant keywords naturally
  - Structure content with clear headings
  - Make it actionable and valuable`;

  const result = await streamText({
    model: openai('gpt-4-turbo'),
    prompt: systemPrompt,
    temperature: 0.7,
    maxTokens: 1000,
  });

  return result.toAIStreamResponse();
}