npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hdr/browser

v0.3.1

Published

An objective-oriented, typed browser automation framework for LLM applications.

Downloads

7

Readme

@hdr/browser

An objective-oriented, typed browser automation framework for LLM applications.

Control local (or sandboxed) Chrome installations with passthrough models. Gather structured content from the internet with user-defined types.

Installation

npm i --save @hdr/browser

Execute directly from the terminal with npx.

npx @hdr/browser [flags]

Usage

Usage will vary depending on whether you employ this package as an executable or as an imported module.

Usage as an imported module

The basic usage is based around the AgentBrowser class. It requires the Agent class (which requires an instantiated chat completion API class), Browser class, as well as a Logger class.

const {
  Logger,
  Browser,
  Agent,
  Inventory,
  AgentBrowser,
} = require("@hdr/browser");

const openAIChatApi = new OpenAIChatApi(
  {
    apiKey: process.env.OPENAI_API_KEY,
  },
  { model: "gpt-4" }
);
const agent = new Agent(openAIChatApi);

// Browser takes a `headless` boolean
const browser = await Browser.create(true);

// Logger takes a `logLevel` string
const logger = new Logger("info");

If you need the agent to use sensitive data such as usernames and passwords, credit cards, addresses, etc. to do a task, you can place that information inside the agent's inventory. The inventory scrambles this data so that the underlying LLM api never sees the actual information. The browser monitors the values the agent enters into text fields and then intercepts and replaces the scrambled data with the real thing.

// Inventory is optional, but helps when you have data you want to use for the objective
const inventory = new Inventory([
  { value: "student", name: "Username", type: "string" },
  { value: "Password123", name: "Password", type: "string" },
]);

The AgentBrowser uses zod under the hood to control the types returned by your LLM. If you want to specify a custom return type, you can do so by extending the ModelResponse schema with the desired type.

const extendedModelResponseSchema = ModelResponseSchema.extend({
  numberArray: z.array(
    z.number().optional().describe("your description here") // Note: the description is important since it tells the LLM what kind of data is important
  ),
});
const agentBrowser = new AgentBrowser(agent, browser, logger, inventory);

Once instantiated, the AgentBrowser is used with the browse method.

const response = await agentBrowser.browse(
  {
    startUrl: "https://duckduckgo.com",
    objective: ["Your task here"],
    // 10 is a good default for our navigation limit
    maxIterations: 10,
  },
  extendedModelResponseSchema
);

A complete example of using the browser in your projects to produce typed and structured results is included in the examples folder.

Using npx

The following flags are used when calling the script directly:

  • --objective "Your task" describes the task involved for the agent to perform using the browser. In this case, we're using "Your task" as an example.
  • --startUrl "https://duckduckgo.com" describes where on the internet to start achieving the objective.
  • --agentProvider "openai" describes what provider to use for achieving the objective.
  • --agentModel "gpt-4" passes which model to use.
  • --agentApiKey YOUR_KEY passes any applicable API key to the agent provider.
  • --headless sets whether to open a headless Chrome instance. By default, this is set to false, which will open a visible, automated Chrome window when performing a task.
  • --config allows you to pass in a .json file for setting config flags and user data inventory. For more information, see below.

Taken together, an example would be:

npx @hdr/browser --agentProvider openai --agentModel gpt-4 --agentApiKey [key] --objective "how many editors are on wikipedia?" --startUrl "https://google.com"

Storing commonly reused information

When running @hdr/browser under npx, we will check for both environment variables and an optional config.json file.

config.json

Here is an example config.json:

{
  "agentProvider": "openai",
  "agentModel": "gpt-4",
  "agentApiKey": "a-key",
  "inventory": [
    {
      "value": "student",
      "name": "Username",
      "type": "string"
    },
    {
      "value": "Password123",
      "name": "Password",
      "type": "string"
    }
  ],
  "headless": true
}

You can then call your browser by running

npx @hdr/browser --config config.json

The script will ask for your start URL and objective if not provided in the config.json or with the --objective and --startUrl flags.

Setting environment variables

You can also set all flags as environment variables. We check for the following:

  • HDR_AGENT_PROVIDER
  • HDR_AGENT_MODEL
  • HDR_AGENT_API_KEY
  • HDR_HEADLESS

Objective, start URL and inventory cannot be set with environment variables.

Running as a server

If you are using @hdr/browser in another language stack, like Python, we recommend running the browser in server mode. To start the webserver, you can run npm run serve or you can run the server from a container using:

docker build . -t hdr/browser # on arm64 macOS, you may need --platform linux/amd64
docker run -p 3000:3000 -t hdr/browser # --platform linux/amd64

You can access documentation for using the server at localhost:3000/doc.

Contributing

Before contributing to this project, please review CONTRIBUTING.

To connect with others building with @hdr/browser, feel free to join our Discord community.

Other licenses

By default, @hdr/browser sends anonymised, abstracted telemetry to our collective memory, which is governed by its own license agreement and our privacy policy.