UNPKG

genkitx-azure-openai

Version:
101 lines 2.83 kB
import { GenerationCommonConfigSchema, Message, z } from "genkit"; import { modelRef } from "genkit/model"; const Whisper1ConfigSchema = GenerationCommonConfigSchema.extend({ language: z.string().optional(), timestamp_granularities: z.array(z.enum(["word", "segment"])).optional(), response_format: z.enum(["json", "text", "srt", "verbose_json", "vtt"]).optional() }); const whisper1 = modelRef({ name: "azure-openai/whisper-1", info: { label: "OpenAI - Whisper", supports: { media: true, output: ["text", "json"], multiturn: false, systemRole: false, tools: false } }, configSchema: Whisper1ConfigSchema }); function toWhisper1Request(request) { const message = new Message(request.messages[0]); const media = message.media; if (!media?.url) { throw new Error("No media found in the request"); } const mediaBuffer = Buffer.from( media.url.slice(media.url.indexOf(",") + 1), "base64" ); const mediaFile = new File([mediaBuffer], "input", { type: media.contentType ?? media.url.slice("data:".length, media.url.indexOf(";")) }); const options = { model: "whisper-1", file: mediaFile, prompt: message.text, temperature: request.config?.temperature, language: request.config?.language, timestamp_granularities: request.config?.timestamp_granularities }; const outputFormat = request.output?.format; const customFormat = request.config?.response_format; if (outputFormat && customFormat) { if (outputFormat === "json" && customFormat !== "json" && customFormat !== "verbose_json") { throw new Error( `Custom response format ${customFormat} is not compatible with output format ${outputFormat}` ); } } if (outputFormat === "media") { throw new Error(`Output format ${outputFormat} is not supported.`); } options.response_format = customFormat || outputFormat || "text"; for (const k in options) { if (options[k] === void 0) { delete options[k]; } } return options; } function toGenerateResponse(result) { return { candidates: [ { index: 0, finishReason: "stop", message: { role: "model", content: [ { text: typeof result === "string" ? result : result.text } ] } } ] }; } function whisper1Model(ai, client) { return ai.defineModel( { name: whisper1.name, ...whisper1.info, configSchema: whisper1.configSchema }, async (request) => { const result = await client.audio.transcriptions.create( toWhisper1Request(request) ); return toGenerateResponse(result); } ); } export { Whisper1ConfigSchema, whisper1, whisper1Model }; //# sourceMappingURL=whisper.mjs.map