@forgeframework/ai-docx
v0.3.0
Published
AI-powered document analysis for the Forge Framework.
Maintainers
Readme
@forgeframework/ai-docx
AI-powered document analysis for the Forge Framework.
Part of the Forge Framework — a TypeScript framework for backend microservices and web applications.
Installation
npm install @forgeframework/ai-docxUsage
import { DocxAIProvider, DocumentInputType, OCRProcessor } from '@forgeframework/ai-docx';
import { DefaultAIProviderFactory, AIFramework, AIModelProvider } from '@forgeframework/ai-llm';
import { ConfigLoader } from '@forgeframework/config';
import { LoggerFactory } from '@forgeframework/logging';
import { z } from 'zod';
const config = await new ConfigLoader({ sources: [{ type: 'env', prefix: 'FORGE' }] }).load();
const factory = new DefaultAIProviderFactory(config, LoggerFactory);
const logger = LoggerFactory.create('doc-service');
const llmProvider = factory.createInstance({
framework: AIFramework.NATIVE,
provider: AIModelProvider.OPENAI,
model: 'gpt-4o',
apiKey: config.get('ai.openai.apiKey'),
});
const docProvider = new DocxAIProvider(llmProvider, logger);
const result = await docProvider.analyze(
'/path/to/invoice.pdf',
'Extract all line items and totals from this invoice.',
{ inputType: DocumentInputType.FILE_PATH, model: 'gpt-4o' },
);
const InvoiceSchema = z.object({
vendorName: z.string(),
invoiceNumber: z.string(),
lineItems: z.array(z.object({
description: z.string(),
quantity: z.number(),
unitPrice: z.number(),
total: z.number(),
})),
total: z.number(),
});
const extracted = await docProvider.extractStructured('/path/to/invoice.pdf', {
schema: InvoiceSchema,
prompt: 'Extract structured invoice data.',
inputType: DocumentInputType.FILE_PATH,
retryOnParseError: true,
maxRetries: 2,
});
const batchResult = await docProvider.analyzeBatch(
[
{ data: '/path/to/doc1.pdf', inputType: DocumentInputType.FILE_PATH, id: 'doc-1' },
{ data: '/path/to/doc2.pdf', inputType: DocumentInputType.FILE_PATH, id: 'doc-2' },
],
'Summarize this document in 3 sentences.',
{ model: 'gpt-4o', inputType: DocumentInputType.FILE_PATH, concurrency: 2, continueOnError: true },
);
const ocr = new OCRProcessor(llmProvider, logger);
const ocrResult = await ocr.extractText(imageBuffer, { language: 'en', enhanceContrast: true });Documentation
Full API reference and guides: https://carbonforge.io/framework/docs/ai/ai-docx
(Documentation site launching soon.)
License
MIT © Carbon Forge
