Skip to content

webfuse-com/D2Snap

Repository files navigation

D2Snap

Example of downsampling on an image (top) and a DOM (bottom)

D2Snap is a first-of-its-kind DOM downsampling algorithm, designed for use with LLM-based web agents.

Integrate

D2Snap.d2Snap(
  dom: DOM,
  rE: number, rA: number, rT: number,
  options?: Options
): Promise<{
  html: string;
  meta: {};
}>

D2Snap.adaptiveD2Snap(
  dom: DOM,
  maxTokens: number = 4096,
  maxIterations: number = 5,
  options?: Options
): Promise<{
  html: string;
  meta: {};
  parameters: {};
  adaptiveIterations: number;
}>
type DOM = Document | Element | string;
type Options = {
  debug?: boolean;                              // false
  uiFeatureHeuristics?: object;                 // compare src/types.ts:UIFeatureHeuristicsJSON
  uiFeatureHeuristicsReplaceDefault?: boolean;  // false
  skipMarkdown?: boolean;                       // false
  skipTextRank?: boolean;                       // false
  uniqueIDs?: boolean;                          // false
};

The downsampling UI feature heuristics can be overridden via options.uiFeatureHeuristics (full replacement via uiFeatureHeuristicsReplaceDefault: true). Wildcards for aria and data attributes are supported ({aria-|data-}*).

Browser

<script src="https://cdn.jsdelivr.net/gh/webfuse-com/D2Snap@main/dist.browser/D2Snap.js"></script>

Module

npm install webfuse-com/D2Snap

Install jsdom to use the library with Node.js:

npm install jsdom
import * as D2Snap from "@webfuse-com/d2snap";

Example

<section class="container" tabindex="3" required="true" type="example">
  <div class="mx-auto" data-topic="products" required="false">
    <h1>Our Pizza</h1>
    <div>
      <div class="shadow-lg">
        <h2>Margherita</h2>
        <p>
         A simple classic: mozzarella, tomatoes and basil.
         An everyday choice!
        </p>
        <button type="button">Add</button>
      </div>
      <div class="shadow-lg">
        <h2>Capricciosa</h2>
        <p>
          A rich taste: mozzarella, ham, mushrooms, artichokes and olives.
          A true favourite!
        </p>
        <button type="button">Add</button>
      </div>
    </div>
  </div>
</section>

↓ D2Snap ↓

<section class="container" required="true" type="example">
  <div class="mx-auto" required="false">
    # Our Pizza
    <div>
      ## Margherita
      A simple classic: mozzarella, tomatoes and basil.
      An everyday choice!
      <button type="button">Add</button>
      ## Capricciosa
      A rich taste: mozzarella, ham, mushrooms, artichokes and olives.
      A true favourite!
      <button type="button">Add</button>
    </div>
  </div>
</section>

↓ D2Snap ↓

# Our Pizza
## Margherita
A simple classic: mozzarella, tomatoes and basil.
An everyday choice!
<button>Add</button>
## Capricciosa
A rich taste: mozzarella, ham, mushrooms, artichokes and olives.
A true favourite!
<button>Add</button>

Experiments

Setup

npm install
npm install jsdom

Build

npm run build

Test

npm run test

Evaluate

Provide LLM API provider key(s) to .env (compare example).

cs eval && pip install -r requirements.txt
npm run eval:success:<subject>
npm run eval:ratio

<subject> ∈ { gui, dom, bu, D2Snap }

npm run eval:success:D2Snap -- --verbose --split 10,20 --provider openai --model gpt-4o

Re-create Snapshots

npm run snapshots:create

Beyond Pixels: Exploring DOM Downsampling for LLM-Based Web Agents
Thassilo M. SchiepanskiNicholas Piël
Surfly BV

About

Beyond Pixels: Exploring DOM Downsampling for LLM-Based Web Agents

Resources

Stars

Watchers

Forks

Releases

Contributors

Languages