ai-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.76 • Public • Published

ai-validator

ai-validator is a library that helps extract and validate structured data from the output text of language models.

npm Node.js Package

Description

ai-validator is a powerful and flexible tool that allows you to validate and extract structured data from text generated by a language model. It uses the zod library to create JSON schema. The schema is used to validate the structured data from the generated text and to generate a prompt for the language model. This way you can build dynamic next-generation services.

Installation

To install ai-validator, run the following command:

npm install ai-validator

Usage

See demo - demo

Install dependencies:

npm install openai zod dotenv ai-validator
import { Configuration, OpenAIApi } from 'openai';
import { z } from 'zod';
import { AiValidator } from 'ai-validator';
import * as dotenv from 'dotenv';

Configure the OpenAI API:

dotenv.config();
const configuration = new Configuration({ apiKey: process.env.API_KEY });
const openai = new OpenAIApi(configuration);

Create a schema:

const schema = z.object({
  accountNumber: z.string().describe('Account number'),
  transactions: z
    .array(
      z.object({
        type: z.enum(['debit', 'credit']).describe('Type of transaction'),
        amount: z.number().describe('Amount of transaction'),
        date: z.string().describe('Date of transaction'),
      }),
    )
    .describe('Transactions on the account'),
    balance: z.number().describe('Sum of all transactions (debit negative, credit positive)'),
});

Create a validator:

const bankStatement =
  'There were three transactions on account number "123456789": a debit of $100 on May 1, 2023, a credit of $200 on May 15, 2023, and a debit of $50 on May 22, 2023.';
  
const validator = AiValidator.input`${bankStatement} ${schema}`;

Generate a prompt:

const prompt = validator.prompt();

Generate a completion:

const completion = await openai.createChatCompletion({
  model: 'gpt-3.5-turbo',
  messages: [{ role: 'user', content: prompt }],
});

Parse the completion:

const parsed = validator.parse(completion.data.choices[0].message?.content);
console.log(parsed);

Example

{
  "accountNumber": "123456789",
  "transactions": [
    {
      "type": "debit",
      "amount": 100,
      "date": "2023-05-01"
    },
    {
      "type": "credit",
      "amount": 200,
      "date": "2023-05-15"
    },
    {
      "type": "debit",
      "amount": 50,
      "date": "2023-05-22"
    }
  ],
  "balance": 50
}

Contributing

Contributions are always welcome!

Package Sidebar

Install

npm i ai-validator

Weekly Downloads

6

Version

1.0.76

License

MIT

Unpacked Size

12.3 kB

Total Files

9

Last publish

Collaborators

  • rsaryev