UNPKG

genkitx-mistral

Version:

Firebase Genkit AI framework plugin for Mistral AI APIs.

96 lines 2.86 kB
import { __async, __spreadProps, __spreadValues } from "./chunk-MLCSNVBT.mjs"; import { GenerationCommonConfigSchema, z } from "genkit"; import { modelRef } from "genkit/model"; const ImageURLSchema = z.object({ url: z.string(), detail: z.string().nullable().optional() }); const OCRSchema = GenerationCommonConfigSchema.extend({ document: z.object({ type: z.enum(["document_url", "image_url"]).optional(), documentUrl: z.string(), documentName: z.string().nullable().optional(), imageUrl: z.union([z.string(), ImageURLSchema]).optional() }) }); const ocr = 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); }) ); } export { OCRModel, ocr }; //# sourceMappingURL=ocr.mjs.map