import { post, save, short } from "./lib.ts"; // Minimal one-page PDF with a secret word. function pdf(text: string) { const objs: string[] = []; objs.push("<< /Type /Catalog /Pages 2 0 R >>"); objs.push("<< /Type /Pages /Kids [3 0 R] /Count 1 >>"); objs.push("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 144] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>"); const stream = `BT /F1 18 Tf 20 100 Td (${text}) Tj ET`; objs.push(`<< /Length ${stream.length} >>\nstream\n${stream}\nendstream`); objs.push("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"); let out = "%PDF-1.4\n"; const offsets: number[] = []; objs.forEach((o, i) => { offsets.push(out.length); out += `${i + 1} 0 obj\n${o}\nendobj\n`; }); const xref = out.length; out += `xref\n0 ${objs.length + 1}\n0000000000 65535 f \n`; for (const o of offsets) out += `${String(o).padStart(10, "0")} 00000 n \n`; out += `trailer\n<< /Size ${objs.length + 1} /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF\n`; return Buffer.from(out, "latin1").toString("base64"); } const b64 = pdf("The secret word is pamplemousse."); const results: Record = {}; const ask = (model: string, doc: any) => post("/chat/completions", { model, max_tokens: 60, messages: [{ role: "user", content: [{ type: "text", text: "What is the secret word in this document? Answer with the word only." }, doc] }], }); results.base64Small = await ask("mistral-small-latest", { type: "document_url", document_url: `data:application/pdf;base64,${b64}`, document_name: "secret.pdf" }); console.log("small base64 pdf", results.base64Small.status, JSON.stringify(results.base64Small.body?.usage), short(results.base64Small.body?.choices?.[0]?.message?.content ?? results.base64Small.body, 300)); results.urlSmall = await ask("mistral-small-latest", { type: "document_url", document_url: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" }); console.log("small public pdf url", results.urlSmall.status, JSON.stringify(results.urlSmall.body?.usage), short(results.urlSmall.body?.choices?.[0]?.message?.content ?? results.urlSmall.body, 300)); results.base64Ministral = await ask("ministral-8b-latest", { type: "document_url", document_url: `data:application/pdf;base64,${b64}` }); console.log("ministral-8b base64 pdf", results.base64Ministral.status, JSON.stringify(results.base64Ministral.body?.usage), short(results.base64Ministral.body?.choices?.[0]?.message?.content ?? results.base64Ministral.body, 300)); results.base64Codestral = await ask("codestral-latest", { type: "document_url", document_url: `data:application/pdf;base64,${b64}` }); console.log("codestral base64 pdf", results.base64Codestral.status, short(results.base64Codestral.body?.choices?.[0]?.message?.content ?? results.base64Codestral.body, 300)); results.fileType = await ask("mistral-small-latest", { type: "file", file_id: "00000000-0000-0000-0000-000000000000" }); console.log("file type bogus id", results.fileType.status, short(results.fileType.body, 300)); save("07-document.json", results);