UNPKG

langcode

Version:

A Plugin-Based Framework for Managing and Using LangChain

67 lines (66 loc) 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("../../types"); const pyinterpreter_1 = require("@langchain/community/experimental/tools/pyinterpreter"); const pyodide_1 = require("pyodide"); class PythonExecutorPlugin { constructor() { this.name = "pythonExecutor"; this.description = "Python kodlarını LangChain'in PythonInterpreterTool aracıyla çalıştıran plugin."; this.type = types_1.PluginType.Tool; this.RunConfigExample = { code: "", packages: [""], micropipPackages: [""], }; this.InitConfigExample = {}; this.pythonInterpreter = null; this.pyodideInstance = null; } expose() { return { name: this.name, description: this.description, type: this.type, InitConfigExample: this.InitConfigExample, RunConfigExample: this.RunConfigExample, pyodideInstance: this.pyodideInstance, pythonInterpreter: this.pythonInterpreter, }; } async init(config) { const pyodideInstance = await (0, pyodide_1.loadPyodide)(); this.pyodideInstance = pyodideInstance; this.pythonInterpreter = new pyinterpreter_1.PythonInterpreterTool({ instance: pyodideInstance, }); } async run(args) { if (!this.pythonInterpreter) throw new Error("PythonExecutorPlugin başlatılmadı."); const { code, packages, micropipPackages } = args; try { // Pyodide resmi paketleri yükle if (packages === null || packages === void 0 ? void 0 : packages.length) { for (const pkg of packages) { await this.pythonInterpreter.addPackage(pkg); } } // micropip paketlerini yükle (PyPI'den saf python paketleri) if (micropipPackages === null || micropipPackages === void 0 ? void 0 : micropipPackages.length) { await this.pythonInterpreter.addPackage("micropip"); const micropip = this.pythonInterpreter.pyodideInstance.pyimport("micropip"); for (const pkg of micropipPackages) { await micropip.install(pkg); } } // Python kodunu çalıştır const result = await this.pythonInterpreter.invoke(code); return { result }; } catch (error) { return { error: error.message || error }; } } } exports.default = PythonExecutorPlugin;