@omniyat/aimodule 1.0.0
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/README.md +180 -0
- package/dist/index.cjs +300 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +300 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# 🤖 @brinda_yawa/ai-module
|
|
2
|
+
|
|
3
|
+
A unified Node.js module for AI-powered invoice extraction and task generation using Google's Gemini models.
|
|
4
|
+
|
|
5
|
+
## ✨ Features
|
|
6
|
+
|
|
7
|
+
- 🧾 **Invoice Extraction** - Extract structured data from PDF and image invoices
|
|
8
|
+
- ✅ **Task Generation** - Generate structured task lists from intervention descriptions
|
|
9
|
+
- 🖼️ **Multi-modal Support** - Handles images (PNG, JPG, JPEG, WEBP) and audio files
|
|
10
|
+
- 🎯 **Structured Output** - Returns clean, structured JSON data
|
|
11
|
+
- 🔑 **Single Initialization** - One `init()` call configures all services
|
|
12
|
+
- 🧩 **Modular Design** - Access each service independently via `AiModule.invoices` or `AiModule.tasks`
|
|
13
|
+
|
|
14
|
+
## 📦 Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @brinda_yawa/ai-module
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 🚀 Quick Start
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import AiModule from '@brinda_yawa/ai-module';
|
|
24
|
+
import fs from 'fs';
|
|
25
|
+
|
|
26
|
+
// Initialize the module once
|
|
27
|
+
AiModule.init({
|
|
28
|
+
apiKey: 'YOUR_GEMINI_API_KEY',
|
|
29
|
+
model: 'gemini-2.5-flash' // optional, default: gemini-2.5-flash
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Extract invoice data from a PDF
|
|
33
|
+
const buffer = fs.readFileSync('invoice.pdf');
|
|
34
|
+
const base64 = buffer.toString('base64');
|
|
35
|
+
const invoiceData = await AiModule.invoices.extractFromBase64(base64);
|
|
36
|
+
|
|
37
|
+
// Generate tasks from an intervention description
|
|
38
|
+
const tasks = await AiModule.tasks.generateTasks(
|
|
39
|
+
'Pneu crevé',
|
|
40
|
+
'Intervention de dépannage suite à un pneu crevé...'
|
|
41
|
+
);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 📖 API Reference
|
|
45
|
+
|
|
46
|
+
### `AiModule.init(options)`
|
|
47
|
+
|
|
48
|
+
Initializes all services with the provided configuration. Must be called before using any service.
|
|
49
|
+
|
|
50
|
+
| Parameter | Type | Required | Default | Description |
|
|
51
|
+
|-----------|------|----------|---------|-------------|
|
|
52
|
+
| `apiKey` | `string` | ✅ | — | Your Google Gemini API key |
|
|
53
|
+
| `model` | `string` | ❌ | `gemini-2.5-flash` | Gemini model to use |
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
AiModule.init({ apiKey: process.env.GEMINI_API_KEY });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
### `AiModule.invoices`
|
|
62
|
+
|
|
63
|
+
Access the invoice extraction service.
|
|
64
|
+
|
|
65
|
+
#### `AiModule.invoices.extractFromBase64(base64Data)`
|
|
66
|
+
|
|
67
|
+
Extracts structured invoice data from a base64-encoded file.
|
|
68
|
+
|
|
69
|
+
| Parameter | Type | Required | Description |
|
|
70
|
+
|-----------|------|----------|-------------|
|
|
71
|
+
| `base64Data` | `string` | ✅ | Base64 string (with or without `data:...;base64,` prefix) |
|
|
72
|
+
|
|
73
|
+
**Returns:** `Promise<Object | null>`
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
const buffer = fs.readFileSync('invoice.pdf');
|
|
77
|
+
const base64 = buffer.toString('base64');
|
|
78
|
+
|
|
79
|
+
const result = await AiModule.invoices.extractFromBase64(base64);
|
|
80
|
+
console.log(result);
|
|
81
|
+
// {
|
|
82
|
+
// invoiceNumber: "INV-2024-001",
|
|
83
|
+
// date: "2024-01-15",
|
|
84
|
+
// total: 1250.00,
|
|
85
|
+
// vendor: { name: "...", address: "..." },
|
|
86
|
+
// items: [...]
|
|
87
|
+
// }
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Supported formats:** PDF, PNG, JPG, JPEG, WEBP
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
### `AiModule.tasks`
|
|
95
|
+
|
|
96
|
+
Access the task generation service.
|
|
97
|
+
|
|
98
|
+
#### `AiModule.tasks.generateTasks(title, description, joinedImage?, joinedAudio?)`
|
|
99
|
+
|
|
100
|
+
Generates a structured list of tasks from an intervention description.
|
|
101
|
+
|
|
102
|
+
| Parameter | Type | Required | Description |
|
|
103
|
+
|-----------|------|----------|-------------|
|
|
104
|
+
| `title` | `string` | ✅ | Short title of the intervention |
|
|
105
|
+
| `description` | `string` | ✅ | Detailed description of the intervention |
|
|
106
|
+
| `joinedImage` | `{ buffer: Buffer, extension: string }` | ❌ | Optional image file |
|
|
107
|
+
| `joinedAudio` | `{ buffer: Buffer, extension: string }` | ❌ | Optional audio file |
|
|
108
|
+
|
|
109
|
+
**Returns:** `Promise<Array<{ title: string, description: string }>>`
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
// Text only
|
|
113
|
+
const tasks = await AiModule.tasks.generateTasks(
|
|
114
|
+
'Vidange moteur',
|
|
115
|
+
'Vidange complète du moteur avec remplacement du filtre à huile.'
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// With an image
|
|
119
|
+
const imageBuffer = fs.readFileSync('photo.webp');
|
|
120
|
+
const tasks = await AiModule.tasks.generateTasks(
|
|
121
|
+
'Pneu crevé',
|
|
122
|
+
'Intervention suite à un pneu crevé...',
|
|
123
|
+
{ buffer: imageBuffer, extension: '.webp' }
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
console.log(tasks);
|
|
127
|
+
// [
|
|
128
|
+
// { title: "Diagnostic", description: "Inspecter le pneumatique..." },
|
|
129
|
+
// { title: "Réparation", description: "Poser une mèche ou remplacer..." },
|
|
130
|
+
// ...
|
|
131
|
+
// ]
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Supported image formats:** PNG, JPG, JPEG, WEBP
|
|
135
|
+
**Supported audio formats:** MP3, WAV
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## 🗂️ Module Structure
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
ai-module/
|
|
143
|
+
├── index.js # AiModule entry point
|
|
144
|
+
├── services/
|
|
145
|
+
│ ├── InvoiceExtractor.js # Invoice extraction logic
|
|
146
|
+
│ └── TasksGenerator.js # Task generation logic
|
|
147
|
+
└── utils/
|
|
148
|
+
└── util.js # Shared utilities (prompts, mime types...)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## 🔑 Environment Variables
|
|
152
|
+
|
|
153
|
+
It is recommended to store your API key in a `.env` file:
|
|
154
|
+
|
|
155
|
+
```env
|
|
156
|
+
GEMINI_API_KEY=your_api_key_here
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```javascript
|
|
160
|
+
import dotenv from 'dotenv';
|
|
161
|
+
dotenv.config();
|
|
162
|
+
|
|
163
|
+
AiModule.init({ apiKey: process.env.GEMINI_API_KEY });
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## ⚠️ Error Handling
|
|
167
|
+
|
|
168
|
+
```javascript
|
|
169
|
+
try {
|
|
170
|
+
AiModule.init({ apiKey: process.env.GEMINI_API_KEY });
|
|
171
|
+
|
|
172
|
+
const tasks = await AiModule.tasks.generateTasks(title, description);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (error.message.includes('not initialized')) {
|
|
175
|
+
console.error('Call AiModule.init() before using any service.');
|
|
176
|
+
} else {
|
|
177
|
+
console.error('Unexpected error:', error.message);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|