Learn how to build a chatbot that can understand PDFs using the AI SDK and Next.js
Some language models like Anthropic's Claude Sonnet 3.5 and Google's Gemini 2.0 can understand PDFs and respond to questions about their contents. In this example, we'll show you how to build a chat interface that accepts PDF uploads.
This example requires a provider that supports PDFs, such as Anthropic's
Claude 3.7, Google's Gemini 2.5, or OpenAI's GPT-4.1. Check the provider
documentation for up-to-date support
information.
The code uses the useChat hook which handles the file upload and message streaming. The experimental_attachments option allows you to send files alongside messages.
Make sure to set up your environment variables with your Anthropic API key:
.env.local
ANTHROPIC_API_KEY=xxxxxxxxx
Now you can upload PDFs and ask questions about their contents. The LLM will analyze the PDF and provide relevant responses based on the document's content.