UNPKG

humanifyjs

Version:

> Deobfuscate Javascript code using LLMs ("AI")

34 lines (33 loc) 1.48 kB
import { getLlama, LlamaChatSession, LlamaGrammar } from "node-llama-cpp"; import { getModelPath, getModelWrapper } from "../../local-models.js"; import { verbose } from "../../verbose.js"; const IS_CI = process.env["CI"] === "true"; export async function llama(opts) { var _a; const disableGpu = (_a = opts.disableGpu) !== null && _a !== void 0 ? _a : IS_CI; const llama = await getLlama({ gpu: disableGpu ? false : "auto" }); const modelOpts = { modelPath: getModelPath(opts === null || opts === void 0 ? void 0 : opts.model), gpuLayers: disableGpu ? 0 : undefined }; verbose.log("Loading model with options", modelOpts); const model = await llama.loadModel(modelOpts); const context = await model.createContext({ seed: opts === null || opts === void 0 ? void 0 : opts.seed }); return async (systemPrompt, userPrompt, responseGrammar) => { const session = new LlamaChatSession({ contextSequence: context.getSequence(), autoDisposeSequence: true, systemPrompt, chatWrapper: getModelWrapper(opts.model) }); const response = await session.promptWithMeta(userPrompt, { temperature: 0.8, grammar: new LlamaGrammar(llama, { grammar: `${responseGrammar}` }), stopOnAbortSignal: true }); session.dispose(); return responseGrammar.parseResult(response.responseText); }; }