@minded-ai/mindedjs 3.0.8-beta.12 ā 3.1.9-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.js +2 -9
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/runCommand.d.ts +1 -1
- package/dist/cli/runCommand.d.ts.map +1 -1
- package/dist/cli/runCommand.js +31 -23
- package/dist/cli/runCommand.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/internalTools/documentExtraction/documentExtraction.d.ts +112 -102
- package/dist/internalTools/documentExtraction/documentExtraction.d.ts.map +1 -1
- package/dist/internalTools/documentExtraction/documentExtraction.js +146 -705
- package/dist/internalTools/documentExtraction/documentExtraction.js.map +1 -1
- package/dist/internalTools/documentExtraction/extractStructuredData.d.ts +57 -0
- package/dist/internalTools/documentExtraction/extractStructuredData.d.ts.map +1 -0
- package/dist/internalTools/documentExtraction/extractStructuredData.js +121 -0
- package/dist/internalTools/documentExtraction/extractStructuredData.js.map +1 -0
- package/dist/internalTools/documentExtraction/parseDocumentLocal.d.ts +16 -0
- package/dist/internalTools/documentExtraction/parseDocumentLocal.d.ts.map +1 -0
- package/dist/internalTools/documentExtraction/parseDocumentLocal.js +547 -0
- package/dist/internalTools/documentExtraction/parseDocumentLocal.js.map +1 -0
- package/dist/internalTools/documentExtraction/parseDocumentManaged.d.ts +13 -0
- package/dist/internalTools/documentExtraction/parseDocumentManaged.d.ts.map +1 -0
- package/dist/internalTools/documentExtraction/parseDocumentManaged.js +150 -0
- package/dist/internalTools/documentExtraction/parseDocumentManaged.js.map +1 -0
- package/dist/nodes/addAppToolNode.d.ts.map +1 -1
- package/dist/nodes/addAppToolNode.js +20 -1
- package/dist/nodes/addAppToolNode.js.map +1 -1
- package/dist/toolsLibrary/classifier.d.ts +2 -2
- package/dist/toolsLibrary/parseDocument.d.ts +11 -10
- package/dist/toolsLibrary/parseDocument.d.ts.map +1 -1
- package/dist/toolsLibrary/parseDocument.js +33 -189
- package/dist/toolsLibrary/parseDocument.js.map +1 -1
- package/dist/toolsLibrary/withBrowserSession.d.ts.map +1 -1
- package/dist/toolsLibrary/withBrowserSession.js +70 -2
- package/dist/toolsLibrary/withBrowserSession.js.map +1 -1
- package/dist/types/Flows.types.d.ts +1 -0
- package/dist/types/Flows.types.d.ts.map +1 -1
- package/dist/types/Flows.types.js.map +1 -1
- package/dist/utils/schemaUtils.js +1 -1
- package/dist/utils/schemaUtils.js.map +1 -1
- package/docs/tooling/document-processing.md +235 -174
- package/package.json +2 -1
- package/src/cli/index.ts +2 -10
- package/src/cli/runCommand.ts +31 -25
- package/src/index.ts +2 -1
- package/src/internalTools/documentExtraction/documentExtraction.ts +184 -767
- package/src/internalTools/documentExtraction/extractStructuredData.ts +140 -0
- package/src/internalTools/documentExtraction/parseDocumentLocal.ts +660 -0
- package/src/internalTools/documentExtraction/parseDocumentManaged.ts +152 -0
- package/src/nodes/addAppToolNode.ts +30 -7
- package/src/toolsLibrary/parseDocument.ts +38 -206
- package/src/toolsLibrary/withBrowserSession.ts +89 -4
- package/src/types/Flows.types.ts +1 -0
- package/src/utils/schemaUtils.ts +1 -1
|
@@ -1,132 +1,243 @@
|
|
|
1
1
|
# Document Processing
|
|
2
2
|
|
|
3
|
-
Parse and extract data from images, PDFs, Word documents, spreadsheets, and more using AI-powered document processing. This tool handles both document parsing and data extraction
|
|
3
|
+
Parse and extract data from images, PDFs, Word documents, spreadsheets, and more using AI-powered document processing. This tool handles both document parsing and data extraction.
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
7
|
**Supported formats:** Images (JPG, PNG, GIF, BMP, WebP, TIFF), Documents (PDF, DOC, DOCX, TXT, RTF, ODT), Spreadsheets (XLS, XLSX, CSV, ODS), Presentations (PPT, PPTX, ODP), Web formats (HTML, HTM, MD, XML)
|
|
8
8
|
|
|
9
|
-
**
|
|
9
|
+
**Available extraction modes:**
|
|
10
10
|
|
|
11
|
-
1. **Structured Extraction**: Extract data into a predefined Zod schema using AI
|
|
12
|
-
2. **
|
|
13
|
-
3. **Raw Text Extraction**:
|
|
11
|
+
1. **Structured Extraction with Schema**: Extract data into a predefined Zod schema using AI
|
|
12
|
+
2. **Structured Extraction with Prompt**: Guide extraction using custom prompts
|
|
13
|
+
3. **Raw Text Extraction**: Parse document and extract plain text without AI processing
|
|
14
14
|
|
|
15
15
|
## Processing Modes
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Document processing supports two modes: **managed** (default) and **local**. In managed mode documents are uploaded to Minded cloud and processed there, providing secure API key storage, automatic cost tracking, and no SDK configuration. Local mode processes documents directly in your SDK using LlamaCloud.
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
To use local mode, set the `DOCUMENT_PROCESSING_MODE` environment variable to `local` and provide a `LLAMA_CLOUD_API_KEY`:
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
```bash
|
|
22
|
+
DOCUMENT_PROCESSING_MODE=local
|
|
23
|
+
LLAMA_CLOUD_API_KEY=your_llama_cloud_api_key
|
|
24
|
+
```
|
|
22
25
|
|
|
23
|
-
|
|
26
|
+
## Using in Flows
|
|
24
27
|
|
|
25
|
-
|
|
28
|
+
Document processing includes built-in AI extraction - use the node's `prompt` and `outputSchema` properties to specify what data to extract. No additional extraction tool is needed.
|
|
26
29
|
|
|
27
|
-
|
|
30
|
+
**Available properties:**
|
|
31
|
+
|
|
32
|
+
- `parameters.documentSource` (string, required): URL or file path to the document (auto-detected)
|
|
33
|
+
- `parameters.returnStructuredOutput` (boolean, optional, default: `false`): Set to `true` to enable AI-powered extraction, `false` for raw text only. When `true`, requires either `prompt` or `outputSchema` (or both)
|
|
34
|
+
- `prompt` (string, optional): Instructions for AI-powered extraction. Ignored when `returnStructuredOutput` is `false`
|
|
35
|
+
- `outputSchema` (schema object, optional): Define the structure of extracted data for structured extraction. Ignored when `returnStructuredOutput` is `false`
|
|
28
36
|
|
|
29
|
-
|
|
37
|
+
### Examples
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
Raw text extraction without AI processing:
|
|
32
40
|
|
|
33
41
|
```yaml
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
type: appTool
|
|
46
|
-
displayName: 'Parse Uploaded Document'
|
|
47
|
-
actionKey: 'minded-parse-documents'
|
|
48
|
-
actionName: 'Parse Document'
|
|
49
|
-
appName: 'Minded'
|
|
50
|
-
parameters:
|
|
51
|
-
documentSource: '{state.memory.filePath}'
|
|
52
|
-
systemPrompt: 'Extract invoice number, amount, date, and vendor'
|
|
53
|
-
|
|
54
|
-
- name: 'extract-names-addresses' # Must be unique within the flow
|
|
55
|
-
type: appTool
|
|
56
|
-
displayName: 'Extract Names and Addresses'
|
|
57
|
-
actionKey: 'minded-parse-documents'
|
|
58
|
-
actionName: 'Parse Document'
|
|
59
|
-
appName: 'Minded'
|
|
60
|
-
parameters:
|
|
61
|
-
documentSource: '{state.memory.uploadedFile}'
|
|
62
|
-
systemPrompt: 'Extract all names and addresses from this document'
|
|
42
|
+
name: Main flow
|
|
43
|
+
nodes:
|
|
44
|
+
- name: 'parse-invoice'
|
|
45
|
+
type: appTool
|
|
46
|
+
displayName: 'Parse Invoice'
|
|
47
|
+
actionKey: 'minded-parse-documents'
|
|
48
|
+
actionName: 'Parse Document'
|
|
49
|
+
appName: 'Minded'
|
|
50
|
+
parameters:
|
|
51
|
+
documentSource: '{state.memory.invoiceUrl}'
|
|
52
|
+
returnStructuredOutput: false
|
|
63
53
|
```
|
|
64
54
|
|
|
65
|
-
|
|
55
|
+
Structured extraction with schema and prompt:
|
|
66
56
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
-
|
|
57
|
+
```yaml
|
|
58
|
+
name: Main flow
|
|
59
|
+
nodes:
|
|
60
|
+
- name: 'parse-uploaded-document'
|
|
61
|
+
type: appTool
|
|
62
|
+
displayName: 'Parse Uploaded Document'
|
|
63
|
+
actionKey: 'minded-parse-documents'
|
|
64
|
+
actionName: 'Parse Document'
|
|
65
|
+
appName: 'Minded'
|
|
66
|
+
parameters:
|
|
67
|
+
documentSource: '{state.memory.filePath}'
|
|
68
|
+
returnStructuredOutput: true
|
|
69
|
+
prompt: 'Extract invoice number, amount, date, and vendor'
|
|
70
|
+
outputSchema:
|
|
71
|
+
- name: invoiceNumber
|
|
72
|
+
type: string
|
|
73
|
+
description: Invoice number
|
|
74
|
+
- name: amount
|
|
75
|
+
type: number
|
|
76
|
+
description: Total amount
|
|
77
|
+
- name: date
|
|
78
|
+
type: string
|
|
79
|
+
description: Invoice date
|
|
80
|
+
- name: vendor
|
|
81
|
+
type: string
|
|
82
|
+
description: Vendor name
|
|
83
|
+
```
|
|
71
84
|
|
|
72
|
-
|
|
85
|
+
Unstructured extraction guided by prompt only:
|
|
86
|
+
|
|
87
|
+
```yaml
|
|
88
|
+
name: Main flow
|
|
89
|
+
nodes:
|
|
90
|
+
- name: 'extract-names-addresses'
|
|
91
|
+
type: appTool
|
|
92
|
+
displayName: 'Extract Names and Addresses'
|
|
93
|
+
actionKey: 'minded-parse-documents'
|
|
94
|
+
actionName: 'Parse Document'
|
|
95
|
+
appName: 'Minded'
|
|
96
|
+
parameters:
|
|
97
|
+
documentSource: '{state.memory.uploadedFile}'
|
|
98
|
+
returnStructuredOutput: true
|
|
99
|
+
prompt: 'Extract all names and addresses from this document'
|
|
100
|
+
```
|
|
73
101
|
|
|
74
|
-
|
|
102
|
+
## Programmatic Usage
|
|
75
103
|
|
|
76
|
-
|
|
104
|
+
The SDK provides three main functions for document processing:
|
|
105
|
+
|
|
106
|
+
1. **`parseDocumentAndExtractStructuredData`** - Parse and optionally extract structured data with AI
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
parseDocumentAndExtractStructuredData<T>({
|
|
110
|
+
documentSource: string, // Required: URL or file path
|
|
111
|
+
sessionId: string, // Required: Session identifier
|
|
112
|
+
returnStructuredOutput: boolean, // Required: Enable/disable AI extraction
|
|
113
|
+
llm?: BaseLanguageModel, // Optional: LLM instance (required when returnStructuredOutput is true)
|
|
114
|
+
outputSchema?: ZodType<T>, // Optional: Zod schema for structured extraction
|
|
115
|
+
outputSchemaPrompt?: string, // Optional: Instructions for extraction
|
|
116
|
+
processingMode?: DocumentProcessingMode, // Optional: Processing mode (default: DocumentProcessingMode.MANAGED)
|
|
117
|
+
llamaCloudApiKey?: string // Optional: API key for local mode
|
|
118
|
+
}): Promise<{
|
|
119
|
+
rawContent?: string,
|
|
120
|
+
structuredContent?: T | string,
|
|
121
|
+
metadata?: { fileSize?: number, fileType: string, processingTime: number, contentLength: number }
|
|
122
|
+
}>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
2. **`parseDocument`** - Parse document and extract raw text only
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
parseDocument({
|
|
129
|
+
documentSource: string, // Required: URL or file path
|
|
130
|
+
sessionId: string, // Required: Session identifier
|
|
131
|
+
processingMode?: DocumentProcessingMode, // Optional: Processing mode (default: DocumentProcessingMode.MANAGED)
|
|
132
|
+
llamaCloudApiKey?: string // Optional: API key for local mode
|
|
133
|
+
}): Promise<{
|
|
134
|
+
rawContent?: string,
|
|
135
|
+
metadata?: { fileSize?: number, fileType: string, processingTime: number, contentLength: number }
|
|
136
|
+
}>
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
3. **`extractStructuredDataFromString`** - Extract structured data from already parsed text
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
extractStructuredDataFromString<T>({
|
|
143
|
+
content: string, // Required: Text content to extract from
|
|
144
|
+
llm: BaseLanguageModel, // Required: LLM instance
|
|
145
|
+
sessionId: string, // Required: Session identifier
|
|
146
|
+
schema?: ZodType<T>, // Optional: Zod schema for structured extraction
|
|
147
|
+
prompt?: string // Optional: Instructions for extraction
|
|
148
|
+
}): Promise<T | string>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Structured Extraction with Schema
|
|
152
|
+
|
|
153
|
+
Extract data matching a predefined Zod schema.
|
|
154
|
+
|
|
155
|
+
{% hint style="info" %}
|
|
156
|
+
**Note:** Use `nullable()` instead of `optional()` for fields that are not required.
|
|
157
|
+
{% endhint %}
|
|
77
158
|
|
|
78
159
|
```typescript
|
|
79
|
-
import {
|
|
160
|
+
import { parseDocumentAndExtractStructuredData } from '@minded-ai/mindedjs';
|
|
80
161
|
import { z } from 'zod';
|
|
81
162
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
163
|
+
// Invoice processing with structured schema
|
|
164
|
+
const invoiceSchema = z.object({
|
|
165
|
+
invoiceNumber: z.string(),
|
|
166
|
+
vendor: z.string(),
|
|
167
|
+
amount: z.number(),
|
|
168
|
+
dueDate: z.string(),
|
|
169
|
+
lineItems: z.array(
|
|
170
|
+
z.object({
|
|
171
|
+
description: z.string(),
|
|
172
|
+
quantity: z.number(),
|
|
173
|
+
unitPrice: z.number(),
|
|
174
|
+
}),
|
|
175
|
+
),
|
|
86
176
|
});
|
|
87
177
|
|
|
88
|
-
const result = await
|
|
178
|
+
const result = await parseDocumentAndExtractStructuredData({
|
|
179
|
+
documentSource: './invoice.pdf',
|
|
180
|
+
sessionId: state.sessionId,
|
|
181
|
+
returnStructuredOutput: true,
|
|
89
182
|
llm: agent.llm,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
systemPrompt: 'Extract personal information from this ID document',
|
|
183
|
+
outputSchema: invoiceSchema,
|
|
184
|
+
outputSchemaPrompt: 'Extract all invoice details including line items',
|
|
93
185
|
});
|
|
94
186
|
|
|
95
|
-
console.log(result.
|
|
187
|
+
console.log(result.structuredContent); // Typed data matching your schema
|
|
188
|
+
console.log(result.rawContent); // Original raw text
|
|
189
|
+
console.log(result.metadata); // Processing metadata
|
|
96
190
|
```
|
|
97
191
|
|
|
98
|
-
###
|
|
192
|
+
### Structured Extraction with Prompt Only
|
|
193
|
+
|
|
194
|
+
Extract data using AI with a custom prompt, without a predefined schema.
|
|
99
195
|
|
|
100
196
|
```typescript
|
|
101
|
-
|
|
197
|
+
import { parseDocumentAndExtractStructuredData } from '@minded-ai/mindedjs';
|
|
198
|
+
|
|
199
|
+
// Contract analysis with prompt guidance
|
|
200
|
+
const result = await parseDocumentAndExtractStructuredData({
|
|
201
|
+
documentSource: './contract.pdf',
|
|
202
|
+
sessionId: state.sessionId,
|
|
203
|
+
returnStructuredOutput: true,
|
|
102
204
|
llm: agent.llm,
|
|
103
|
-
|
|
104
|
-
systemPrompt: 'Extract parties involved, key dates, payment terms, and termination clauses',
|
|
205
|
+
outputSchemaPrompt: 'Extract parties involved, key dates, payment terms, and termination clauses',
|
|
105
206
|
});
|
|
106
207
|
|
|
107
|
-
console.log(result.
|
|
208
|
+
console.log(result.structuredContent); // String or object with extracted information
|
|
108
209
|
```
|
|
109
210
|
|
|
110
211
|
### Raw Text Extraction
|
|
111
212
|
|
|
213
|
+
Parse document without AI processing to get plain text only.
|
|
214
|
+
|
|
112
215
|
```typescript
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
216
|
+
import { parseDocument } from '@minded-ai/mindedjs';
|
|
217
|
+
|
|
218
|
+
const result = await parseDocument({
|
|
219
|
+
documentSource: './document.pdf',
|
|
220
|
+
sessionId: state.sessionId,
|
|
118
221
|
});
|
|
119
222
|
|
|
120
|
-
console.log(result.
|
|
223
|
+
console.log(result.rawContent); // Raw extracted text
|
|
224
|
+
console.log(result.metadata); // File size, type, processing time, content length
|
|
121
225
|
```
|
|
122
226
|
|
|
123
227
|
### Using URLs
|
|
124
228
|
|
|
229
|
+
All functions accept URLs in addition to file paths via the `documentSource` parameter.
|
|
230
|
+
|
|
125
231
|
```typescript
|
|
126
|
-
|
|
232
|
+
import { parseDocumentAndExtractStructuredData } from '@minded-ai/mindedjs';
|
|
233
|
+
import { z } from 'zod';
|
|
234
|
+
|
|
235
|
+
const result = await parseDocumentAndExtractStructuredData({
|
|
236
|
+
documentSource: 'https://example.com/invoice.pdf',
|
|
237
|
+
sessionId: state.sessionId,
|
|
238
|
+
returnStructuredOutput: true,
|
|
127
239
|
llm: agent.llm,
|
|
128
|
-
|
|
129
|
-
schema: z.object({
|
|
240
|
+
outputSchema: z.object({
|
|
130
241
|
invoiceNumber: z.string(),
|
|
131
242
|
amount: z.number(),
|
|
132
243
|
dueDate: z.string(),
|
|
@@ -134,138 +245,88 @@ const result = await extractFromDocument({
|
|
|
134
245
|
});
|
|
135
246
|
```
|
|
136
247
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
### Environment Variables
|
|
140
|
-
|
|
141
|
-
```bash
|
|
142
|
-
# Processing mode: 'managed' (default) or 'local'
|
|
143
|
-
DOCUMENT_PROCESSING_MODE=managed
|
|
144
|
-
|
|
145
|
-
# Required for local mode only:
|
|
146
|
-
LLAMA_CLOUD_API_KEY=your_llama_cloud_api_key
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
### Document Processor Options
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
const result = await extractFromDocument({
|
|
153
|
-
llm: agent.llm,
|
|
154
|
-
documentPath: './image.jpg',
|
|
155
|
-
schema: yourSchema,
|
|
156
|
-
config: {
|
|
157
|
-
llamaCloudApiKey: 'your-key',
|
|
158
|
-
useBase64: true,
|
|
159
|
-
maxImageWidth: 1600,
|
|
160
|
-
imageQuality: 90,
|
|
161
|
-
},
|
|
162
|
-
});
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### LLM Configuration
|
|
166
|
-
|
|
167
|
-
```typescript
|
|
168
|
-
const result = await extractFromDocument({
|
|
169
|
-
llm: agent.llm,
|
|
170
|
-
documentPath: './document.pdf',
|
|
171
|
-
schema: yourSchema,
|
|
172
|
-
llmConfig: {
|
|
173
|
-
model: 'gpt-4o',
|
|
174
|
-
temperature: 0.1,
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
## Example Use Cases
|
|
248
|
+
### Identity Document Verification
|
|
180
249
|
|
|
181
|
-
|
|
250
|
+
Extract personal information from ID documents with structured validation.
|
|
182
251
|
|
|
183
252
|
```typescript
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
vendor: z.string(),
|
|
187
|
-
amount: z.number(),
|
|
188
|
-
dueDate: z.string(),
|
|
189
|
-
lineItems: z.array(
|
|
190
|
-
z.object({
|
|
191
|
-
description: z.string(),
|
|
192
|
-
quantity: z.number(),
|
|
193
|
-
unitPrice: z.number(),
|
|
194
|
-
}),
|
|
195
|
-
),
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
const result = await extractFromDocument({
|
|
199
|
-
llm: agent.llm,
|
|
200
|
-
documentPath: './invoice.pdf',
|
|
201
|
-
schema: invoiceSchema,
|
|
202
|
-
systemPrompt: 'Extract all invoice details including line items',
|
|
203
|
-
});
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
### Identity Document Verification
|
|
253
|
+
import { parseDocumentAndExtractStructuredData } from '@minded-ai/mindedjs';
|
|
254
|
+
import { z } from 'zod';
|
|
207
255
|
|
|
208
|
-
```typescript
|
|
209
256
|
const idSchema = z.object({
|
|
210
257
|
documentType: z.enum(['passport', 'driver_license', 'national_id']),
|
|
211
258
|
fullName: z.string(),
|
|
212
259
|
dateOfBirth: z.string(),
|
|
213
260
|
documentNumber: z.string(),
|
|
214
261
|
expiryDate: z.string().nullable(),
|
|
262
|
+
address: z.string().nullable(),
|
|
215
263
|
});
|
|
216
264
|
|
|
217
|
-
const result = await
|
|
265
|
+
const result = await parseDocumentAndExtractStructuredData({
|
|
266
|
+
documentSource: './id-card.jpg',
|
|
267
|
+
sessionId: state.sessionId,
|
|
268
|
+
returnStructuredOutput: true,
|
|
218
269
|
llm: agent.llm,
|
|
219
|
-
|
|
220
|
-
|
|
270
|
+
outputSchema: idSchema,
|
|
271
|
+
outputSchemaPrompt: 'Extract personal information from this ID document',
|
|
221
272
|
});
|
|
222
273
|
```
|
|
223
274
|
|
|
224
|
-
### Extract
|
|
275
|
+
### Extract from Already Parsed Content
|
|
276
|
+
|
|
277
|
+
Use `extractStructuredDataFromString` to extract structured data from text you've already obtained.
|
|
225
278
|
|
|
226
279
|
```typescript
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
280
|
+
import { extractStructuredDataFromString } from '@minded-ai/mindedjs';
|
|
281
|
+
import { z } from 'zod';
|
|
282
|
+
|
|
283
|
+
// With schema for structured extraction
|
|
284
|
+
const structured = await extractStructuredDataFromString({
|
|
285
|
+
content: 'Invoice #12345\nTotal: $500.00\nDate: 2024-01-15',
|
|
286
|
+
llm: agent.llm,
|
|
287
|
+
schema: z.object({
|
|
288
|
+
invoiceNumber: z.string(),
|
|
289
|
+
totalAmount: z.number(),
|
|
290
|
+
date: z.string(),
|
|
291
|
+
}),
|
|
292
|
+
sessionId: state.sessionId,
|
|
234
293
|
});
|
|
294
|
+
// Returns: {invoiceNumber: "12345", totalAmount: 500, date: "2024-01-15"}
|
|
235
295
|
|
|
236
|
-
|
|
296
|
+
// Without schema for unstructured extraction
|
|
297
|
+
const unstructured = await extractStructuredDataFromString({
|
|
298
|
+
content: 'Invoice #12345\nTotal: $500.00\nDate: 2024-01-15',
|
|
237
299
|
llm: agent.llm,
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
systemPrompt: 'Extract all names and addresses from this document',
|
|
300
|
+
prompt: 'Extract the invoice number, total amount, and date',
|
|
301
|
+
sessionId: state.sessionId,
|
|
241
302
|
});
|
|
303
|
+
// Returns: String or object with LLM's analysis
|
|
242
304
|
```
|
|
243
305
|
|
|
244
|
-
|
|
306
|
+
### Local Processing Mode
|
|
245
307
|
|
|
246
|
-
|
|
308
|
+
By default, document processing uses the Minded cloud service. To process documents locally using LlamaCloud, set the `DOCUMENT_PROCESSING_MODE` environment variable and provide a LlamaCloud API key:
|
|
247
309
|
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
Solution (local mode only): Set the `LLAMA_CLOUD_API_KEY` environment variable.
|
|
253
|
-
|
|
254
|
-
**Image Too Large**
|
|
310
|
+
```bash
|
|
311
|
+
# Processing mode: 'managed' (default) or 'local'
|
|
312
|
+
DOCUMENT_PROCESSING_MODE=local
|
|
255
313
|
|
|
256
|
-
|
|
257
|
-
|
|
314
|
+
# Required for local mode:
|
|
315
|
+
LLAMA_CLOUD_API_KEY=your_llama_cloud_api_key
|
|
258
316
|
```
|
|
259
317
|
|
|
260
|
-
|
|
318
|
+
You can also pass the API key and processing mode directly to the function:
|
|
261
319
|
|
|
262
|
-
|
|
320
|
+
```typescript
|
|
321
|
+
import { parseDocument, DocumentProcessingMode } from '@minded-ai/mindedjs';
|
|
263
322
|
|
|
323
|
+
const result = await parseDocument({
|
|
324
|
+
documentSource: './contract.pdf',
|
|
325
|
+
processingMode: DocumentProcessingMode.LOCAL,
|
|
326
|
+
sessionId: state.sessionId,
|
|
327
|
+
llamaCloudApiKey: process.env.LLAMA_CLOUD_API_KEY,
|
|
328
|
+
});
|
|
264
329
|
```
|
|
265
|
-
Error: LLM extraction failed: Invalid schema
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
Solution: Verify your Zod schema matches the expected data structure.
|
|
269
330
|
|
|
270
331
|
## Supported File Types
|
|
271
332
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@minded-ai/mindedjs",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.9-beta.1",
|
|
4
4
|
"description": "MindedJS is a TypeScript library for building agents.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"globals": "^16.2.0",
|
|
44
44
|
"mocha": "^10.3.0",
|
|
45
45
|
"nodemon": "^3.1.10",
|
|
46
|
+
"prettier": "^3.7.4",
|
|
46
47
|
"sinon": "^20.0.0",
|
|
47
48
|
"ts-node": "^10.9.2",
|
|
48
49
|
"typedoc": "^0.28.5",
|
package/src/cli/index.ts
CHANGED
|
@@ -181,17 +181,9 @@ async function main() {
|
|
|
181
181
|
const agentPath = args[1]; // Optional path argument
|
|
182
182
|
runValidateCommand(agentPath);
|
|
183
183
|
} else if (command === 'run') {
|
|
184
|
-
// Command to run the agent with inline JSON input
|
|
185
|
-
// Usage: npx minded run
|
|
184
|
+
// Command to run the agent with optional inline JSON input
|
|
185
|
+
// Usage: npx minded run [json_input]
|
|
186
186
|
const jsonInput = args[1];
|
|
187
|
-
if (!jsonInput) {
|
|
188
|
-
logger.error({ msg: 'Please provide inline JSON input' });
|
|
189
|
-
console.error('\nUsage:');
|
|
190
|
-
console.error(' npx minded run \'{"body": {...}}\'');
|
|
191
|
-
console.error(' npx minded run \'{"body": {...}, "headers": {...}}\'');
|
|
192
|
-
process.exit(1);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
187
|
await runCommand(jsonInput);
|
|
196
188
|
} else {
|
|
197
189
|
logger.error({
|
package/src/cli/runCommand.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
1
2
|
import * as fs from 'fs';
|
|
2
3
|
import * as path from 'path';
|
|
3
4
|
import { z } from 'zod';
|
|
@@ -9,7 +10,7 @@ import { logger } from '../utils/logger';
|
|
|
9
10
|
* - headers: Optional, HTTP-like headers
|
|
10
11
|
*/
|
|
11
12
|
const runInputSchema = z.object({
|
|
12
|
-
body: z.any(),
|
|
13
|
+
body: z.record(z.any()),
|
|
13
14
|
headers: z.record(z.string()).optional(),
|
|
14
15
|
});
|
|
15
16
|
|
|
@@ -107,7 +108,7 @@ async function loadAgent(projectDir: string, agentPath: string): Promise<any> {
|
|
|
107
108
|
/**
|
|
108
109
|
* Main run command handler
|
|
109
110
|
*/
|
|
110
|
-
export async function runCommand(jsonInput
|
|
111
|
+
export async function runCommand(jsonInput?: string): Promise<void> {
|
|
111
112
|
const projectDir = process.cwd();
|
|
112
113
|
const mindedConfigPath = path.join(projectDir, 'minded.json');
|
|
113
114
|
|
|
@@ -134,31 +135,36 @@ export async function runCommand(jsonInput: string): Promise<void> {
|
|
|
134
135
|
process.exit(1);
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
//
|
|
138
|
-
let
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
// Use default empty body if no input provided
|
|
139
|
+
let input: RunInput;
|
|
140
|
+
if (jsonInput) {
|
|
141
|
+
// Parse and validate the JSON input
|
|
142
|
+
let parsedInput: unknown;
|
|
143
|
+
try {
|
|
144
|
+
parsedInput = parseJsonInput(jsonInput);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
logger.error({ message: 'Failed to parse input', err: err instanceof Error ? err.message : String(err) });
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
145
149
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
150
|
+
// Validate against schema
|
|
151
|
+
const validationResult = runInputSchema.safeParse(parsedInput);
|
|
152
|
+
if (!validationResult.success) {
|
|
153
|
+
logger.error({
|
|
154
|
+
msg: 'Invalid input schema',
|
|
155
|
+
errors: validationResult.error.errors.map((e) => `${e.path.join('.')}: ${e.message}`),
|
|
156
|
+
});
|
|
157
|
+
console.error('\nExpected schema:');
|
|
158
|
+
console.error(' {');
|
|
159
|
+
console.error(' "body": <any>, // Required');
|
|
160
|
+
console.error(' "headers": {...} // Optional');
|
|
161
|
+
console.error(' }');
|
|
162
|
+
process.exit(1);
|
|
163
|
+
}
|
|
164
|
+
input = validationResult.data;
|
|
165
|
+
} else {
|
|
166
|
+
input = { body: {} };
|
|
159
167
|
}
|
|
160
|
-
|
|
161
|
-
const input: RunInput = validationResult.data;
|
|
162
168
|
const triggerName = 'cli';
|
|
163
169
|
|
|
164
170
|
console.log(`\nš Running agent with trigger: ${triggerName}\n`);
|
package/src/index.ts
CHANGED
|
@@ -71,7 +71,8 @@ export type { Tool, ToolExecuteInput } from './types/Tools.types';
|
|
|
71
71
|
export { ToolType } from './types/Tools.types';
|
|
72
72
|
|
|
73
73
|
// Document processing utilities
|
|
74
|
-
export {
|
|
74
|
+
export { parseDocumentAndExtractStructuredData, parseDocument, DocumentProcessingMode } from './internalTools/documentExtraction/documentExtraction';
|
|
75
|
+
export { extractStructuredDataFromString } from './internalTools/documentExtraction/extractStructuredData';
|
|
75
76
|
export type {
|
|
76
77
|
DocumentProcessorConfig,
|
|
77
78
|
DocumentExtractionOptions,
|