langcode
Version:
A Plugin-Based Framework for Managing and Using LangChain
78 lines (77 loc) • 2.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../../types");
const axios_1 = __importDefault(require("axios"));
const promises_1 = __importDefault(require("fs/promises"));
const form_data_1 = __importDefault(require("form-data"));
class SpeechToTextPlugin {
constructor() {
this.name = "speechToText";
this.description = "Bir ses dosyasını OpenAI Whisper API kullanarak metne çeviren plugin.";
this.type = types_1.PluginType.Tool;
this.RunConfigExample = {
audioFilePath: "",
prompt: ""
};
this.InitConfigExample = {
openAIApiKey: "sk-...",
openAIModel: "whisper-1",
};
this.apiKey = null;
this.model = "whisper-1";
}
expose() {
return {
name: this.name,
description: this.description,
type: this.type,
InitConfigExample: this.InitConfigExample,
RunConfigExample: this.RunConfigExample,
apiKey: this.apiKey,
model: this.model
};
}
async init(config) {
if (!config.openAIApiKey) {
throw new Error("openAIApiKey is required for SpeechToTextPlugin.");
}
this.apiKey = config.openAIApiKey;
if (config.openAIModel) {
this.model = config.openAIModel;
}
}
async run(args) {
if (!this.apiKey) {
throw new Error("Plugin has not been initialized with an OpenAI API key.");
}
const { audioFilePath, prompt } = args;
if (!audioFilePath) {
throw new Error("'audioFilePath' parametresi zorunludur.");
}
// 1) Ses dosyasını oku
const fileData = await promises_1.default.readFile(audioFilePath);
// 2) OpenAI Audio Transcription API’ine isteği hazırlamak için form-data kullan
const formData = new form_data_1.default();
formData.append("model", this.model);
formData.append("file", fileData, "audio.wav");
if (prompt) {
// Modelin transkripsiyonu iyileştirmesi için bir ön prompt geçebilirsiniz
formData.append("prompt", prompt);
}
try {
// 3) API’ye isteği gönder
const response = await axios_1.default.post("https://api.openai.com/v1/audio/transcriptions", formData, {
headers: Object.assign(Object.assign({}, formData.getHeaders()), { Authorization: `Bearer ${this.apiKey}` }),
});
// 4) Cevabı döndür (örneğin { text: "..." } içerir)
return response.data;
}
catch (err) {
throw new Error(`OpenAI Whisper isteği başarısız: ${err.message}`);
}
}
}
exports.default = SpeechToTextPlugin;