CLOUD
AI

AI Document Processing: OCR + RAG

CLOUD|April 17, 2026|3 min read|302 views
AI Document Processing: OCR + RAG

Convert PDF to text → store in vector database → ask AI. Complete document processing on your private server.

AI Document Processing Guide

Contract analysis, data extraction from invoices, document classification, answering questions from PDF files — all of these tasks can be automated with AI. This guide explains how to build a document processing system on your own server.

The Problem It Solves

Without AI:
├── Employee reads each invoice manually: 3-5 min/invoice
├── 100 invoices/day = 5 hours of manual work
└── Human error rate: 2-5%

With AI:
├── Process 100 invoices: under 1 minute
├── Accuracy: 94-98% (continuously improving)
└── Structured data ready for ERP

Types of Document Processing

1. Data Extraction

import anthropic
import base64

def extract_invoice_data(pdf_path: str) -> dict:
 """Extract invoice data from PDF"""
 
 client = anthropic.Anthropic
 
 with open(pdf_path, "rb") as f:
 pdf_data = base64.standard_b64encode(f.read).decode("utf-8")
 
 message = client.messages.create(
 model="claude-3-5-sonnet-20241022",
 max_tokens=1024,
 messages=[
 {
 "role": "user",
 "content": [
 {
 "type": "document",
 "source": {
 "type": "base64",
 "media_type": "application/pdf",
 "data": pdf_data,
 },
 },
 {
 "type": "text",
 "text": """Extract from this invoice:
 - Invoice number
 - Issue date
 - Vendor name and registration number
 - Amount before tax
 - Tax amount (15%)
 - Total
 
 Return the result as JSON."""
 }
 ],
 }
 ],
 )
 
 import json
 return json.loads(message.content[0].text)

2. Document Q&A System (RAG)

from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import PGVector
from langchain_anthropic import ChatAnthropic
from langchain.chains import RetrievalQA

CONNECTION_STRING = "postgresql+psycopg2://postgres:password@localhost/docdb"

vectorstore = PGVector.from_documents(
 documents=load_documents(["contract1.pdf", "contract2.pdf"]),
 embedding=embeddings,
 connection_string=CONNECTION_STRING,
 collection_name="contracts"
)

qa_chain = RetrievalQA.from_chain_type(
 llm=ChatAnthropic(model="claude-3-5-sonnet-20241022"),
 chain_type="stuff",
 retriever=vectorstore.as_retriever(search_kwargs={"k": 5})
)

answer = qa_chain.invoke({"query": "What are the contract termination terms?"})
print(answer["result"])

Batch Processing

import asyncio

async def process_document_batch(pdf_paths: list) -> list:
 """Process a batch of documents in parallel"""
 
 semaphore = asyncio.Semaphore(5) # 5 documents at a time
 
 async def limited_process(path):
 async with semaphore:
 return await process_single(path)
 
 tasks = [limited_process(p) for p in pdf_paths]
 return await asyncio.gather(*tasks)

Server Environment Setup

# Install requirements
pip install anthropic langchain langchain-anthropic langchain-community \
 pypdf psycopg2-binary pgvector python-dotenv

# PostgreSQL with pgvector
apt install postgresql postgresql-contrib
sudo -u postgres psql -c "CREATE EXTENSION vector;"

كلاود (cloud.net.sa) provides a ready AI Hosting template with PostgreSQL + pgvector pre-installed — you can start developing a document processing system immediately without setting up infrastructure.

Conclusion

AI document processing saves hours of manual work daily. The complete setup requires a VPS with 4+ GB RAM, PostgreSQL + pgvector, and the Python libraries mentioned. Accuracy exceeds 95% for most types of business documents.


Get Started

Ready to run AI on your own server? Explore AI hosting solutions or configure your server with the specs you need.

Share