genkitx-mistral
Version:
Firebase Genkit AI framework plugin for Mistral AI APIs.
153 lines • 5.24 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
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 __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
var ocr_exports = {};
__export(ocr_exports, {
OCRModel: () => OCRModel,
ocr: () => ocr
});
module.exports = __toCommonJS(ocr_exports);
var import_genkit = require("genkit");
var import_model = require("genkit/model");
const ImageURLSchema = import_genkit.z.object({
url: import_genkit.z.string(),
detail: import_genkit.z.string().nullable().optional()
});
const OCRSchema = import_genkit.GenerationCommonConfigSchema.extend({
document: import_genkit.z.object({
type: import_genkit.z.enum(["document_url", "image_url"]).optional(),
documentUrl: import_genkit.z.string(),
documentName: import_genkit.z.string().nullable().optional(),
imageUrl: import_genkit.z.union([import_genkit.z.string(), ImageURLSchema]).optional()
})
});
const ocr = (0, import_model.modelRef)({
name: "mistral/ocr",
info: {
versions: ["mistral-ocr-latest"],
label: "Mistral - OCR",
supports: {
media: false,
output: ["text", "json"],
multiturn: false,
systemRole: false,
tools: false
}
},
configSchema: OCRSchema
});
function toOCRRequest(request) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
const docType = ((_b = (_a = request.config) == null ? void 0 : _a.document) == null ? void 0 : _b.type) || "document_url";
const options = {
model: "mistral-ocr-latest",
document: docType === "document_url" ? {
type: "document_url",
documentUrl: ((_d = (_c = request.config) == null ? void 0 : _c.document) == null ? void 0 : _d.documentUrl) || "",
documentName: ((_f = (_e = request.config) == null ? void 0 : _e.document) == null ? void 0 : _f.documentName) || null
} : {
type: "image_url",
imageUrl: typeof ((_h = (_g = request.config) == null ? void 0 : _g.document) == null ? void 0 : _h.imageUrl) === "string" ? request.config.document.imageUrl : ((_k = (_j = (_i = request.config) == null ? void 0 : _i.document) == null ? void 0 : _j.imageUrl) == null ? void 0 : _k.url) || ""
}
};
Object.keys(options).forEach((k) => {
if (options[k] === void 0) {
delete options[k];
}
});
return options;
}
function toGenerateResponse(result) {
const candidates = result.pages.map(
(page, index) => ({
index,
finishReason: "stop",
message: {
role: "model",
content: [
{
text: page.markdown
},
...page.images.map((image) => ({
media: {
contentType: "image/png",
url: `data:image/png;base64,${image.imageBase64}`
}
}))
]
}
})
);
return { candidates };
}
function OCRModel(ai, client) {
return ai.defineModel(
__spreadProps(__spreadValues({
name: ocr.name
}, ocr.info), {
configSchema: ocr.configSchema
}),
(request) => __async(this, null, function* () {
const result = yield client.ocr.process(toOCRRequest(request));
return toGenerateResponse(result);
})
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
OCRModel,
ocr
});
//# sourceMappingURL=ocr.js.map