UNPKG

@genkit-ai/compat-oai

Version:

Genkit AI framework plugin for OpenAI APIs.

112 lines 3.89 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var whisper_exports = {}; __export(whisper_exports, { SUPPORTED_WHISPER_MODELS: () => SUPPORTED_WHISPER_MODELS, WHISPER_MODEL_INFO: () => WHISPER_MODEL_INFO, WhisperConfigSchema: () => WhisperConfigSchema, defineOpenAIWhisperModel: () => defineOpenAIWhisperModel, openAIWhisperModelRef: () => openAIWhisperModelRef }); module.exports = __toCommonJS(whisper_exports); var import_genkit = require("genkit"); var import_plugin = require("genkit/plugin"); var import_audio = require("../audio.js"); var import_translate = require("../translate.js"); var import_utils = require("../utils.js"); const WHISPER_MODEL_INFO = { supports: { media: true, output: ["text", "json"], multiturn: false, systemRole: false, tools: false } }; const WhisperConfigSchema = import_audio.TranscriptionConfigSchema.extend({ /** When true, uses Translation API instead of Transcription. Default: false */ translate: import_genkit.z.boolean().optional().default(false) }); function defineOpenAIWhisperModel(params) { const { name, client: defaultClient, pluginOptions, modelRef: modelRef2 } = params; const modelName = (0, import_utils.toModelName)(name, pluginOptions?.name); const actionName = modelRef2?.name ?? `${pluginOptions?.name ?? "openai"}/${modelName}`; return (0, import_plugin.model)( { name: actionName, ...modelRef2?.info, configSchema: modelRef2?.configSchema }, async (request, { abortSignal }) => { const { translate, ...cleanConfig } = request.config ?? {}; const cleanRequest = { ...request, config: cleanConfig }; const client = (0, import_utils.maybeCreateRequestScopedOpenAIClient)( pluginOptions, request, defaultClient ); if (translate === true) { const params2 = (0, import_translate.toTranslationRequest)(modelName, cleanRequest); const result = await client.audio.translations.create(params2, { signal: abortSignal }); return (0, import_translate.translationToGenerateResponse)(result); } else { const params2 = (0, import_audio.toSttRequest)(modelName, cleanRequest); const result = await client.audio.transcriptions.create( { ...params2, stream: false }, { signal: abortSignal } ); return (0, import_audio.transcriptionToGenerateResponse)(result); } } ); } function openAIWhisperModelRef(params) { const { name, info = WHISPER_MODEL_INFO, configSchema, config = void 0 } = params; return (0, import_genkit.modelRef)({ name, configSchema: configSchema || WhisperConfigSchema, info, config, namespace: "openai" }); } const SUPPORTED_WHISPER_MODELS = { "whisper-1": openAIWhisperModelRef({ name: "whisper-1" }) }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SUPPORTED_WHISPER_MODELS, WHISPER_MODEL_INFO, WhisperConfigSchema, defineOpenAIWhisperModel, openAIWhisperModelRef }); //# sourceMappingURL=whisper.js.map