A modern version of the most basic finance tool, the piggy bank. I built this with and for my kids because I don't have quarters around me to pay/bribe them to do chores. My IOUs finally lost the charm and they stopped caring about doing extra work. Now, I can say 'Siri piggy bank, add fifty cents to Jimmy's account for taking out the trash' and he "gets" the money right away. They love following up with 'Siri piggy bank, how much money does Jimmy have', and get the satisfaction of it being more!
It's the easiest and most fun way to teach kids about money, savings and work value.
- AI-Powered Interface: Natural language interaction with your piggy bank
- Session Management: Maintains conversation context with automatic session cleanup
- Multi-tenant Support: Subscription-based access with secure token authentication
- Account Management: Create and manage multiple accounts per subscription
- Transaction Operations: Add money, withdraw money, and transfer between accounts
- Transaction History: View detailed transaction records
- Parallel Tool Execution: AI can execute multiple operations simultaneously
-
Run the setup script:
chmod +x setup.sh ./setup.sh
This will automatically:
- Create a virtual environment (if it doesn't exist)
- Activate the virtual environment
- Upgrade pip
- Install all required dependencies
-
Set up environment variables: Create a
.envfile in the project root with your OpenAI API key:OPEN_AI_KEY=your_openai_api_key_here -
Run the application:
python main.py
The API will be available at http://localhost:5000
To use the API, you first need a subscription token. You can generate one by running the generate_subscription.py script:
python generate_subscription.py "Your Name"This will create a new subscription and output an authentication token. Use this token as a Bearer token in your API requests.
The application provides an AI-powered conversational interface instead of traditional REST endpoints. All requests require authentication using a Bearer token.
Add Authorization header to your requests.
Authorization: Bearer <your_subscription_token>
- Save this shortcut to your device https://www.icloud.com/shortcuts/27ab3b09cd3441b18337ad3a0299890e
- Name it something like 'Piggy Bank'
- Edit the shortcut to add your authentication token in the "Get contents of" step:
- Click
Show More. - Set the auth_token value in the Text box.
- Click
- Click play to test the shortcut with some prompt, i.e.
Hi! - It should respond with a greeting.
- To continue the chat you will need to reprompt
siri piggy bankand then your prompt.
The primary endpoint for interacting with the AI assistant.
Request Body:
{
"query": "Your natural language request",
"session_id": "optional_session_id_for_conversation_continuity"
}Response:
{
"response": "AI assistant's response",
"session_id": "session_id_for_future_requests"
}Example Requests:
"Create an account called 'savings'""Add $100 to my savings account for salary""Transfer $50 from savings to checking for bills""What's my current balance in all accounts?""Show me recent transactions for my checking account"
The AI assistant can perform the following operations through natural language:
- Create Account: "Create a new account named [name]"
- List Accounts: "Show me all my accounts" or "What accounts do I have?"
- Get Balance: "What's my balance in [account]?" or "How much money is in [account]?"
- Add Money: "Add $[amount] to [account] for [reason]"
- Withdraw Money: "Withdraw $[amount] from [account] for [reason]"
- Transfer Money: "Transfer $[amount] from [account1] to [account2] for [reason]"
- Transaction History: "Show me transactions for [account]" or "What are my recent transactions?"
- Sessions automatically expire after 60 seconds of inactivity
- Include the
session_idfrom previous responses to maintain conversation context - The AI can remember previous context within the same session
- Sessions are automatically cleaned up on server startup and during operation
The application uses SQLite with the following tables:
-
subscriptions- Store subscription information and authentication tokensid(Primary Key)name- Name of the subscription holderauth_token- Unique authentication token
-
accounts- Store account information per subscriptionid(Primary Key)name- Account name (normalized)subscription_id- Foreign key to subscriptions table- Unique constraint on (name, subscription_id)
-
transactions- Store all financial transactionsid(Primary Key)account_id- Foreign key to accounts tableamount- Transaction amount (positive for deposits, negative for withdrawals)reason- Description/reason for the transactiontimestamp- When the transaction occurred
sessions- Store AI conversation sessionsid- Unique session identifier (UUID)subscription_id- Foreign key to subscriptions tablemessages- JSON-encoded conversation historylast_access- Last activity timestamp for session cleanupcreated_at- Session creation timestamp
The database file (pigbank.db) is created automatically when the application starts.
- Uses OpenAI's GPT-4 Turbo model for natural language processing
- Implements function calling to execute financial operations
- Supports parallel tool execution for complex multi-step requests
- Maintains conversation context through session management
- Subscription-based multi-tenant architecture
- Bearer token authentication for all requests
- Automatic token validation before processing requests
- Comprehensive error handling with appropriate HTTP status codes
- Database transaction rollback on errors
- Detailed logging for debugging and monitoring
Here are some example interactions with the AI assistant:
curl -X POST http://localhost:5000/agent \
-H "Authorization: Bearer your_token_here" \
-H "Content-Type: application/json" \
-d '{"query": "Create a savings account for me"}'curl -X POST http://localhost:5000/agent \
-H "Authorization: Bearer your_token_here" \
-H "Content-Type: application/json" \
-d '{"query": "Add $500 to my savings account for monthly salary"}'curl -X POST http://localhost:5000/agent \
-H "Authorization: Bearer your_token_here" \
-H "Content-Type: application/json" \
-d '{"query": "Create a checking account, transfer $100 from savings to checking, and show me all account balances"}'# First request
curl -X POST http://localhost:5000/agent \
-H "Authorization: Bearer your_token_here" \
-H "Content-Type: application/json" \
-d '{"query": "What accounts do I have?"}'
# Follow-up request using session_id from previous response
curl -X POST http://localhost:5000/agent \
-H "Authorization: Bearer your_token_here" \
-H "Content-Type: application/json" \
-d '{"query": "Add $50 to the first account", "session_id": "session_id_from_previous_response"}'# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
pytest test/ -v# Format code
black . --line-length 88
# Lint code
flake8 .
# Type checking
mypy .The project includes pre-configured VS Code tasks:
- Install Dependencies
- Install Dev Dependencies
- Run Flask App
- Run Tests
- Format Code
- Lint Code
- Type Check
To run this application automatically on a Raspberry Pi at startup, you can use the provided systemd service file.
-
Make the
run.shscript executable:chmod +x run.sh
-
Move the
systemdservice file:sudo mv piggybank.service /etc/systemd/system/
-
Enable and start the service:
sudo systemctl enable piggybank.service sudo systemctl start piggybank.service -
Check the service status:
sudo systemctl status piggybank.service
- Flask - Web framework
- openai - OpenAI API client for AI functionality
- python-dotenv - Environment variable management
See requirements-dev.txt for testing and development tools.
This project is licensed under the terms specified in the LICENSE file.