Skip to content
reaatechREAATECH

Files · AWS Bedrock Code Sandbox for SMB Financial Report Generation

73 (1 binary, 524.9 kB total)attempt 2

README.md·3675 B·markdown
markdown
# AWS Bedrock Code Sandbox for SMB Financial Report Generation
 
> Let your team ask natural-language questions about financial data and get accurate, formatted reports generated safely in isolated code environments.
 
A reference recipe demonstrating how to build a production-grade financial report generation pipeline using AWS Bedrock for LLM-powered Python code generation, E2B sandboxes for secure code execution, and the `@reaatech/*` package family for reliability, caching, and budget control.
 
## How It Works
 
1. **Classify**`@reaatech/confidence-router` determines the report type (balance sheet, income statement, cash flow, or custom)
2. **Check Cache**`@reaatech/llm-cache` avoids recomputing identical queries
3. **Check Budget**`@reaatech/agent-budget-engine` caps total LLM spend per report
4. **Generate Code** — AWS Bedrock (Claude Sonnet) writes Python analysis code
5. **Execute**`@e2b/code-interpreter` runs the code in a secure sandbox
6. **Validate** — Structured output repair parses and validates the JSON result
7. **Format**`pdf-lib` generates PDF and `xlsx` generates Excel reports
8. **Return** — Base64-encoded PDF and XLSX are returned in the API response
 
## Architecture
 
| Package | Role |
|---|---|
| `@reaatech/confidence-router` | Request classification |
| `@reaatech/agent-budget-engine` | LLM spend capping |
| `@reaatech/llm-cache` | Response deduplication |
| `@reaatech/circuit-breaker-agents` | Sandbox fault tolerance |
| `@aws-sdk/client-bedrock-runtime` | LLM inference (Bedrock Converse API) |
| `@e2b/code-interpreter` | Secure code sandbox |
| `pdf-lib` + `xlsx` | Output formatting |
| `zod` | Schema validation |
 
## Prerequisites
 
- **AWS Account** with Bedrock access (Claude Sonnet 4 or compatible model)
- **E2B Account** with an API key ([e2b.dev](https://e2b.dev))
- **AWS CLI** configured with credentials (`aws configure`)
 
## Quick Start
 
```bash
pnpm install
```
 
Copy `.env.example` to `.env` and fill in your credentials:
 
```bash
cp .env.example .env
# Edit .env with your AWS region, keys, and E2B API key
```
 
Start the dev server:
 
```bash
pnpm dev
```
 
Generate a report:
 
```bash
curl -X POST http://localhost:3000/api/report/generate \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Generate a balance sheet for Q1 2026"}'
```
 
## Environment Variables
 
| Variable | Description |
|---|---|
| `AWS_REGION` | AWS region for Bedrock (e.g., `us-east-1`) |
| `AWS_ACCESS_KEY_ID` | AWS access key ID |
| `AWS_SECRET_ACCESS_KEY` | AWS secret access key |
| `E2B_API_KEY` | E2B API key for sandbox execution |
 
## Project Structure
 
```
app/
  api/report/generate/route.ts   POST /api/report/generate
  page.tsx                        Home page (demo UI)
  layout.tsx                      Root layout
src/
  lib/
    types.ts                      Zod schemas + TS types
    pricing-provider.ts           Bedrock model pricing
    spend-store.ts                In-memory spend tracking
    embedder.ts                   Deterministic embedding
    output-repair.ts              JSON validation & repair
    cache-factory.ts              LLM cache setup
  services/
    bedrock-service.ts            Bedrock Converse API client
    sandbox-service.ts            E2B sandbox executor (with CircuitBreaker)
    report-formatter.ts           PDF + XLSX generation
    report-generator.ts           Pipeline orchestrator
tests/                            Vitest test suite
```
 
## Running Tests
 
```bash
pnpm test          # vitest run with coverage
pnpm typecheck     # TypeScript type checking
pnpm lint          # ESLint
```
 
## License
 
MIT — see [LICENSE](./LICENSE).