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 🙏

© 2025 – Pkg Stats / Ryan Hefner

llmatch-js

v2025.6.16739

Published

A JavaScript library for invoking LLMs and matching their responses with patterns

Downloads

17

Readme

llmatch-js

A JavaScript library for invoking LLMs and matching their responses with patterns. This is a JavaScript port of the Python llmatch package.

Installation

npm install llmatch-js

Features

  • Pattern-matching on LLM responses using regular expressions
  • Automatic retries with exponential backoff
  • Support for any LLM client with a compatible interface
  • Detailed verbose logging option

Usage

Basic Example

const { llmatch } = require('llmatch-js');
import { ChatLLM7 } from "langchain-llm7";

// Initialize your LLM client
const chat = new ChatLLM7()

// Create a wrapper for the LLM that matches the expected interface
const llm = {
  invoke: async (messages, options = {}) => {
    const response = await chat.invoke({
      messages: messages,
      ...options
    });
    
    return {
      content: response.content,
      raw: response
    };
  }
};

// Use llmatch to extract structured data
async function extractJson() {
  const result = await llmatch({
    llm,
    query: "Generate a JSON object with information about a random book.",
    pattern: /```json\n([\s\S]*?)```/,
    verbose: true
  });
  
  if (result.success) {
    // Parse the extracted JSON
    const bookData = JSON.parse(result.extractedData[0]);
    console.log("Book data:", bookData);
  } else {
    console.error("Failed to extract JSON:", result.errorMessage);
  }
}

extractJson().catch(console.error);

API Reference

llmatch(options)

Main function that invokes an LLM and matches the response against a pattern.

| Parameter | Type | Default | Description | |-----------|------|---------|-------------| | llm | Object | null | An already initialized instance of the LLM client | | query | string | "Extract relevant data." | The main query or instruction for the LLM | | pattern | string | RegExp | /(.*?)/s | A regex string or compiled RegExp object | | context | string | null | Optional additional text to provide context to the LLM | | promptTemplate | string | "{query}\n\n{context}\nWrite the answer in the next format: {format}" | Template string | | maxRetries | number | 15 | Maximum number of attempts to make | | initialDelay | number | 1.0 | Seconds to wait before the first retry | | backoffFactor | number | 1.5 | Multiplier for the delay between retries | | verbose | boolean | false | If true, prints detailed logs of the process | | passThrough | Object | {} | Additional parameters to pass to the LLM invocation |

Return Value

The function returns a Promise that resolves to an object with the following properties:

| Property | Type | Description | |----------|------|-------------| | success | boolean | True if a valid response was found | | extractedData | Array | null | Array of strings matching the pattern (capturing groups), or null if no pattern was provided or matched | | finalContent | string | null | The content of the last successful or final failed LLM response | | retriesAttempted | number | Number of retries made (0 means success on first try) | | errorMessage | string | null | Description of the error if success is false | | rawResponse | any | The raw response object from the last LLM call |

LLM Interface Requirements

Your LLM client should implement the following interface:

interface LLMClient {
  invoke(messages: Array<{role: string, content: string}>, options?: any): Promise<{
    content: string;
    [key: string]: any;
  }>;
}

License

MIT