What this tutorial covers
- Access and extract website posts and contents using Browserbase
- Use the Vercel AI SDK to create a chat interface
- Stream the results from the LLM
Usage
To use BrowseGPT, you need to have the Vercel AI SDK and Browserbase installed. The following packages are recommended:- ai for the chat interface
- zod for data validation
- playwright for data extraction
- @ai-sdk/openai for OpenAI models
- @ai-sdk/anthropic for Anthropic models
- @mozilla/readability for the readability library
- jsdom for DOM manipulation
Getting started
For this tutorial, you’ll need:- Browserbase credentials:
- An LLM API key from one of the following:
Browserbase sessions often run longer than 15 seconds. By signing up for the
Pro Plan on Vercel, you can increase the Vercel
function duration limit.
Imports and dependencies
Nextjs uses Route Handlers to handle API requests. These include methods such asGET, POST, PUT, DELETE, etc.
To create a new route handler, create a new file in the app/api directory.
In this example, the file is called route.ts for the chat route.
From here, import the necessary dependencies.
route.ts
Helper functions
These are utility functions used throughout the application.getDebugUrl fetches debug information for a Browserbase session, while createSession initializes a new Browserbase session for web interactions.
Main API route handler
This section sets up the main API route handler. It configures the runtime environment, sets a maximum duration for the API call, and defines the POST method that handles incoming requests. The Vercel AI SDK’s streamText function processes messages and streams responses. The maximum duration is set to 300 seconds (5 minutes), since Browserbase sessions often run longer than 15 seconds (Vercel’s default timeout).route.ts
Tools
Next, create the tools needed for this route handler. These tools are used depending on the user’s request. For example, if the user wants to search the web, thegoogleSearch tool handles it. If they want to get the content of a page, the getPageContent tool is used.
Keep in mind that you have the option to choose any LLM model that is compatible with the Vercel AI SDK.
In testing, gpt-4.1 worked best for tool calling, and claude-sonnet-4-6 worked best for generating responses.
Create Browserbase session tool
This tool creates a new Browserbase session. It’s used when a fresh browsing context is needed for web interactions. The tool returns the session ID and debug URL, which are used in subsequent operations.createSession() and getDebugUrl() functions from earlier create a new Browserbase session and get the debug URL.
This lets you embed the debug URL in the response so the frontend can display the Browserbase session.
Google Search tool
This tool performs a web search using Browserbase. It takes a search query as input and returns the search results.Ask for confirmation tool
This tool asks the user for confirmation before performing a specific action. It takes a confirmation prompt as input and returns the user’s response.Get page content tool
The last tool isgetPageContent.
This tool retrieves the content of a web page using Playwright. It then uses jsdom to parse the HTML content into a DOM structure and Readability to extract the main content of the page.
Finally, it uses the Anthropic Claude model to generate a summary of the page’s content.
Frontend
Now that the tools and route handler are set up, you can create the frontend. Use the useChat hook to create a chat interface. Here’s a simple example of how to use BrowseGPT in a Next.js frontend application:Conclusion
You’ve now seen how to use the Vercel AI SDK to create a chat interface that searches the web using Browserbase. You can view a demo of this tutorial here. The code for this tutorial is open-sourced here.BrowseGPT demo
Demo of BrowseGPT that allows you to search the web using a chat interface.
BrowseGPT repository
BrowseGPT is a tool that allows you to search the web using a chat
interface.