BrowseGPT
BrowseGPT is a tool that allows you to search the web using a chat interface.
Introduction
BrowseGPT is a tool that allows you to search the web using a chat interface.
It is built on top of the Vercel AI SDK and Browserbase.
What this tutorial covers
-
Access and scrape 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.
We recommend using the following packages:
- ai for the chat interface
- zod for data validation
- playwright for web-scraping
- @vercel/ai for the Vercel AI SDK
- @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 as GET
, POST
, PUT
, DELETE
, etc.
To create a new route handler, create a new file in the app/api
directory.
In this example, we’ll call this file route.ts
for the chat route.
From here, we’ll import the necessary dependencies.
This section imports necessary libraries and modules for the application.
It includes the Vercel AI SDK, Zod for schema validation, Playwright for web automation, and libraries for content extraction and processing.
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 will handle incoming requests.
You can see we use the Vercel AI SDK’s streamText function to process messages and stream responses.
We set the maximum duration to 300 seconds (5 minutes), since our Browserbase sessions often run longer than 15 seconds (Vercel’s default timeout).
Tools
Next, we’ll create the tools needed for this Route Handler. These tools would be used depending on the user’s request.
For example, if they want to search the web, we’ll use the googleSearch
tool. If they want to get the content of a page, we’ll use the getPageContent
tool.
Keep in mind that you have the option to choose any LLM model that is compatible with the Vercel AI SDK.
We found that using gpt-4-turbo
was the best for tool calling, and claude-3-5-sonnet-20241022
was the 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.
As you can see, we used the createSession()
and getDebugUrl()
we made earlier to create a new Browserbase session and get the debug URL.
This is so later we can embed the debug URL in the response and our frontend can use it to view the Browserbase session.
Google Search tool
This tool performs a search on the web 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 we’ll create is the getPageContent
tool.
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 we have our tools and route handler set up, we can create our frontend.
We’ll 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 can search the web using Browserbase.
You can view a demo of this tutorial here.
We’ve also open-sourced the code for this tutorial here.
Was this page helpful?