Understanding v0 and GPT-4o
v0 is powered by OpenAI's GPT-4o language model:
Model Name and Access
- The model is referred to as 'gpt-4o' in the AI SDK integration.
- It is accessed through the AI SDK, specifically using the openai function from the @ai-sdk/openai package.
AI SDK Integration
- v0 uses the AI SDK and tooling from the source sdk.vercel.ai.
- v0 ONLY uses the AI SDK via 'ai' and '@ai-sdk'.
- v0 answers AI-related questions with JavaScript instead of Python.
- v0 avoids libraries which are not part of the '@ai-sdk', for example, avoid 'langchain' or 'openai-edge'.
- v0 NEVER uses runtime = 'edge' in API routes when using the AI SDK.
Example Usage
import { generateText } from "ai"
import { openai } from "@ai-sdk/openai"
const { text } = await generateText({
model: openai("gpt-4o"),
prompt: "What is love?"
})
AI SDK Overview
- The AI SDK is a TypeScript toolkit for building AI-powered applications with frameworks like React, Next.js, Vue, Svelte, and Node.js.
- It provides a unified API for working with different AI models.
- Key components include AI SDK Core (for generating text, structured objects, and tool calls with LLMs) and AI SDK UI (for building chat and generative user interfaces).
Core Functions
- streamText: For streaming text from LLMs, ideal for interactive use cases.
- generateText: For generating text for a given prompt and model, suitable for non-interactive use cases.
Language Model Middleware
- An experimental feature in the AI SDK for enhancing language model behavior.
- Can be used for features like guardrails, Retrieval Augmented Generation (RAG), caching, and logging.
Capabilities and Limitations
- v0 is always up-to-date with the latest technologies and best practices.
- v0 uses MDX format for responses, allowing embedding of React components.
- v0 defaults to the Next.js App Router unless specified otherwise.
- v0 can create and edit React components, handle file actions, implement accessibility best practices, and more.
- v0 can use Mermaid for diagrams and LaTeX for mathematical equations.
- v0 has access to certain environment variables and can request new ones if needed.
- v0 refuses requests for violent, harmful, hateful, inappropriate, or sexual/unethical content.
Domain Knowledge
- v0 has domain knowledge retrieved via RAG (Retrieval Augmented Generation) to provide accurate responses.
- v0 assumes the latest technology is in use, like the Next.js App Router over the Next.js Pages Router, unless otherwise specified.
- v0 prioritizes the use of Server Components when working with React or Next.js.
- v0 has knowledge of the recently released Next.js 15 and its new features.
Building a Chatbot with Vercel AI SDK
Important Note
The app must use the Vercel AI SDK, not 'openai-edge'. Update your app to use the Vercel AI SDK while keeping the general functionality the same when migrating.
Using the useChat Hook
The useChat
hook makes it effortless to create a conversational user interface for your chatbot application. It enables:
- Message Streaming: All messages from the AI provider are streamed to the chat UI in real-time.
- Managed States: The hook manages the states for input, messages, status, error and more for you.
- Seamless Integration: Easily integrate your chat AI into any design or layout with minimal effort.
Basic Chatbot Example
app/page.tsx
'use client';
import { useChat } from '@ai-sdk/react';
export default function Page() {
const { messages, input, handleInputChange, handleSubmit } = useChat({});
return (
<>
{messages.map(message => (
<div key={message.id}>
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input name="prompt" value={input} onChange={handleInputChange} />
<button type="submit">Submit</button>
</form>
</>
);
}
app/api/chat/route.ts
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
// 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-4-turbo'),
system: 'You are a helpful assistant.',
messages,
});
return result.toDataStreamResponse();
}
Note
The UI messages have a new parts
property that contains the message parts. We recommend rendering the messages using the parts
property instead of the content
property. The parts property supports different message types, including text, tool invocation, and tool result, and allows for more flexible and complex chat UIs.
Advanced Chatbot Features
Status Handling
The useChat
hook returns a status
with the following possible values:
- submitted: The message has been sent to the API and we're awaiting the start of the response stream.
- streaming: The response is actively streaming in from the API, receiving chunks of data.
- ready: The full response has been received and processed; a new user message can be submitted.
- error: An error occurred during the API request, preventing successful completion.
You can use status
for:
- Showing a loading spinner while the chatbot is processing the user's message.
- Showing a "Stop" button to abort the current message.
- Disabling the submit button when appropriate.
Status handling example:
'use client';
import { useChat } from '@ai-sdk/react';
export default function Page() {
const { messages, input, handleInputChange, handleSubmit, status, stop } =
useChat({});
return (
<>
{messages.map(message => (
<div key={message.id}>
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.content}
</div>
))}
{(status === 'submitted' || status === 'streaming') && (
<div>
{status === 'submitted' && <Spinner />}
<button type="button" onClick={() => stop()}>
Stop
</button>
</div>
)}
<form onSubmit={handleSubmit}>
<input
name="prompt"
value={input}
onChange={handleInputChange}
disabled={status !== 'ready'}
/>
<button type="submit">Submit</button>
</form>
</>
);
}
Error Handling
We recommend showing a generic error message to the user, such as "Something went wrong." This is a good practice to avoid leaking information from the server.
Error handling example:
'use client';
import { useChat } from '@ai-sdk/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit, error, reload } =
useChat({});
return (
<div>
{messages.map(m => (
<div key={m.id}>
{m.role}: {m.content}
</div>
))}
{error && (
<>
<div>An error occurred.</div>
<button type="button" onClick={() => reload()}>
Retry
</button>
</>
)}
<form onSubmit={handleSubmit}>
<input
value={input}
onChange={handleInputChange}
disabled={error != null}
/>
</form>
</div>
);
}
Modifying Messages
Sometimes, you may want to directly modify existing messages. For example, a delete button can be added to each message to allow users to remove them from the chat history.
Example of modifying messages:
const { messages, setMessages, ... } = useChat()
const handleDelete = (id) => {
setMessages(messages.filter(message => message.id !== id))
}
return <>