Azure-Samples / document-intelligence-code-samples

Sample site for Document Intelligence code samples and associated media.
MIT License
86 stars 48 forks source link

DocumentIntelligenceClient is not a function #31

Open Danscap opened 6 months ago

Danscap commented 6 months ago

Edit 5/24/2024 5:50 PM I have tried running the example from the legacy 3.1 documents and do not run into this issue. It seems this is only an issue with the v4.0 documentation.

Output is `Pages:

Original I have tried running the example from the v 4.0 documentation and received this error. I am running Node 16.13.0 on Windows 10.

I'm not sure what I am doing wrong or if the example has faulty code. Any guidance or resolution would be helpful. Thank you.

Please provide us with the following information:

This issue is for a: (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request
- [x] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Minimal steps to reproduce

Copy and run the prebuilt model example from the javascript documentation

Any log messages given by the failure

An error occurred: TypeError: DocumentIntelligenceClient is not a function

Expected/desired behavior

Successfully analyze the sample document

OS and Version?

Windows 7, 8 or 10. Linux (which distribution). macOS (Yosemite? El Capitan? Sierra?) Windows 10

Versions

Mention any other details that might be useful


Thanks! We'll be in touch soon.

HarshaNalluru commented 6 months ago

Here is an example that works.

const DocumentIntelligence = require("@azure-rest/ai-document-intelligence").default,
    { getLongRunningPoller, isUnexpected } = require("@azure-rest/ai-document-intelligence");
const { AzureKeyCredential } = require("@azure/core-auth");

const { config } = require("dotenv")
config()

// sample document
const invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
const key = process.env.KEY;
const endpoint = process.env.ENDPOINT;

async function main() {

    const client = DocumentIntelligence(endpoint, new AzureKeyCredential(key));

    const initialResponse = await client
        .path("/documentModels/{modelId}:analyze", "prebuilt-invoice")
        .post({
            contentType: "application/json",
            body: {
                // The Document Intelligence service will access the URL to the invoice image and extract data from it
                urlSource: invoiceUrl,
            },
        });

    if (isUnexpected(initialResponse)) {
        throw initialResponse.body.error;
    }

    const poller = await getLongRunningPoller(client, initialResponse);

    poller.onProgress((state) => console.log("Operation:", state.result, state.status));
    const analyzeResult = (await poller.pollUntilDone()).body.analyzeResult;

    const documents = analyzeResult?.documents;

    const result = documents && documents[0];
    if (result) {
        console.log(result.fields);
    } else {
        throw new Error("Expected at least one invoice in the result.");
    }

    console.log(
        "Extracted invoice:",
        result.docType,
        `(confidence: ${result.confidence || "<undefined>"})`,
    );
    console.log("Fields:", result.fields);
}

main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

DocumentIntelligence is the default export of the SDK. createClient method can also be imported which returns an object of type DocumentIntelligenceClient. Moreover, you can name it however you want while importing a default export and it will work exactly the same. For instance,

const DocumentIntelligenceClient = require("@azure-rest/ai-document-intelligence").default,
        { getLongRunningPoller, isUnexpected } = require("@azure-rest/ai-document-intelligence");