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

@guardrails-ai/core

v0.1.1

Published

A Javascript wrapper for guardrails-ai

Downloads

78

Readme

guardrails-js

A Javascript wrapper for guardrails-ai.

This library contains limited support for using guardrails-ai in javascript.

The following methods and properties are supported:

  • Guard.fromRail
  • Guard.fromRailString
  • Guard.fromString
  • Guard.parse (without an llm_api)
  • Guard.history

The key differences between this wrapper and the python library are as follows:

  1. All methods and properties are in camelCase instead of snake_case
  2. No support for custom validators
  3. No support for re-asking (though you can perform reasks yourself outside of the library using ValidationOutcome.reask or guard.history.at(#).reask_prompts when defined)
  4. LLM calls must be made by the user and the text response passed into parse

In addition to above, this library also supports the readonly properties on the ValidationOutcome class as well as readonly versions of the History & Logs related classes like Call, Iteration, etc..

See the JS docs here

Installation

npm i @guardrails-ai/core

Example

import { Guard, Validators } from '@guardrails-ai/core';

const guard = await Guard.fromRail('./my-railspec.rail');
      
const messages = ['Hello World!', 'Goodbye World!'];

const response = await guard.parse(
    'Hello World!',
    {
        promptParams: { 'messages': messages }
    }
);

console.log(response);

Caveats and Oddities

The current version of the library uses a IO bridge so both javascript and python3 must be available in the environment.

For the best experience, you may also need to explicitly call for the bridge to exit at the end of the node process. We export an exit function to serve this purpose.

Below is a simple end-to-end test we use that demonstrates the concepts above:

import assert from 'node:assert';
import process from 'node:process';
import { Guard, Validators, exit } from '@guardrails-ai/core';

process.on('exit', (code) => {
  console.log(`About to exit with code: ${code}`);
  exit();
});

async function main () {
  try {
    const guard = await Guard.fromString(
      [await Validators.ValidLength(1, 10, 'fix')],
      {
        description: 'A word.',
        prompt: 'Generate a single word with a length betwen 1 and 10.'
      }
    );

    const firstResponse = await guard.parse('Hello World!');
    console.log("first response: ", JSON.stringify(firstResponse, null, 2));
    assert.equal(firstResponse.validationPassed, true);
    assert.equal(firstResponse.validatedOutput, 'Hello Worl');
    assert.equal(guard.history.at(0).status, 'pass');
    
    const secondResponse = await guard.parse('Hello World 2!');
    console.log("second response: ", JSON.stringify(secondResponse, null, 2));
    assert.equal(secondResponse.validationPassed, true);
    assert.equal(secondResponse.validatedOutput, 'Hello Worl');
    assert.equal(guard.history.at(1).status, 'pass');

    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}
await main();

We run this with the following command:

node e2e.test.js