ai-functions 2.1.1 → 2.3.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/.turbo/turbo-build.log +1 -4
- package/CHANGELOG.md +68 -1
- package/README.md +397 -157
- package/dist/ai-promise.d.ts +50 -3
- package/dist/ai-promise.d.ts.map +1 -1
- package/dist/ai-promise.js +410 -51
- package/dist/ai-promise.js.map +1 -1
- package/dist/ai-schemas.d.ts +56 -0
- package/dist/ai-schemas.d.ts.map +1 -0
- package/dist/ai-schemas.js +53 -0
- package/dist/ai-schemas.js.map +1 -0
- package/dist/ai.d.ts +16 -242
- package/dist/ai.d.ts.map +1 -1
- package/dist/ai.js +54 -837
- package/dist/ai.js.map +1 -1
- package/dist/batch/anthropic.d.ts +6 -4
- package/dist/batch/anthropic.d.ts.map +1 -1
- package/dist/batch/anthropic.js +83 -145
- package/dist/batch/anthropic.js.map +1 -1
- package/dist/batch/bedrock.d.ts +8 -30
- package/dist/batch/bedrock.d.ts.map +1 -1
- package/dist/batch/bedrock.js +155 -338
- package/dist/batch/bedrock.js.map +1 -1
- package/dist/batch/cloudflare.d.ts +8 -20
- package/dist/batch/cloudflare.d.ts.map +1 -1
- package/dist/batch/cloudflare.js +68 -189
- package/dist/batch/cloudflare.js.map +1 -1
- package/dist/batch/google.d.ts +6 -20
- package/dist/batch/google.d.ts.map +1 -1
- package/dist/batch/google.js +70 -238
- package/dist/batch/google.js.map +1 -1
- package/dist/batch/index.d.ts +4 -1
- package/dist/batch/index.d.ts.map +1 -1
- package/dist/batch/index.js +4 -1
- package/dist/batch/index.js.map +1 -1
- package/dist/batch/memory.d.ts +1 -1
- package/dist/batch/memory.d.ts.map +1 -1
- package/dist/batch/memory.js +14 -10
- package/dist/batch/memory.js.map +1 -1
- package/dist/batch/openai.d.ts +11 -14
- package/dist/batch/openai.d.ts.map +1 -1
- package/dist/batch/openai.js +52 -156
- package/dist/batch/openai.js.map +1 -1
- package/dist/batch/provider.d.ts +111 -0
- package/dist/batch/provider.d.ts.map +1 -0
- package/dist/batch/provider.js +233 -0
- package/dist/batch/provider.js.map +1 -0
- package/dist/batch-map.d.ts.map +1 -1
- package/dist/batch-map.js +23 -17
- package/dist/batch-map.js.map +1 -1
- package/dist/batch-queue.d.ts +65 -0
- package/dist/batch-queue.d.ts.map +1 -1
- package/dist/batch-queue.js +169 -14
- package/dist/batch-queue.js.map +1 -1
- package/dist/budget.d.ts +272 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +513 -0
- package/dist/budget.js.map +1 -0
- package/dist/cache.d.ts +295 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +433 -0
- package/dist/cache.js.map +1 -0
- package/dist/context.d.ts +42 -8
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +64 -62
- package/dist/context.js.map +1 -1
- package/dist/digital-objects-registry.d.ts +229 -0
- package/dist/digital-objects-registry.d.ts.map +1 -0
- package/dist/digital-objects-registry.js +617 -0
- package/dist/digital-objects-registry.js.map +1 -0
- package/dist/embeddings.d.ts +2 -2
- package/dist/embeddings.d.ts.map +1 -1
- package/dist/errors.d.ts +22 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +35 -0
- package/dist/errors.js.map +1 -0
- package/dist/eval/runner.d.ts +10 -1
- package/dist/eval/runner.d.ts.map +1 -1
- package/dist/eval/runner.js +41 -35
- package/dist/eval/runner.js.map +1 -1
- package/dist/eval-log/in-memory.d.ts +34 -0
- package/dist/eval-log/in-memory.d.ts.map +1 -0
- package/dist/eval-log/in-memory.js +84 -0
- package/dist/eval-log/in-memory.js.map +1 -0
- package/dist/eval-log/index.d.ts +29 -0
- package/dist/eval-log/index.d.ts.map +1 -0
- package/dist/eval-log/index.js +39 -0
- package/dist/eval-log/index.js.map +1 -0
- package/dist/eval-log/types.d.ts +101 -0
- package/dist/eval-log/types.d.ts.map +1 -0
- package/dist/eval-log/types.js +16 -0
- package/dist/eval-log/types.js.map +1 -0
- package/dist/function-registry.d.ts +116 -0
- package/dist/function-registry.d.ts.map +1 -0
- package/dist/function-registry.js +546 -0
- package/dist/function-registry.js.map +1 -0
- package/dist/generate.d.ts +9 -3
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +18 -22
- package/dist/generate.js.map +1 -1
- package/dist/index.d.ts +35 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +89 -42
- package/dist/index.js.map +1 -1
- package/dist/logger.d.ts +118 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +187 -0
- package/dist/logger.js.map +1 -0
- package/dist/middleware/budget.d.ts +84 -0
- package/dist/middleware/budget.d.ts.map +1 -0
- package/dist/middleware/budget.js +110 -0
- package/dist/middleware/budget.js.map +1 -0
- package/dist/middleware/cache.d.ts +103 -0
- package/dist/middleware/cache.d.ts.map +1 -0
- package/dist/middleware/cache.js +228 -0
- package/dist/middleware/cache.js.map +1 -0
- package/dist/middleware/embed-cache.d.ts +99 -0
- package/dist/middleware/embed-cache.d.ts.map +1 -0
- package/dist/middleware/embed-cache.js +128 -0
- package/dist/middleware/embed-cache.js.map +1 -0
- package/dist/middleware/index.d.ts +11 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +11 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/trace.d.ts +103 -0
- package/dist/middleware/trace.d.ts.map +1 -0
- package/dist/middleware/trace.js +176 -0
- package/dist/middleware/trace.js.map +1 -0
- package/dist/primitives.d.ts +120 -1
- package/dist/primitives.d.ts.map +1 -1
- package/dist/primitives.js +398 -26
- package/dist/primitives.js.map +1 -1
- package/dist/retry.d.ts +368 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +646 -0
- package/dist/retry.js.map +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -10
- package/dist/schema.js.map +1 -1
- package/dist/telemetry.d.ts +128 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +285 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/template.d.ts.map +1 -1
- package/dist/template.js +6 -1
- package/dist/template.js.map +1 -1
- package/dist/tool-orchestration.d.ts +453 -0
- package/dist/tool-orchestration.d.ts.map +1 -0
- package/dist/tool-orchestration.js +763 -0
- package/dist/tool-orchestration.js.map +1 -0
- package/dist/type-guards.d.ts +28 -0
- package/dist/type-guards.d.ts.map +1 -0
- package/dist/type-guards.js +29 -0
- package/dist/type-guards.js.map +1 -0
- package/dist/types.d.ts +135 -17
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +36 -1
- package/dist/types.js.map +1 -1
- package/dist/wrap-for-v3.d.ts +80 -0
- package/dist/wrap-for-v3.d.ts.map +1 -0
- package/dist/wrap-for-v3.js +89 -0
- package/dist/wrap-for-v3.js.map +1 -0
- package/examples/00-quickstart.ts +232 -0
- package/examples/01-rag-chatbot.ts +212 -0
- package/examples/02-multi-agent-research.ts +290 -0
- package/examples/03-email-classification.ts +379 -0
- package/examples/04-content-moderation.ts +400 -0
- package/examples/05-document-extraction.ts +455 -0
- package/examples/06-streaming-chat-nextjs.ts +437 -0
- package/examples/07-cloudflare-worker.ts +483 -0
- package/examples/08-batch-processing.ts +491 -0
- package/examples/09-budget-constrained.ts +527 -0
- package/examples/10-tool-orchestration.ts +565 -0
- package/examples/11-retry-resilience.ts +403 -0
- package/examples/12-caching-strategies.ts +422 -0
- package/examples/README.md +145 -0
- package/package.json +10 -6
- package/src/ai-promise.ts +528 -99
- package/src/ai-schemas.ts +122 -0
- package/src/ai.ts +69 -1153
- package/src/batch/anthropic.ts +96 -161
- package/src/batch/bedrock.ts +203 -454
- package/src/batch/cloudflare.ts +99 -282
- package/src/batch/google.ts +91 -297
- package/src/batch/index.ts +4 -1
- package/src/batch/memory.ts +15 -10
- package/src/batch/openai.ts +65 -193
- package/src/batch/provider.ts +336 -0
- package/src/batch-map.ts +29 -24
- package/src/batch-queue.ts +200 -11
- package/src/budget.ts +740 -0
- package/src/cache.ts +681 -0
- package/src/context.ts +122 -76
- package/src/digital-objects-registry.ts +750 -0
- package/src/errors.ts +37 -0
- package/src/eval/runner.ts +63 -38
- package/src/eval-log/in-memory.ts +90 -0
- package/src/eval-log/index.ts +46 -0
- package/src/eval-log/types.ts +110 -0
- package/src/function-registry.ts +671 -0
- package/src/generate.ts +33 -33
- package/src/index.ts +325 -49
- package/src/logger.ts +232 -0
- package/src/middleware/budget.ts +171 -0
- package/src/middleware/cache.ts +299 -0
- package/src/middleware/embed-cache.ts +195 -0
- package/src/middleware/index.ts +23 -0
- package/src/middleware/trace.ts +248 -0
- package/src/primitives.ts +589 -62
- package/src/retry.ts +902 -0
- package/src/schema.ts +8 -17
- package/src/telemetry.ts +403 -0
- package/src/template.ts +8 -4
- package/src/tool-orchestration.ts +1173 -0
- package/src/type-guards.ts +31 -0
- package/src/types.ts +164 -25
- package/src/wrap-for-v3.ts +105 -0
- package/test/ai-promise.test.ts +1080 -0
- package/test/ai-proxy.test.ts +1 -1
- package/test/backward-compat.test.ts +147 -0
- package/test/batch-autosubmit-errors.test.ts +610 -0
- package/test/batch-blog-posts.test.ts +87 -129
- package/test/budget-tracking.test.ts +800 -0
- package/test/cache.test.ts +712 -0
- package/test/context-isolation.test.ts +687 -0
- package/test/core-functions.test.ts +183 -579
- package/test/decide.test.ts +154 -322
- package/test/define.test.ts +211 -8
- package/test/digital-objects-registry.test.ts +760 -0
- package/test/embedding-cache-middleware.test.ts +140 -0
- package/test/evals/deterministic.eval.test.ts +376 -0
- package/test/generate-core.test.ts +140 -229
- package/test/implicit-batch.test.ts +22 -65
- package/test/json-parse-error-handling.test.ts +463 -0
- package/test/retry-policy-integration.test.ts +117 -0
- package/test/retry.test.ts +1016 -0
- package/test/schema.test.ts +55 -19
- package/test/streaming.test.ts +316 -0
- package/test/template.test.ts +1164 -0
- package/test/tool-orchestration.test.ts +1040 -0
- package/test/wrap-for-v3.test.ts +612 -0
- package/vitest.config.js +6 -0
- package/vitest.config.ts +20 -0
- package/dist/rpc/auth.d.ts +0 -69
- package/dist/rpc/auth.d.ts.map +0 -1
- package/dist/rpc/auth.js +0 -136
- package/dist/rpc/auth.js.map +0 -1
- package/dist/rpc/client.d.ts +0 -62
- package/dist/rpc/client.d.ts.map +0 -1
- package/dist/rpc/client.js +0 -103
- package/dist/rpc/client.js.map +0 -1
- package/dist/rpc/deferred.d.ts +0 -60
- package/dist/rpc/deferred.d.ts.map +0 -1
- package/dist/rpc/deferred.js +0 -96
- package/dist/rpc/deferred.js.map +0 -1
- package/dist/rpc/index.d.ts +0 -22
- package/dist/rpc/index.d.ts.map +0 -1
- package/dist/rpc/index.js +0 -38
- package/dist/rpc/index.js.map +0 -1
- package/dist/rpc/local.d.ts +0 -42
- package/dist/rpc/local.d.ts.map +0 -1
- package/dist/rpc/local.js +0 -50
- package/dist/rpc/local.js.map +0 -1
- package/dist/rpc/server.d.ts +0 -165
- package/dist/rpc/server.d.ts.map +0 -1
- package/dist/rpc/server.js +0 -405
- package/dist/rpc/server.js.map +0 -1
- package/dist/rpc/session.d.ts +0 -32
- package/dist/rpc/session.d.ts.map +0 -1
- package/dist/rpc/session.js +0 -43
- package/dist/rpc/session.js.map +0 -1
- package/dist/rpc/transport.d.ts +0 -306
- package/dist/rpc/transport.d.ts.map +0 -1
- package/dist/rpc/transport.js +0 -731
- package/dist/rpc/transport.js.map +0 -1
- package/src/batch/anthropic.js +0 -256
- package/src/batch/bedrock.js +0 -584
- package/src/batch/cloudflare.js +0 -287
- package/src/batch/google.js +0 -359
- package/src/batch/index.js +0 -30
- package/src/batch/memory.js +0 -187
- package/src/batch/openai.js +0 -402
- package/src/eval/index.js +0 -7
- package/src/eval/models.js +0 -119
- package/src/eval/runner.js +0 -147
- package/test/schema.test.js +0 -96
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Data Extraction from Documents Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates extracting structured data from various document types
|
|
5
|
+
* using ai-functions. It shows how to:
|
|
6
|
+
* - Extract entities from unstructured text
|
|
7
|
+
* - Parse specific document formats (invoices, resumes, contracts)
|
|
8
|
+
* - Handle tables and structured sections
|
|
9
|
+
* - Validate extracted data
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```bash
|
|
13
|
+
* ANTHROPIC_API_KEY=sk-... npx tsx examples/05-document-extraction.ts
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { ai, extract, list, is, configure, schema } from '../src/index.js'
|
|
18
|
+
|
|
19
|
+
// ============================================================================
|
|
20
|
+
// Sample Documents
|
|
21
|
+
// ============================================================================
|
|
22
|
+
|
|
23
|
+
const sampleInvoice = `
|
|
24
|
+
INVOICE #INV-2024-0042
|
|
25
|
+
|
|
26
|
+
Bill To: Ship To:
|
|
27
|
+
Acme Corporation Acme Corporation
|
|
28
|
+
123 Business Lane 456 Warehouse Rd
|
|
29
|
+
San Francisco, CA 94102 Oakland, CA 94612
|
|
30
|
+
|
|
31
|
+
Invoice Date: January 15, 2024
|
|
32
|
+
Due Date: February 14, 2024
|
|
33
|
+
Payment Terms: Net 30
|
|
34
|
+
|
|
35
|
+
| Item | Qty | Unit Price | Total |
|
|
36
|
+
|------------------------|-----|------------|------------|
|
|
37
|
+
| Widget Pro (WP-100) | 50 | $29.99 | $1,499.50 |
|
|
38
|
+
| Widget Basic (WB-50) | 100 | $14.99 | $1,499.00 |
|
|
39
|
+
| Premium Support (1yr) | 1 | $999.00 | $999.00 |
|
|
40
|
+
| Shipping & Handling | - | - | $45.00 |
|
|
41
|
+
|
|
42
|
+
Subtotal: $4,042.50
|
|
43
|
+
Tax (8.5%): $343.61
|
|
44
|
+
TOTAL DUE: $4,386.11
|
|
45
|
+
|
|
46
|
+
Payment Methods: Check, Wire Transfer, ACH
|
|
47
|
+
Bank: First National Bank
|
|
48
|
+
Account: 1234567890
|
|
49
|
+
Routing: 021000021
|
|
50
|
+
`
|
|
51
|
+
|
|
52
|
+
const sampleResume = `
|
|
53
|
+
SARAH CHEN
|
|
54
|
+
Software Engineer | AI/ML Specialist
|
|
55
|
+
san.chen@email.com | (555) 123-4567 | linkedin.com/in/sarahchen | github.com/schen
|
|
56
|
+
|
|
57
|
+
SUMMARY
|
|
58
|
+
Experienced software engineer with 6+ years building ML systems at scale.
|
|
59
|
+
Passionate about NLP and computer vision applications.
|
|
60
|
+
|
|
61
|
+
EXPERIENCE
|
|
62
|
+
|
|
63
|
+
Senior Machine Learning Engineer | TechCorp Inc. | Jan 2021 - Present
|
|
64
|
+
- Led team of 5 engineers building recommendation system serving 10M users
|
|
65
|
+
- Reduced model inference latency by 40% through optimization
|
|
66
|
+
- Implemented A/B testing framework for ML experiments
|
|
67
|
+
- Technologies: Python, TensorFlow, Kubernetes, AWS SageMaker
|
|
68
|
+
|
|
69
|
+
Software Engineer | StartupAI | June 2018 - Dec 2020
|
|
70
|
+
- Built NLP pipeline processing 1M documents daily
|
|
71
|
+
- Developed real-time fraud detection system (99.2% accuracy)
|
|
72
|
+
- Created data labeling tools that reduced annotation time by 60%
|
|
73
|
+
|
|
74
|
+
EDUCATION
|
|
75
|
+
|
|
76
|
+
M.S. Computer Science | Stanford University | 2018
|
|
77
|
+
- Focus: Machine Learning, Natural Language Processing
|
|
78
|
+
- GPA: 3.9/4.0
|
|
79
|
+
|
|
80
|
+
B.S. Computer Science | UC Berkeley | 2016
|
|
81
|
+
- Summa Cum Laude
|
|
82
|
+
- Dean's List all semesters
|
|
83
|
+
|
|
84
|
+
SKILLS
|
|
85
|
+
|
|
86
|
+
Languages: Python, TypeScript, Go, SQL
|
|
87
|
+
ML/AI: TensorFlow, PyTorch, scikit-learn, Hugging Face
|
|
88
|
+
Cloud: AWS, GCP, Kubernetes, Docker
|
|
89
|
+
Other: Git, CI/CD, Agile/Scrum
|
|
90
|
+
|
|
91
|
+
CERTIFICATIONS
|
|
92
|
+
- AWS Certified Machine Learning Specialty (2023)
|
|
93
|
+
- Google Professional ML Engineer (2022)
|
|
94
|
+
`
|
|
95
|
+
|
|
96
|
+
const sampleContract = `
|
|
97
|
+
SERVICE AGREEMENT
|
|
98
|
+
|
|
99
|
+
This Service Agreement ("Agreement") is entered into as of March 1, 2024
|
|
100
|
+
("Effective Date") by and between:
|
|
101
|
+
|
|
102
|
+
Provider: CloudServices LLC
|
|
103
|
+
Address: 789 Tech Blvd, Austin, TX 78701
|
|
104
|
+
Contact: contracts@cloudservices.com
|
|
105
|
+
|
|
106
|
+
Client: RetailCo Inc.
|
|
107
|
+
Address: 321 Commerce St, Dallas, TX 75201
|
|
108
|
+
Contact: legal@retailco.com
|
|
109
|
+
|
|
110
|
+
1. SERVICES
|
|
111
|
+
Provider agrees to provide cloud hosting services including:
|
|
112
|
+
- Dedicated server instances (4x 32-core, 128GB RAM)
|
|
113
|
+
- 99.9% uptime SLA
|
|
114
|
+
- 24/7 technical support
|
|
115
|
+
- Daily automated backups
|
|
116
|
+
|
|
117
|
+
2. TERM
|
|
118
|
+
Initial term: 24 months
|
|
119
|
+
Auto-renewal: Yes, 12-month periods unless cancelled with 60 days notice
|
|
120
|
+
|
|
121
|
+
3. FEES
|
|
122
|
+
Monthly fee: $8,500
|
|
123
|
+
Annual total: $102,000
|
|
124
|
+
Payment due: Net 15 from invoice date
|
|
125
|
+
|
|
126
|
+
4. TERMINATION
|
|
127
|
+
Either party may terminate with 60 days written notice.
|
|
128
|
+
Early termination fee: 3 months of fees.
|
|
129
|
+
|
|
130
|
+
5. CONFIDENTIALITY
|
|
131
|
+
Both parties agree to maintain confidentiality of proprietary information.
|
|
132
|
+
Duration: 3 years after termination.
|
|
133
|
+
|
|
134
|
+
Signed:
|
|
135
|
+
_____________________ _____________________
|
|
136
|
+
CloudServices LLC RetailCo Inc.
|
|
137
|
+
Date: March 1, 2024 Date: March 1, 2024
|
|
138
|
+
`
|
|
139
|
+
|
|
140
|
+
// ============================================================================
|
|
141
|
+
// Extraction Schemas
|
|
142
|
+
// ============================================================================
|
|
143
|
+
|
|
144
|
+
interface InvoiceData {
|
|
145
|
+
invoiceNumber: string
|
|
146
|
+
invoiceDate: string
|
|
147
|
+
dueDate: string
|
|
148
|
+
billTo: {
|
|
149
|
+
company: string
|
|
150
|
+
address: string
|
|
151
|
+
}
|
|
152
|
+
shipTo: {
|
|
153
|
+
company: string
|
|
154
|
+
address: string
|
|
155
|
+
}
|
|
156
|
+
lineItems: {
|
|
157
|
+
description: string
|
|
158
|
+
quantity: number
|
|
159
|
+
unitPrice: number
|
|
160
|
+
total: number
|
|
161
|
+
}[]
|
|
162
|
+
subtotal: number
|
|
163
|
+
tax: number
|
|
164
|
+
total: number
|
|
165
|
+
paymentTerms: string
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
interface ResumeData {
|
|
169
|
+
name: string
|
|
170
|
+
title: string
|
|
171
|
+
contact: {
|
|
172
|
+
email: string
|
|
173
|
+
phone: string
|
|
174
|
+
linkedin?: string
|
|
175
|
+
github?: string
|
|
176
|
+
}
|
|
177
|
+
summary: string
|
|
178
|
+
experience: {
|
|
179
|
+
title: string
|
|
180
|
+
company: string
|
|
181
|
+
period: string
|
|
182
|
+
highlights: string[]
|
|
183
|
+
}[]
|
|
184
|
+
education: {
|
|
185
|
+
degree: string
|
|
186
|
+
school: string
|
|
187
|
+
year: string
|
|
188
|
+
details?: string
|
|
189
|
+
}[]
|
|
190
|
+
skills: {
|
|
191
|
+
category: string
|
|
192
|
+
items: string[]
|
|
193
|
+
}[]
|
|
194
|
+
certifications: string[]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
interface ContractData {
|
|
198
|
+
type: string
|
|
199
|
+
effectiveDate: string
|
|
200
|
+
parties: {
|
|
201
|
+
role: string
|
|
202
|
+
name: string
|
|
203
|
+
address: string
|
|
204
|
+
contact: string
|
|
205
|
+
}[]
|
|
206
|
+
term: {
|
|
207
|
+
duration: string
|
|
208
|
+
autoRenewal: boolean
|
|
209
|
+
cancellationNotice: string
|
|
210
|
+
}
|
|
211
|
+
financials: {
|
|
212
|
+
monthlyFee: number
|
|
213
|
+
annualTotal: number
|
|
214
|
+
paymentTerms: string
|
|
215
|
+
}
|
|
216
|
+
keyTerms: string[]
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ============================================================================
|
|
220
|
+
// Extractors
|
|
221
|
+
// ============================================================================
|
|
222
|
+
|
|
223
|
+
async function extractInvoice(document: string): Promise<InvoiceData> {
|
|
224
|
+
console.log('\n--- Extracting Invoice Data ---')
|
|
225
|
+
|
|
226
|
+
const data = await ai`Extract all data from this invoice:
|
|
227
|
+
|
|
228
|
+
${document}
|
|
229
|
+
|
|
230
|
+
Provide a complete extraction with:
|
|
231
|
+
- invoiceNumber: the invoice number/ID
|
|
232
|
+
- invoiceDate: invoice date
|
|
233
|
+
- dueDate: payment due date
|
|
234
|
+
- billTo: { company, address }
|
|
235
|
+
- shipTo: { company, address }
|
|
236
|
+
- lineItems: array of { description, quantity (number), unitPrice (number), total (number) }
|
|
237
|
+
- subtotal: number
|
|
238
|
+
- tax: number
|
|
239
|
+
- total: number
|
|
240
|
+
- paymentTerms: payment terms`
|
|
241
|
+
|
|
242
|
+
return data as unknown as InvoiceData
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function extractResume(document: string): Promise<ResumeData> {
|
|
246
|
+
console.log('\n--- Extracting Resume Data ---')
|
|
247
|
+
|
|
248
|
+
const data = await ai`Extract all data from this resume:
|
|
249
|
+
|
|
250
|
+
${document}
|
|
251
|
+
|
|
252
|
+
Provide a complete extraction with:
|
|
253
|
+
- name: candidate name
|
|
254
|
+
- title: current/desired title
|
|
255
|
+
- contact: { email, phone, linkedin, github }
|
|
256
|
+
- summary: professional summary
|
|
257
|
+
- experience: array of { title, company, period, highlights: array of achievements }
|
|
258
|
+
- education: array of { degree, school, year, details }
|
|
259
|
+
- skills: array of { category, items: array of skills }
|
|
260
|
+
- certifications: array of certification names`
|
|
261
|
+
|
|
262
|
+
return data as unknown as ResumeData
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function extractContract(document: string): Promise<ContractData> {
|
|
266
|
+
console.log('\n--- Extracting Contract Data ---')
|
|
267
|
+
|
|
268
|
+
const data = await ai`Extract all data from this contract:
|
|
269
|
+
|
|
270
|
+
${document}
|
|
271
|
+
|
|
272
|
+
Provide a complete extraction with:
|
|
273
|
+
- type: type of agreement
|
|
274
|
+
- effectiveDate: when the contract starts
|
|
275
|
+
- parties: array of { role (provider/client), name, address, contact }
|
|
276
|
+
- term: { duration, autoRenewal: boolean, cancellationNotice }
|
|
277
|
+
- financials: { monthlyFee: number, annualTotal: number, paymentTerms }
|
|
278
|
+
- keyTerms: array of important contract terms/obligations`
|
|
279
|
+
|
|
280
|
+
return data as unknown as ContractData
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ============================================================================
|
|
284
|
+
// Entity Extraction
|
|
285
|
+
// ============================================================================
|
|
286
|
+
|
|
287
|
+
async function extractEntities(text: string): Promise<void> {
|
|
288
|
+
console.log('\n--- Entity Extraction ---')
|
|
289
|
+
|
|
290
|
+
// Extract different entity types
|
|
291
|
+
const emails = await extract`all email addresses from: ${text}`
|
|
292
|
+
console.log('Emails:', emails)
|
|
293
|
+
|
|
294
|
+
const phones = await extract`all phone numbers from: ${text}`
|
|
295
|
+
console.log('Phones:', phones)
|
|
296
|
+
|
|
297
|
+
const dates = await extract`all dates and time periods from: ${text}`
|
|
298
|
+
console.log('Dates:', dates)
|
|
299
|
+
|
|
300
|
+
const amounts = await extract`all monetary amounts and numbers from: ${text}`
|
|
301
|
+
console.log('Amounts:', amounts)
|
|
302
|
+
|
|
303
|
+
const companies = await extract`all company/organization names from: ${text}`
|
|
304
|
+
console.log('Companies:', companies)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ============================================================================
|
|
308
|
+
// Validation
|
|
309
|
+
// ============================================================================
|
|
310
|
+
|
|
311
|
+
async function validateExtraction(
|
|
312
|
+
original: string,
|
|
313
|
+
extracted: unknown,
|
|
314
|
+
docType: string
|
|
315
|
+
): Promise<{ valid: boolean; issues: string[] }> {
|
|
316
|
+
console.log(`\n--- Validating ${docType} Extraction ---`)
|
|
317
|
+
|
|
318
|
+
const { valid, issues } = await ai`Validate this extraction against the original document:
|
|
319
|
+
|
|
320
|
+
Original Document:
|
|
321
|
+
${original.substring(0, 1000)}...
|
|
322
|
+
|
|
323
|
+
Extracted Data:
|
|
324
|
+
${JSON.stringify(extracted, null, 2)}
|
|
325
|
+
|
|
326
|
+
Check for:
|
|
327
|
+
- Missing important information
|
|
328
|
+
- Incorrect values or parsing errors
|
|
329
|
+
- Data type issues (strings vs numbers)
|
|
330
|
+
|
|
331
|
+
Provide:
|
|
332
|
+
- valid: boolean (true if extraction is accurate and complete)
|
|
333
|
+
- issues: array of any problems found (empty if valid)`
|
|
334
|
+
|
|
335
|
+
const validationResult = {
|
|
336
|
+
valid: valid as boolean,
|
|
337
|
+
issues: issues as string[],
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
console.log(`Valid: ${validationResult.valid}`)
|
|
341
|
+
if (validationResult.issues.length > 0) {
|
|
342
|
+
console.log('Issues:', validationResult.issues)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return validationResult
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ============================================================================
|
|
349
|
+
// Format Display
|
|
350
|
+
// ============================================================================
|
|
351
|
+
|
|
352
|
+
function displayInvoice(data: InvoiceData): void {
|
|
353
|
+
console.log(`
|
|
354
|
+
Invoice: ${data.invoiceNumber}
|
|
355
|
+
Date: ${data.invoiceDate} | Due: ${data.dueDate}
|
|
356
|
+
|
|
357
|
+
Bill To: ${data.billTo?.company}
|
|
358
|
+
${data.billTo?.address}
|
|
359
|
+
|
|
360
|
+
Items:`)
|
|
361
|
+
for (const item of data.lineItems || []) {
|
|
362
|
+
console.log(` - ${item.description}: ${item.quantity} x $${item.unitPrice} = $${item.total}`)
|
|
363
|
+
}
|
|
364
|
+
console.log(`
|
|
365
|
+
Subtotal: $${data.subtotal}
|
|
366
|
+
Tax: $${data.tax}
|
|
367
|
+
TOTAL: $${data.total}`)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function displayResume(data: ResumeData): void {
|
|
371
|
+
console.log(`
|
|
372
|
+
Candidate: ${data.name}
|
|
373
|
+
Title: ${data.title}
|
|
374
|
+
Contact: ${data.contact?.email} | ${data.contact?.phone}
|
|
375
|
+
|
|
376
|
+
Summary: ${data.summary?.substring(0, 100)}...
|
|
377
|
+
|
|
378
|
+
Experience:`)
|
|
379
|
+
for (const exp of data.experience || []) {
|
|
380
|
+
console.log(` ${exp.title} @ ${exp.company} (${exp.period})`)
|
|
381
|
+
}
|
|
382
|
+
console.log(`
|
|
383
|
+
Education:`)
|
|
384
|
+
for (const edu of data.education || []) {
|
|
385
|
+
console.log(` ${edu.degree} - ${edu.school} (${edu.year})`)
|
|
386
|
+
}
|
|
387
|
+
console.log(`
|
|
388
|
+
Skills: ${(data.skills || [])
|
|
389
|
+
.flatMap((s) => s.items)
|
|
390
|
+
.slice(0, 10)
|
|
391
|
+
.join(', ')}...
|
|
392
|
+
Certifications: ${(data.certifications || []).length}`)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function displayContract(data: ContractData): void {
|
|
396
|
+
console.log(`
|
|
397
|
+
Contract Type: ${data.type}
|
|
398
|
+
Effective: ${data.effectiveDate}
|
|
399
|
+
|
|
400
|
+
Parties:`)
|
|
401
|
+
for (const party of data.parties || []) {
|
|
402
|
+
console.log(` ${party.role}: ${party.name}`)
|
|
403
|
+
}
|
|
404
|
+
console.log(`
|
|
405
|
+
Term: ${data.term?.duration}
|
|
406
|
+
Auto-Renewal: ${data.term?.autoRenewal}
|
|
407
|
+
|
|
408
|
+
Financials:
|
|
409
|
+
Monthly: $${data.financials?.monthlyFee}
|
|
410
|
+
Annual: $${data.financials?.annualTotal}
|
|
411
|
+
|
|
412
|
+
Key Terms: ${(data.keyTerms || []).length} extracted`)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// ============================================================================
|
|
416
|
+
// Main Example
|
|
417
|
+
// ============================================================================
|
|
418
|
+
|
|
419
|
+
async function main() {
|
|
420
|
+
console.log('\n=== Document Data Extraction Example ===\n')
|
|
421
|
+
|
|
422
|
+
// Configure the AI provider
|
|
423
|
+
configure({
|
|
424
|
+
model: 'sonnet',
|
|
425
|
+
provider: 'anthropic',
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
// Extract and display invoice
|
|
429
|
+
const invoiceData = await extractInvoice(sampleInvoice)
|
|
430
|
+
displayInvoice(invoiceData)
|
|
431
|
+
await validateExtraction(sampleInvoice, invoiceData, 'Invoice')
|
|
432
|
+
|
|
433
|
+
// Extract and display resume
|
|
434
|
+
const resumeData = await extractResume(sampleResume)
|
|
435
|
+
displayResume(resumeData)
|
|
436
|
+
await validateExtraction(sampleResume, resumeData, 'Resume')
|
|
437
|
+
|
|
438
|
+
// Extract and display contract
|
|
439
|
+
const contractData = await extractContract(sampleContract)
|
|
440
|
+
displayContract(contractData)
|
|
441
|
+
await validateExtraction(sampleContract, contractData, 'Contract')
|
|
442
|
+
|
|
443
|
+
// General entity extraction demo
|
|
444
|
+
await extractEntities(sampleResume)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
main()
|
|
448
|
+
.then(() => {
|
|
449
|
+
console.log('\n=== Example Complete ===\n')
|
|
450
|
+
process.exit(0)
|
|
451
|
+
})
|
|
452
|
+
.catch((error) => {
|
|
453
|
+
console.error('\nError:', error.message)
|
|
454
|
+
process.exit(1)
|
|
455
|
+
})
|