> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/language-reference/docai.md).

# DocAI

Collection of DocAI functions. Needs the configuration of several underlying services (see Tenant settings).

### Methods

#### classify

```javascript
classify(document: string, possibleTypes: string[])
```

Method to classify a document/image.

**Parameters**

&#x20;   **document&#x20;*****string***

&#x20;       A base64 document or image you want to classify.

&#x20;   **possibleTypes&#x20;*****string\[]***

&#x20;       The possible types you expect for the document.

**Return type**

&#x20;   { result: { type: string, confidence: number, reasoning: string }, model: string, usage: { promptTokens: number, completionTokens: number, totalTokens: number } }

**Examples**

```javascript
// Classify a PDF document.
DocAI.classify('base64encodedPDFDocument', [
  "appraisal report",
  "passport",
  "salary slip"
]);
```

#### extract

```javascript
extract(document: string, exampleFormat: { [key: string]: any; })
```

Method to extract information from a document/image.

**Parameters**

&#x20;   **document&#x20;*****string***

&#x20;       A base64 document or image from which you want to extract information.

&#x20;   **exampleFormat&#x20;*****{ \[key: string]: any; }***

&#x20;       The format you want the result to have.

**Return type**

&#x20;   { result: { \[key: string]: any; }, model: string, usage: { promptTokens: number, completionTokens: number, totalTokens: number } }

**Examples**

```javascript
// Extract information of a PDF document.
DocAI.extract('base64encodedPDFDocument', {
  address: {
       street: "Dorpsstraat",
       number: "123",
       postcode: "1234 BB",
       city: "The Hague"
   }
});
```

#### extractMarkdown

```javascript
extractMarkdown(document: string, markdownMethod?: 'internal' | 'azure')
```

Converts a document to markdown. Returns a string containing the document's markdown. The document can be a PDF, image or other document type. Images are OCRed before conversion.

**Parameters**

&#x20;   **document&#x20;*****string***

&#x20;       A base64 encoded document or image that you want to convert to markdown.

&#x20;   **markdownMethod&#x20;*****'internal' | 'azure'***

&#x20;       (optional) The method to use for markdown extraction. `internal` uses an internal algorithm for extraction, `azure` uses Azure Document Intelligence to extract the markdown. The internal method may be more accurate for some documents, but the implementation is still experimental and subject to change in future versions.

**Return type**

&#x20;   { result: string }

**Examples**

```javascript
// Convert a document or image to markdown.
DocAI.extractMarkdown('base64encodedDocument');
```

#### ask

```javascript
ask(document: string, prompt: string, options?: { 
        /**
         * The LLM model to use for this DocAI method. The LLM should be up and running in your environment.
         */
        model?: string,
    })
```

Method to ask for information from a document/image. Images are OCRed before querying. This method uses the underlying LLM to answer the question based on the content of the document. It cannot be used to describe images, it only answers based on the text in the document.

**Parameters**

&#x20;   **document&#x20;*****string***

&#x20;       A base64 document or image that you want to query for information.

&#x20;   **prompt&#x20;*****string***

&#x20;       The prompt/question you want to ask about the document.

&#x20;   **options&#x20;*****{ /*****\* \* The LLM model to use for this DocAI method. The LLM should be up and running in your environment.&#x20;*****/ model?: string, }***

&#x20;       (optional) The options for the DocAI service. If not specified, the default options will be used.

**Return type**

&#x20;   { result: any, model: string, usage: { promptTokens: number, completionTokens: number, totalTokens: number } }

**Examples**

```javascript
// Ask a question about a PDF document.
DocAI.ask('base64encodedPDFDocument', 
   "Did the valuation reveal any special features?"
);
```

#### chat

```javascript
chat(prompt: string, options?: { 
        /**
         * The LLM model to use for this DocAI method. The LLM should be up and running in your environment.
         */
        model?: string,
    })
```

Method to ask the underlying LLM for general information, not related to any document. This returns the response from the LLM along with model and usage information. The result can be either a text or an object.

**Parameters**

&#x20;   **prompt&#x20;*****string***

&#x20;       The prompt/question you want to ask.

&#x20;   **options&#x20;*****{ /*****\* \* The LLM model to use for this DocAI method. The LLM should be up and running in your environment.&#x20;*****/ model?: string, }***

&#x20;       (optional) The options for the DocAI service. If not specified, the default options will be used.

**Return type**

&#x20;   { result: any, model: string, usage: { promptTokens: number, completionTokens: number, totalTokens: number } }

**Examples**

```javascript
// Ask a question.
DocAI.chat("What to look for in a valuation report?");
```

#### extractStructuredData

```javascript
extractStructuredData(document: string, model: 'prebuilt-idDocument' | 'prebuilt-invoice' | 'prebuilt-receipt' | 'prebuilt-contract', options?: { 
        /**
         * Confidence threshold for extracted data. Defaults to 0.5. If the confidence of a field is below this threshold, it will be omitted from the result.
         */
        confidenceThreshold?: number,
        /**
         * Whether to use Azure's markdown conversion. Defaults to false. Only valid in combination with model prebuilt-layout. If true, the document will be converted to markdown using Azure's PDF to Markdown service. If false, Rulecube's custom extraction will be used.
         */
        useAzureMarkdown?: boolean
    })
```

Method to extract structured data from a document/image using a pretrained model. The following models are supported: id-documents, invoices, receipts, and contracts.

**Parameters**

&#x20;   **document&#x20;*****string***

&#x20;       A base64 document or image from which you want to extract structured data.

&#x20;   **model&#x20;*****'prebuilt-idDocument' | 'prebuilt-invoice' | 'prebuilt-receipt' | 'prebuilt-contract'***

&#x20;       The model defining the structured data to extract from the document.

&#x20;   **options&#x20;*****{ /*****\* \* Confidence threshold for extracted data. Defaults to 0.5. If the confidence of a field is below this threshold, it will be omitted from the result.&#x20;*****/ confidenceThreshold?: number, /*****\* \* Whether to use Azure's markdown conversion. Defaults to false. Only valid in combination with model prebuilt-layout. If true, the document will be converted to markdown using Azure's PDF to Markdown service. If false, Rulecube's custom extraction will be used.&#x20;*****/ useAzureMarkdown?: boolean }***

&#x20;       (optional) The options for structured data extraction.

**Return type**

&#x20;   { result: { \[key: string]: any; } }

**Examples**

```javascript
// Extract structured data from a passport:
DocAI.extractStructuredData('base64encodedPassportImage', 'prebuilt-idDocument', 'structured');
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rulecube.com/v2.5/language-reference/docai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
