-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
101 lines (83 loc) · 3.2 KB
/
example_usage.py
File metadata and controls
101 lines (83 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Example usage of the LLMExtractor programmatically.
This demonstrates how to use the extraction system in your own code,
rather than through the CLI.
"""
import os
from pathlib import Path
from dotenv import load_dotenv
from src import LLMExtractor, setup_logging
def main():
# Setup
load_dotenv()
setup_logging(log_level="INFO")
# Initialize extractor
extractor = LLMExtractor(
model=os.getenv("MODEL_NAME", "llama3.2"),
ollama_host=os.getenv("OLLAMA_HOST", "http://localhost:11434"),
temperature=0.1,
max_retries=3
)
# Example 1: Extract from invoice
print("=" * 60)
print("EXAMPLE 1: Invoice Extraction")
print("=" * 60)
invoice_text = Path("sample_inputs/invoice_tech.txt").read_text()
result = extractor.extract(
text=invoice_text,
schema_type="invoice"
)
if result.success:
print(f"\n✓ Success after {result.total_attempts} attempt(s)")
print(f"\nInvoice Number: {result.data.invoice_number}")
print(f"Total Amount: {result.data.currency} {result.data.total_amount}")
print(f"Vendor: {result.data.vendor_name}")
print(f"Items: {len(result.data.items)}")
else:
print(f"\n✗ Failed: {result.error_message}")
# Example 2: Extract from email
print("\n" + "=" * 60)
print("EXAMPLE 2: Email Extraction")
print("=" * 60)
email_text = Path("sample_inputs/email_project.txt").read_text()
result = extractor.extract(
text=email_text,
schema_type="email"
)
if result.success:
print(f"\n✓ Success after {result.total_attempts} attempt(s)")
print(f"\nFrom: {result.data.sender_name} <{result.data.sender_email}>")
print(f"To: {result.data.recipient_name} <{result.data.recipient_email}>")
print(f"Subject: {result.data.subject}")
print(f"Intent: {result.data.intent}")
print(f"\nAction Items ({len(result.data.action_items)}):")
for i, item in enumerate(result.data.action_items, 1):
print(f" {i}. {item}")
print(f"\nKey Entities: {', '.join(result.data.key_entities)}")
else:
print(f"\n✗ Failed: {result.error_message}")
# Example 3: Extract from support ticket
print("\n" + "=" * 60)
print("EXAMPLE 3: Support Ticket Extraction")
print("=" * 60)
ticket_text = Path("sample_inputs/support_ticket_urgent.txt").read_text()
result = extractor.extract(
text=ticket_text,
schema_type="support_ticket"
)
if result.success:
print(f"\n✓ Success after {result.total_attempts} attempt(s)")
print(f"\nCustomer: {result.data.customer_name}")
print(f"Email: {result.data.customer_email}")
print(f"Category: {result.data.issue_category}")
print(f"Priority: {result.data.priority.value.upper()}")
print(f"Summary: {result.data.summary}")
if result.data.error_codes:
print(f"Error Codes: {', '.join(result.data.error_codes)}")
else:
print(f"\n✗ Failed: {result.error_message}")
print("\n" + "=" * 60)
print("All examples completed!")
print("=" * 60)
if __name__ == "__main__":
main()