Skip to content

Docusaurus #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 71 additions & 97 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,100 +1,74 @@
{
"env": {
"es2022": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
"env": {
"es2022": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 13,
"project": ["tsconfig.json"]
},
"plugins": [
"@typescript-eslint",
"eslint-plugin-tsdoc",
"unused-imports",
"import"
],
"rules": {
"comma-dangle": ["error", "never"],
"eol-last": ["error", "never"],
"func-style": ["error", "declaration"],
"indent": [
"error",
2,
{
"SwitchCase": 1
}
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 13,
"project": ["tsconfig.json"]
},
"plugins": [
"@typescript-eslint",
"eslint-plugin-tsdoc",
"unused-imports",
"import"
"object-curly-spacing": ["error", "always"],
"object-property-newline": [
"error",
{
"allowAllPropertiesOnSameLine": true
}
],
"rules": {
"comma-dangle": [
"error",
"never"
],
"eol-last": [
"error",
"never"
],
"func-style": [
"error",
"declaration"
],
"indent": [
"error",
2,
{
"SwitchCase": 1
}
],
"object-curly-spacing": [
"error",
"always"
],
"object-property-newline": [
"error",
{
"allowAllPropertiesOnSameLine": true
}
],
"prefer-arrow-callback": [
"error"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"space-before-function-paren": [
"error",
"always"
],
"arrow-parens": [
"error",
"as-needed",
{
"requireForBlockBody": true
}
],
"tsdoc/syntax": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/ban-ts-comment": "warn",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"error",
{
"vars": "all",
"varsIgnorePattern": "^_",
"args": "after-used",
"argsIgnorePattern": "^_"
}
],
"import/no-cycle": "error",
"import/order": "error",
"no-shadow": "error",
"no-trailing-spaces": "error",
"brace-style": [
"error",
"1tbs",
{
"allowSingleLine": true
}
]
}
}
"prefer-arrow-callback": ["error"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"space-before-function-paren": ["error", "always"],
"arrow-parens": [
"error",
"as-needed",
{
"requireForBlockBody": true
}
],
"tsdoc/syntax": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/ban-ts-comment": "warn",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"error",
{
"vars": "all",
"varsIgnorePattern": "^_",
"args": "after-used",
"argsIgnorePattern": "^_"
}
],
"import/no-cycle": "error",
"import/order": "error",
"no-shadow": "error",
"no-trailing-spaces": "error",
"brace-style": [
"error",
"1tbs",
{
"allowSingleLine": true
}
]
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.venv
node_modules
dist
*.tgz
coverage
coverage
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# guardrails-js

A Javascript wrapper for guardrails-ai.

This library contains limited support for using [guardrails-ai](https://pypi.org/project/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

- 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`
1. No support for custom validators
1. 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)
Expand All @@ -21,67 +24,66 @@ In addition to above, this library also supports the readonly properties on the
See the JS docs [here](/docs/modules.md)

## Installation

```sh
npm i @guardrails-ai/core
```

## Example

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

const guard = await Guard.fromRail("./my-railspec.rail");

const guard = await Guard.fromRail('./my-railspec.rail');

const messages = ['Hello World!', 'Goodbye World!'];
const messages = ["Hello World!", "Goodbye World!"];

const response = await guard.parse(
'Hello World!',
{
promptParams: { 'messages': messages }
}
);
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.
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:

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

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

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

const firstResponse = await guard.parse('Hello World!');
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!');
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');
assert.equal(secondResponse.validatedOutput, "Hello Worl");
assert.equal(guard.history.at(1).status, "pass");

process.exit(0);
} catch (error) {
Expand All @@ -93,6 +95,7 @@ await main();
```

We run this with the following command:

```sh
node e2e.test.js
```
```
Loading