UNPKG

langcode

Version:

A Plugin-Based Framework for Managing and Using LangChain

62 lines (61 loc) 2.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const openai_1 = require("@langchain/openai"); const promises_1 = __importDefault(require("fs/promises")); const path_1 = __importDefault(require("path")); const axios_1 = __importDefault(require("axios")); const types_1 = require("../../types"); class DallePlugin { constructor() { this.name = "dalle"; this.description = "OpenAI DALL·E API ile görsel üretir."; this.type = types_1.PluginType.Tool; this.dalle = null; this.RunConfigExample = { prompt: "", outputPath: "" }; this.InitConfigExample = { openAIApiKey: "sk-...", modelName: "dall-e-3", size: "1024x1024", n: 1 }; } expose() { return { name: this.name, description: this.description, type: this.type, InitConfigExample: this.InitConfigExample, RunConfigExample: this.RunConfigExample, dalle: this.dalle }; } async init(config) { this.dalle = new openai_1.DallEAPIWrapper({ openAIApiKey: config.openAIApiKey, modelName: config.modelName || "dall-e-3", size: config.size || "1024x1024", n: config.n || 1, }); await this.dalle.invoke("ping"); } async run(args) { if (!this.dalle) throw new Error("Plugin başlatılmadı"); const { prompt, outputPath } = args; const imageUrl = await this.dalle.invoke(prompt); if (outputPath) { const response = await axios_1.default.get(imageUrl, { responseType: "arraybuffer" }); const dir = path_1.default.dirname(outputPath); await promises_1.default.mkdir(dir, { recursive: true }); await promises_1.default.writeFile(outputPath, response.data); } return imageUrl; } } exports.default = DallePlugin;