LogoLogo
SupportDashboard
  • Community
  • Welcome to Hyperbrowser
  • Get Started
    • Quickstart
      • AI Agents
        • Browser Use
        • Claude Computer Use
        • OpenAI CUA
      • Web Scraping
        • Scrape
        • Crawl
        • Extract
      • Browser Automation
        • Puppeteer
        • Playwright
        • Selenium
  • Agents
    • Browser Use
    • Claude Computer Use
    • OpenAI CUA
  • HyperAgent
    • About HyperAgent
      • HyperAgent SDK
      • HyperAgent Types
  • Quickstart
  • Multi-Page actions
  • Custom Actions
  • MCP Support
    • Tutorial
  • Examples
    • Custom Actions
    • LLM support
    • Cloud Support
      • Setting Up
      • Proxies
      • Profiles
    • MCP Examples
      • Google Sheets
      • Weather
        • Weather Server
    • Output to Schema
  • Web Scraping
    • Scrape
    • Crawl
    • Extract
  • Sessions
    • Overview
      • Session Parameters
    • Advanced Privacy & Anti-Detection
      • Stealth Mode
      • Proxies
      • Static IPs
      • CAPTCHA Solving
      • Ad Blocking
    • Profiles
    • Recordings
    • Live View
    • Extensions
    • Downloads
  • Guides
    • Model Context Protocol
    • Scraping
    • AI Function Calling
    • Extract Information with an LLM
    • Using Hyperbrowser Session
    • CAPTCHA Solving
  • Integrations
    • ⛓️LangChain
    • 🦙LlamaIndex
  • reference
    • Pricing
    • SDKs
      • Node
        • Sessions
        • Profiles
        • Scrape
        • Crawl
        • Extensions
      • Python
        • Sessions
        • Profiles
        • Scrape
        • Crawl
        • Extensions
    • API Reference
      • Sessions
      • Scrape
      • Crawl
      • Extract
      • Agents
        • Browser Use
        • Claude Computer Use
        • OpenAI CUA
      • Profiles
      • Extensions
Powered by GitBook
On this page
  • Setup
  • Installation
  • Setup your Environment
  • Code
Export as PDF
  1. Guides

AI Function Calling

Using Hyperbrowser with OpenAI and Anthropic Function Tools

PreviousScrapingNextExtract Information with an LLM

Last updated 1 month ago

Hyperbrowser integrates seamlessly with OpenAI and Anthropic's function calling APIs, enabling you to enhance your AI applications with web scraping and crawling capabilities. This guide will walk you through setting up and using Hyperbrowser's scrape and crawl tools with OpenAI and Anthropic.

Setup

Installation

First, install the necessary dependencies to run our script.

npm install @hyperbrowser/sdk dotenv openai
pip install hyperbrowser openai python-dotenv

Setup your Environment

To use Hyperbrowser with your code, you will need an API Key. You can get one easily from the . Once you have your API Key, add it to your .env file as HYPERBROWSER_API_KEY . You will also need an OPENAI_API_KEY.

Code

import OpenAI from "openai";
import { Hyperbrowser } from "@hyperbrowser/sdk";
import { WebsiteCrawlTool, WebsiteScrapeTool,WebsiteExtractTool } from "@hyperbrowser/sdk/tools";
import { config } from "dotenv";

config();

// Initialize clients
const hb = new Hyperbrowser({ apiKey: process.env.HYPERBROWSER_API_KEY });
const oai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

async function handleToolCall(tc) {
  console.log("Handling tool call");

  try {
    const args = JSON.parse(tc.function.arguments);
    console.log(`Tool call ID: ${tc.id}`);
    console.log(`Function name: ${tc.function.name}`);
    console.log(`Function args: ${JSON.stringify(args, null, 2)}`);
    console.log("-".repeat(50));

    if (
      tc.function.name === WebsiteCrawlTool.openaiToolDefinition.function.name
    ) {
      const response = await WebsiteCrawlTool.runnable(hb, args);
      return {
        tool_call_id: tc.id,
        content: response,
        role: "tool",
      };
    } else if (
      tc.function.name === WebsiteScrapeTool.openaiToolDefinition.function.name
    ) {
      const response = await WebsiteScrapeTool.runnable(hb, args);
      return {
        tool_call_id: tc.id,
        content: response,
        role: "tool",
      };
    } else if (
      tc.function.name === WebsiteExtractTool.openaiToolDefinition.function.name
    ) {
      const response = await WebsiteExtractTool.runnable(hb, args);
      return {
        tool_call_id: tc.id,
        content: response,
        role: "tool",
      };
    } else {
      return {
        tool_call_id: tc.id,
        content: "Unknown tool call",
        role: "tool",
      };
    }
  } catch (e) {
    console.error(e);
    return {
      role: "tool",
      tool_call_id: tc.id,
      content: `Error occurred: ${e}`,
    };
  }
}

const messages = [
  {
    role: "user",
    content: "What does Hyperbrowser.ai do? Provide citations.",
  },
];

async function openaiChat() {
  while (true) {
    const resp = await oai.beta.chat.completions.parse({
      model: "gpt-4o-mini",
      messages: messages,
      tools: [
        WebsiteCrawlTool.openaiToolDefinition,
        WebsiteScrapeTool.openaiToolDefinition,
        WebsiteExtractTool.openaiToolDefinition,
      ],
    });

    const choice = resp.choices[0];
    messages.push(choice.message);

    if (choice.finish_reason === "tool_calls") {
      for (const tc of choice.message.tool_calls) {
        const result = await handleToolCall(tc);
        messages.push(result);
      }
    } else if (choice.finish_reason === "stop") {
      console.log(choice.message.content);
      break;
    } else {
      throw new Error("Unknown Error Occurred");
    }
  }
}

openaiChat();
import json
import os

from hyperbrowser import Hyperbrowser
from hyperbrowser.tools import WebsiteCrawlTool, WebsiteScrapeTool, WebsiteExtractTool

from openai import OpenAI
from openai.types.chat import (
    ChatCompletionMessageToolCall,
    ChatCompletionMessageParam,
    ChatCompletionToolMessageParam,
)
from dotenv import load_dotenv

load_dotenv()

oai = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
hb = Hyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))


def handle_tool_call(
    tc: ChatCompletionMessageToolCall,
) -> ChatCompletionToolMessageParam:
    print("Handling tool call")

    try:
        args = json.loads(tc.function.arguments)
        print(f"Tool call ID: {tc.id}")
        print(f"Function name: {tc.function.name}")
        print(f"Function args: {args}")
        print("-" * 50)
        if (
            tc.function.name
            == WebsiteCrawlTool.openai_tool_definition["function"]["name"]
        ):
            response = WebsiteCrawlTool.runnable(hb, args)
            return {"tool_call_id": tc.id, "content": response, "role": "tool"}
        elif (
            tc.function.name
            == WebsiteScrapeTool.openai_tool_definition["function"]["name"]
        ):
            response = WebsiteScrapeTool.runnable(hb, args)
            return {"tool_call_id": tc.id, "content": response, "role": "tool"}
        elif (
            tc.function.name
            == WebsiteExtractTool.openai_tool_definition["function"]["name"]
        ):
            response = WebsiteExtractTool.runnable(hb, args)
            return {"tool_call_id": tc.id, "content": response, "role": "tool"}
        else:
            return {
                "tool_call_id": tc.id,
                "content": "Unknown tool call",
                "role": "tool",
            }
    except Exception as e:
        print(e)
        return {"role": "tool", "tool_call_id": tc.id, "content": f"Error occured: {e}"}


messages: list[ChatCompletionMessageParam] = [
    {
        "role": "user",
        "content": "What does Hyperbrowser.ai do? Provide citations.",
    },
]

while True:
    response = oai.beta.chat.completions.parse(
        messages=messages,
        model="gpt-4o-mini",
        tools=[
            WebsiteCrawlTool.openai_tool_definition,
            WebsiteScrapeTool.openai_tool_definition,
            WebsiteExtractTool.openai_tool_definition,
        ],
    )

    choice = response.choices[0]
    messages.append(choice.message)  # type: ignore
    if choice.finish_reason == "tool_calls":
        for tc in choice.message.tool_calls:  # type: ignore
            result = handle_tool_call(tc=tc)
            messages.append(result)

    elif choice.finish_reason == "stop":
        print(choice.message.content)
        break

    else:
        raise Exception("Unknown Error Occured")

Hyperbrowser exposes a WebsiteCrawlTool, a WebsiteScrapeTool, and a WebsiteExtractTool to be used for OpenAI and Anthropic function calling. Each class provides a tool definition for both OpenAI and Anthropic that defines the schema which can be passed into the tools argument. Then, in the handleToolCall function, we parse the tool call arguments and dispatch the appropriate tool class runnable function based on the function name which will return the result of the scrape or crawl in formatted markdown.

dashboard