A sophisticated refund processing system built with LangGraph that follows a structured Mermaid decision tree for accurate and transparent refund decisions.
- LangGraph Workflow: Structured workflow management with visual decision paths
- Mermaid Decision Tree: Exact implementation of business rules in flowchart format
- State Management: Comprehensive conversation state tracking
- Information Extraction: AI-powered extraction and validation of user information
- Multiple Decision Paths: Handles all refund scenarios with proper routing
- Transparent Processing: Clear decision path tracking and reasoning
LG/
├── config/
│ ├── config.py # Configuration management
│ └── .env # Environment variables
├── data/
│ └── customer_data.json # Sample customer data
├── src/
│ ├── state.py # State schema and management
│ ├── mermaid_tree.py # Decision tree structure
│ ├── extraction.py # Information extraction
│ ├── decision_nodes.py # Individual decision nodes
│ ├── workflow.py # LangGraph workflow
│ └── bot.py # Main conversation interface
├── main.py # Application entry point
├── requirements.txt # Dependencies
└── README.md # This file
The bot handles the following decision points from the Mermaid chart:
- Account Status Check: Validates customer account standing
- Loyalty Tier Routing: Bronze/Silver vs Gold customer paths
- Fraud Flag Check: Additional validation for Gold customers
- Return Abuse Detection: Identifies customers who abuse returns
- Item Category Classification: Physical/Digital/Perishable items
- Return Eligibility: Checks if item is marked as returnable
- Item Condition Assessment: Damaged/Defective/Normal condition
- Return Window Validation: Within/Expired timing checks
- Delivery Status: Delivered vs undelivered items
- Shipping Issue Handling: Lost/Delayed/Neither scenarios
- Seller Type Routing: In-house vs Third-party sellers
- Policy Compliance: Various policy checks
- Payment Method Processing: CreditCard/GiftCard/BNPL/Prepaid
- RefundApproved: Standard approval
- RefundDenied1-12: Various denial reasons
- PartialRefund: Late return or gift card scenarios
- ManualReview: Requires human intervention
- Clone or download the project
- Install dependencies:
pip install -r requirements.txt
- Configure environment variables in
config/.env:OPENAI_API_KEY=your_openai_api_key_here MODEL_NAME=gpt-4o-mini DEBUG_MODE=True
Run the bot:
python main.pyBasic Refund Request:
You: I want to return my broken laptop
Bot: What type of item are you returning? Please specify: Physical, Digital, or Perishable.
You: Physical
Bot: Is this item marked as returnable? Please specify: Yes or No.
You: Yes
Bot: [Processing through decision tree...]
Bot: [APPROVED] Decision: RefundApproved
Complex Scenario:
You: I need a refund for software I bought with a gift card
Bot: What type of item are you returning? Please specify: Physical, Digital, or Perishable.
You: Digital
Bot: [DENIED] Decision: RefundDenied4
Reason: Digital goods not refundable
status- Show current workflow status and decision pathreset- Start a new conversationexport- Save conversation history to JSONdebug- Show detailed state informationhelp- Show comprehensive helpquit- Exit the system
OPENAI_API_KEY: Your OpenAI API key (required)MODEL_NAME: OpenAI model to use (default: gpt-4o-mini)EXTRACTION_TEMPERATURE: Temperature for information extraction (default: 0.5)NAVIGATION_TEMPERATURE: Temperature for decision navigation (default: 0.0)QUESTION_TEMPERATURE: Temperature for question generation (default: 0.3)DEBUG_MODE: Enable debug output (default: True)
Modify data/customer_data.json to test different customer scenarios:
{
"customer_id": "CUST_67890",
"account_status": "good_standing",
"loyalty_tier": "Gold",
"fraud_flag": "No",
"return_abuse": "No"
}The system uses LangGraph to create a structured workflow that mirrors the Mermaid decision tree:
- Entry Point: Load customer data and extract information
- Decision Nodes: Individual nodes for each decision point
- Conditional Routing: Smart routing based on extracted information
- Terminal Nodes: Final decision points
- Question Generation: Dynamic question creation for missing information
Comprehensive state tracking includes:
- User conversation history
- Extracted information
- Customer data
- Decision path tracking
- Current workflow status
AI-powered extraction with:
- Natural language understanding
- Context-aware mapping
- Validation against valid keywords
- Fallback pattern matching