genkitx-azure-openai
Version:
Genkit AI framework plugin for Azure OpenAI APIs.
127 lines • 4 kB
JavaScript
;
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, {
Whisper1ConfigSchema: () => Whisper1ConfigSchema,
whisper1: () => whisper1,
whisper1Model: () => whisper1Model
});
module.exports = __toCommonJS(whisper_exports);
var import_genkit = require("genkit");
var import_model = require("genkit/model");
const Whisper1ConfigSchema = import_genkit.GenerationCommonConfigSchema.extend({
language: import_genkit.z.string().optional(),
timestamp_granularities: import_genkit.z.array(import_genkit.z.enum(["word", "segment"])).optional(),
response_format: import_genkit.z.enum(["json", "text", "srt", "verbose_json", "vtt"]).optional()
});
const whisper1 = (0, import_model.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 import_genkit.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);
}
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Whisper1ConfigSchema,
whisper1,
whisper1Model
});
//# sourceMappingURL=whisper.js.map