UNPKG

okesa

Version:

Okesa: LLM-powered Natural Language Processing đŸ’Ŧ

366 lines (365 loc) â€ĸ 15.4 kB
"use strict"; /** * Okesa NLP Engine: A simple and easy-to-use Natural Language Processing (NLP) library powered by OpenAI. * @file index.ts * @description Okesa NLP Engine is a simple and easy-to-use Natural Language Processing (NLP) library powered by OpenAI. This library provides a set of functions to perform various NLP tasks such as POS tagging, NER, Sentiment Analysis, Language Detection, Text Classification, Text Summarization, Keyword Extraction, Spell Checking, Stemming and Lemmatization, and Dependency Parsing. * @license MIT * @copyright Š 2025 Aditya Patange * @module okesa * âšĄī¸ "What is my data, is not for you to see." - AdiPat */ 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.dependencyParsing = exports.stemmingLemmatization = exports.spellChecking = exports.keywordExtraction = exports.textSummarization = exports.textClassification = exports.languageDetection = exports.sentimentAnalysis = exports.ner = exports.tokenize = exports.posTagging = exports.init = void 0; const openai_1 = require("@ai-sdk/openai"); const ai_1 = require("./ai"); const zod_1 = require("zod"); const chalk_1 = __importDefault(require("chalk")); const constants_1 = require("./constants"); const dotenv_1 = __importDefault(require("dotenv")); const prompts_1 = require("./prompts"); let initialized = false; /** * Log messages with different modes using chalk. * @param message The message to log. * @param mode The mode of the log (debug, info, error). */ function log(message, mode = "info") { const modes = { debug: chalk_1.default.blueBright, info: chalk_1.default.greenBright, error: chalk_1.default.redBright, }; const emojis = { debug: "🐛", info: "â„šī¸", error: "❌", }; console.log(modes[mode](`${emojis[mode]} ${message}`)); } /** * * Initialize the Okesa NLP module. * This function checks if the OpenAI API key is set and valid. * @param void * @returns void * */ function init() { return __awaiter(this, arguments, void 0, function* (silent = true) { try { if (!silent) { log("🚀 Initializing Okesa NLP Engine...", "info"); } if (!silent) { log("🔑 Checking for the OpenAI API key in the environment variables...", "info"); } dotenv_1.default.config({ path: ".env", override: true }); const apiKey = process.env.OPENAI_API_KEY; const pingTestMaxTokens = 48; if (!apiKey) { log("Please set the OPENAI_API_KEY environment variable before running the program.", "error"); return false; } else { if (!silent) { log("✅ OpenAI API key found. Running ping test now...", "info"); } } // ping test to check if the API key is valid yield ai_1.AI.generateText({ prompt: "What is the meaning of life?", model: (0, openai_1.openai)("gpt-4o"), maxTokens: pingTestMaxTokens, }); if (!silent) { log("✅ OpenAI API key is valid. Okesa NLP Engine is ready to use.", "info"); } return true; } catch (error) { log(`Unexpected error in init function: ${error === null || error === void 0 ? void 0 : error.message}.`, "error"); console.error(error); return false; } }); } exports.init = init; /** * Internal function to handle initialization and call the generateObject method. * @param options The options object containing text and modelName. * @param prompt The prompt to be used for the OpenAI API. * @param system The system message for the OpenAI API. * @param schema The schema for the expected response. * @param schemaDescription The description of the schema. * @returns The result of the generateObject method. */ function generateObjectInternal(options_1, promptKey_1, schema_1, schemaDescription_1) { return __awaiter(this, arguments, void 0, function* (options, promptKey, schema, schemaDescription, system = prompts_1.PROMPTS.SYSTEM()) { try { if (!Object.keys(prompts_1.PROMPTS).includes(promptKey)) { log(`Invalid prompt key: ${promptKey}.`, "error"); return null; } if (!initialized) { const success = yield init(options.silentStart); if (!success) return null; initialized = true; } const { text, modelName = constants_1.DEFAULT_MODEL_NAME } = options; const prompt = prompts_1.PROMPTS[promptKey](text); try { const model = (0, openai_1.openai)(modelName); const config = { prompt, system, schema, schemaDescription, model, }; const { object } = yield ai_1.AI.generateObject(config); return object; } catch (error) { log(`Unexpected error: ${error === null || error === void 0 ? void 0 : error.message}.`, "error"); console.error(error); return null; } } catch (error) { log(`Unexpected error in generateObjectInternal function: ${error === null || error === void 0 ? void 0 : error.message}.`, "error"); console.error(error); return null; } }); } /** * Perform POS tagging on the given text. * @param options The options object containing text and modelName. * @returns The POS tagging result. */ function posTagging(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "POS_TAGGING"; const schema = zod_1.z.object({ result: zod_1.z.array(zod_1.z.object({ word: zod_1.z.string(), pos: zod_1.z.string(), })), }); const schemaDescription = `POS tagging Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || []; }); } exports.posTagging = posTagging; /** * Tokenize the given text. * @param options The options object containing text and modelName. * @returns The tokenization result. */ function tokenize(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "TOKENIZATION"; const schema = zod_1.z.object({ result: zod_1.z.object({ words: zod_1.z.string(), sentences: zod_1.z.string(), phrases: zod_1.z.string(), whitespace: zod_1.z.string(), symbols: zod_1.z.string(), }), }); const schemaDescription = `Tokenization Results Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.tokenize = tokenize; /** * Perform Named Entity Recognition (NER) on the given text. * @param options The options object containing text and modelName. * @returns The NER result. */ function ner(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "NER"; const schema = zod_1.z.object({ result: zod_1.z.array(zod_1.z.object({ entity: zod_1.z.string(), label: zod_1.z.string(), start: zod_1.z.number(), end: zod_1.z.number(), })), }); const schemaDescription = `NER Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || []; }); } exports.ner = ner; /** * Perform Sentiment Analysis on the given text. * @param options The options object containing text and modelName. * @returns The sentiment analysis result. */ function sentimentAnalysis(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "SENTIMENT_ANALYSIS"; const schema = zod_1.z.object({ result: zod_1.z.object({ sentiment: zod_1.z.enum(["positive", "negative", "neutral"]), confidence: zod_1.z.number().min(0).max(1), }), }); const schemaDescription = `Sentiment Analysis Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.sentimentAnalysis = sentimentAnalysis; /** * Detect the language of the given text. * @param options The options object containing text and modelName. * @returns The language detection result. */ function languageDetection(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "LANGUAGE_DETECTION"; const schema = zod_1.z.object({ result: zod_1.z.object({ language: zod_1.z.string(), confidence: zod_1.z.number().min(0).max(1), }), }); const schemaDescription = `Language Detection Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.languageDetection = languageDetection; /** * Classify the given text into predefined classes. * @param options The options object containing text and modelName. * @returns The text classification result. */ function textClassification(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "TEXT_CLASSIFICATION"; const schema = zod_1.z.object({ result: zod_1.z.object({ class: zod_1.z.string(), confidence: zod_1.z.number().min(0).max(1), }), }); const schemaDescription = `Text Classification Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.textClassification = textClassification; /** * Generate a concise summary of the given text. * @param options The options object containing text and modelName. * @returns The text summarization result. */ function textSummarization(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "TEXT_SUMMARIZATION"; const schema = zod_1.z.object({ result: zod_1.z.object({ summary: zod_1.z.string(), }), }); const schemaDescription = `Text Summarization Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.textSummarization = textSummarization; /** * Extract keywords from the given text. * @param options The options object containing text and modelName. * @returns The keyword extraction result. */ function keywordExtraction(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "KEYWORD_EXTRACTION"; const schema = zod_1.z.object({ result: zod_1.z.array(zod_1.z.string()), }); const schemaDescription = `Keyword Extraction Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.keywordExtraction = keywordExtraction; /** * Perform spell checking on the given text. * @param options The options object containing text and modelName. * @returns The spell checking result. */ function spellChecking(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "SPELL_CHECKING"; const schema = zod_1.z.object({ result: zod_1.z.object({ corrected_text: zod_1.z.string(), }), }); const schemaDescription = `Spell Checking Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.spellChecking = spellChecking; /** * Perform stemming and lemmatization on the given text. * @param options The options object containing text and modelName. * @returns The stemming and lemmatization result. */ function stemmingLemmatization(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "STEMMING_LEMMATIZATION"; const schema = zod_1.z.object({ result: zod_1.z.array(zod_1.z.string()), }); const schemaDescription = `Stemming and Lemmatization Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.stemmingLemmatization = stemmingLemmatization; /** * Perform dependency parsing on the given text. * @param options The options object containing text and modelName. * @returns The dependency parsing result. */ function dependencyParsing(options) { return __awaiter(this, void 0, void 0, function* () { const promptKey = "DEPENDENCY_PARSING"; const schema = zod_1.z.object({ result: zod_1.z.array(zod_1.z.object({ word: zod_1.z.string(), dependency: zod_1.z.string(), head: zod_1.z.number(), })), }); const schemaDescription = `Dependency Parsing Schema`; const result = yield generateObjectInternal(options, promptKey, schema, schemaDescription); return (result === null || result === void 0 ? void 0 : result.result) || null; }); } exports.dependencyParsing = dependencyParsing;