CoolFace
Apppublic

GargiG15/Accounts_Payable_AI_Auditor_

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
App README

AP Workflow OpenEnv ๐Ÿงพ

![OpenEnv](https://openenv.dev) ![Python](https://python.org) ![License](LICENSE)

Overview

AP Workflow OpenEnv simulates the Accounts Payable (AP) invoice processing workflow โ€” a critical real-world finance task performed by every company on earth. AP clerks handle incoming invoices by:

  1. 1.Validating invoice data against Purchase Orders (POs)
  2. 2.Detecting fraudulent or duplicate invoices
  3. 3.Routing high-value invoices for manager approval
  4. 4.Posting approved invoices to the General Ledger (GL)

Humans spend 20โ€“40 hours/week on AP tasks. AI agents that can automate this accurately save enterprises millions annually. This environment enables RL researchers and agent developers to train and evaluate agents on this domain.


Why This Environment?

  • โ€”Real-world utility: AP processing is a billion-dollar automation target. No existing OpenEnv covers financial document workflows.
  • โ€”Multi-step reasoning: Requires reading structured data, cross-referencing POs, detecting anomalies, and making routing decisions.
  • โ€”Fraud detection: Agents must identify signals like amount spikes, fake vendor names, and round-number patterns.
  • โ€”Partial rewards: Dense reward shaping guides agents step-by-step rather than sparse end-of-episode signals.

Action Space

Actions are structured JSON with the following tools:

ToolParametersDescription
validate_datainvoice_id, amount, vendor_idVerify invoice fields against PO
flag_fraudinvoice_id, reasonMark invoice as fraudulent
mark_duplicateinvoice_id, original_idMark invoice as a duplicate
route_approvalinvoice_id, approver_id, amountRoute high-value invoice for approval
post_ledgerinvoice_id, gl_code, amountPost validated invoice to GL
rejectinvoice_id, reasonReject invalid invoice
request_infoinvoice_id, fieldRequest additional information

Example Action

json
{
  "tool_call": "validate_data",
  "parameters": {
    "invoice_id": "INV-A1B2C3D4",
    "amount": 1250.00,
    "vendor_id": "V002"
  },
  "reasoning": "Checking if invoice amount matches PO approved amount within 5% tolerance"
}

Observation Space

Each observation contains:

FieldTypeDescription
task_idstrCurrent task identifier
task_promptstrNatural language objective
step_numberintCurrent step count
current_invoiceInvoiceNext invoice to process
pending_invoicesList[Invoice]All unprocessed invoices
purchase_ordersList[PO]Available POs for matching
ledger_entriesList[Entry]Posted ledger entries
approvals_historyList[Approval]Manager approval records
last_action_resultstrFeedback from previous action
available_actionsList[str]Valid tool names

Invoice Object

json
{
  "invoice_id": "INV-A1B2C3D4",
  "vendor_name": "TechParts Ltd.",
  "vendor_id": "V002",
  "amount": 1250.00,
  "date": "2025-01-15",
  "due_date": "2025-02-14",
  "po_number": "PO-XYZ123",
  "line_items": [...],
  "status": "pending",
  "is_duplicate": false,
  "is_fraudulent": false
}

Tasks

Easy: Single Invoice Validation

  • โ€”Max Steps: 20
  • โ€”Description: Validate one basic invoice against a Purchase Order. Check amount, date, vendor, and post to the correct GL account.
  • โ€”Grader:
  • โ€”1.0 = Correctly validated + posted to correct GL
  • โ€”0.7 = Posted but wrong GL code
  • โ€”0.5 = Validated but not posted
  • โ€”0.0 = Valid invoice rejected
  • โ€”Expected baseline (GPT-4o): ~0.92

Medium: Fraud & Duplicate Detection

  • โ€”Max Steps: 35
  • โ€”Description: Process 3 invoices: 1 valid, 1 fraudulent, 1 duplicate. Correctly classify each.
  • โ€”Grader:
  • โ€”(TP_fraud + TP_dup + TP_valid_posted) / total + 0.2 ร— prevented_loss_ratio
  • โ€”False positives penalized at -0.1 each
  • โ€”Expected baseline (GPT-4o): ~0.67

Hard: Full AP Cycle with Approvals

  • โ€”Max Steps: 50
  • โ€”Description: Process 5 invoices over a simulated 3-day cycle. Includes 1 high-value invoice requiring approval routing, 1 fraudulent, 1 duplicate. Must post all valid invoices to correct GL accounts.
  • โ€”Grader:
  • โ€”0.4 ร— validation_accuracy + 0.3 ร— approval_routing_correct + 0.3 ร— ledger_accuracy
  • โ€”Episode ends when all invoices processed or max steps reached
  • โ€”Expected baseline (GPT-4o): ~0.41

Reward Function

Rewards are shaped to provide dense feedback throughout the episode:

EventReward
Correct field validation (per field)+0.10
Fraud correctly caught+0.30
Duplicate correctly caught+0.30
Correct approval routing+0.30
Invoice posted to correct GL+0.50
Invoice posted to wrong GL+0.20
Valid invoice rejected-0.30
False positive (wrong fraud/dup flag)-0.10
Hallucinated invoice ID-0.20
Loop penalty (>20 steps)-0.05/step

Project Structure

ap-openenv/
โ”œโ”€โ”€ openenv.yaml     # OpenEnv metadata and task list
โ”œโ”€โ”€ models.py        # Pydantic: Observation, Action, Reward, StepResult
โ”œโ”€โ”€ env.py           # APWorkflowEnv: step() / reset() / state()
โ”œโ”€โ”€ grader.py        # Task-specific graders (0.0โ€“1.0)
โ”œโ”€โ”€ data_generator.py # Invoice/PO generation with fraud injection
โ”œโ”€โ”€ server.py        # FastAPI OpenEnv endpoints
โ”œโ”€โ”€ inference.py     # Baseline inference script (OpenAI client)
โ”œโ”€โ”€ Dockerfile       # Container definition
โ”œโ”€โ”€ requirements.txt # Python dependencies
โ””โ”€โ”€ README.md        # This file

Setup & Usage

Local Development

bash
# Install dependencies
pip install -r requirements.txt

# Start the server
python server.py
# โ†’ Server running at http://localhost:8000

# In another terminal, run baseline
export OPENAI_API_KEY=sk-your-key
python inference.py

Docker

bash
# Build
docker build -t ap-env .

# Run
docker run -p 8000:8000 ap-env

# Run baseline against dockerized env
export OPENAI_API_KEY=sk-your-key
export AP_ENV_URL=http://localhost:8000
python inference.py

Hugging Face Spaces

Deploy directly to HF Spaces โ€” the server runs FastAPI on port 7860 (set PORT=7860).


API Reference

POST /reset

json
{"task_id": "single_invoice_validation", "seed": 42}

POST /step

json
{
  "tool_call": "validate_data",
  "parameters": {"invoice_id": "INV-...", "amount": 1000.0},
  "reasoning": "Checking fields"
}

GET /state

Returns full internal state for debugging.

POST /grade

json
{"task_id": "single_invoice_validation"}

Returns {"score": 0.92, "breakdown": {...}, "passed": true, "feedback": "..."}


Baseline Scores

TaskModelScoreSteps
singleinvoicevalidationGPT-4o-mini~0.925
fraudduplicatedetectionGPT-4o-mini~0.6718
fullapcycleGPT-4o-mini~0.4142

Reproduce with:

bash
export OPENAI_API_KEY=your-key
export MODEL_NAME=gpt-4o-mini
python inference.py

Environment Variables

VariableDefaultDescription
OPENAI_API_KEYโ€”OpenAI API key (required for inference)
API_BASE_URLhttps://api.openai.com/v1LLM API base URL
MODEL_NAMEgpt-4o-miniModel to use
AP_ENV_URLhttp://localhost:8000Environment server URL
AP_ENV_SEED42Random seed for reproducibility
PORT8000Server port

License

MIT License โ€” see LICENSE.