directus-extension-ai-sdk-bundle
Version:
A small bundle of Flow Operations which enable interaction with the [OpenAI](https://beta.openai.com/overview) and [Stability](https://stability.ai/) API's.
60 lines (57 loc) • 2.07 kB
JavaScript
import OpenAI from "openai";
import { openAIField } from "../configuration/fields";
import { getSetting } from "../lib/util";
export default {
id: 'whisper-operation',
handler: async (
{ asset_id, api_key, translate=false, prompt=undefined, temperature=0 },
{ services, database, getSchema }
) => {
const { FilesService, SettingsService, AssetsService } = services;
const schema = await getSchema();
const settings = new SettingsService({ schema, knex: database });
const files = new FilesService({ schema, knex: database });
const assets = new AssetsService({ schema, knex: database })
// open api
const apiKey = await getSetting(settings, openAIField.field, api_key);
const openai = new OpenAI({ apiKey });
prompt = prompt === 'undefined' ? undefined : prompt;
// check the file for compatibility
const file = await files.readOne(asset_id);
if (!file) {
throw new Error('file not found');
}
if (file.filesize > 25000000) {
throw new Error('file is more than the 25mb limit!');
}
const allowedMimes = ['audio/mpeg', 'video/mpeg', 'video/mp4', 'audio/x-wav', 'video/webm', 'audio/webm'];
if (!allowedMimes.includes(file.type)) {
throw new Error(`mime "${file.type}" is allowed (${allowedMimes.join(', ')})`);
}
const allowedExtensions = ['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm'];
if (!allowedExtensions.some(ext => file.filename_download?.endsWith(ext))) {
throw new Error(`extension is not one of ${allowedExtensions.join(', ')}`);
}
// transcribe audio
const audio = await assets.getAsset(asset_id, {});
if (translate) {
const response = await openai.audio.translations.create({
file: audio.stream,
model: "whisper-1",
prompt: prompt,
response_format: 'json',
temperature
});
return { response: response.text };
} else {
const response = await openai.audio.transcriptions.create({
file: audio.stream,
model: "whisper-1",
prompt: prompt,
response_format: 'json',
temperature
});
return { response: response.text };
}
},
};