validai
Version:
AI-powered text validation for slang and gibberish detection
97 lines (96 loc) • 4.19 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidAI = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const gemini_1 = require("./providers/gemini");
dotenv_1.default.config();
/**
* Main class for ValidAI text validation
*/
class ValidAI {
/**
* Creates a new ValidAI instance
* @param apiKey - API key for the chosen provider
* @param provider - AI provider to use (currently only supports "gemini")
* @param model - Model name for the chosen provider
*/
constructor(apiKey, provider, model) {
if (provider !== "gemini") {
throw new Error("Currently only Gemini provider is supported");
}
this.provider = new gemini_1.GeminiProvider(apiKey, model);
}
/**
* Validates text input for slang and gibberish content
* @param input - Record of field names to text content
* @returns Promise containing validation results and overall success status
* @throws Error if any input field is not a string
*/
validate(input) {
return __awaiter(this, void 0, void 0, function* () {
const result = {};
const textsToValidate = {};
// Validate input types
for (const key in input) {
if (typeof input[key] !== "string") {
throw new Error(`The field ${key} is not a string input, input should contain only string values`);
}
textsToValidate[key] = input[key];
}
try {
// Validate texts using the provider
const validationResults = yield this.provider.validate(textsToValidate);
const success = !Object.values(validationResults).some(result => result.slang || result.gibbrish);
return {
success,
results: validationResults
};
}
catch (error) {
console.error(`Error validating inputs: ${error}`);
// Return error result for all fields
for (const key in textsToValidate) {
result[key] = {
slang: false,
slangReason: "Error validating the text",
gibbrish: false,
gibbrishReason: "Error validating the text"
};
}
return {
success: false,
results: result
};
}
});
}
}
exports.ValidAI = ValidAI;
// Export types for consumers
__exportStar(require("./types"), exports);