UNPKG

llama-ocr

Version:

Image to markdown (OCR) with Llama 3.2 Vision.

58 lines (55 loc) 2.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ocr = ocr; const together_ai_1 = __importDefault(require("together-ai")); const fs_1 = __importDefault(require("fs")); async function ocr({ filePath, apiKey = process.env.TOGETHER_API_KEY, model = "Llama-3.2-90B-Vision", }) { const visionLLM = model === "free" ? "meta-llama/Llama-Vision-Free" : `meta-llama/${model}-Instruct-Turbo`; const together = new together_ai_1.default({ apiKey, }); let finalMarkdown = await getMarkDown({ together, visionLLM, filePath }); return finalMarkdown; } async function getMarkDown({ together, visionLLM, filePath, }) { const systemPrompt = `Convert the provided image into Markdown format. Ensure that all content from the page is included, such as headers, footers, subtexts, images (with alt text if possible), tables, and any other elements. Requirements: - Output Only Markdown: Return solely the Markdown content without any additional explanations or comments. - No Delimiters: Do not use code fences or delimiters like \`\`\`markdown. - Complete Content: Do not omit any part of the page, including headers, footers, and subtext. `; const finalImageUrl = isRemoteFile(filePath) ? filePath : `data:image/jpeg;base64,${encodeImage(filePath)}`; const output = await together.chat.completions.create({ model: visionLLM, messages: [ { role: "user", // @ts-expect-error content: [ { type: "text", text: systemPrompt }, { type: "image_url", image_url: { url: finalImageUrl, }, }, ], }, ], }); return output.choices[0].message.content; } function encodeImage(imagePath) { const imageFile = fs_1.default.readFileSync(imagePath); return Buffer.from(imageFile).toString("base64"); } function isRemoteFile(filePath) { return filePath.startsWith("http://") || filePath.startsWith("https://"); }