Overview
The AI Services API provides intelligent financial assistance through category prediction, conversational AI, and automated insights. All endpoints require authentication and leverage Google Gemini AI. Base Path:/api
Authentication: Required (@api_login_required decorator)
AI Engine: Google Gemini (configured via app.ai_service)
Predict Category
Automatically predict the most appropriate category for a transaction based on its description./api/predict-category
How It Works
- Fetches the user’s existing category list
- Sends transaction description + category menu to AI
- AI selects the best matching category from user’s options
- Returns category ID, name, type, and confidence score
Request Body
Transaction description to analyze (e.g., “Mua cà phê sáng”, “Đổ xăng xe”)
Response
success, no_match, or errorMatched category ID (only on success)
Category name (only on success)
Category type:
thu (income) or chi (expense)AI confidence score (0-100)
Error message (only on error)
Example Request
Example Response
Success
No Match
Error
Implementation Details
Source:app/routes/ai.py:25-61
Chat with AI Assistant
Converse with an AI financial advisor that understands your financial context./api/chat
Context Provided to AI
The AI receives comprehensive financial context:- Wallet Balances
- 30-Day Summary
- Recent Transactions
Current balance for all user wallets
Request Body
User’s question or message to the AI assistant
Response Format
Returns a streaming response (text/plain) where AI response is sent incrementally as it’s generated.
Example Request
Example Response (Streaming)
Context Building
Source:app/routes/ai.py:75-108
Chat History Storage
- Conversations are logged to
ChatbotLogtable after streaming completes - Each log entry includes question, full answer, timestamp, and user ID
- Used for conversation history and quality monitoring
Error Response
Get Chat History
Retrieve recent chat conversation history./api/chat/history
Response
Array of message objects in chronological order (oldest first)
Example Request
Example Response
Implementation Details
Source:app/routes/ai.py:150-161
- Retrieves last 20 messages
- Ordered by creation date (newest first in query)
- Reversed before returning (oldest first for display)
- Messages interleaved between user and AI roles
Dashboard Insights
Get quick AI-generated financial insights for dashboard display./api/dashboard-insights
Features
- Returns exactly 3 concise financial tips
- Based on current month’s income and expenses
- Respects user’s AI settings (
UserSetting.ai_suggestions) - Formatted as clean, actionable advice (no greetings or markdown)
Response
success, disabled, or errorArray of 3 insight strings (only on success)
Status message (on disabled/error)
Example Request
Example Response
Success
Disabled
Error
AI Prompt Engineering
Source:app/routes/ai.py:191-202
The prompt enforces strict formatting:
Post-Processing
- Collects full streaming response
- Splits by newlines
- Strips leading bullets/markers (
-*•) - Filters empty lines
- Returns first 3 insights
Implementation Reference
Source:app/routes/ai.py
Error Codes
| Status Code | Description |
|---|---|
| 200 | Request successful |
| 400 | Missing required parameter |
| 401 | Unauthorized (not logged in) |
| 500 | AI service error or database failure |
Related Endpoints
- Transactions API - Store AI-predicted categories with transactions
- User Settings - Toggle AI suggestions on/off
