Get started with OpenAI GPT-4.5
With the release of OpenAI's GPT-4.5 model, there has never been a better time to start building AI applications, particularly those that require a deeper understanding of the world.
The AI SDK is a powerful TypeScript toolkit for building AI applications with large language models (LLMs) like OpenAI GPT-4.5 alongside popular frameworks like React, Next.js, Vue, Svelte, Node.js, and more.
OpenAI GPT-4.5
OpenAI recently released GPT-4.5, their largest and best model for chat yet. GPT‑4.5 is a step forward in scaling up pretraining and post-training. By scaling unsupervised learning, GPT‑4.5 improves its ability to recognize patterns, draw connections, and generate creative insights without reasoning.
Based on early testing, developers may find GPT‑4.5 particularly useful for applications that benefit from its higher emotional intelligence and creativity such as writing help, communication, learning, coaching, and brainstorming. It also shows strong capabilities in agentic planning and execution, including multi-step coding workflows and complex task automation.
Benchmarks
GPT-4.5 demonstrates impressive performance across various benchmarks:
- SimpleQA Accuracy: 62.5% (higher is better)
- SimpleQA Hallucination Rate: 37.1% (lower is better)
Prompt Engineering for GPT-4.5
GPT-4.5 performs best with the following approach:
- Be clear and specific: GPT-4.5 responds well to direct, well-structured prompts.
- Use delimiters for clarity: Use delimiters like triple quotation marks, XML tags, or section titles to clearly indicate distinct parts of the input.
Getting Started with the AI SDK
The AI SDK is the TypeScript toolkit designed to help developers build AI-powered applications with React, Next.js, Vue, Svelte, Node.js, and more. Integrating LLMs into applications is complicated and heavily dependent on the specific model provider you use.
The AI SDK abstracts away the differences between model providers, eliminates boilerplate code for building chatbots, and allows you to go beyond text output to generate rich, interactive components.
At the center of the AI SDK is AI SDK Core, which provides a unified API to call any LLM. The code snippet below is all you need to call OpenAI GPT-4.5 with the AI SDK:
import { generateText } from 'ai';import { openai } from '@ai-sdk/openai';
const { text } = await generateText({ model: openai('gpt-4.5-preview'), prompt: 'Explain the concept of quantum entanglement.',});
Generating Structured Data
While text generation can be useful, you might want to generate structured JSON data. For example, you might want to extract information from text, classify data, or generate synthetic data. AI SDK Core provides two functions (generateObject
and streamObject
) to generate structured data, allowing you to constrain model outputs to a specific schema.
import { generateObject } from 'ai';import { openai } from '@ai-sdk/openai';import { z } from 'zod';
const { object } = await generateObject({ model: openai('gpt-4.5-preview'), 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.',});
This code snippet will generate a type-safe recipe that conforms to the specified zod schema.
Using Tools with the AI SDK
GPT-4.5 supports tool calling out of the box, allowing it to interact with external systems and perform discrete tasks. Here's an example of using tool calling with the AI SDK:
import { generateText, tool } from 'ai';import { openai } from '@ai-sdk/openai';
const { text } = await generateText({ model: openai('gpt-4.5-preview'), prompt: 'What is the weather like today in San Francisco?', tools: { getWeather: tool({ description: 'Get the weather in a location', parameters: z.object({ location: z.string().describe('The location to get the weather for'), }), execute: async ({ location }) => ({ location, temperature: 72 + Math.floor(Math.random() * 21) - 10, }), }), },});
In this example, the getWeather
tool allows the model to fetch real-time weather data (simulated for simplicity), enhancing its ability to provide accurate and up-to-date information.
Building Interactive Interfaces
AI SDK Core can be paired with AI SDK UI, another powerful component of the AI SDK, to streamline the process of building chat, completion, and assistant interfaces with popular frameworks like Next.js, Nuxt, SvelteKit, and SolidStart.
AI SDK UI provides robust abstractions that simplify the complex tasks of managing chat streams and UI updates on the frontend, enabling you to develop dynamic AI-driven interfaces more efficiently.
With four main hooks — useChat
, useCompletion
, useObject
, and useAssistant
— you can incorporate real-time chat capabilities, text completions, streamed JSON, and interactive assistant features into your app.
Let's explore building a chatbot with Next.js, the AI SDK, and OpenAI GPT-4.5:
In a new Next.js application, first install the AI SDK and the OpenAI provider:
npm install ai @ai-sdk/openai
Then, create a route handler for the chat endpoint:
import { openai } from '@ai-sdk/openai';import { streamText } from 'ai';
// Allow responses up to 30 secondsexport const maxDuration = 30;
export async function POST(req: Request) { const { messages } = await req.json();
const result = streamText({ model: openai('gpt-4.5-preview'), messages, });
return result.toDataStreamResponse();}
Finally, update the root page (app/page.tsx
) to use the useChat
hook:
'use client';
import { useChat } from '@ai-sdk/react';
export default function Page() { const { messages, input, handleInputChange, handleSubmit, error } = 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> </> );}
The useChat hook on your root page (app/page.tsx
) will make a request to your AI provider endpoint (app/api/chat/route.ts
) whenever the user submits a message. The messages are then displayed in the chat UI.
Get Started
Ready to get started? Here's how you can dive in:
- Explore the documentation at sdk.vercel.ai/docs to understand the full capabilities of the AI SDK.
- Check out practical examples at sdk.vercel.ai/examples to see the SDK in action and get inspired for your own projects.
- Dive deeper with advanced guides on topics like Retrieval-Augmented Generation (RAG) and multi-modal chat at sdk.vercel.ai/docs/guides.
- Check out ready-to-deploy AI templates at vercel.com/templates?type=ai.