run-llama/LlamaIndexTS

Llamaparse error when parsing docx file

Open

#1,363 opened on Oct 22, 2024

 (12 comments) (0 reactions) (1 assignee)TypeScript (524 forks)auto 404
bughelp wanted

Repository metrics

Stars
 (3,078 stars)
PR merge metrics
 (PR metrics pending)

Description

Llamaparse parsing for docx doesn't work in 0.7.3. This works via the web UI which appears to use the public API. I had hoped 1340 would address this but it has not.

Demonstration code. (Change the file name and the api key env.)

import { LlamaParseReader } from "llamaindex";
import { ParserLanguages } from "@llamaindex/cloud/api/dist";
import fs from "fs";

type LlamaParseReaderParams = Partial<Omit<LlamaParseReader, "language" | "apiKey">>  & {
    language?: ParserLanguages | ParserLanguages[] | undefined;
    apiKey?: string | undefined;
}

async function main() {
    const path = "/tmp/somedoc.docx"
    if (!fs.existsSync(path)) {
        console.error(`File ${path} does not exist`);
        process.exit(1);
    } else {
        console.log(`File ${path} exists`);
    }

    const apiKey = process.env.LLAMAINDEX_KEY;
    const params : LlamaParseReaderParams = { 
        verbose: true,
        parsingInstruction: "Extract the text from the document a long with any images and tables.  This is a document for a course and the contents of the images are important.",
        fastMode: false,
        gpt4oMode: true,
        useVendorMultimodalModel: true,
        vendorMultimodalModelName: "anthropic-sonnet-3.5",
        // vendorMultimodalApiKey?: string | undefined;
        premiumMode: true,
        resultType: "markdown", 
        apiKey: apiKey,
        doNotCache: true,
    };

    // set up the llamaparse reader
    const reader = new LlamaParseReader(params);

    const buffer = fs.readFileSync(path);
    const documents = await reader.loadDataAsContent(new Uint8Array(buffer));

    let allText = "";
    documents.forEach(doc => {
        allText += doc.text;
    });

    console.log(allText);
  } 
  main().catch(console.error);

Contributor guide