Line Items Extraction - מערכת חילוץ פריטים מתקדמת עם AI + Regex Hybrid
מערכת חילוץ הפריטים (Line Items Extraction) היא מודול מתקדם לחילוץ אוטומטי של פריטים, כמויות, מחירים ונתונים פיננסיים מחשבוניות וקבלות.
Google Gemini, OpenAI, Anthropic
10+ תבניות מתקדמות
עברית + אנגלית + תרגום
חילוץ באמצעות תבניות Regular Expression מתקדמות.
חילוץ באמצעות מודלי שפה מתקדמים.
שילוב של שתי השיטות עם השוואה.
def _calculate_extraction_score(items, confidence): # Base confidence weight (40%) score = confidence * 0.4 # Completeness score per item (40%) for item in items: item_score = 0 if item.get('description'): item_score += 0.2 if item.get('quantity'): item_score += 0.15 if item.get('unit_price'): item_score += 0.15 if item.get('line_total'): item_score += 0.15 if item.get('product_code') or item.get('sku'): item_score += 0.1 if item.get('vat_rate'): item_score += 0.1 ... # Bonus for number of items (20%) items_bonus = min(len(items) * 0.02, 0.2) return min(score + completeness + items_bonus, 1.0)
המערכת מזהה אוטומטית את שפת המסמך ושומרת את הנתונים בהתאם:
description | שקי מלט 50 ק"ג |
description_english | Cement bags 50 kg |
description_hebrew | NULL |
original_language | he |
unit_of_measure | ק"ג |
unit_of_measure_normalized | kg |
description | 24" LCD Monitor |
description_english | NULL |
description_hebrew | מסך LCD 24 אינץ' |
original_language | en |
unit_of_measure | unit |
unit_of_measure_normalized | unit |
description תמיד מכיל את הטקסט בשפה המקורית של המסמך.
התרגום נשמר בשדות הנפרדים description_english או description_hebrew.
| שם שדה | סוג | תיאור |
|---|---|---|
| זיהוי פריט | ||
line_number | Integer | מספר שורה בחשבונית |
product_code | String(50) | קוד מוצר |
sku | String(50) | מק"ט (Stock Keeping Unit) |
barcode | String(50) | ברקוד |
catalog_number | String(50) | מספר קטלוגי |
serial_number | String(100) | מספר סריאלי |
| תיאור ושפה | ||
description | String(500) | תיאור בשפה המקורית |
description_english | String(500) | תרגום לאנגלית (אם מקור עברית) |
description_hebrew | String(500) | תרגום לעברית (אם מקור אנגלית) |
original_language | String(5) | שפת המקור: 'he', 'en', 'mixed' |
| כמות ומחיר | ||
quantity | Numeric(10,3) | כמות |
unit_price | Numeric(12,2) | מחיר ליחידה |
line_total | Numeric(12,2) | סה"כ שורה |
unit_of_measure | String(30) | יחידת מידה (מקור) |
unit_of_measure_normalized | String(20) | יחידה מנורמלת (אנגלית) |
currency | String(3) | מטבע (ILS, USD, EUR) |
| הנחה | ||
discount_percent | Float | אחוז הנחה |
discount_amount | Numeric(10,2) | סכום הנחה |
price_before_discount | Numeric(12,2) | מחיר לפני הנחה |
| מע"מ | ||
vat_rate | Float | שיעור מע"מ (%) |
vat_amount | Numeric(10,2) | סכום מע"מ |
is_vat_exempt | Boolean | פטור ממע"מ |
tax_code | String(20) | קוד מס |
| קטגוריה | ||
category | String(100) | קטגוריה (שפה מקורית) |
category_english | String(100) | קטגוריה באנגלית |
subcategory | String(100) | תת-קטגוריה |
item_type | String(50) | סוג: product/service/shipping |
| הערות ומידע נוסף | ||
notes | Text | הערות |
internal_notes | Text | הערות פנימיות |
warranty_info | String(255) | מידע אחריות |
cost_price | Numeric(12,2) | מחיר עלות |
| Metadata חילוץ | ||
extraction_method | String(20) | 'regex', 'ai', 'hybrid', 'manual' |
ai_provider | String(30) | 'google', 'openai', 'anthropic' |
confidence_score | Float | ציון ביטחון (0-1) |
confidence_details | JSON | פירוט ציונים |
raw_text | Text | טקסט גולמי מ-OCR |
| התאמה למלאי | ||
matched_inventory_id | Integer | FK לפריט מלאי |
matched_product_id | Integer | FK למוצר |
match_confidence | Float | ציון התאמה |
/api/email-scanning/documents/<document_id>/line-items
מחזיר את כל הפריטים שחולצו מהמסמך.
// Response Example
{
"success": true,
"data": {
"items": [
{
"id": 1,
"line_number": 1,
"description": "שקי מלט 50 ק\"ג",
"description_english": "Cement bags 50 kg",
"original_language": "he",
"product_code": "BLD-001",
"sku": "BLD-001",
"quantity": 20,
"unit_price": 45.00,
"line_total": 900.00,
"unit_of_measure": "ק\"ג",
"unit_of_measure_normalized": "kg",
"extraction_method": "hybrid",
"ai_provider": "google",
"confidence_score": 0.92
}
],
"total_items": 6,
"extraction_method": "hybrid",
"confidence": 0.92
}
}
/api/email-scanning/documents/<document_id>/extract-line-items
// Request Body { "use_ai": true, "ai_provider": "google", // optional: "google", "openai", "anthropic" "force": true // optional: re-extract even if exists } // Response { "success": true, "data": { "items_extracted": 6, "extraction_method": "hybrid", "ai_provider": "google", "confidence": 0.92, "document_language": "he" } }
מודל: gemini-2.0-flash
יתרונות:
Environment Variable:
GOOGLE_AI_API_KEY
מודל: gpt-4o-mini
יתרונות:
Environment Variable:
OPENAI_API_KEY
מודל: claude-3-haiku
יתרונות:
Environment Variable:
ANTHROPIC_API_KEY
# AI API Keys GOOGLE_AI_API_KEY=AIzaSy... OPENAI_API_KEY=sk-... ANTHROPIC_API_KEY=sk-ant-... # OCR Configuration OCR_DEFAULT_LANGUAGE=heb+eng OCR_CONFIDENCE_THRESHOLD=0.7 # Line Items Extraction LINE_ITEMS_USE_AI=true LINE_ITEMS_AI_PROVIDER=google # google, openai, anthropic, auto LINE_ITEMS_HYBRID_MODE=true
| קובץ | תיאור |
|---|---|
app/models_email_scanning.py |
מודל DocumentLineItem (35+ שדות) |
app/services/ocr_service.py |
LineItemExtractor class (~720 שורות) |
app/tasks/ocr_tasks.py |
Celery tasks לעיבוד OCR |
app/api_email_scanning.py |
API endpoints לחילוץ פריטים |
from app.services.ocr_service import ocr_service # Extract line items from OCR text invoice_text = """ חשבונית מס' 1087 מק"ט פריט כמות מחיר סה"כ BLD-001 שקי מלט 50 ק"ג 20 45.00 900.00 BLD-002 בלוקים 20x20x40 100 8.50 850.00 """ # Hybrid extraction (AI + Regex) result = ocr_service.extract_line_items(invoice_text) print(f"Method: {result['extraction_method']}") print(f"AI Provider: {result.get('ai_provider', 'N/A')}") print(f"Items found: {len(result['items'])}") print(f"Confidence: {result['confidence']:.0%}") for item in result['items']: print(f" - {item['sku']}: {item['description']} = {item['line_total']}")
// Get line items from document async function getLineItems(documentId) { const response = await fetch( `/api/email-scanning/documents/${documentId}/line-items`, { credentials: 'include', headers: { 'Content-Type': 'application/json' } } ); const result = await response.json(); const data = result.data || result; console.log(`Extraction method: ${data.extraction_method}`); console.log(`Items: ${data.items.length}`); data.items.forEach(item => { console.log(`${item.sku}: ${item.description}`); if (item.description_english) { console.log(` (EN: ${item.description_english})`); } }); }
# Output Example
Method: hybrid
AI Provider: google
Items found: 6
Confidence: 92%
- BLD-001: שקי מלט 50 ק"ג = 900.0
(EN: Cement bags 50 kg)
- BLD-002: בלוקים 20x20x40 = 850.0
(EN: Blocks 20x20x40)
- PLM-100: צינור PVC 4 אינץ' = 325.0
(EN: PVC pipe 4 inch)