New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@guardrails-ai/core

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@guardrails-ai/core

A Javascript wrapper for guardrails-ai

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
9
-18.18%
Maintainers
2
Weekly downloads
 
Created
Source

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:

  • All methods and properties are in camelCase instead of snake_case
  • No support for custom validators
  • 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)
  • 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

FAQs

Package last updated on 21 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts