validai
Version:
AI-powered text validation for slang and gibberish detection
61 lines (58 loc) • 2.61 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeminiProvider = void 0;
const generative_ai_1 = require("@google/generative-ai");
/**
* Class handling text validation using Google's Gemini AI
*/
class GeminiProvider {
/**
* Creates a new GeminiProvider instance
* @param apiKey - Google API key
* @param model - Gemini model name
*/
constructor(apiKey, model) {
this.genAI = new generative_ai_1.GoogleGenerativeAI(apiKey);
this.model = model;
}
/**
* Validates text content using Gemini AI
* @param texts - Record of text fields to validate
* @returns Promise containing validation results for each field
*/
validate(texts) {
return __awaiter(this, void 0, void 0, function* () {
const model = this.genAI.getGenerativeModel({ model: this.model });
const prompt = `
Analyze the following texts and determine for each:
1. if the text contains any offensive words in any language if there are offensive words then reponse this words are restricted and provide the reason for the restriction
2. if the text contains any gibberish words or spam words or repeated words
For each text field, provide output in JSON format as follows:
{
"fieldName": {
"Restricted": boolean,
"RestrictedReason": string,
"gibbrish": boolean,
"gibbrishReason": string
}
}
Input texts:
${JSON.stringify(texts, null, 2)}
`;
const response = yield model.generateContent(prompt);
const result = yield response.response.text();
const cleanedResult = result.replace(/```json|```/g, "").trim();
return JSON.parse(cleanedResult);
});
}
}
exports.GeminiProvider = GeminiProvider;