@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
172 lines • 5.25 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 translate_exports = {};
__export(translate_exports, {
TRANSLATION_MODEL_INFO: () => TRANSLATION_MODEL_INFO,
TranslationConfigSchema: () => TranslationConfigSchema,
compatOaiTranslationModelRef: () => compatOaiTranslationModelRef,
defineCompatOpenAITranslationModel: () => defineCompatOpenAITranslationModel,
toTranslationRequest: () => toTranslationRequest,
translationToGenerateResponse: () => translationToGenerateResponse
});
module.exports = __toCommonJS(translate_exports);
var import_genkit = require("genkit");
var import_plugin = require("genkit/plugin");
var import_utils = require("./utils.js");
const TRANSLATION_MODEL_INFO = {
supports: {
media: true,
output: ["text", "json"],
multiturn: false,
systemRole: false,
tools: false
}
};
const TranslationConfigSchema = import_genkit.GenerationCommonConfigSchema.pick({
temperature: true
}).extend({
response_format: import_genkit.z.enum(["json", "text", "srt", "verbose_json", "vtt"]).optional()
});
function toTranslationRequest(modelName, request, requestBuilder) {
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 {
temperature,
version: modelVersion,
maxOutputTokens,
stopSequences,
topK,
topP,
...restOfConfig
} = request.config ?? {};
let options = {
model: modelVersion ?? modelName,
file: mediaFile,
prompt: message.text,
temperature
};
if (requestBuilder) {
requestBuilder(request, options);
} else {
options = {
...options,
...restOfConfig
// passthrough rest of the config
};
}
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 translationToGenerateResponse(result) {
return {
message: {
role: "model",
content: [
{
text: typeof result === "string" ? result : result.text
}
]
},
finishReason: "stop",
raw: result
};
}
function defineCompatOpenAITranslationModel(params) {
const {
name,
client: defaultClient,
pluginOptions,
modelRef: modelRef2,
requestBuilder
} = params;
const modelName = (0, import_utils.toModelName)(name, pluginOptions?.name);
const actionName = `${pluginOptions?.name ?? "compat-oai"}/${modelName}`;
return (0, import_plugin.model)(
{
name: actionName,
...modelRef2?.info,
configSchema: modelRef2?.configSchema
},
async (request, { abortSignal }) => {
const params2 = toTranslationRequest(modelName, request, requestBuilder);
const client = (0, import_utils.maybeCreateRequestScopedOpenAIClient)(
pluginOptions,
request,
defaultClient
);
const result = await client.audio.translations.create(params2, {
signal: abortSignal
});
return translationToGenerateResponse(result);
}
);
}
function compatOaiTranslationModelRef(params) {
const {
name,
info = TRANSLATION_MODEL_INFO,
configSchema,
config = void 0,
namespace
} = params;
return (0, import_genkit.modelRef)({
name,
configSchema: configSchema || TranslationConfigSchema,
info,
config,
namespace
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TRANSLATION_MODEL_INFO,
TranslationConfigSchema,
compatOaiTranslationModelRef,
defineCompatOpenAITranslationModel,
toTranslationRequest,
translationToGenerateResponse
});
//# sourceMappingURL=translate.js.map