1.0.76 • Published 12 months ago

ai-validator v1.0.76

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

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!

1.0.76

12 months ago

1.0.75

12 months ago

1.0.74

12 months ago

1.0.73

12 months ago

1.0.71

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.56

12 months ago

1.0.55

12 months ago

1.0.51

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago