Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
/ ai-validator Public archive

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

License

Notifications You must be signed in to change notification settings

rsaryev/ai-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦋 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!

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published