genkitx-azure-openai
Version:
Genkit AI framework plugin for Azure OpenAI APIs.
142 lines • 4.28 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 tts_exports = {};
__export(tts_exports, {
RESPONSE_FORMAT_MEDIA_TYPES: () => RESPONSE_FORMAT_MEDIA_TYPES,
SUPPORTED_TTS_MODELS: () => SUPPORTED_TTS_MODELS,
TTSConfigSchema: () => TTSConfigSchema,
tts1: () => tts1,
tts1Hd: () => tts1Hd,
ttsModel: () => ttsModel
});
module.exports = __toCommonJS(tts_exports);
var import_genkit = require("genkit");
var import_model = require("genkit/model");
const TTSConfigSchema = import_genkit.GenerationCommonConfigSchema.extend({
voice: import_genkit.z.enum(["alloy", "echo", "fable", "onyx", "nova", "shimmer"]).optional().default("alloy"),
speed: import_genkit.z.number().min(0.25).max(4).optional(),
response_format: import_genkit.z.enum(["mp3", "opus", "aac", "flac", "wav", "pcm"]).optional()
});
const tts1 = (0, import_model.modelRef)({
name: "azure-openai/tts-1",
info: {
label: "OpenAI - Text-to-speech 1",
supports: {
media: false,
output: ["media"],
multiturn: false,
systemRole: false,
tools: false
}
},
configSchema: TTSConfigSchema
});
const tts1Hd = (0, import_model.modelRef)({
name: "azure-openai/tts-1-hd",
info: {
label: "OpenAI - Text-to-speech 1 HD",
supports: {
media: false,
output: ["media"],
multiturn: false,
systemRole: false,
tools: false
}
},
configSchema: TTSConfigSchema
});
const SUPPORTED_TTS_MODELS = {
"tts-1": tts1,
"tts-1-hd": tts1Hd
};
const RESPONSE_FORMAT_MEDIA_TYPES = {
mp3: "audio/mpeg",
opus: "audio/opus",
aac: "audio/aac",
flac: "audio/flac",
wav: "audio/wav",
pcm: "audio/L16"
};
function toTTSRequest(modelName, request) {
const mappedModelName = request.config?.version || modelName;
const options = {
model: mappedModelName,
input: new import_genkit.Message(request.messages[0]).text,
voice: request.config?.voice ?? "alloy",
speed: request.config?.speed,
response_format: request.config?.response_format
};
for (const k in options) {
if (options[k] === void 0) {
delete options[k];
}
}
return options;
}
function toGenerateResponse(result, responseFormat = "mp3") {
const mediaType = RESPONSE_FORMAT_MEDIA_TYPES[responseFormat];
return {
candidates: [
{
index: 0,
finishReason: "stop",
message: {
role: "model",
content: [
{
media: {
contentType: mediaType,
url: `data:${mediaType};base64,${result.toString("base64")}`
}
}
]
}
}
]
};
}
function ttsModel(ai, name, client) {
const modelId = `azure-openai/${name}`;
const model = SUPPORTED_TTS_MODELS[name];
if (!model) throw new Error(`Unsupported model: ${name}`);
return ai.defineModel(
{
name: modelId,
...model.info,
configSchema: model.configSchema
},
async (request) => {
const ttsRequest = toTTSRequest(name, request);
const result = await client.audio.speech.create(ttsRequest);
const resultArrayBuffer = await result.arrayBuffer();
const resultBuffer = Buffer.from(new Uint8Array(resultArrayBuffer));
return toGenerateResponse(resultBuffer, ttsRequest.response_format);
}
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RESPONSE_FORMAT_MEDIA_TYPES,
SUPPORTED_TTS_MODELS,
TTSConfigSchema,
tts1,
tts1Hd,
ttsModel
});
//# sourceMappingURL=tts.js.map