Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.tensorlake.ai/llms.txt

Use this file to discover all available pages before exploring further.

View Source Code

Check out the full source code for this example on GitHub.
This tutorial demonstrates how to build a Personal Finance Manager using Tensorlake and Claude. This application can parse PDF bank statements, categorize transactions using LLMs, store them in a PostgreSQL database, and answer natural language questions about your spending.

Overview

The Personal Finance Manager consists of two main agents:
  1. Finance Analyzer Agent: Parses PDF statements, extracts transactions, categorizes them using Claude, and stores them in a database.
  2. Finance Query Agent: Translates natural language questions (e.g., “How much did I spend on groceries?”) into SQL queries, executes them, and visualizes the results.

Prerequisites

  • Python 3.11+
  • Tensorlake Account and CLI installed.
  • Anthropic API Key
  • PostgreSQL Database (e.g., Neon, Supabase, or local)

Project Structure

personal-finance/
├── app.py          # Main application logic and agents
├── models.py       # Pydantic models for transactions and queries
├── config.py       # Configuration and prompts
└── requirements.txt

Implementation (app.py)

Here is a simplified view of the core logic for the Finance Analyzer Agent.
import os
from typing import List

from anthropic import Anthropic
from tensorlake import Agent, Assistant, File, User, configure_logging, get_logger, Image, application, function
# import your models and config here

# Define the runtime environment with necessary dependencies
image = Image(name="finance-manager").run("pip install anthropic pandas psycopg2-binary tensorlake pydantic")

class FinanceAnalyzerAgent(Agent):
    """
    Parses PDF statements and stores categorized transactions.
    """
    def __init__(self, name: str, system_prompt: str, tools: List):
        super().__init__(name, system_prompt, tools)
        # Initialize Anthropic client and DB connection
        self.anthropic_client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

    def call(self, user_message: User, files: List[File]) -> Assistant:
        if not files:
            return Assistant(content="Please upload a PDF statement.")
            
        pdf_file = files[0]
        # 1. Extract text from PDF (using a helper or library)
        pdf_text = self._extract_text(pdf_file)
        
        # 2. Extract transactions using Claude
        transactions = self._extract_transactions_from_text(pdf_text)
        
        # 3. Categorize transactions using Claude
        categorized = self._categorize_transactions(transactions)
        
        # 4. Store in Database
        self._insert_into_db(categorized)
        
        return Assistant(content=f"Processed statement. Added {len(categorized)} transactions.")

    # Helper methods implementation...
And the Finance Query Agent:
class FinanceQueryAgent(Agent):
    """
    Answers questions about financial data using SQL.
    """
    def call(self, user_message: User, files: List) -> Assistant:
        # 1. Get Database Schema
        schema = self._get_db_schema()
        
        # 2. Generate SQL query using Claude based on user question and schema
        sql_query = self._generate_sql(user_message.content, schema)
        
        # 3. Execute SQL
        results = self._execute_sql(sql_query)
        
        # 4. Formulate answer
        answer = self._generate_answer(user_message.content, results)
        
        return Assistant(content=answer)

Running Locally

  1. Set up your environment variables:
    export ANTHROPIC_API_KEY=your_key
    export DATABASE_URL=postgresql://user:password@host:port/dbname
    
  2. Run the application:
    python app.py path/to/statement.pdf
    

Deploying to Tensorlake

Deploy your finance manager to the cloud securely.
tl secrets set ANTHROPIC_API_KEY=your_key
tl secrets set DATABASE_URL=your_db_url
tl deploy app.py
Your personal finance assistant is now ready to help you track your spending!