The AI SDK provides a streamlined way to integrate OpenAI's powerful models into your applications. It abstracts away the complexities of working directly with the OpenAI API, making it easier to build AI-powered features 12.
First, you'll need to install the AI SDK and the OpenAI integration package:
npm install ai @ai-sdk/openaiBefore using OpenAI's models, you need to obtain an API key:
Add the API key to your environment variables:
OPENAI_API_KEY='sk-...3Yu5'Here's a simple example of generating text with OpenAI's models using the AI SDK:
example.tsx
OpenAI's Responses API offers advanced features like chat history persistence, web search, and more. Here's how to use it with the AI SDK 1:
responses-example.tsx
You can also generate structured JSON data using the AI SDK:
structured-data.tsx
The AI SDK supports tool calling with OpenAI models, allowing them to interact with external systems 1:
tool-example.tsx
With the Responses API, you can persist chat history across requests 1:
chat-history.tsx
The AI SDK supports various OpenAI models, including 4:
For more information and advanced usage, refer to the AI SDK documentation.