convez.ai
PluginDevOps & InfraestruturaAvançado
loganathan-m-it

Inteligência de Documentos Serverless com AWS Bedrock

loganathan-m-it/serverless-document-intelligence-bedrock

API serverless para análise inteligente de documentos com Claude no Bedrock

Ver no GitHub
InicialPluginScore · de 0 a 100

Arquitetura de referência em produção para análise inteligente de documentos usando Claude 3.5 Sonnet via AWS Bedrock. Implementa upload, análise automática com extração de resumos e entidades, e Q&A sobre documentos através de Lambda, API Gateway, DynamoDB e S3 — totalmente serverless e escalável.

Instalação

$npx convez-ai loganathan-m-it/serverless-document-intelligence-bedrock

1 comando no terminal: detecta o formato do repositório (marketplace ou plugin avulso) e instala via Claude Code. Requer Node 18+ e o Claude Code instalado.

Métricas

Estrelas
0
Forks
0
Acompanhando
0
Saúde
57%
Último commit
há 6 meses
Versão
—
Issues abertas
0
Contribuidores
1

Casos de uso

  • Processamento em lote de contratos e documentos legais
  • Análise automática de registros médicos e prontuários
  • Extração de entidades e resumos de especificações técnicas
  • Chatbot Q&A sobre repositórios de documentos

Veredito

Projeto maduro e bem estruturado que demonstra padrões arquiteturais reais de produção para IA em escala serverless. Ideal para times que precisam oferecer processamento inteligente de documentos sem gerenciar infraestrutura, com ênfase em segurança (IAM, criptografia) e observabilidade (CloudWatch, X-Ray).

README

A tradução para o português deste README ainda está na fila.

Serverless Document Intelligence on AWS Bedrock

A production-grade, fully serverless document intelligence API powered by AWS Bedrock (Anthropic Claude 3.5 Sonnet). Upload a document → get an AI-generated summary, key entities, and Q&A capability — in seconds.

AWS Python Serverless License


Why this exists

Healthcare records, legal contracts, vendor agreements, and technical specifications all share one painful trait: a human has to read them carefully before any decision. That's hours per document, multiplied across thousands of documents per month.

This project is a reference architecture I built to demonstrate how to offload that cognitive work to Anthropic Claude via AWS Bedrock, while keeping the system:

  • Serverless — no EC2, no containers to patch, scales to zero when idle
  • Secure — least-privilege IAM, encrypted S3, Bedrock guardrails
  • Observable — structured logs, CloudWatch metrics, X-Ray tracing
  • Cost-aware — pay only for what you process; full cost breakdown below

It's deliberately minimal in lines of code but represents the architectural patterns I've used in production for healthcare and food-tech platforms over 13+ years.


Architecture

                    ┌─────────────────┐
                    │     Client      │
                    │  (curl / app)   │
                    └────────┬────────┘
                             │ HTTPS
                             ▼
                    ┌─────────────────┐
                    │   API Gateway   │  ◄── API key + throttling
                    │   (REST API)    │
                    └────────┬────────┘
                             │
              ┌──────────────┼──────────────┐
              │              │              │
              ▼              ▼              ▼
      ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
      │   Upload    │ │   Analyze   │ │     Ask     │
      │   Lambda    │ │   Lambda    │ │   Lambda    │
      └──────┬──────┘ └──────┬──────┘ └──────┬──────┘
             │               │               │
             │      ┌────────┼────────┐      │
             │      │        │        │      │
             ▼      ▼        ▼        ▼      ▼
        ┌─────────┐ ┌──────────────┐ ┌──────────────┐
        │   S3    │ │ AWS Bedrock  │ │  DynamoDB    │
        │ Bucket  │ │   (Claude    │ │  (results +  │
        │         │ │   3.5 Sonnet)│ │   history)   │
        └─────────┘ └──────────────┘ └──────────────┘
                             │
                             ▼
                    ┌─────────────────┐
                    │   CloudWatch    │
                    │  Logs & Metrics │
                    └─────────────────┘

Data flow:

  1. POST /documents — Client uploads a document; Lambda stores it in S3 (server-side encrypted), returns a document_id.
  2. POST /documents/{id}/analyze — Lambda fetches the document, calls Bedrock (Claude) with a structured prompt, persists summary + entities to DynamoDB.
  3. POST /documents/{id}/ask — Lambda retrieves the document context and calls Bedrock with the user question; conversation history is appended to DynamoDB.

See docs/architecture.md for sequence diagrams and design decisions.


Use cases this enables

DomainDocument typeOutput
HealthcarePatient records, lab reportsStructured summary, flagged abnormalities, ICD-10 hints
LegalContracts, NDAs, MSAsKey clauses, obligations, renewal dates, risk red-flags
ProcurementVendor proposals, RFPsPricing summary, deliverables list, deviation from template
EngineeringTechnical specs, RFCsTL;DR, component impact list, dependencies, open questions

Tech stack

  • Runtime: Python 3.12
  • Compute: AWS Lambda (3 functions, ARM64/Graviton2)
  • API: Amazon API Gateway (REST, regional)
  • Storage: Amazon S3 (SSE-S3, lifecycle to Glacier after 90 days)
  • Database: Amazon DynamoDB (on-demand billing, TTL on conversation history)
  • AI: Amazon Bedrock — anthropic.claude-3-5-sonnet-20241022-v2:0
  • IaC: Serverless Framework v3
  • Observability: CloudWatch Logs, CloudWatch Metrics, AWS X-Ray
  • Testing: pytest + moto (AWS mocking)

Quick start

Prerequisites

  • AWS account with Bedrock model access enabled for Claude 3.5 Sonnet (request access here)
  • AWS CLI configured (aws configure)
  • Node.js 20+ (for Serverless Framework)
  • Python 3.12+

Deploy in 3 commands

# 1. Install Serverless Framework + plugins
npm install -g serverless@3
npm install

# 2. Install Python dependencies
pip install -r requirements.txt

# 3. Deploy to AWS
sls deploy --stage dev --region us-east-1

Output will include the API Gateway endpoint, e.g.:

endpoints:
  POST - https://abc123.execute-api.us-east-1.amazonaws.com/dev/documents
  POST - https://abc123.execute-api.us-east-1.amazonaws.com/dev/documents/{id}/analyze
  POST - https://abc123.execute-api.us-east-1.amazonaws.com/dev/documents/{id}/ask

Tear down

sls remove --stage dev --region us-east-1

API examples

1. Upload a document

curl -X POST https://your-api/dev/documents \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "filename": "vendor-contract.txt",
    "content_base64": "VGhpcyBNYXN0ZXIgU2VydmljZXMgQWdyZWVtZW50IGlzIGVudGVyZWQgaW50by4uLg=="
  }'

Response:

{
  "document_id": "doc_a3f8c2b1e4d5",
  "status": "uploaded",
  "size_bytes": 4821
}

2. Analyze the document

curl -X POST https://your-api/dev/documents/doc_a3f8c2b1e4d5/analyze \
  -H "x-api-key: $API_KEY"

Response:

{
  "document_id": "doc_a3f8c2b1e4d5",
  "summary": "A Master Services Agreement between Acme Corp (vendor) and Globex Inc (client) for cloud infrastructure services. Term: 24 months auto-renewing. Total value: $480,000 over the initial term.",
  "entities": {
    "parties": ["Acme Corp", "Globex Inc"],
    "effective_date": "2026-01-15",
    "renewal_date": "2028-01-15",
    "total_value_usd": 480000,
    "key_obligations": [
      "99.9% uptime SLA",
      "30-day breach cure period",
      "Mutual NDA for 5 years post-termination"
    ]
  },
  "risk_flags": [
    "Auto-renewal with only 30-day opt-out window",
    "Liability cap is unusually low (3 months of fees)"
  ],
  "tokens_used": { "input": 1842, "output": 412 },
  "model": "anthropic.claude-3-5-sonnet-20241022-v2:0",
  "processed_at": "2026-04-29T10:14:22Z"
}

3. Ask follow-up questions

curl -X POST https://your-api/dev/documents/doc_a3f8c2b1e4d5/ask \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "question": "What happens if we miss a payment?" }'

Response:

{
  "document_id": "doc_a3f8c2b1e4d5",
  "question": "What happens if we miss a payment?",
  "answer": "Per Section 4.3, late payments accrue interest at 1.5% per month. After 30 days of non-payment, Acme Corp may suspend services with 7 days written notice. After 60 days, the agreement may be terminated for cause.",
  "citations": ["Section 4.3 — Late Payment", "Section 9.1 — Termination for Cause"],
  "tokens_used": { "input": 1956, "output": 87 }
}

Cost estimate

Based on us-east-1 pricing as of 2026, processing 1,000 documents/month (~5KB each, average 2 follow-up questions per doc):

ServiceUsageMonthly cost
Lambda3,000 invocations, 512MB$0.05
API Gateway3,000 REST requests$0.01
S35MB storage + requests$0.01
DynamoDB on-demand6,000 reads + 3,000 writes$0.04
Bedrock (Claude 3.5 Sonnet)~6M input + 1M output tokens~$33.00
CloudWatch Logs~500MB ingested$0.25
Total~$33.36/month

Bedrock dominates the cost. For higher volumes, consider Claude 3 Haiku for 10x cheaper inference on simpler documents.


Security

  • IAM: Each Lambda has its own least-privilege role; no * resources except CloudWatch Logs.
  • S3: Server-side encryption with SSE-S3, public access blocked, versioning enabled.
  • DynamoDB: Encryption at rest (AWS-managed KMS), TTL auto-deletes conversation history after 90 days.
  • API Gateway: API key required on every endpoint; per-key throttling at 10 req/sec.
  • Bedrock: Optional Bedrock Guardrails integration in src/services/bedrock_service.py to filter PII and harmful content.
  • Secrets: No secrets in code; Bedrock auth via IAM role, API keys via AWS Systems Manager Parameter Store.

See docs/security.md for the full threat model.


Observability

Every Lambda emits structured JSON logs with a correlation ID propagated from API Gateway:

{
  "level": "INFO",
  "correlation_id": "c8a1f2b3-...",
  "document_id": "doc_a3f8c2b1e4d5",
  "event": "bedrock.invoke.success",
  "model": "anthropic.claude-3-5-sonnet-20241022-v2:0",
  "input_tokens": 1842,
  "output_tokens": 412,
  "latency_ms": 2341
}

A pre-built CloudWatch dashboard (infra/dashboard.json) tracks:

  • Request rate per endpoint
  • p50 / p95 / p99 latency
  • Bedrock token usage and cost
  • Error rate, throttle rate, DLQ depth

Project structure

serverless-document-intelligence-bedrock/
├── README.md
├── serverless.yml              # IaC: Lambda, API GW, S3, DynamoDB, IAM
├── requirements.txt            # Python deps
├── package.json                # Serverless Framework plugins
├── src/
│   ├── handlers/
│   │   ├── upload.py           # POST /documents
│   │   ├── analyze.py          # POST /documents/{id}/analyze
│   │   └── ask.py              # POST /documents/{id}/ask
│   ├── services/
│   │   ├── bedrock_service.py  # Claude prompt + invocation
│   │   ├── s3_service.py       # Document storage
│   │   └── dynamo_service.py   # Metadata + conversation history
│   └── utils/
│       ├── logger.py           # Structured logging
│       └── responses.py        # API Gateway response helpers
├── tests/
│   ├── test_handlers.py
│   └── test_bedrock_service.py
├── docs/
│   ├── architecture.md
│   ├── security.md
│   └── prompts.md              # Claude prompt design rationale
└── scripts/
    └── invoke_local.sh         # Local testing helper

Lessons learned (production notes)

A few things I've learned the hard way that this repo encodes:

  1. Bedrock retries are mandatory. Throttling exceptions from Bedrock are common during traffic spikes — the service uses exponential backoff with jitter inside bedrock_service.py.
  2. Token counting is not free. Use Anthropic's tokenizer ahead of the Bedrock call to reject documents that exceed the context window before paying for a failed inference.
  3. Prompts are infrastructure. Treat docs/prompts.md like Terraform — version it, code-review it, and tag it in DynamoDB on every result so you can replay old documents against new prompts.
  4. DynamoDB TTL beats cron jobs. Auto-expiring conversation history with TTL is simpler and cheaper than a cleanup Lambda.
  5. API Gateway response transforms hide latency. Always return raw Lambda responses and let the client handle shaping — easier to debug.

Roadmap

  • Streaming responses via API Gateway WebSocket + Bedrock streaming API
  • Multi-document Q&A using S3 Vectors + RAG
  • Bedrock Guardrails integration for PII/PHI redaction
  • Step Functions orchestration for batch document processing
  • Frontend playground (React + Cognito)

License

MIT — see LICENSE.


About

Built by Loganathan M — Senior Software Engineer & AWS Architect with 13+ years across Java, Python, Node.js, and AWS serverless. This repo is part of a portfolio demonstrating production-grade AWS + GenAI patterns I use in client engagements.

Ver o original no GitHub

Bom para

Relacionados